-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThnkrChart.js
225 lines (193 loc) · 8.45 KB
/
ThnkrChart.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
var ThnkrChart = function (gWith, gHight) {
var _self = this;
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
_self.guid = guid();
_self.DataSeries = [];
_self.Ticks = 20;
_self.TickDuration = 1000;
_self.MaxValue = 100;
_self.w = gWith;
_self.h = gHight;
_self.margin = { top: 50, right: 120, bottom: 60, left: 300 };
_self.width = _self.w - _self.margin.left - _self.margin.right;
_self.height = _self.h - _self.margin.top - _self.margin.bottom;
_self.xText = '';
_self.yText = '';
_self.titleText = '';
_self.chartSeries = {};
_self.Init = function() {
d3.select('#chart-' + _self.guid).remove();
_self.svg = d3.select("body").append("svg")
.attr("id", 'chart-' + _self.guid)
.attr("width", _self.w)
.attr("height", _self.h)
.append("g")
.attr("transform", "translate(" + _self.margin.left + "," + _self.margin.top + ")");
// Use Clipping to hide chart mechanics
_self.svg.append("defs").append("clipPath")
.attr("id", "clip-" + _self.guid)
.append("rect")
.attr("width", _self.width)
.attr("height", _self.height);
// Generate colors from DataSeries Names
_self.color = d3.scale.category10();
_self.color.domain(_self.DataSeries.map(function (d) { return d.Name; }));
// X,Y Scale
_self.xscale = d3.scale.linear().domain([0, _self.Ticks]).range([0, _self.width]);
_self.yscale = d3.scale.linear().domain([0, _self.MaxValue]).range([_self.height, 0]);
// X,Y Axis
_self.xAxis = d3.svg.axis()
.scale(d3.scale.linear()
.domain([0, _self.Ticks])
.range([_self.width, 0]))
.orient("bottom");
_self.yAxis = d3.svg.axis()
.scale(_self.yscale)
.orient("left");
// Line/Area Chart
_self.line = d3.svg.line()
.interpolate("basis")
.x(function (d, i) { return _self.xscale(i - 1); })
.y(function (d) { return _self.yscale(d.Value); });
_self.area = d3.svg.area()
.interpolate("basis")
.x(function (d, i) { return _self.xscale(i - 1); })
.y0(_self.height)
.y1(function (d) { return _self.yscale(d.Value); });
// Title
_self.Title = _self.svg.append("text")
.attr("id", "title-" + _self.guid)
.style("text-anchor", "middle")
.text(_self.titleText)
.attr("transform", function (d, i) { return "translate(" + _self.width / 2 + "," + -10 + ")"; });
// X axis text
_self.svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + _self.yscale(0) + ")")
.call(_self.xAxis)
.append("text")
.attr("id", "xName-" + _self.guid)
.attr("x", _self.width / 2)
.attr("dy", "3em")
.style("text-anchor", "middle")
.text(_self.xText);
// Y axis text
_self.svg.append("g")
.attr("class", "y axis")
.call(_self.yAxis)
.append("text")
.attr("id", "yName-" + _self.guid)
.attr("transform", "rotate(-90)")
.attr("y", 0)
.attr("x", -_self.height / 2)
.attr("dy", "-3em")
.style("text-anchor", "middle")
.text(_self.yText);
// Vertical grid lines
_self.svg.selectAll(".vline").data(d3.range(_self.Ticks)).enter()
.append("line")
.attr("x1", function (d) { return d * (_self.width / _self.Ticks); })
.attr("x2", function (d) { return d * (_self.width / _self.Ticks); })
.attr("y1", function (d) { return 0; })
.attr("y2", function (d) { return _self.height; })
.style("stroke", "#eee")
.style("opacity", .5)
.attr("clip-path", "url(#clip-" + _self.guid + ")")
.attr("transform", "translate(" + (_self.width / _self.Ticks) + "," + 0 + ")");
// Horizontal grid lines
_self.svg.selectAll(".hline").data(d3.range(_self.Ticks)).enter()
.append("line")
.attr("x1", function (d) { return 0; })
.attr("x2", function (d) { return _self.width; })
.attr("y1", function (d) { return d * (_self.height / (_self.MaxValue / 10)); })
.attr("y2", function (d) { return d * (_self.height / (_self.MaxValue / 10)); })
.style("stroke", "#eee")
.style("opacity", .5)
.attr("clip-path", "url(#clip-" + _self.guid + ")")
.attr("transform", "translate(" + 0 + "," + 0 + ")");
// Bind DataSeries to chart
_self.Series = _self.svg.selectAll(".Series")
.data(_self.DataSeries)
.enter().append("g")
.attr("clip-path", "url(#clip-" + _self.guid + ")")
.attr("class", "Series");
// Draw path from Series Data Points
_self.path = _self.Series.append("path")
.attr("class", "area")
.attr("d", function (d) { return _self.area(d.Data); })
.style("fill", function (d) { return _self.color(d.Name); })
.style("fill-opacity", .25)
.style("stroke", function (d) { return _self.color(d.Name); });
// Legend
_self.Legend = _self.svg.selectAll(".Legend")
.data(_self.DataSeries)
.enter().append("g")
.attr("class", "Legend");
_self.Legend.append("circle")
.attr("r", 4)
.style("fill", function (d) { return _self.color(d.Name); })
.style("fill-opacity", .5)
.style("stroke", function (d) { return _self.color(d.Name); })
.attr("transform", function (d, i) { return "translate(" + (_self.width + 6) + "," + (10 + (i * 20)) + ")"; });
_self.Legend.append("text")
.text(function (d) { return d.Name; })
.attr("dx", "0.5em")
.attr("dy", "0.25em")
.style("text-anchor", "start")
.attr("transform", function (d, i) { return "translate(" + (_self.width + 6) + "," + (10 + (i * 20)) + ")"; });
_self.tick = function (id) {
_self.thisTick = new Date();
var elapsed = parseInt(_self.thisTick - _self.lastTick);
var elapsedTotal = parseInt(_self.lastTick - _self.firstTick);
if (elapsed < 900 && elapsedTotal > 0) {
_self.lastTick = _self.thisTick;
return;
}
if (id < _self.DataSeries.length - 1 && elapsedTotal > 0) {
return;
}
_self.lastTick = _self.thisTick;
// Add new values
for (i in _self.DataSeries) {
_self.DataSeries[i].Data.push({ Value: _self.chartSeries[_self.DataSeries[i].Name] });
// Backfill missing values
while (_self.DataSeries[i].Data.length - 1 < _self.Ticks + 3) {
_self.DataSeries[i].Data.unshift({ Value: 0 })
}
}
d3.select("#yName-" + _self.guid).text(_self.yText);
d3.select("#xName-" + _self.guid).text(_self.xText);
d3.select("#title-" + _self.guid).text(_self.titleText);
_self.path
.attr("d", function (d) { return _self.area(d.Data); })
.attr("transform", null)
.transition()
.duration(_self.TickDuration)
.ease("linear")
.attr("transform", "translate(" + _self.xscale(-1) + ",0)")
.each("end", function (d, i) { _self.tick(i); });
// Remove oldest values
for (i in _self.DataSeries) {
_self.DataSeries[i].Data.shift();
}
}
_self.firstTick = new Date();
_self.lastTick = new Date();
_self.start = function () {
_self.firstTick = new Date();
_self.lastTick = new Date();
_self.tick(0);
}
_self.start();
}
_self.addSeries = function (SeriesName) {
_self.chartSeries[SeriesName] = 0;
_self.DataSeries.push({ Name: SeriesName, Data: [{ Value: 0 }] });
_self.Init();
}
}