-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot.js
327 lines (269 loc) · 8.71 KB
/
Plot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
Dave_js.Plot = function Plot(type) {
//figure out what plotter we will be using
if (typeof Dave_js[type] !== 'function') {
console.log('Unknown plot type: "' + type + '"');
return null;
}
this.type = type;
this.plotter = new Dave_js[type](this);
//get a new set of properties
this.chart = new Dave_js.ChartProperties();
//create the canvas elements
this.dataCanvas = document.createElement("canvas");
this.dataCanvas.id = this.chart.dataCanvasId;
this.dataCanvas.className = "davejs-data-canvas";
this.decorCanvas = document.createElement("canvas");
this.decorCanvas.id = this.chart.decorCanvasId;
this.decorCanvas.className = "davejs-decor-canvas";
//initialize canvas context
this.dataCtx = this.dataCanvas.getContext("2d");
this.decorCtx = this.decorCanvas.getContext("2d");
this.canvasBox = document.getElementsByTagName("body")[0];
};
//this is an obect which defines the bounds on the data
//each plot type will define this object differently so it will be overridden
Dave_js.Plot.prototype.range = null;
Dave_js.Plot.prototype.renderInto = function renderInto(canvasDivID) {
var el = document.getElementById(canvasDivID);
if (this.canvasBox !== null) {
this.canvasBox = el;
} else {
console.log(
'Could not attach canvas to ' + canvasDivID + '. Element does not exist.'
);
console.log(
'Attaching to body tag instead.'
);
}
this.canvasBox.appendChild(this.dataCanvas);
this.canvasBox.appendChild(this.decorCanvas);
};
Dave_js.Plot.prototype.configure = function configure(labels) {
var
decorCanvas = this.decorCanvas || {},
dataCanvas = this.dataCanvas || {},
chart = this.chart || {},
flags = chart.flags || {},
dataCtx = this.dataCtx || {},
decorCtx = this.decorCtx || {},
plotter = this.plotter,
font = this.chart.cssFont || '',
plotRegion, fontSize;
labels = labels || {};
//save which variables are for which axis
chart.axisVars = labels.axisVars || {};
//verify font and measure its height
if (typeof font !== 'string') {
font = '12px monospace';
fontSize = 12;
} else {
fontSize = parseInt(font, 10);
if (!fontSize) {
fontSize = 12;
font = '12px ' + font;
}
}
chart.cssFont = dataCtx.font = decorCtx.font = font;
chart.fontSize = fontSize;
//figure out how much space the ticmarks and labels will occupy
this.chart.plotRegion = plotRegion =
this.plotter.calculateMargins.call(this, labels);
//get the size of the plotting area
dataCanvas.width = chart.width;
dataCanvas.height = chart.height;
decorCanvas.width = chart.width + plotRegion.left + plotRegion.right;
decorCanvas.height = chart.height + plotRegion.top + plotRegion.bottom;
//move the data canvas to the origin
dataCanvas.style.left = plotRegion.left + "px";
dataCanvas.style.top = plotRegion.top + "px";
//draw background of the data canvas
dataCtx.save();
if (chart.bgImg) {
//resize the image to fit the plotting area
chart.bgImg.width = dataCanvas.width;
chart.bgImg.height = dataCanvas.height;
dataCtx.drawImage(chart.bgImg,0 , 0);
} else {
dataCtx.fillStyle = chart.colors.bgColor;
dataCtx.fillRect(0,0, dataCanvas.width, dataCanvas.height);
}
dataCtx.restore();
//calculate the value/pixes ratio
if (flags.autoRange) {
plotter.autoRange.call(this);
}
};
Dave_js.Plot.prototype.decorate = function decorate(labels) {
var ctx = this.decorCtx;
//print title (bold)
if (typeof labels.plotTitle == "string") {
ctx.save();
ctx.textAlign = "center";
ctx.fillStyle = this.chart.colors.text;
ctx.font = "bold " + this.chart.cssFont;
ctx.fillText(
labels.plotTitle,
(this.chart.width / 2), -5
);
this.ctx.restore();
}
if (labels.axisLabels) {
plotter.labelAxes.call(labels.axisLabels);
}
};
Dave_js.Plot.prototype.setOrigin = function setOrigin(x, y) {
this.chart.origin.x = Dave_js.forceNumber(x) || this.chart.origin.x;
this.chart.origin.y = Dave_js.forceNumber(y) || this.chart.origin.y;
};
Dave_js.Plot.prototype.setPlotSize = function setPlotSize(sizes) {
var size;
if(!sizes){
return;
}
if((size = Dave_js.Utils.forceNumber(sizes.height))){
this.chart.height = size;
}
if((size = Dave_js.Utils.forceNumber(sizes.width))){
this.chart.width = size;
}
};
Dave_js.Plot.prototype.setColor = function setColor(type, color) {
this.chart.colors[type] = color;
};
Dave_js.Plot.prototype.setSubPlot = function setSubPlot(bool) {
this.chart.flags.subPlot = bool;
};
Dave_js.Plot.prototype.setCoordDisp = function setCoordDisp(bool) {
this.chart.flags.showCoords = bool;
};
//first argument is an array containing the name of each tracker.
//each aditional argument is an array containing tracker data
Dave_js.Plot.prototype.setTrackers = function setTrackers() {
this.vars.trackLabels = arguments[0].slice(0);
for (var array_i = 1 ; array_i < arguments.length; array_i) {
this.vars.trackers[array_i] = arguments[array_i].slice(0);
}
};
Dave_js.Plot.prototype.setHistBars = function setHistBars(ratio) {
this.chart.histBarRatio = +ratio || 1;
};
Dave_js.Plot.prototype.setBorderColor = function setBorderColor(color) {
this.chart.colors.borderColor = color;
};
Dave_js.Plot.prototype.setBackgroundColor = function setBackgroundColor(color) {
this.chart.colors.bgColor = color;
};
Dave_js.Plot.prototype.setBackgroundImage = function setBackgroundImage(id) {
var el = document.getElementById(id);
if(!id || id.tagName != 'IMG'){
console.log(
'Could not set background image, ' + el + ' is not an "IMG" tag.'
);
} else {
this.chart.bgImg = el;
}
};
Dave_js.Plot.prototype.setGrid = function setGrid() {
this.chart.flags.grid = true;
};
Dave_js.Plot.prototype.setLegend = function setLegend() {
this.chart.flags.legend = true;
};
Dave_js.Plot.prototype.setZoomable = function setZoomable() {
this.chart.flags.zoomable = true;
};
Dave_js.Plot.prototype.setAutoRange = function setAutoRange(bool) {
this.chart.flags.autoRange = bool;
};
Dave_js.Plot.prototype.getDataStore = function getDataStore() {
return this.dataStore;
};
Dave_js.Plot.prototype.setDataStore = function setDataStore(ds) {
this.dataStore = ds;
};
Dave_js.Plot.prototype.getChartProps = function getChartProps() {
return this.chart;
};
Dave_js.Plot.prototype.drawData = function drawData(data) {
var
plotter = this.plotter,
ctx = this.dataCtx || {},
chart = this.chart || {},
plotRegion = chart.plotRegion || {},
brushWidth = +data.brushWidth || 2,
color = data.color || 'black',
style, coords, dot;
//make sure the required variables are set
if(!data || !data.vars){
console.log('No data to draw!');
return;
}
//default to drawing just a line plot
style = (data.style || "line").toLowerCase();
//convert all of the values to pixel coordinates
coords = plotter.getCoords.call(this, data);
//configure plotting context
ctx.save();
ctx.translate(0, chart.height);
ctx.scale(1, -1);
//set colors for this plot
ctx.fillStyle = color;
ctx.strokeStyle = color;
//draw the various styles that were specified
if(style.indexOf("line") != -1){
if(typeof plotter.drawLines == 'function'){
plotter.drawLines.call(this, coords);
} else {
console.log(this.type + " plotter can not plot lines.");
}
}
if(style.indexOf("point") != -1){
dot = (
typeof data.dot == 'function' ?
dot :
Dave_js.Utils.squareDotFactory({color: color, width: brushWidth})
);
coords.forEach(dot, this);
}
if(style.indexOf("function") != -1){
if(typeof plotter.drawFunction == 'function'){
plotter.drawPoints.call(this, data);
} else {
console.log(this.type + " plotter can not plot function.");
}
}
ctx.restore();
};
Dave_js.Plot.prototype.invertCoords = function invertCoords(coords) {
return this.plotter.invertCoords(coords);
};
Dave_js.Plot.prototype.drawTitleLegend = function drawTitleLegend(vars) {
var
ctx = this.decorCtx,
numVars = vars.length,
offset = 0, var_i;
ctx.save();
ctx.translate(this.chart.plotRegion.left, 0);
ctx.textAlign = "start";
ctx.textBaseline = "top";
while ((var_i = vars.pop())) {
ctx.fillStyle = var_i.color;
ctx.fillText(var_i.text, offset, 0);
offset += ctx.measureText(var_i.text + ' ').width;
}
ctx.restore();
};
Dave_js.Plot.prototype.drawAxes = function drawAxes() {
var
chart = this.chart || {},
flags = chart.flags || {},
plotRegion = chart.plotRegion || {},
ctx = this.decorCtx;
//add the grid based on the
if (!flags.hasRange) {
this.plotter.drawGrid.call(this);
}
};
Dave_js.Plot.prototype.getMargins = function getPlotMargins() {
return this.chart.plotRegion;
};