sea-chart 2.0.0 → 2.0.1-alpha.1

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.
@@ -42,7 +42,7 @@ const Settings = _ref => {
42
42
  departments,
43
43
  globalTheme,
44
44
  lang,
45
- isRenderByD3
45
+ hideTypeToggle
46
46
  } = _ref;
47
47
  const [type, setType] = (0, _react.useState)(_constants.CHART_SETTINGS_TYPE.DATA);
48
48
  const [labelColorConfigs, setLabelColorConfigs] = (0, _react.useState)([]);
@@ -99,7 +99,7 @@ const Settings = _ref => {
99
99
  }, /*#__PURE__*/_react.default.createElement(_contexts.settingsContext.Provider, {
100
100
  value: {
101
101
  hideTitleStyleSetting,
102
- isRenderByD3
102
+ hideTypeToggle
103
103
  }
104
104
  }, /*#__PURE__*/_react.default.createElement("div", {
105
105
  className: "sea-chart-settings"
@@ -145,6 +145,6 @@ const settingsPropTypes = exports.settingsPropTypes = {
145
145
  hideTitleStyleSetting: _propTypes.default.bool,
146
146
  children: _propTypes.default.any,
147
147
  onChange: _propTypes.default.func.isRequired,
148
- isRenderByD3: _propTypes.default.bool
148
+ hideTypeToggle: _propTypes.default.bool
149
149
  };
150
150
  var _default = exports.default = Settings;
@@ -22,7 +22,7 @@ const ChartType = _ref => {
22
22
  dataSources
23
23
  } = _ref;
24
24
  const {
25
- isRenderByD3
25
+ hideTypeToggle
26
26
  } = (0, _react.useContext)(_contexts.settingsContext);
27
27
  const [isDialogShow, setDialogShow] = (0, _react.useState)(false);
28
28
  const openTypesDialog = (0, _react.useCallback)(event => {
@@ -62,9 +62,9 @@ const ChartType = _ref => {
62
62
  }, /*#__PURE__*/_react.default.createElement(_reactstrap.Label, null, _intl.default.get('Chart_type')), /*#__PURE__*/_react.default.createElement("div", {
63
63
  className: "sea-chart-selected-type-container d-flex align-items-center",
64
64
  style: {
65
- backgroundColor: isRenderByD3 ? '#f5f5f5' : '#fff'
65
+ backgroundColor: hideTypeToggle ? '#f5f5f5' : '#fff'
66
66
  }
67
- }, /*#__PURE__*/_react.default.createElement("span", null, _intl.default.get(_constants.CHART_TYPE_SHOW[type])), !isRenderByD3 && /*#__PURE__*/_react.default.createElement("div", {
67
+ }, /*#__PURE__*/_react.default.createElement("span", null, _intl.default.get(_constants.CHART_TYPE_SHOW[type])), !hideTypeToggle && /*#__PURE__*/_react.default.createElement("div", {
68
68
  className: "sea-chart-type-icon-container",
69
69
  onClick: openTypesDialog
70
70
  }, /*#__PURE__*/_react.default.createElement(_components.Icon, {
@@ -257,7 +257,6 @@ class View extends _react.default.PureComponent {
257
257
  globalTheme,
258
258
  chartColorTheme,
259
259
  isCalculateByView,
260
- isRenderByD3,
261
260
  dtableStoreValue,
262
261
  integratedEventTypes,
263
262
  integratedEventBus,
@@ -326,7 +325,6 @@ class View extends _react.default.PureComponent {
326
325
  customRender: render,
327
326
  globalTheme: globalTheme ? globalTheme : _constants.THEME_NAME_MAP.LIGHT,
328
327
  isCalculateByView: isCalculateByView,
329
- isRenderByD3: isRenderByD3,
330
328
  isBigScreenPlugin: isBigScreenPlugin,
331
329
  chartColorTheme: chartColorTheme,
332
330
  integratedEventTypes: integratedEventTypes,
@@ -355,7 +353,6 @@ const propTypes = exports.propTypes = {
355
353
  departments: _propTypes.default.array,
356
354
  dtableStoreValue: _propTypes.default.object,
357
355
  isCalculateByView: _propTypes.default.bool,
358
- isRenderByD3: _propTypes.default.bool,
359
356
  // event
360
357
  integratedEventTypes: _propTypes.default.object,
361
358
  integratedEventBus: _propTypes.default.object,
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
3
4
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
@@ -8,7 +9,7 @@ exports.default = void 0;
8
9
  var _react = _interopRequireDefault(require("react"));
9
10
  var _propTypes = _interopRequireDefault(require("prop-types"));
10
11
  var _lodashEs = require("lodash-es");
11
- var _classnames = _interopRequireDefault(require("classnames"));
12
+ var d3 = _interopRequireWildcard(require("d3"));
12
13
  var _constants = require("../../constants");
13
14
  var _utils = require("../../utils");
14
15
  var _colorUtils = require("../../utils/color-utils");
@@ -18,124 +19,202 @@ class Bar extends _chartComponent.default {
18
19
  constructor(props) {
19
20
  super(props);
20
21
  this.handleResize = () => {
21
- this.chart && this.chart.destroy();
22
+ this.chart.node() && this.chart.node().remove();
22
23
  this.createChart();
23
24
  this.drawChart();
24
25
  };
25
26
  this.createChart = () => {
27
+ this.initChart(this.container);
28
+ };
29
+ this.drawChart = () => {
26
30
  const {
27
- chart
31
+ customRender
32
+ } = this.props;
33
+ let {
34
+ result: data
35
+ } = this.props;
36
+ data = _utils.BaseUtils.formatEmptyName(data, '', _intl.default.get('Empty'));
37
+ if (!Array.isArray(data)) return;
38
+ this.draw(data);
39
+ this.renderAxisLabel(this.props.chart, this.props.tables, this.container);
40
+ (0, _utils.isFunction)(customRender) && customRender(this.chart);
41
+ };
42
+ this.getFillColor = data => {
43
+ const {
44
+ chart,
45
+ chartColorTheme
28
46
  } = this.props;
29
47
  const {
30
- y_axis_show_value
48
+ y_axis_label_color,
49
+ color_option,
50
+ y_axis_label_color_rules
31
51
  } = chart.config;
32
- const appendPadding = [y_axis_show_value ? 17 : 0, 0, 0, 0]; // used to display value on the top
33
- this.initChart(this.container, {
34
- appendPadding
52
+ let chartBarColor = y_axis_label_color || _constants.CHART_STYLE_COLORS[0];
53
+ if (chartColorTheme) {
54
+ chartBarColor = _utils.BaseUtils.getCurrentTheme(chartColorTheme).colors[0];
55
+ }
56
+ const colorRules = color_option === _constants.TYPE_COLOR_USING.USE_RULES && y_axis_label_color_rules && (0, _colorUtils.getConvertedColorRules)(y_axis_label_color_rules);
57
+ const color = (0, _colorUtils.getLabelColor)({
58
+ chart: chart.config,
59
+ colorRules,
60
+ value: data.value
61
+ });
62
+ return color || chartBarColor;
63
+ };
64
+ this.showTooltip = (event, data, title) => {
65
+ const {
66
+ offsetX,
67
+ offsetY
68
+ } = event;
69
+ const newTooltipData = {
70
+ title: title,
71
+ items: [{
72
+ color: this.getFillColor(data),
73
+ name: data.name,
74
+ value: data.value
75
+ }]
76
+ };
77
+ this.setState({
78
+ tooltipData: newTooltipData
79
+ });
80
+ this.setState({
81
+ toolTipPosition: {
82
+ offsetX,
83
+ offsetY
84
+ }
35
85
  });
36
- this.chart.on('interval:click', e => {
37
- this.props.toggleRecords(e.data.data);
86
+ };
87
+ this.moveTooltip = event => {
88
+ const {
89
+ offsetX,
90
+ offsetY
91
+ } = event;
92
+ this.setState({
93
+ toolTipPosition: {
94
+ offsetX,
95
+ offsetY
96
+ }
38
97
  });
39
98
  };
40
- this.clearChart = () => {
41
- this.chart.annotation().clear(true);
42
- this.chart.clear(true);
99
+ this.hiddenTooltip = event => {
100
+ this.setState({
101
+ toolTipPosition: null
102
+ });
43
103
  };
44
- this.drawChart = () => {
45
- let {
46
- result: data
104
+ this.setActiveAndInActiveState = (state, data) => {
105
+ const {
106
+ chart
47
107
  } = this.props;
48
- data = _utils.BaseUtils.formatEmptyName(data, '', _intl.default.get('Empty'));
49
- if (!Array.isArray(data)) return;
50
- this.loadData(data);
51
- this.draw(data);
52
- requestAnimationFrame(() => {
53
- this.chart.render();
54
- this.renderAxisLabel(this.props.chart, this.props.tables);
108
+ const paths = d3.selectAll(".rects-wrapper-".concat(chart === null || chart === void 0 ? void 0 : chart.id, " path"));
109
+ if (state === 'active') {
110
+ paths.nodes().forEach(path => {
111
+ d3.select(path).attr('opacity', 1);
112
+ });
113
+ return;
114
+ }
115
+ paths.nodes().forEach(path => {
116
+ if (path.id !== data.slugId) {
117
+ d3.select(path).attr('opacity', 0.3);
118
+ }
55
119
  });
56
120
  };
57
121
  this.draw = data => {
58
122
  const {
59
123
  chart,
60
- summaryColumn,
61
124
  globalTheme,
62
- customRender,
63
- chartColorTheme,
64
125
  tables
65
126
  } = this.props;
66
127
  const theme = _constants.CHART_THEME_COLOR[globalTheme];
128
+ const {
129
+ display_goal_line,
130
+ goal_value,
131
+ goal_label
132
+ } = chart.style_config || {};
67
133
  const {
68
134
  y_axis_summary_type,
69
135
  y_axis_summary_column_key,
70
136
  y_axis_column_key,
71
- y_axis_summary_method,
72
- y_axis_label_color,
73
137
  y_axis_show_value,
74
138
  label_font_size,
75
- color_option,
76
- y_axis_label_color_rules,
77
- table_id
139
+ table_id,
140
+ y_axis_max,
141
+ y_axis_min,
142
+ y_axis_auto_range
78
143
  } = chart.config;
144
+ const tooltipTitle = this.getTitle(tables, table_id, y_axis_summary_type, y_axis_column_key || y_axis_summary_column_key);
79
145
  const {
80
- display_goal_line,
81
- goal_value,
82
- goal_label
83
- } = chart.style_config || {};
84
- // y-axis label width:6 + 10
85
- const chartWidth = this.chart.width - 6 - 10; // 10 is y-axis number width and 6 is gap between number and y axis line
86
- const singleBarWidth = Math.round(chartWidth / (2 * data.length));
87
- const singleBarRadius = Math.min(singleBarWidth / 5, 10);
88
- let chartBarColor = y_axis_label_color || _constants.CHART_STYLE_COLORS[0];
89
- if (chartColorTheme) {
90
- chartBarColor = _utils.BaseUtils.getCurrentTheme(chartColorTheme).colors[0];
91
- }
92
- const colorRules = color_option === _constants.TYPE_COLOR_USING.USE_RULES && y_axis_label_color_rules && (0, _colorUtils.getConvertedColorRules)(y_axis_label_color_rules);
93
- const title = this.getTitle(tables, table_id, y_axis_summary_type, y_axis_column_key || y_axis_summary_column_key);
94
- this.drawLabels(data);
95
- // set Coord type
96
- this.chart.coordinate('rect');
97
- this.autoAdjustDataOptions(chart, data, summaryColumn);
98
- this.chart.interval().label(y_axis_show_value ? 'value' : false, {
99
- style: {
100
- fontSize: _utils.BaseUtils.getLabelFontSize(label_font_size),
101
- fill: theme.labelColor,
102
- ...this.labelStroke
146
+ width: chartWidth,
147
+ height: chartHeight
148
+ } = this.chartBoundingClientRect;
149
+ const insertPadding = 30;
150
+ const borderRadius = 10;
151
+ const x = d3.scaleBand().domain(data.map(item => item.name)).range([insertPadding, chartWidth - insertPadding]).paddingInner(0.4).paddingOuter(0.1);
152
+ const y = d3.scaleLinear().domain(y_axis_auto_range ? [0, d3.max(data, d => d.value)] : [y_axis_min, y_axis_max]).range([chartHeight - insertPadding, insertPadding]);
153
+ this.chart.append('g').attr('transform', "translate(0, ".concat(chartHeight - insertPadding, ")")).call(d3.axisBottom(x).tickSizeOuter(0).tickSizeInner(5)).call(g => {
154
+ g.selectAll('.domain').attr('stroke', theme.XAxisColor);
155
+ g.selectAll('.tick line').attr('stroke', theme.XAxisColor);
156
+ g.selectAll('text').attr('font-size', theme.fontSize);
157
+ g.selectAll('text').attr('fill', theme.textColor);
158
+ });
159
+ this.chart.append('g').attr('transform', "translate(".concat(insertPadding, ", 0)")).call(d3.axisLeft(y).tickSizeInner(0).ticks(5).tickFormat(d => d)).call(g => {
160
+ g.select('.domain').remove();
161
+ g.selectAll('line').node().remove(); // delete the first line
162
+ g.selectAll('.tick line').clone().attr('x2', chartWidth - insertPadding * 2).attr('stroke', theme.gridColor).attr('stroke-dasharray', '8,3');
163
+ g.selectAll('text').attr('font-size', theme.fontSize);
164
+ g.selectAll('text').attr('fill', theme.textColor);
165
+ });
166
+ this.chart.append('g').attr('class', "rects-wrapper-".concat(chart === null || chart === void 0 ? void 0 : chart.id)).selectAll().data(data).join('path').attr('fill', d => d3.color(this.getFillColor(d))).attr('data-x', d => x(d.name)).attr('data-y', d => y(d.value)).attr('data-width', x.bandwidth()).attr('data-value', d => d.value).attr('id', (d, index) => d.slugId).attr('d', d => {
167
+ // Do not draw if the range is smaller than the minimum
168
+ if (!y_axis_auto_range && d.value <= y_axis_min) {
169
+ return '';
103
170
  }
104
- }).position('name*value').size(singleBarWidth).color('value', value => {
105
- const color = (0, _colorUtils.getLabelColor)({
106
- chart: chart.config,
107
- colorRules,
108
- value
109
- });
110
- return color || chartBarColor;
111
- }).tooltip('name*value', (name, value) => {
112
- return {
113
- title,
114
- value: _utils.BaseUtils.getSummaryValueDisplayString(summaryColumn, value, y_axis_summary_method),
115
- name
116
- };
117
- }).state({
118
- active: {
119
- style: {
120
- stroke: null
171
+ const path = d3.path();
172
+ const startX = x(d.name);
173
+ const startY = y(d.value);
174
+ path.moveTo(startX, startY + borderRadius);
175
+ path.quadraticCurveTo(startX, startY, startX + borderRadius, startY);
176
+ path.lineTo(startX + x.bandwidth() - borderRadius, startY);
177
+ path.quadraticCurveTo(startX + x.bandwidth(), startY, startX + x.bandwidth(), startY + borderRadius);
178
+ path.lineTo(startX + x.bandwidth(), y_axis_auto_range ? y(0) : y(y_axis_min));
179
+ path.lineTo(startX, y_axis_auto_range ? y(0) : y(y_axis_min));
180
+ path.closePath();
181
+ return path.toString();
182
+ }).call(g => {
183
+ const parentNode = d3.select('.rects-wrapper');
184
+ g.nodes().forEach(path => {
185
+ // Add label
186
+ if (y_axis_show_value) {
187
+ parentNode.append('text').attr('y', Number(path.dataset.y) - 10).attr('fill', theme.labelColor).attr('font-size', _utils.BaseUtils.getLabelFontSize(label_font_size)).text(path.dataset.value).call(g => {
188
+ const {
189
+ width
190
+ } = g.node().getBoundingClientRect();
191
+ g.attr('x', Number(path.dataset.x) + Number(path.dataset.width) / 2 - width);
192
+ });
121
193
  }
122
- }
123
- }).style('value', value => {
124
- return {
125
- radius: [singleBarRadius, singleBarRadius, 0, 0]
126
- };
194
+ });
195
+ }).on('click', (event, data) => {
196
+ this.props.toggleRecords(data);
197
+ }).on('mouseover', (event, data) => {
198
+ this.showTooltip(event, data, tooltipTitle);
199
+ this.setActiveAndInActiveState('inActive', data);
200
+ }).on('mousemove', event => {
201
+ this.moveTooltip(event);
202
+ }).on('mouseleave', (event, data) => {
203
+ this.hiddenTooltip();
204
+ this.setActiveAndInActiveState('active', data);
127
205
  });
128
- if (display_goal_line && goal_label && goal_value) {
129
- this.setDispalyGoalLine(goal_label, goal_value, chartWidth);
206
+ if (display_goal_line) {
207
+ this.chart.append('g').attr('class', 'annotation-wrapper').append('line').attr('stroke', '#aaa').attr('x1', insertPadding).attr('y1', y(goal_value)).attr('x2', chartWidth - insertPadding).attr('y2', y(goal_value)).call(g => {
208
+ const parentNode = d3.select('.annotation-wrapper');
209
+ parentNode.append('text').attr('x', chartWidth - insertPadding - 30).attr('y', y(goal_value) - 10).attr('fill', '#666').text(goal_label);
210
+ });
130
211
  }
131
- this.chart.legend(false);
132
- this.setToolTip();
133
- (0, _utils.isFunction)(customRender) && customRender(this.chart);
134
- this.setNameLabelAndTooltip(theme, this.labelCount);
135
- this.setValueLabel(theme);
136
- this.chart.interaction('element-highlight');
137
212
  };
138
213
  this.chart = null;
214
+ this.state = {
215
+ tooltipData: null,
216
+ toolTipPosition: null
217
+ };
139
218
  }
140
219
  componentDidMount() {
141
220
  this.createChart();
@@ -153,22 +232,18 @@ class Bar extends _chartComponent.default {
153
232
  }
154
233
  }
155
234
  componentWillUnmount() {
156
- var _this$chart2;
157
- // chart doesn't have autoPadding before they were rendered
158
- ((_this$chart2 = this.chart) === null || _this$chart2 === void 0 ? void 0 : _this$chart2.autoPadding) && this.chart.destroy();
235
+ this.chart.node() && this.chart.node().remove();
159
236
  window.removeEventListener('resize', this.debouncedHandleResize);
160
237
  }
161
238
  render() {
162
239
  const {
163
- chart
164
- } = this.props;
240
+ tooltipData,
241
+ toolTipPosition
242
+ } = this.state;
165
243
  return /*#__PURE__*/_react.default.createElement("div", {
166
- className: (0, _classnames.default)('sea-chart-container', {
167
- 'show-x-axis-label': this.isShowXAxisLabel(chart),
168
- 'show-y-axis-label': this.isShowYAxisLabel(chart)
169
- }),
244
+ className: "sea-chart-container",
170
245
  ref: ref => this.container = ref
171
- });
246
+ }, this.getToolTipContainer(tooltipData, toolTipPosition));
172
247
  }
173
248
  }
174
249
  Bar.propTypes = {
@@ -7,10 +7,9 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.default = void 0;
9
9
  var _react = _interopRequireWildcard(require("react"));
10
- var _dtableUtils = require("dtable-utils");
11
10
  var d3 = _interopRequireWildcard(require("d3"));
12
11
  var _lodashEs = require("lodash-es");
13
- var _customG = require("../../utils/custom-g2");
12
+ var _dtableUtils = require("dtable-utils");
14
13
  var _utils = require("../../utils");
15
14
  var _constants = require("../../constants");
16
15
  var _intl = _interopRequireDefault(require("../../intl"));
@@ -49,25 +48,7 @@ class ChartComponent extends _react.Component {
49
48
  const column = this.getColumn(tables, tableId, columnKey);
50
49
  return column.name || '';
51
50
  };
52
- this.initChart = (container, _ref) => {
53
- let {
54
- appendPadding,
55
- autoFit = true,
56
- height = '100%',
57
- width = '100%'
58
- } = _ref;
59
- let config = {
60
- container: container,
61
- autoFit: autoFit,
62
- width,
63
- height
64
- };
65
- if (appendPadding) {
66
- config.appendPadding = appendPadding;
67
- }
68
- this.chart = new _customG.Chart(config);
69
- };
70
- this.initD3Chart = container => {
51
+ this.initChart = container => {
71
52
  const {
72
53
  width,
73
54
  height
@@ -76,14 +57,6 @@ class ChartComponent extends _react.Component {
76
57
  this.container.appendChild(this.chart.node());
77
58
  this.chartBoundingClientRect = this.chart.node().getBoundingClientRect();
78
59
  };
79
- this.loadData = data => {
80
- this.chart.data(data, {
81
- 'name': {
82
- type: 'cat',
83
- nice: false
84
- }
85
- });
86
- };
87
60
  this.getChartDisplayLabels = (containerWidth, minItemWidth, charts) => {
88
61
  let labels = [];
89
62
  let labelCount = Math.floor(containerWidth / minItemWidth);