-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineChart.js
342 lines (303 loc) · 9.89 KB
/
LineChart.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"use strict";
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var React = require("react");
var d3 = require("d3");
var Chart = require("./Chart");
var Axis = require("./Axis");
var Path = require("./Path");
var Tooltip = require("./Tooltip");
var DefaultPropsMixin = require("./DefaultPropsMixin");
var HeightWidthMixin = require("./HeightWidthMixin");
var ArrayifyMixin = require("./ArrayifyMixin");
var AccessorMixin = require("./AccessorMixin");
var DefaultScalesMixin = require("./DefaultScalesMixin");
var TooltipMixin = require("./TooltipMixin");
var DataSet = React.createClass({
displayName: "DataSet",
propTypes: {
data: React.PropTypes.array.isRequired,
line: React.PropTypes.func.isRequired,
colorScale: React.PropTypes.func.isRequired
},
render: function render() {
var _props = this.props;
var width = _props.width;
var height = _props.height;
var data = _props.data;
var line = _props.line;
var strokeWidth = _props.strokeWidth;
var strokeLinecap = _props.strokeLinecap;
var strokeDasharray = _props.strokeDasharray;
var colorScale = _props.colorScale;
var translateX = _props.translateX;
var translateY = _props.translateY;
var values = _props.values;
var label = _props.label;
var onMouseEnter = _props.onMouseEnter;
var onMouseLeave = _props.onMouseLeave;
var sizeId = width + "x" + height;
var lines = data.map(function (stack, index) {
console.log("====================================== index :"+ index );
console.log("======================================= stack:");
console.log(stack);
return React.createElement(Path, {
key: "" + label(stack) + "." + index,
className: "line",
d: line(values(stack)),
stroke: colorScale(label(stack)),
strokeWidth: typeof strokeWidth === "function" ? strokeWidth(label(stack)) : strokeWidth,
strokeLinecap: typeof strokeLinecap === "function" ? strokeLinecap(label(stack)) : strokeLinecap,
strokeDasharray: typeof strokeDasharray === "function" ? strokeDasharray(label(stack)) : strokeDasharray,
data: values(stack),
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave,
style: { clipPath: "url(#lineClip_" + sizeId + ")" }
});
});
/*
The <rect> below is needed in case we want to show the tooltip no matter where on the chart the mouse is.
Not sure if this should be used.
*/
return React.createElement(
"g",
{transform : "translate("+translateX+","+translateY+")", className : "anmation" },
React.createElement(
"defs",
null,
React.createElement(
"clipPath",
{ id: "lineClip_" + sizeId },
React.createElement("rect", { width: width, height: height })
)
),
lines,
React.createElement("rect", { width: width, height: height, fill: "none", stroke: "none", style: { pointerEvents: "all" },
onMouseMove: function (evt) {
onMouseEnter(evt, data);
},
onMouseLeave: function (evt) {
onMouseLeave(evt);
}
})
);
}
});
var LineChart = React.createClass({
displayName: "LineChart",
mixins: [DefaultPropsMixin, HeightWidthMixin, ArrayifyMixin, AccessorMixin, DefaultScalesMixin, TooltipMixin],
propTypes: {
interpolate: React.PropTypes.string,
defined: React.PropTypes.func
},
getInitialState: function getInitialState(){
return {
translateX: 0,
translateY: 0
}
},
getDefaultProps: function getDefaultProps() {
return {
interpolate: "linear",
defined: function () {
return true;
},
shape: "circle",
shapeColor: null
};
},
/*
The code below supports finding the data values for the line closest to the mouse cursor.
Since it gets all events from the Rect overlaying the Chart the tooltip gets shown everywhere.
For now I don't want to use this method.
*/
_tooltipHtml: function _tooltipHtml(data, position) {
var _props = this.props;
var x = _props.x;
var y0 = _props.y0;
var y = _props.y;
var values = _props.values;
var label = _props.label;
var xScale = this._xScale;
var yScale = this._yScale;
var xValueCursor = xScale.invert(position[0]);
var yValueCursor = yScale.invert(position[1]);
var xBisector = d3.bisector(function (e) {
return x(e);
}).left;
var valuesAtX = data.map(function (stack) {
var idx = xBisector(values(stack), xValueCursor);
var indexRight = idx === values(stack).length ? idx - 1 : idx;
var valueRight = x(values(stack)[indexRight]);
var indexLeft = idx === 0 ? idx : idx - 1;
var valueLeft = x(values(stack)[indexLeft]);
var index = undefined;
if (Math.abs(xValueCursor - valueRight) < Math.abs(xValueCursor - valueLeft)) {
index = indexRight;
} else {
index = indexLeft;
}
return { label: label(stack), value: values(stack)[index] };
});
valuesAtX.sort(function (a, b) {
return y(a.value) - y(b.value);
});
var yBisector = d3.bisector(function (e) {
return y(e.value);
}).left;
var yIndex = yBisector(valuesAtX, yValueCursor);
var yIndexRight = yIndex === valuesAtX.length ? yIndex - 1 : yIndex;
var yIndexLeft = yIndex === 0 ? yIndex : yIndex - 1;
var yValueRight = y(valuesAtX[yIndexRight].value);
var yValueLeft = y(valuesAtX[yIndexLeft].value);
var index = undefined;
if (Math.abs(yValueCursor - yValueRight) < Math.abs(yValueCursor - yValueLeft)) {
index = yIndexRight;
} else {
index = yIndexLeft;
}
this._tooltipData = valuesAtX[index];
var html = this.props.tooltipHtml(valuesAtX[index].label, valuesAtX[index].value);
var xPos = xScale(valuesAtX[index].value.x);
var yPos = yScale(valuesAtX[index].value.y);
return [html, xPos, yPos];
},
/*
_tooltipHtml(data, position) {
let {x, y0, y, values, label} = this.props;
let [xScale, yScale] = [this._xScale, this._yScale];
let xValueCursor = xScale.invert(position[0]);
let yValueCursor = yScale.invert(position[1]);
let xBisector = d3.bisector(e => { return x(e); }).left;
let xIndex = xBisector(data, xScale.invert(position[0]));
let indexRight = xIndex == data.length ? xIndex - 1 : xIndex;
let valueRight = x(data[indexRight]);
let indexLeft = xIndex == 0 ? xIndex : xIndex - 1;
let valueLeft = x(data[indexLeft]);
let index;
if (Math.abs(xValueCursor - valueRight) < Math.abs(xValueCursor - valueLeft)) {
index = indexRight;
} else {
index = indexLeft;
}
let yValue = y(data[index]);
let cursorValue = d3.round(yScale.invert(position[1]), 2);
return this.props.tooltipHtml(yValue, cursorValue);
},
*/
/*
stroke,
strokeWidth,
strokeLinecap,
strokeDasharray,
*/
render: function render() {
var _this = this;
var _props = this.props;
var height = _props.height;
var width = _props.width;
var margin = _props.margin;
var colorScale = _props.colorScale;
var interpolate = _props.interpolate;
var defined = _props.defined;
var stroke = _props.stroke;
var values = _props.values;
var translateX = this.state.translateX;
var translateY = this.state.translateY;
var isAnmiation = _props.isAnmiation;
var label = _props.label;
var x = _props.x;
var y = _props.y;
var xAxis = _props.xAxis;
var yAxis = _props.yAxis;
var shape = _props.shape;
var shapeColor = _props.shapeColor;
var data = this._data;
var innerWidth = this._innerWidth;
var innerHeight = this._innerHeight;
var xScale = this._xScale;
var yScale = this._yScale;
var xIntercept = this._xIntercept;
var yIntercept = this._yIntercept;
console.log("============================================ xScale:");
console.log(xScale);
console.log("================================================ yScale:");
console.log(yScale);
var line = d3.svg.line().x(function (e) {
return xScale(x(e));
}).y(function (e) {
return yScale(y(e));
}).interpolate(interpolate).defined(defined);
var tooltipSymbol = undefined;
if (!this.state.tooltip.hidden) {
var symbol = d3.svg.symbol().type(shape);
var symbolColor = shapeColor ? shapeColor : colorScale(this._tooltipData.label);
var translate = this._tooltipData ? "translate(" + xScale(x(this._tooltipData.value)) + ", " + yScale(y(this._tooltipData.value)) + ")" : "";
tooltipSymbol = this.state.tooltip.hidden ? null : React.createElement("path", {
className: "dot",
d: symbol(),
transform: translate,
fill: symbolColor,
onMouseEnter: function (evt) {
_this.onMouseEnter(evt, data);
},
onMouseLeave: function (evt) {
_this.onMouseLeave(evt);
}
});
}
return React.createElement(
"div",
null,
React.createElement(
Chart,
{ height: height, width: width, margin: margin },
React.createElement(DataSet, _extends({
height: innerHeight,
width: innerWidth,
data: data,
line: line,
colorScale: colorScale,
values: values,
label: label,
translateX: translateX,
translateY: translateY,
onMouseEnter: this.onMouseEnter,
onMouseLeave: this.onMouseLeave
}, stroke)),
React.createElement(Axis, _extends({
className: "x axis",
orientation: "bottom",
scale: xScale,
height: innerHeight,
width: innerWidth,
zero: yIntercept
}, xAxis)),
React.createElement(Axis, _extends({
className: "y axis",
orientation: "left",
scale: yScale,
height: innerHeight,
width: innerWidth,
zero: xIntercept
}, yAxis)),
this.props.children,
tooltipSymbol
),
React.createElement(Tooltip, this.state.tooltip)
);
},
componentDidMount(){
let isAnmiation = this.props.isAnmiation,
self = this,
translateX = this.state.translateX;
if(isAnmiation){
// setInterval(function(){
// translateX = translateX -1;
// self.setState({
// translateX : translateX
// });
// },300);
}
}
});
module.exports = LineChart;