sea-chart 2.0.14 → 2.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,12 @@ var _horizontalBarGroup = _interopRequireDefault(require("./horizontal-bar-group
21
21
  var _horizontalBarStack = _interopRequireDefault(require("./horizontal-bar-stack"));
22
22
  var _completeness = _interopRequireDefault(require("./completeness"));
23
23
  var _completenessGroup = _interopRequireDefault(require("./completeness-group"));
24
+ var _line = _interopRequireDefault(require("./line"));
25
+ var _lineGroup = _interopRequireDefault(require("./line-group"));
26
+ var _area = _interopRequireDefault(require("./area"));
27
+ var _areaGroup = _interopRequireDefault(require("./area-group"));
28
+ var _pie = _interopRequireDefault(require("./pie"));
29
+ var _ring = _interopRequireDefault(require("./ring"));
24
30
  var _basicNumberCard = _interopRequireDefault(require("./basic-number-card"));
25
31
  var _trend = _interopRequireDefault(require("./trend"));
26
32
  var _tableElement = _interopRequireDefault(require("./table-element"));
@@ -195,6 +201,42 @@ const Wrapper = _ref => {
195
201
  canvasStyle: canvasStyle
196
202
  }));
197
203
  }
204
+ case _constants.CHART_TYPE.LINE:
205
+ {
206
+ return /*#__PURE__*/_react.default.createElement(_line.default, Object.assign({}, baseProps, {
207
+ canvasStyle: canvasStyle
208
+ }));
209
+ }
210
+ case _constants.CHART_TYPE.LINE_GROUP:
211
+ {
212
+ return /*#__PURE__*/_react.default.createElement(_lineGroup.default, Object.assign({}, baseProps, {
213
+ canvasStyle: canvasStyle
214
+ }));
215
+ }
216
+ case _constants.CHART_TYPE.AREA:
217
+ {
218
+ return /*#__PURE__*/_react.default.createElement(_area.default, Object.assign({}, baseProps, {
219
+ canvasStyle: canvasStyle
220
+ }));
221
+ }
222
+ case _constants.CHART_TYPE.AREA_GROUP:
223
+ {
224
+ return /*#__PURE__*/_react.default.createElement(_areaGroup.default, Object.assign({}, baseProps, {
225
+ canvasStyle: canvasStyle
226
+ }));
227
+ }
228
+ case _constants.CHART_TYPE.PIE:
229
+ {
230
+ return /*#__PURE__*/_react.default.createElement(_pie.default, Object.assign({}, baseProps, {
231
+ canvasStyle: canvasStyle
232
+ }));
233
+ }
234
+ case _constants.CHART_TYPE.RING:
235
+ {
236
+ return /*#__PURE__*/_react.default.createElement(_ring.default, Object.assign({}, baseProps, {
237
+ canvasStyle: canvasStyle
238
+ }));
239
+ }
198
240
  case _constants.CHART_TYPE.BASIC_NUMBER_CARD:
