-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineChart.tsx
365 lines (343 loc) · 11.3 KB
/
LineChart.tsx
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import * as d3 from "d3";
import { Line } from "d3";
import { ScaleContinuousNumeric } from "d3-scale";
import React from "react";
import { ENV_UP_TO_QA, getCurrentEnv } from "~/layout/AppConfiguration";
import {
AnimationConstants,
buildChartLayerClipIdUrl,
buildD3LineGenerator,
ChartCommon,
DataLabelCoordinate,
draw,
drawClippedChartLayer,
hasLabelOverlap,
LineData,
oneOffDummyData,
Point,
} from "~/layout/components/charts/ChartUtils";
import VerticalCartesianChart, {
CartesianChartProps,
} from "~/layout/components/charts/vertical-cartesian-chart/VerticalCartesianChart";
import { ChartAxis } from "../../../../../types";
import "../Chart.css";
/**
* Daydream line chart
* @par,
Transitionsam {CartesianChartProps} props
* @returns {JSX.Element}
* @constructor
*/
const LINE_STROKE_WIDTH = 3;
export default React.memo((props: CartesianChartProps): JSX.Element => {
return (
<VerticalCartesianChart
exploreBlockId={props.exploreBlockId}
chartType={props.chartType}
lines={props.lines}
minValue={props.minValue}
maxValue={props.maxValue}
domainAxisName={props.domainAxisName}
leftRangeAxisName={props.leftRangeAxisName}
rightRangeAxisName={props.rightRangeAxisName}
domainLabels={props.domainLabels}
domainAxisScale={props.domainAxisScale}
leftRangeAxisScale={props.leftRangeAxisScale}
rightRangeAxisScale={props.rightRangeAxisScale}
userLeftMin={props.userLeftMin}
userLeftMax={props.userLeftMax}
userRightMin={props.userRightMin}
userRightMax={props.userRightMax}
userBottomMin={props.userBottomMin}
userBottomMax={props.userBottomMax}
drawChart={drawLines}
drawDataLabels={drawLineDataLabels}
formatMap={props.formatMap}
/>
);
});
export const drawLines = (
common: ChartCommon
): d3.Selection<any, any, any, any> => {
const leftAxisLines = common.lines.filter(
(line: LineData) => line.axisPosition === ChartAxis.Left
);
const rightAxisLines = common.lines.filter(
(line: LineData) => line.axisPosition === ChartAxis.Right
);
myDrawLines(common, leftAxisLines, common.scales.leftRange, ChartAxis.Left);
return myDrawLines(
common,
rightAxisLines,
common.scales.rightRange,
ChartAxis.Right
);
};
const myDrawLines = (
common: ChartCommon,
axisedLines: LineData[],
axisRange: ScaleContinuousNumeric<any, any>,
axisPosition: ChartAxis
): d3.Selection<any, any, any, any> => {
/**
* - D3 provides 2D SVG rendering, manipulation, and interaction within the dom
* - https://d3js.org/
* - D3 has its own state management, referred to as data binding
* - Syntax is based on dot notation and call chains
* - Upon first render or additional new data, the "enter" method is used to draw new elements
* - Updates to existing elements are handled by the "update" method
* - Elements are removed by the "exit" method (currently just using default behavior)
* - Layers are drawn from the bottom up, like photoshop or the dom itself
*/
// Draw chart layer
const chartLayerClass = `lineLayer-${axisPosition}`;
const chartLayer = drawClippedChartLayer(chartLayerClass, common);
// Define a clipping path for the right-to-left curtain animation
const clipId: string = `curtain-clip-${common.exploreBlockId}`;
const clipClass = `curtainClip-${axisPosition}`;
const curtainClip: d3.Selection<any, any, any, any> = draw(
chartLayer,
clipClass,
oneOffDummyData,
(enter) =>
enter.append("clipPath").attr("class", clipClass).attr("id", clipId)
);
// Draw the animated right-to-left curtain using the clipping path
const curtainClass = `curtain-rect-${axisPosition}`;
draw(
curtainClip,
curtainClass,
oneOffDummyData,
(enter) =>
enter
.append("rect")
.attr("class", curtainClass)
.attr("width", 0)
.attr("height", common.height)
.transition()
.delay(AnimationConstants.curtainDelay)
.duration(AnimationConstants.curtainDuration)
.ease(d3.easeLinear)
.attr("width", common.width),
(update) => update.attr("width", common.width).attr("height", common.height)
);
// Draw lines
const d3LineGenerator: Line<any> = buildD3LineGenerator(
common.scales.domain,
axisRange
);
const getDashedLineConfig = (line: LineData) => (line.isDashed ? [5, 6] : 0);
const getLineColor = (line: LineData) => line.color;
const applyPropertiesForCypressAutomationTesting = function (line: LineData) {
if (ENV_UP_TO_QA.includes(getCurrentEnv())) {
// Only apply these properties in QA and below
const svgPathElement = d3.select(this);
svgPathElement.attr("data-line-label", line.label);
for (let i = 0; i < line.points.length; i++) {
const point = line.points[i];
const formattedValue = common.fieldMap[point.fieldId].displayFormatter(
point.value
);
svgPathElement.attr(
`exp-block-${common.exploreBlockId}-data-point-${i}`,
formattedValue
); // Apply datapoint values as multiple DOM properties eg data-point-0="123"
}
}
};
// New line rendering behavior and animation
const lineClass = `chartline-${axisPosition}`;
const onLineEnter = (enter) =>
enter
.append("path")
.attr("class", lineClass)
.attr("clip-path", `url(#${clipId})`) // Apply the animation clipping path on enter
.each(applyPropertiesForCypressAutomationTesting)
.attr("d", (line: LineData) => d3LineGenerator(line.points))
.attr("fill", "none")
.attr("stroke", getLineColor)
.attr("stroke-width", LINE_STROKE_WIDTH)
.attr("stroke-dasharray", getDashedLineConfig)
.attr("opacity", 0)
.transition()
.duration(AnimationConstants.duration)
.ease(AnimationConstants.easing)
.attr("opacity", 1);
// Existing line update rendering and animation
const chartLayerClipIdUrl = buildChartLayerClipIdUrl(common.exploreBlockId);
const onLineUpdate = (update) =>
update
.attr("clip-path", chartLayerClipIdUrl) // Apply the chart layer crop on update
.each(applyPropertiesForCypressAutomationTesting)
.transition()
.delay(80)
.duration(AnimationConstants.duration)
.ease(d3.easePoly)
.attr("opacity", 1)
.attr("d", (line: LineData) => d3LineGenerator(line.points))
.attr("stroke", getLineColor)
.attr("stroke-width", LINE_STROKE_WIDTH)
.attr("stroke-dasharray", getDashedLineConfig);
// D3 data binding: join the line data to the dom svg paths
// This way, we can update the data and the dom will be updated accordingly by the d3 engine
chartLayer
.selectAll(`.${lineClass}`)
.data(axisedLines)
.join(onLineEnter, onLineUpdate);
const pointLayerClass = `pointLayer-${axisPosition}`;
const pointLayer = draw(
chartLayer,
pointLayerClass,
axisedLines,
(enter) => enter.append("g").attr("class", pointLayerClass),
(update) => update
);
// Draw orphaned points
const pointClass: string = "point";
const calcPointX = (_: Point, index: number) =>
common.scales.domain(index.toString()) +
common.scales.domain.bandwidth() / 2;
const calcPointY = (point: Point) => axisRange(point.value);
const onPointEnter = (enter) =>
enter
.append("circle")
.attr("class", pointClass)
.attr("clip-path", `url(#${clipId})`)
.attr("cx", calcPointX)
.attr("cy", calcPointY)
.attr("r", 3)
.attr("fill", (d) => d.color);
const onPointUpdate = (update) =>
update
.transition()
.delay(80)
.duration(AnimationConstants.duration)
.ease(d3.easePoly)
.attr("cx", calcPointX)
.attr("cy", calcPointY)
.attr("fill", (d) => d.color);
// Draw points
pointLayer
.selectAll(`.${pointClass}`)
.data((line: LineData) =>
line.points.filter((point: Point) => point.isOrphan)
)
.join(onPointEnter, onPointUpdate);
return chartLayer;
};
/**
* Draw data labels for line charts
* @param {Selection<any, any, any, any>} layer
* @param {ChartCommon} common
* @param dataLabelHistory
* @param axisRelativeLines
* @param myRange
*/
export function drawLineDataLabels(
layer: d3.Selection<any, any, any, any>,
common: ChartCommon,
dataLabelHistory: DataLabelCoordinate[]
): void {
const leftAxisLines = common.lines.filter(
(line) => line.axisPosition === ChartAxis.Left
);
const rightAxisLines = common.lines.filter(
(line) => line.axisPosition === ChartAxis.Right
);
myDrawLineDataLabels(
layer,
common,
dataLabelHistory,
leftAxisLines,
common.scales.leftRange,
ChartAxis.Left
);
myDrawLineDataLabels(
layer,
common,
dataLabelHistory,
rightAxisLines,
common.scales.rightRange,
ChartAxis.Right
);
}
export function myDrawLineDataLabels(
chartLayer: d3.Selection<any, any, any, any>,
common: ChartCommon,
dataLabelHistory: DataLabelCoordinate[],
axisRelativeLines: LineData[],
myRange: ScaleContinuousNumeric<any, any>,
axisPosition: ChartAxis
): void {
const dataLabelLayerClass = `lineDataLabelLayer-${axisPosition}`;
const dataLabelLayer = draw(
chartLayer,
dataLabelLayerClass,
axisRelativeLines.filter((line) => line.displayLabels),
(enter) => enter.append("g").attr("class", dataLabelLayerClass),
(update) => update
);
const labelClass = `dataLabel`;
const delayScalar =
AnimationConstants.curtainDuration /
common.labels.indexedDomainLabels.length;
const labelDelay = (_, i): number =>
i * delayScalar + AnimationConstants.curtainDelay;
const calcPointX = (point: Point) =>
common.scales.domain(point.label) + common.scales.domain.bandwidth() / 2;
const calcPointY = (point: Point) =>
point.value ? myRange(point.value) - 5 : 0;
const getText = (point: Point) => {
const formattedValue = point.value
? common.formatMap[point.lineLabel](point.value)
: "";
const displayLabels = common.fieldMap[point.fieldId].displayLabels;
const allowLabelOverlap = common.fieldMap[point.fieldId].allowLabelOverlap;
if (displayLabels && allowLabelOverlap) {
return formattedValue;
} else if (displayLabels && !allowLabelOverlap) {
if (formattedValue !== "") {
const labelCoord: DataLabelCoordinate = {
x: calcPointX(point),
y: calcPointY(point),
};
if (hasLabelOverlap(labelCoord, dataLabelHistory)) {
return "";
} else {
dataLabelHistory.push(labelCoord);
return formattedValue;
}
}
}
return "";
};
const onDataLabelEnter = (enter) =>
enter
.append("text")
.text(getText)
.attr("text-anchor", "middle")
.attr("class", labelClass)
.attr("x", calcPointX)
.attr("y", (d: Point) => calcPointY(d) + 10)
.attr("opacity", 0)
.transition()
.delay(labelDelay)
.duration(1000)
.ease(d3.easePolyOut)
.attr("y", calcPointY)
.attr("opacity", 1);
const onDataLabelUpdate = (update) =>
update
.transition()
.duration(AnimationConstants.duration)
.ease(d3.easePoly)
.delay(80)
.text(getText)
.attr("x", calcPointX)
.attr("y", calcPointY)
.attr("opacity", 1);
dataLabelLayer
.selectAll(`.${labelClass}`)
.data((line: LineData) => line.points)
.join(onDataLabelEnter, onDataLabelUpdate);
}