Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Add option definitions and validation to Graph3d #3099

Merged
merged 20 commits into from
Jul 23, 2017
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0a33741
Proof of concept with copied options + handling from network
wimrijnders May 24, 2017
e07d68f
Added unit test for Graph3d, for checking default syntax; completed d…
wimrijnders May 24, 2017
95ef79b
Fixes for options in playground example
wimrijnders May 25, 2017
8df2388
Added onclick options to graph3d documentation
wimrijnders May 25, 2017
80a3a09
Fixes in graph3d examples
wimrijnders May 25, 2017
6aa3492
Final fixes for option definitions in Graph3d
wimrijnders May 25, 2017
24bef83
Merge branch 'develop' into optionsGraph3d
wimrijnders Jun 3, 2017
49c4476
Fixed handling of 'undefined' in options, enhanced graph3d unit test
wimrijnders Jun 4, 2017
cd1c203
Disabled console output in graph3d unit test
wimrijnders Jun 4, 2017
1c33c15
merge with develop, fix conflict
wimrijnders Jun 13, 2017
59a045a
Merge branch 'develop' into optionsGraph3d
wimrijnders Jul 3, 2017
be90cea
Merge branch 'develop' into optionsGraph3d
wimrijnders Jul 13, 2017
3de1985
Merge branch 'develop' into optionsGraph3d
wimrijnders Jul 13, 2017
7b8489b
Fix conflicts
wimrijnders Jul 17, 2017
ef2b339
Upgrade webpack module
wimrijnders Jul 17, 2017
cc22771
Merge branch 'optionsGraph3d' of github.com:wimrijnders/vis into opti…
wimrijnders Jul 17, 2017
39639d2
Fixed conflicts
wimrijnders Jul 21, 2017
830c199
Merge branch 'develop' into optionsGraph3d
yotamberk Jul 21, 2017
46286ed
Merge branch 'develop' into optionsGraph3d
wimrijnders Jul 22, 2017
fe0b276
Merge branch 'develop' into optionsGraph3d
yotamberk Jul 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"complexity": [2, 55],
"max-statements": [2, 115],
"no-unreachable": 1,
"no-useless-escape": 0,
"no-console": 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want this in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for now. console is used in the running code to output warning messages.

But this is from a practical viewpoint. I would be happiest if as many checks as possible are enabled for eslint. It's just that console is used a bit too often to casually fix this.