199
241
  {
200
242
  return /*#__PURE__*/_react.default.createElement(_basicNumberCard.default, Object.assign({}, baseProps, {
@@ -0,0 +1,403 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+ var _react = _interopRequireDefault(require("react"));
10
+ var _propTypes = _interopRequireDefault(require("prop-types"));
11
+ var _lodashEs = require("lodash-es");
12
+ var d3 = _interopRequireWildcard(require("d3"));
13
+ var _classnames = _interopRequireDefault(require("classnames"));
14
+ var _dtableUtils = require("dtable-utils");
15
+ var _constants = require("../../constants");
16
+ var _colorRules = require("../../constants/color-rules");
17
+ var _utils = require("../../utils");
18
+ var _intl = _interopRequireDefault(require("../../intl"));
19
+ var _tooltip = _interopRequireDefault(require("../../components/tooltip"));
20
+ var _chartComponent = _interopRequireDefault(require("./chart-component"));
21
+ class LineGroup extends _chartComponent.default {
22
+ constructor(props) {
23
+ super(props);
24
+ this.handleResize = () => {
25
+ this.chart.node() && this.chart.node().remove();
26
+ this.createChart();
27
+ this.drawChart();
28
+ };
29
+ this.createChart = () => {
30
+ const {
31
+ chart
32
+ } = this.props;
33
+ const {
34
+ y_axis_show_label,
35
+ x_axis_show_label,
36
+ y_axis_show_value
37
+ } = chart.config;
38
+ const initConfig = {
39
+ insertPadding: 30,
40
+ marginLeft: y_axis_show_label ? 20 : 0,
41
+ marginBottom: x_axis_show_label ? 20 : 0,
42
+ marginTop: y_axis_show_value ? 15 : 0
43
+ };
44
+ this.initChart(this.container, chart === null || chart === void 0 ? void 0 : chart.id, initConfig);
45
+ };
46
+ this.drawChart = () => {
47
+ const {
48
+ customRender
49
+ } = this.props;
50
+ let {
51
+ result: data
52
+ } = this.props;
53
+ data = _utils.BaseUtils.formatEmptyName(data, '', _intl.default.get('Empty'));
54
+ if (!Array.isArray(data)) return;
55
+ this.draw(data);
56
+ this.renderAxisLabel(this.props.chart, this.props.tables, this.container);
57
+ (0, _utils.isFunction)(customRender) && customRender(this.chart);
58
+ };
59
+ this.showTooltip = (event, data, rectsWrapper) => {
60
+ const {
61
+ offsetX,
62
+ offsetY
63
+ } = event;
64
+ const {
65
+ title,
66
+ items
67
+ } = this.getTooltipItems(data, rectsWrapper);
68
+ const newTooltipData = {
69
+ title,
70
+ items
71
+ };
72
+ this.setState({
73
+ tooltipData: newTooltipData
74
+ });
75
+ this.setState({
76
+ toolTipPosition: {
77
+ offsetX,
78
+ offsetY
79
+ }
80
+ });
81
+ };
82
+ this.moveTooltip = (event, data, rectsWrapper) => {
83
+ const {
84
+ offsetX,
85
+ offsetY
86
+ } = event;
87
+ const {
88
+ title,
89
+ items
90
+ } = this.getTooltipItems(data, rectsWrapper);
91
+ const newTooltipData = {
92
+ title,
93
+ items
94
+ };
95
+ this.setState({
96
+ tooltipData: newTooltipData
97
+ });
98
+ this.setState({
99
+ toolTipPosition: {
100
+ offsetX,
101
+ offsetY
102
+ }
103
+ });
104
+ };
105
+ this.hiddenTooltip = event => {
106
+ this.setState({
107
+ toolTipPosition: null
108
+ });
109
+ };
110
+ this.getTooltipItems = (data, rectsWrapper) => {
111
+ var _rectsWrapper$selectA;
112
+ const title = !data.name && typeof data.name !== 'number' ? _intl.default.get(_constants.EMPTY_NAME) : data.name;
113
+ const circleList = (_rectsWrapper$selectA = rectsWrapper.selectAll('circle')) === null || _rectsWrapper$selectA === void 0 ? void 0 : _rectsWrapper$selectA.nodes();
114
+ const items = circleList.filter(circle => circle.getAttribute('data-name') === title).map(circle => {
115
+ return {
116
+ color: this.colorMap[circle.getAttribute('data-groupName')],
117
+ name: circle.getAttribute('data-groupName'),
118
+ value: circle.getAttribute('data-text')
119
+ };
120
+ });
121
+ return {
122
+ title,
123
+ items
124
+ };
125
+ };
126
+ this.draw = data => {
127
+ const {
128
+ chart,
129
+ globalTheme,
130
+ columnGroupbyColumn,
131
+ chartColorTheme
132
+ } = this.props;
133
+ const theme = _constants.CHART_THEME_COLOR[globalTheme];
134
+ const {
135
+ display_goal_line,
136
+ goal_value,
137
+ goal_label
138
+ } = chart.style_config || {};
139
+ const {
140
+ y_axis_show_value,
141
+ label_font_size,
142
+ line_type,
143
+ y_axis_max,
144
+ y_axis_min,
145
+ y_axis_auto_range,
146
+ color_theme
147
+ } = chart.config;
148
+ const {
149
+ width: chartWidth,
150
+ height: chartHeight,
151
+ insertPadding,
152
+ marginTop
153
+ } = this.chartBoundingClientRect;
154
+ const useSingleSelectColumnColor = (columnGroupbyColumn === null || columnGroupbyColumn === void 0 ? void 0 : columnGroupbyColumn.type) === _dtableUtils.CellType.SINGLE_SELECT && color_theme === _colorRules.SUPPORT_SINGLE_SELECT_THEMES_OPTIONS.SINGLE_SELECT_COLUMN_COLORS;
155
+ useSingleSelectColumnColor ? this.setSingleSelectColorMap(data) : this.setColorMap(data, chartColorTheme);
156
+
157
+ // Y axis
158
+ const niceEnd = d3.nice(0, d3.max(data, d => d.value), 5)[1];
159
+ const y = d3.scaleLinear().domain(y_axis_auto_range ? [0, niceEnd] : [y_axis_min || 0, y_axis_max || niceEnd]).range([chartHeight - insertPadding, insertPadding + marginTop]);
160
+ this.chart.append('g').attr('transform', "translate(".concat(insertPadding, ", 0)")).call(d3.axisLeft(y).tickSizeInner(0).ticks(5).tickFormat(d => d)).call(g => this.drawYaxis(g, theme));
161
+
162
+ // X axis
163
+ const xDomain = data.map(item => item.name);
164
+ const x = d3.scaleBand().domain(xDomain).range([insertPadding + this.horizontalOverflowOffset, chartWidth - insertPadding]).paddingInner(0.4).paddingOuter(0.1);
165
+ this.ticksWrapper = this.chart.append('g').attr('transform', "translate(0, ".concat(chartHeight - insertPadding, ")")).call(d3.axisBottom(x).tickSizeOuter(0).tickSizeInner(5)).call(g => {
166
+ this.ticksAddName(g);
167
+ g.selectAll('.domain').attr('stroke', theme.XAxisColor);
168
+ g.selectAll('.tick line').attr('stroke', theme.XAxisColor);
169
+ g.selectAll('text').attr('font-size', theme.fontSize);
170
+ g.selectAll('text').attr('fill', theme.textColor);
171
+ this.checkTickOverlap(g);
172
+ });
173
+
174
+ // Line
175
+ const rectsWrapper = this.chart.append('g').attr('class', "rects-wrapper-".concat(chart === null || chart === void 0 ? void 0 : chart.id));
176
+ const circleDatas = [];
177
+ const groupData = d3.groups(data, d => d.group_name);
178
+ groupData.forEach(item => {
179
+ const circleData = xDomain.map(() => ({}));
180
+ const line = d3.line().x((d, index) => {
181
+ const xVal = x(d.name) + x.bandwidth() / 2;
182
+ circleData[index]['x'] = xVal;
183
+ circleData[index]['name'] = d.name;
184
+ return xVal;
185
+ }).y((d, index) => {
186
+ const yVal = y(d.value);
187
+ circleData[index]['y'] = yVal;
188
+ circleData[index]['value'] = d.value;
189
+ return yVal;
190
+ }).curve(line_type === _constants.CHART_LINE_TYPES[1] ? d3.curveBumpX : d3.curveLinear);
191
+ const group_name = item[0];
192
+ const data = item[1];
193
+ rectsWrapper.append('path').attr('fill', 'none').attr('stroke', this.colorMap[group_name]).attr('stroke-width', 2).attr('d', () => line(data));
194
+ circleDatas.push({
195
+ group_name,
196
+ circleData
197
+ });
198
+ });
199
+
200
+ // circle
201
+ circleDatas.forEach(_ref => {
202
+ let {
203
+ group_name,
204
+ circleData
205
+ } = _ref;
206
+ circleData.forEach(item => {
207
+ if (Object.keys(item).length > 0) {
208
+ rectsWrapper.append('circle').attr('cx', item.x).attr('cy', item.y).attr('r', 3).attr('fill', this.colorMap[group_name]).attr('opacity', y_axis_show_value ? 1 : 0).attr('data-groupName', group_name).attr('data-text', item.value).attr('data-name', item.name).call(g => {
209
+ // circle label
210
+ if (y_axis_show_value) {
211
+ const curCircleEl = g.node();
212
+ rectsWrapper.append('text').attr('fill', theme.labelColor).attr('font-size', _utils.BaseUtils.getLabelFontSize(label_font_size)).text(curCircleEl.getAttribute('data-text')).call(g => {
213
+ const {
214
+ width
215
+ } = g.node().getBoundingClientRect();
216
+ const translateX = Number(curCircleEl.getAttribute('cx')) - width / 2;
217
+ const translateY = Number(curCircleEl.getAttribute('cy')) - 10;
218
+ g.attr('transform', "translate(".concat(translateX, ", ").concat(translateY, ")"));
219
+ });
220
+ }
221
+ }).on('click', (event, data) => {
222
+ this.props.toggleRecords(data);
223
+ });
224
+ }
225
+ });
226
+ });
227
+ this.chart.on('mouseover', event => {
228
+ this.updateCircleAndTickStyle({
229
+ event,
230
+ state: 'zoomIn',
231
+ circleDatas,
232
+ rectsWrapper,
233
+ eventState: 'over'
234
+ });
235
+ }).on('mousemove', event => {
236
+ this.updateCircleAndTickStyle({
237
+ event,
238
+ state: 'zoomIn',
239
+ circleDatas,
240
+ rectsWrapper,
241
+ eventState: 'move'
242
+ });
243
+ }).on('mouseleave', event => {
244
+ this.updateCircleAndTickStyle({
245
+ event,
246
+ state: 'zoomOut',
247
+ circleDatas,
248
+ rectsWrapper,
249
+ eventState: 'leave'
250
+ });
251
+ });
252
+ if (display_goal_line && goal_label && goal_value) {
253
+ this.setDispalyGoalLine(goal_label, goal_value, insertPadding, y);
254
+ }
255
+ this.setLegend({
256
+ legendName: 'group_name',
257
+ theme,
258
+ legendPosition: 'top-right',
259
+ data
260
+ });
261
+ };
262
+ this.updateCircleAndTickStyle = _ref2 => {
263
+ var _rectsWrapper$selectA2, _this$ticksWrapper$se;
264
+ let {
265
+ event,
266
+ state,
267
+ circleDatas,
268
+ rectsWrapper,
269
+ eventState
270
+ } = _ref2;
271
+ const {
272
+ y_axis_show_value
273
+ } = this.props.chart.config;
274
+ const {
275
+ height: chartHeight,
276
+ insertPadding,
277
+ marginTop
278
+ } = this.chartBoundingClientRect;
279
+ const {
280
+ offsetX
281
+ } = event;
282
+ const minDistanceArr = [];
283
+ circleDatas.forEach(_ref3 => {
284
+ let {
285
+ group_name,
286
+ circleData
287
+ } = _ref3;
288
+ const item = this.getMinDistanceItem(offsetX, circleData);
289
+ minDistanceArr.push(item);
290
+ });
291
+ const minIndex = d3.minIndex(minDistanceArr, d => d.distance);
292
+ const minDistanceItem = minDistanceArr[minIndex];
293
+
294
+ // circle
295
+ const circleList = (_rectsWrapper$selectA2 = rectsWrapper.selectAll('circle')) === null || _rectsWrapper$selectA2 === void 0 ? void 0 : _rectsWrapper$selectA2.nodes();
296
+ if (circleList.length !== 0) {
297
+ if (state === 'zoomIn') {
298
+ circleList.forEach(circle => {
299
+ const name = circle.getAttribute('data-name');
300
+ if (name === minDistanceItem.name) {
301
+ d3.select(circle).attr('opacity', 1);
302
+ d3.select(circle).attr('r', 5);
303
+ } else {
304
+ d3.select(circle).attr('opacity', 0);
305
+ d3.select(circle).attr('r', 3);
306
+ }
307
+ });
308
+ } else {
309
+ circleList.forEach(circle => {
310
+ !y_axis_show_value && d3.select(circle).attr('opacity', 0);
311
+ d3.select(circle).attr('r', 3);
312
+ });
313
+ }
314
+ }
315
+
316
+ // tick
317
+ const ticks = (_this$ticksWrapper$se = this.ticksWrapper.selectAll('.tick line')) === null || _this$ticksWrapper$se === void 0 ? void 0 : _this$ticksWrapper$se.nodes();
318
+ if (ticks.length !== 0) {
319
+ if (state === 'zoomIn') {
320
+ ticks.forEach(tick => {
321
+ const name = tick.getAttribute('data-name');
322
+ if (name === minDistanceItem.name) {
323
+ d3.select(tick).attr('y2', -(chartHeight - insertPadding * 2 - marginTop));
324
+ } else {
325
+ d3.select(tick).attr('y2', 0);
326
+ }
327
+ });
328
+ } else {
329
+ ticks.forEach(tick => {
330
+ d3.select(tick).attr('y2', 0);
331
+ });
332
+ }
333
+ }
334
+
335
+ // tooltip
336
+ if (eventState === 'over') {
337
+ this.showTooltip(event, minDistanceItem, rectsWrapper);
338
+ }
339
+ if (eventState === 'move') {
340
+ this.moveTooltip(event, minDistanceItem, rectsWrapper);
341
+ }
342
+ if (eventState === 'leave') {
343
+ this.hiddenTooltip();
344
+ }
345
+ };
346
+ this.chart = null;
347
+ this.state = {
348
+ tooltipData: null,
349
+ toolTipPosition: null
350
+ };
351
+ }
352
+ componentDidMount() {
353
+ this.createChart();
354
+ this.drawChart();
355
+ this.debouncedHandleResize = (0, _lodashEs.debounce)(this.handleResize, 300);
356
+ window.addEventListener('resize', this.debouncedHandleResize);
357
+ }
358
+ componentDidUpdate(prevProps) {
359
+ super.componentDidUpdate(prevProps);
360
+ if (_utils.BaseUtils.shouldChartComponentUpdate(prevProps, this.props)) {
361
+ this.createChart();
362
+ this.drawChart();
363
+ }
364
+ }
365
+ componentWillUnmount() {
366
+ this.chart.node() && this.chart.node().remove();
367
+ window.removeEventListener('resize', this.debouncedHandleResize);
368
+ }
369
+ render() {
370
+ const {
371
+ chart
372
+ } = this.props;
373
+ const {
374
+ tooltipData,
375
+ toolTipPosition
376
+ } = this.state;
377
+ return /*#__PURE__*/_react.default.createElement("div", {
378
+ ref: ref => this.container = ref,
379
+ className: (0, _classnames.default)('sea-chart-container', {
380
+ 'show-x-axis-label': this.isShowXAxisLabel(chart),
381
+ 'show-y-axis-label': this.isShowYAxisLabel(chart)
382
+ })
383
+ }, /*#__PURE__*/_react.default.createElement(_tooltip.default, {
384
+ tooltipData: tooltipData,
385
+ toolTipPosition: toolTipPosition,
386
+ chart: this.chart
387
+ }));
388
+ }
389
+ }
390
+ LineGroup.propTypes = {
391
+ canvasStyle: _propTypes.default.object,
392
+ chart: _propTypes.default.object,
393
+ groupbyColumn: _propTypes.default.object,
394
+ columnGroupbyColumn: _propTypes.default.object,
395
+ summaryColumn: _propTypes.default.object,
396
+ result: _propTypes.default.array,
397
+ tables: _propTypes.default.array,
398
+ globalTheme: _propTypes.default.string,
399
+ chartColorTheme: _propTypes.default.string,
400
+ toggleRecords: _propTypes.default.func,
401
+ customRender: _propTypes.default.func
402
+ };
403
+ var _default = exports.default = LineGroup;