Skip to content

Commit 6a1aa64

Browse files
committed
Initial check-in
0 parents  commit 6a1aa64

File tree

270 files changed

+72345
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

270 files changed

+72345
-0
lines changed

data.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function StubbedData() {
2+
return "" +
3+
"Date,A,B\n" +
4+
"20061001,3.01953818828,0.7212041046,2.18487394958,0.599318549691\n" +
5+
"20061002,3.63321799308,0.778297234566,1.69491525424,0.531417655826\n" +
6+
"20061003,2.44328097731,0.644967734352,2.51256281407,0.640539070386\n" +
7+
"20061004,3.52733686067,0.774700921683,2.68456375839,0.66207105053\n" +
8+
"20061005,3.28719723183,0.741636245748,2.35294117647,0.621407707226\n" +
9+
"20061006,1.58450704225,0.523967868159,3.78657487091,0.791868460623\n" +
10+
"20061007,5.32859680284,0.946589405904,4.0404040404,0.807910739509\n" +
11+
"20061008,2.64084507042,0.672799548916,2.37288135593,0.626609885481\n" +
12+
"20061009,2.26480836237,0.620990945917,3.5413153457,0.75897176848\n" +
13+
"20061010,3.29289428076,0.74289969528,2.02702702703,0.579191340004\n" +
14+
"20061011,2.7633851468,0.681234043829,1.1744966443,0.4413034044\n" +
15+
"20061012,3.28719723183,0.741636245748,3.37268128162,0.741327769578\n" +
16+
"20061013,1.77304964539,0.55569466381,1.85810810811,0.555011329732\n" +
17+
"20061014,3.39892665474,0.7664008338,1.67224080268,0.524368852929\n" +
18+
"20061015,2.65017667845,0.675144574777,3.35570469799,0.737661045752\n" +
19+
"20061016,3.63951473137,0.779620631266,2.34899328859,0.620377617453\n" +
20+
"20061017,2.25694444444,0.618859623032,1.68067226891,0.526990133716\n" +
21+
"20061018,4.47504302926,0.857766274964,2.51677852349,0.641599927369\n" +
22+
"20061019,2.44755244755,0.646081155692,1.68067226891,0.526990133716\n" +
23+
"20061020,3.67775831874,0.787656442774,3.066439523,0.711598843969\n" +
24+
"20061021,3.94265232975,0.823839169829,3.85906040268,0.788990618726\n" +
25+
"20061022,2.59067357513,0.660187558973,3.71621621622,0.777438794254\n" +
26+
"20061023,4.33275563258,0.847570482324,3.85906040268,0.788990618726\n" +
27+
"20061024,3.10344827586,0.720049610821,2.84280936455,0.679611549697\n" +
28+
"20061025,1.40350877193,0.492720767725,2.7027027027,0.666482380968\n" +
29+
"20061026,1.95035460993,0.582291234145,2.36486486486,0.624518599275\n" +
30+
"20061027,2.30905861456,0.632980642182,2.03045685279,0.580161203819\n" +
31+
"20061028,4.09252669039,0.835706590809,2.87648054146,0.68754192469\n" +
32+
"20061029,2.66903914591,0.679883997626,2.02360876897,0.578224712918\n" +
33+
"20061030,4.74516695958,0.89127787497,4.36241610738,0.836670992529\n" +
34+
"20061031,2.78260869565,0.685905251933,3.20945945946,0.724388507178\n" +
35+
"20061101,1.5873015873,0.524884521441,1.51260504202,0.500373860545\n" +
36+
"20061102,2.78745644599,0.687083077461,2.0202020202,0.57726130639\n" +
37+
"20061103,5.11463844797,0.925157232782,2.68907563025,0.663168401088\n" +
38+
"20061104,4.9001814882,0.919644816432,3.07692307692,0.713993047527\n" +
39+
"20061105,5.13274336283,0.928343545136,3.55329949239,0.761492892041\n" +
40+
"20061106,1.92644483363,0.575222935029,2.35294117647,0.621407707226\n" +
41+
"20061107,2.46478873239,0.650573541306,1.52027027027,0.502889967904\n" +
42+
"20061108,2.13523131673,0.609772022763,2.6981450253,0.665374048085\n" +
43+
"20061109,3.88007054674,0.811026422222,2.72572402044,0.672079879106\n" +
44+
"20061110,2.63620386643,0.671633132526,3.71621621622,0.777438794254\n" +
45+
"20061111,3.69718309859,0.791736755355,3.0303030303,0.703344064467\n" +
46+
"20061112,3.83944153578,0.802703592906,4.05405405405,0.81058250986\n" +
47+
"20061113,2.47787610619,0.653984033555,2.20338983051,0.604340313133\n" +
48+
"20061114,1.77304964539,0.55569466381,2.22222222222,0.60944692682\n" +
49+
"20061115,2.30088495575,0.630766388737,0.843170320405,0.375484163785\n" +
50+
"20061116,1.57894736842,0.522144132232,2.19594594595,0.602321544724\n" +
51+
"20061118,2.45183887916,0.647198426991,1.69491525424,0.531417655826\n" +
52+
"20061119,3.52733686067,0.774700921683,1.85185185185,0.55316023504\n" +
53+
"20061120,2.97723292469,0.711254751484,2.6981450253,0.665374048085\n" +
54+
"20061121,2.29681978799,0.629665059963,2.01680672269,0.576301104352\n" +
55+
"20061122,3.01418439716,0.719945245328,2.5466893039,0.649125445325\n" +
56+
"20061123,3.78378378378,0.809917534069,2.6936026936,0.664269394219\n" +
57+
"20061124,3.18584070796,0.738851643987,2.01005025126,0.57439025002\n" +
58+
"20061125,2.83185840708,0.697868332879,3.066439523,0.711598843969\n" +
59+
"20061126,3.01953818828,0.7212041046,2.53378378378,0.645878720149\n" +
60+
"20061127,2.81195079086,0.693033387099,1.51006711409,0.499540743312\n" +
61+
"20061128,2.97723292469,0.711254751484,2.54237288136,0.648039583782\n" +
62+
"20061129,1.41093474427,0.495309102312,3.02013422819,0.701020603129";
63+
}