"no-useless-escape": 0
/*
// some disabled options which might be useful
"no-console": 0,
"no-empty": 0,
"no-extra-semi": 0,
"no-fallthrough": 0,
Expand Down
18 changes: 18 additions & 0 deletions docs/graph3d/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,24 @@ <h2 id="Configuration_Options">Configuration Options</h2>
both have the same, maximum with.</td>
</tr>

<tr>
<td>onclick</td>
<td>function</td>
<td>none</td>
<td>Event handler for a click event with signature <code>function onclick(point)</code>.<br>
Parameter <code>point</code> contains data for the nearest graph element relative to the click in
the line of sight. It is an object with the fields:
<ul>
<li><code>id </code> - id of nearest node to the click</li>
<li><code>x </code> - x-coordinate in graph units</li>
<li><code>y </code> - y-coordinate in graph units</li>
<li><code>z </code> - z-coordinate in graph units</li>
<li><code>style</code> - if present, the data value for this point</li>
</ul>

</td>
</tr>

<tr>
<td>showAnimationControls</td>
<td>boolean</td>
Expand Down
3 changes: 1 addition & 2 deletions examples/graph3d/04_animation.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
keepAspectRatio: true,
verticalRatio: 0.5,
animationInterval: 100, // milliseconds
animationPreload: true,
filterValue: 'time'
animationPreload: true
};

// create our graph
Expand Down
2 changes: 1 addition & 1 deletion examples/graph3d/10_styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
keepAspectRatio: true,
verticalRatio: 0.5
};

var camera = graph ? graph.getCameraPosition() : null;

// create our graph
Expand Down
12 changes: 7 additions & 5 deletions examples/graph3d/playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ function getDataDatasource() {
* Retrieve a JSON object with all options
*/
function getOptions() {
return {
var options = {
width: document.getElementById("width").value,
height: document.getElementById("height").value,
style: document.getElementById("style").value,
Expand All @@ -413,8 +413,8 @@ function getOptions() {
showLegend: (document.getElementById("showLegend").checked != false),
showShadow: (document.getElementById("showShadow").checked != false),
keepAspectRatio: (document.getElementById("keepAspectRatio").checked != false),
verticalRatio: document.getElementById("verticalRatio").value,
animationInterval: document.getElementById("animationInterval").value,
verticalRatio: Number(document.getElementById("verticalRatio").value) || undefined,
animationInterval: Number(document.getElementById("animationInterval").value) || undefined,
xLabel: document.getElementById("xLabel").value,
yLabel: document.getElementById("yLabel").value,
zLabel: document.getElementById("zLabel").value,
Expand All @@ -423,8 +423,8 @@ function getOptions() {
animationPreload: (document.getElementById("animationPreload").checked != false),
animationAutoStart:(document.getElementById("animationAutoStart").checked != false),

xCenter: Number(document.getElementById("xCenter").value) || undefined,
yCenter: Number(document.getElementById("yCenter").value) || undefined,
xCenter: document.getElementById("xCenter").value,
yCenter: document.getElementById("yCenter").value,

xMin: Number(document.getElementById("xMin").value) || undefined,
xMax: Number(document.getElementById("xMax").value) || undefined,
Expand All @@ -442,6 +442,8 @@ function getOptions() {
xBarWidth: Number(document.getElementById("xBarWidth").value) || undefined,
yBarWidth: Number(document.getElementById("yBarWidth").value) || undefined
};

return options;
}

/**
Expand Down
23 changes: 19 additions & 4 deletions lib/graph3d/Graph3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ var Point2d = require('./Point2d');
var Slider = require('./Slider');
var StepNumber = require('./StepNumber');
var Settings = require('./Settings');
var Validator = require("./../shared/Validator").default;
var {printStyle} = require('./../shared/Validator');
var {allOptions} = require('./options.js');
var DataGroup = require('./DataGroup');


Expand All @@ -31,7 +34,7 @@ var autoByDefault = undefined;
* If a field is not in this list, a default value of 'autoByDefault' is assumed,
* which is just an alias for 'undefined'.
*/
var DEFAULTS = {
Graph3d.DEFAULTS = {
width : '400px',
height : '400px',
filterLabel : 'time',
Expand Down Expand Up @@ -90,8 +93,6 @@ var DEFAULTS = {
}
},

showLegend : autoByDefault, // determined by graph style
backgroundColor : autoByDefault,

dataColor : {
fill : '#7DC1FF',
Expand All @@ -105,6 +106,13 @@ var DEFAULTS = {
distance : 1.7
},


/*
The following fields are 'auto by default', see above.
*/
showLegend : autoByDefault, // determined by graph style
backgroundColor : autoByDefault,

xBarWidth : autoByDefault,
yBarWidth : autoByDefault,
valueMin : autoByDefault,
Expand Down Expand Up @@ -151,7 +159,7 @@ function Graph3d(container, data, options) {
// create a frame and canvas
this.create();

Settings.setDefaults(DEFAULTS, this);
Settings.setDefaults(Graph3d.DEFAULTS, this);

// the column indexes
this.colX = undefined;
Expand Down Expand Up @@ -628,6 +636,13 @@ Graph3d.prototype.setData = function (data) {
* @param {Object} options
*/
Graph3d.prototype.setOptions = function (options) {
if (options === undefined) return;

let errorFound = Validator.validate(options, allOptions);
if (errorFound === true) {
console.log('%cErrors have been found in the supplied options object.', printStyle);
}

this.animationStop();

Settings.setOptions(options, this);
Expand Down
132 changes: 132 additions & 0 deletions lib/graph3d/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* This object contains all possible options. It will check if the types are correct, if required if the option is one
* of the allowed values.
*
* __any__ means that the name of the property does not matter.
* __type__ is a required field for all objects and contains the allowed types of all objects
*/
let string = 'string';
let bool = 'boolean';
let number = 'number';
let object = 'object'; // should only be in a __type__ property
// Following not used here, but useful for reference
//let array = 'array';
//let dom = 'dom';
//let any = 'any';


let colorOptions = {
fill : { string },
stroke : { string },
strokeWidth: { number },
__type__ : { string, object, 'undefined': 'undefined' }
};


/**
* Order attempted to be alphabetical.
* - x/y/z-prefixes ignored in sorting
* - __type__ always at end
* - globals at end
*/
let allOptions = {
animationAutoStart: { boolean: bool, 'undefined': 'undefined' },
animationInterval : { number },
animationPreload : { boolean: bool },
axisColor : { string },
backgroundColor : colorOptions,
xBarWidth : { number, 'undefined': 'undefined' },
yBarWidth : { number, 'undefined': 'undefined' },
cameraPosition : {
distance : { number },
horizontal: { number },
vertical : { number },
__type__ : { object }
},
xCenter : { string },
yCenter : { string },
dataColor : colorOptions,
dotSizeMinFraction: { number },
dotSizeMaxFraction: { number },
dotSizeRatio : { number },
filterLabel : { string },
gridColor : { string },
onclick : { 'function': 'function' },
keepAspectRatio : { boolean: bool },
xLabel : { string },
yLabel : { string },
zLabel : { string },
legendLabel : { string },
xMin : { number, 'undefined': 'undefined' },
yMin : { number, 'undefined': 'undefined' },
zMin : { number, 'undefined': 'undefined' },
xMax : { number, 'undefined': 'undefined' },
yMax : { number, 'undefined': 'undefined' },
zMax : { number, 'undefined': 'undefined' },
showAnimationControls: { boolean: bool, 'undefined': 'undefined' },
showGrid : { boolean: bool },
showLegend : { boolean: bool, 'undefined': 'undefined' },
showPerspective : { boolean: bool },
showShadow : { boolean: bool },
showXAxis : { boolean: bool },
showYAxis : { boolean: bool },
showZAxis : { boolean: bool },
xStep : { number, 'undefined': 'undefined' },
yStep : { number, 'undefined': 'undefined' },
zStep : { number, 'undefined': 'undefined' },
style: {
number, // TODO: either Graph3d.DEFAULT has string, or number allowed in documentation
string: [
'bar',
'bar-color',
'bar-size',
'dot',
'dot-line',
'dot-color',
'dot-size',
'line',
'grid',
'surface'
]
},
tooltip : { boolean: bool, 'function': 'function' },
tooltipStyle : {
content: {
color : { string },
background : { string },
border : { string },
borderRadius: { string },
boxShadow : { string },
padding : { string },
__type__ : { object }
},
line: {
borderLeft: { string },
height : { string },
width : { string },
__type__ : { object }
},
dot: {
border : { string },
borderRadius: { string },
height : { string },
width : { string },
__type__ : { object},
},
__type__: { object}
},
xValueLabel : { 'function': 'function' },
yValueLabel : { 'function': 'function' },
zValueLabel : { 'function': 'function' },
valueMax : { number, 'undefined': 'undefined' },
valueMin : { number, 'undefined': 'undefined' },
verticalRatio : { number },

//globals :
height: { string },
width: { string },
__type__: { object }
};


export {allOptions};
26 changes: 24 additions & 2 deletions test/Graph3d.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
var assert = require('assert');
var jsdom_global = require('jsdom-global');
var vis = require('../dist/vis');
var Graph3d = vis.Graph3d;
var jsdom_global = require('jsdom-global');
var stdout = require('test-console').stdout;
var Validator = require("./../lib/shared/Validator").default;
//var {printStyle} = require('./../lib/shared/Validator');
var {allOptions, configureOptions} = require('./../lib/graph3d/options.js');

var now = new Date();

describe('Graph3d', function () {

before(function() {
//console.log('before!');
this.jsdom_global = jsdom_global(
Expand All @@ -15,6 +19,24 @@ describe('Graph3d', function () {
this.container = document.getElementById('mynetwork');
});


it('should pass validation for the default options', function () {
assert(Graph3d.DEFAULTS !== undefined);

let errorFound;
let output;
output = stdout.inspectSync(function() {
errorFound = Validator.validate(Graph3d.DEFAULTS, allOptions);
});

// Useful during debugging:
//if (errorFound === true) {
// console.log(JSON.stringify(output, null, 2));
//}
assert(!errorFound, 'DEFAULTS options object does not pass validation');
});


it('accepts new option values on defined instance', function () {
assert(this.container !== null, 'Container div not found');

Expand Down