UNPKG

5.51 kBJavaScriptView Raw
1var util = require('vis-util');
2var Bars = require('./graph2d_types/bar');
3var Lines = require('./graph2d_types/line');
4var Points = require('./graph2d_types/points');
5
6/**
7 * /**
8 * @param {object} group | the object of the group from the dataset
9 * @param {string} groupId | ID of the group
10 * @param {object} options | the default options
11 * @param {array} groupsUsingDefaultStyles | this array has one entree.
12 * It is passed as an array so it is passed by reference.
13 * It enumerates through the default styles
14 * @constructor GraphGroup
15 */
16function GraphGroup(group, groupId, options, groupsUsingDefaultStyles) {
17 this.id = groupId;
18 var fields = ['sampling', 'style', 'sort', 'yAxisOrientation', 'barChart', 'drawPoints', 'shaded', 'interpolation', 'zIndex','excludeFromStacking', 'excludeFromLegend'];
19 this.options = util.selectiveBridgeObject(fields, options);
20 this.usingDefaultStyle = group.className === undefined;
21 this.groupsUsingDefaultStyles = groupsUsingDefaultStyles;
22 this.zeroPosition = 0;
23 this.update(group);
24 if (this.usingDefaultStyle == true) {
25 this.groupsUsingDefaultStyles[0] += 1;
26 }
27 this.itemsData = [];
28 this.visible = group.visible === undefined ? true : group.visible;
29}
30
31/**
32 * this loads a reference to all items in this group into this group.
33 * @param {array} items
34 */
35GraphGroup.prototype.setItems = function (items) {
36 if (items != null) {
37 this.itemsData = items;
38 if (this.options.sort == true) {
39 util.insertSort(this.itemsData,function (a, b) {
40 return a.x > b.x ? 1 : -1;
41 });
42 }
43 }
44 else {
45 this.itemsData = [];
46 }
47};
48
49GraphGroup.prototype.getItems = function () {
50 return this.itemsData;
51};
52
53/**
54 * this is used for barcharts and shading, this way, we only have to calculate it once.
55 * @param {number} pos
56 */
57GraphGroup.prototype.setZeroPosition = function (pos) {
58 this.zeroPosition = pos;
59};
60
61/**
62 * set the options of the graph group over the default options.
63 * @param {Object} options
64 */
65GraphGroup.prototype.setOptions = function (options) {
66 if (options !== undefined) {
67 var fields = ['sampling', 'style', 'sort', 'yAxisOrientation', 'barChart', 'zIndex','excludeFromStacking', 'excludeFromLegend'];
68 util.selectiveDeepExtend(fields, this.options, options);
69
70 // if the group's drawPoints is a function delegate the callback to the onRender property
71 if (typeof options.drawPoints == 'function') {
72 options.drawPoints = {
73 onRender: options.drawPoints
74 }
75 }
76
77 util.mergeOptions(this.options, options, 'interpolation');
78 util.mergeOptions(this.options, options, 'drawPoints');
79 util.mergeOptions(this.options, options, 'shaded');
80
81 if (options.interpolation) {
82 if (typeof options.interpolation == 'object') {
83 if (options.interpolation.parametrization) {
84 if (options.interpolation.parametrization == 'uniform') {
85 this.options.interpolation.alpha = 0;
86 }
87 else if (options.interpolation.parametrization == 'chordal') {
88 this.options.interpolation.alpha = 1.0;
89 }
90 else {
91 this.options.interpolation.parametrization = 'centripetal';
92 this.options.interpolation.alpha = 0.5;
93 }
94 }
95 }
96 }
97 }
98};
99
100
101/**
102 * this updates the current group class with the latest group dataset entree, used in _updateGroup in linegraph
103 * @param {vis.Group} group
104 */
105GraphGroup.prototype.update = function (group) {
106 this.group = group;
107 this.content = group.content || 'graph';
108 this.className = group.className || this.className || 'vis-graph-group' + this.groupsUsingDefaultStyles[0] % 10;
109 this.visible = group.visible === undefined ? true : group.visible;
110 this.style = group.style;
111 this.setOptions(group.options);
112};
113
114/**
115 * return the legend entree for this group.
116 *
117 * @param {number} iconWidth
118 * @param {number} iconHeight
119 * @param {{svg: (*|Element), svgElements: Object, options: Object, groups: Array.<Object>}} framework
120 * @param {number} x
121 * @param {number} y
122 * @returns {{icon: (*|Element), label: (*|string), orientation: *}}
123 */
124GraphGroup.prototype.getLegend = function (iconWidth, iconHeight, framework, x, y) {
125 if (framework == undefined || framework == null) {
126 var svg = document.createElementNS('http://www.w3.org/2000/svg', "svg");
127 framework = {svg: svg, svgElements:{}, options: this.options, groups: [this]}
128 }
129 if (x == undefined || x == null){
130 x = 0;
131 }
132 if (y == undefined || y == null){
133 y = 0.5 * iconHeight;
134 }
135 switch (this.options.style){
136 case "line":
137 Lines.drawIcon(this, x, y, iconWidth, iconHeight, framework);
138 break;
139 case "points": //explicit no break
140 case "point":
141 Points.drawIcon(this, x, y, iconWidth, iconHeight, framework);
142 break;
143 case "bar":
144 Bars.drawIcon(this, x, y, iconWidth, iconHeight, framework);
145 break;
146 }
147 return {icon: framework.svg, label: this.content, orientation: this.options.yAxisOrientation};
148};
149
150GraphGroup.prototype.getYRange = function (groupData) {
151 var yMin = groupData[0].y;
152 var yMax = groupData[0].y;
153 for (var j = 0; j < groupData.length; j++) {
154 yMin = yMin > groupData[j].y ? groupData[j].y : yMin;
155 yMax = yMax < groupData[j].y ? groupData[j].y : yMax;
156 }
157 return {min: yMin, max: yMax, yAxisOrientation: this.options.yAxisOrientation};
158};
159
160module.exports = GraphGroup;