dygraph-canvas.js

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// Copyright 2006 Dan Vanderkam ([email protected])
2+
// All Rights Reserved.
3+
4+
/**
5+
* @fileoverview Subclasses various parts of PlotKit to meet the additional
6+
* needs of DateGraph: grid overlays and error bars
7+
*/
8+
9+
// Subclass PlotKit.Layout to add:
10+
// 1. Sigma/errorBars properties
11+
// 2. Copy error terms for PlotKit.CanvasRenderer._renderLineChart
12+
13+
/**
14+
* Creates a new DateGraphLayout object. Options are the same as those allowed
15+
* by the PlotKit.Layout constructor.
16+
* @param {Object} options Options for PlotKit.Layout
17+
* @return {Object} The DateGraphLayout object
18+
*/
19+
DateGraphLayout = function(options) {
20+
PlotKit.Layout.call(this, "line", options);
21+
};
22+
DateGraphLayout.prototype = new PlotKit.Layout();
23+
24+
/**
25+
* Behaves the same way as PlotKit.Layout, but also copies the errors
26+
* @private
27+
*/
28+
DateGraphLayout.prototype.evaluateWithError = function() {
29+
this.evaluate();
30+
if (!this.options.errorBars) return;
31+
32+
// Copy over the error terms
33+
var i = 0; // index in this.points
34+
for (var setName in this.datasets) {
35+
var j = 0;
36+
var dataset = this.datasets[setName];
37+
if (PlotKit.Base.isFuncLike(dataset)) continue;
38+
for (var j = 0; j < dataset.length; j++, i++) {
39+
var item = dataset[j];
40+
var xv = parseFloat(item[0]);
41+
var yv = parseFloat(item[1]);
42+
43+
if (xv == this.points[i].xval &&
44+
yv == this.points[i].yval) {
45+
this.points[i].errorMinus = parseFloat(item[2]);
46+
this.points[i].errorPlus = parseFloat(item[3]);
47+
}
48+
}
49+
}
50+
};
51+
52+
/**
53+
* Convenience function to remove all the data sets from a graph
54+
*/
55+
DateGraphLayout.prototype.removeAllDatasets = function() {
56+
delete this.datasets;
57+
this.datasets = new Array();
58+
};
59+
60+
/**
61+
* Change the values of various layout options
62+
* @param {Object} new_options an associative array of new properties
63+
*/
64+
DateGraphLayout.prototype.updateOptions = function(new_options) {
65+
MochiKit.Base.update(this.options, new_options ? new_options : {});
66+
};
67+
68+
// Subclass PlotKit.CanvasRenderer to add:
69+
// 1. X/Y grid overlay
70+
// 2. Ability to draw error bars (if required)
71+
72+
/**
73+
* Sets some PlotKit.CanvasRenderer options
74+
* @param {Object} element The canvas to attach to
75+
* @param {Layout} layout The DateGraphLayout object for this graph.
76+
* @param {Object} options Options to pass on to CanvasRenderer
77+
*/
78+
DateGraphCanvasRenderer = function(element, layout, options) {
79+
PlotKit.CanvasRenderer.call(this, element, layout, options);
80+
this.options.shouldFill = false;
81+
this.options.shouldStroke = true;
82+
this.options.drawYGrid = true;
83+
this.options.drawXGrid = true;
84+
this.options.gridLineColor = MochiKit.Color.Color.grayColor();
85+
MochiKit.Base.update(this.options, options);
86+
87+
// TODO(danvk) This shouldn't be necessary: effects should be overlaid
88+
this.options.drawBackground = false;
89+
};
90+
DateGraphCanvasRenderer.prototype = new PlotKit.CanvasRenderer();
91+
92+
/**
93+
* Draw an X/Y grid on top of the existing plot
94+
*/
95+
DateGraphCanvasRenderer.prototype.render = function() {
96+
// Do the ordinary rendering, as before
97+
// TODO(danvk) Call super.render()
98+
this._renderLineChart();
99+
this._renderLineAxis();
100+
101+
// Draw the new X/Y grid
102+
var ctx = this.element.getContext("2d");
103+
if (this.options.drawYGrid) {
104+
var ticks = this.layout.yticks;
105+
ctx.save();
106+
ctx.strokeStyle = this.options.gridLineColor.toRGBString();
107+
ctx.lineWidth = this.options.axisLineWidth;
108+
for (var i = 0; i < ticks.length; i++) {
109+
var x = this.area.x;
110+
var y = this.area.y + ticks[i][0] * this.area.h;
111+
ctx.beginPath();
112+
ctx.moveTo(x, y);
113+
ctx.lineTo(x + this.area.w, y);
114+
ctx.closePath();
115+
ctx.stroke();
116+
}
117+
}
118+
119+
if (this.options.drawXGrid) {
120+
var ticks = this.layout.xticks;
121+
ctx.save();
122+
ctx.strokeStyle = this.options.gridLineColor.toRGBString();
123+
ctx.lineWidth = this.options.axisLineWidth;
124+
for (var i=0; i<ticks.length; i++) {
125+
var x = this.area.x + ticks[i][0] * this.area.w;
126+
var y = this.area.y + this.area.h;
127+
ctx.beginPath();
128+
ctx.moveTo(x, y);
129+
ctx.lineTo(x, this.area.y);
130+
ctx.closePath();
131+
ctx.stroke();
132+
}
133+
}
134+
};
135+
136+
/**
137+
* Overrides the CanvasRenderer method to draw error bars
138+
*/
139+
DateGraphCanvasRenderer.prototype._renderLineChart = function() {
140+
var context = this.element.getContext("2d");
141+
var colorCount = this.options.colorScheme.length;
142+
var colorScheme = this.options.colorScheme;
143+
var setNames = MochiKit.Base.keys(this.layout.datasets);
144+
var errorBars = this.layout.options.errorBars;
145+
var setCount = setNames.length;
146+
var bind = MochiKit.Base.bind;
147+
var partial = MochiKit.Base.partial;
148+
149+
//Update Points
150+
var updatePoint = function(point) {
151+
point.canvasx = this.area.w * point.x + this.area.x;
152+
point.canvasy = this.area.h * point.y + this.area.y;
153+
}
154+
MochiKit.Iter.forEach(this.layout.points, updatePoint, this);
155+
156+
// create paths
157+
var makePath = function(ctx) {
158+
for (var i = 0; i < setCount; i++) {
159+
var setName = setNames[i];
160+
var color = colorScheme[i%colorCount];
161+
var strokeX = this.options.strokeColorTransform;
162+
163+
// setup graphics context
164+
context.save();
165+
context.strokeStyle = color.toRGBString();
166+
context.lineWidth = this.options.strokeWidth;
167+
ctx.beginPath();
168+
var point = this.layout.points[0];
169+
var first_point = true;
170+
var addPoint = function(ctx_, point) {
171+
if (point.name == setName) {
172+
if (first_point)
173+
ctx_.moveTo(point.canvasx, point.canvasy);
174+
else
175+
ctx_.lineTo(point.canvasx, point.canvasy);
176+
first_point = false;
177+
}
178+
};
179+
MochiKit.Iter.forEach(this.layout.points, partial(addPoint, ctx), this);
180+
ctx.stroke();
181+
}
182+
};
183+
184+
var makeErrorBars = function(ctx) {
185+
for (var i = 0; i < setCount; i++) {
186+
var setName = setNames[i];
187+
var color = colorScheme[i % colorCount];
188+
var strokeX = this.options.strokeColorTransform;
189+
190+
// setup graphics context
191+
context.save();
192+
context.strokeStyle = color.toRGBString();
193+
context.lineWidth = this.options.strokeWidth;
194+
var prevX = -1;
195+
var prevYs = [-1, -1];
196+
var count = 0;
197+
var yscale = this.layout.yscale;
198+
var errorTrapezoid = function(ctx_,point) {
199+
count++;
200+
if (point.name == setName) {
201+
var newYs = [ point.y - point.errorPlus * yscale,
202+
point.y + point.errorMinus * yscale ];
203+
newYs[0] = this.area.h * newYs[0] + this.area.y;
204+
newYs[1] = this.area.h * newYs[1] + this.area.y;
205+
if (prevX >= 0) {
206+
ctx_.moveTo(prevX, prevYs[0]);
207+
ctx_.lineTo(point.canvasx, newYs[0]);
208+
ctx_.lineTo(point.canvasx, newYs[1]);
209+
ctx_.lineTo(prevX, prevYs[1]);
210+
ctx_.closePath();
211+
}
212+
prevYs[0] = newYs[0];
213+
prevYs[1] = newYs[1];
214+
prevX = point.canvasx;
215+
}
216+
};
217+
// should be same color as the lines
218+
var err_color = color.colorWithAlpha(0.15);
219+
ctx.fillStyle = err_color.toRGBString();
220+
ctx.beginPath();
221+
MochiKit.Iter.forEach(this.layout.points, partial(errorTrapezoid, ctx), this);
222+
ctx.fill();
223+
}
224+
};
225+
226+
if (errorBars)
227+
bind(makeErrorBars, this)(context);
228+
bind(makePath, this)(context);
229+
context.restore();
230+
};

0 commit comments

Comments
 (0)