sea-chart 2.0.25 → 2.0.27

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.
@@ -0,0 +1,267 @@
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 _topojson = require("topojson");
14
+ var _dtableUtils = require("dtable-utils");
15
+ var _constants = require("../../constants");
16
+ var _utils = require("../../utils");
17
+ var _colorRules = require("../../constants/color-rules");
18
+ var _context = _interopRequireDefault(require("../../context"));
19
+ var _mapJson = _interopRequireDefault(require("../../services/map-json"));
20
+ var _tooltip = _interopRequireDefault(require("../../components/tooltip"));
21
+ var _chartComponent = _interopRequireDefault(require("./chart-component"));
22
+ class Map extends _chartComponent.default {
23
+ constructor(props) {
24
+ super(props);
25
+ this.handleResize = () => {
26
+ this.chart.node() && this.chart.node().remove();
27
+ this.createChart();
28
+ this.drawChart();
29
+ };
30
+ this.createChart = () => {
31
+ const {
32
+ chart
33
+ } = this.props;
34
+ const initConfig = {
35
+ insertPadding: 30
36
+ };
37
+ this.initChart(this.container, chart === null || chart === void 0 ? void 0 : chart.id, initConfig);
38
+ };
39
+ this.drawChart = async () => {
40
+ let {
41
+ result: data,
42
+ chart
43
+ } = this.props;
44
+ if (!Array.isArray(data)) return;
45
+ const {
46
+ map_location,
47
+ map_level = _constants.MAP_LEVEL.COUNTRY,
48
+ type
49
+ } = chart.config;
50
+ const mediaUrl = _context.default.getSetting('mediaUrl');
51
+ const mapJson = await (0, _mapJson.default)(map_level, map_location, mediaUrl);
52
+ if (!mapJson) return;
53
+ const newMapJson = map_level === _constants.MAP_LEVEL.COUNTRY ? (0, _topojson.feature)(mapJson, mapJson.objects) : mapJson;
54
+ const statisticNewData = this.formatMapStatisticData(data, newMapJson, type);
55
+ this.draw(statisticNewData, newMapJson);
56
+ };
57
+ this.showTooltip = event => {
58
+ const title = event.target.getAttribute('data-name');
59
+ const value = Number(event.target.getAttribute('data-value'));
60
+ const {
61
+ offsetX,
62
+ offsetY
63
+ } = event;
64
+ const newTooltipData = {
65
+ title,
66
+ items: [{
67
+ color: '',
68
+ name: '数量',
69
+ value
70
+ }]
71
+ };
72
+ this.setState({
73
+ tooltipData: newTooltipData
74
+ });
75
+ this.setState({
76
+ toolTipPosition: {
77
+ offsetX,
78
+ offsetY
79
+ }
80
+ });
81
+ };
82
+ this.moveTooltip = event => {
83
+ const {
84
+ offsetX,
85
+ offsetY
86
+ } = event;
87
+ this.setState({
88
+ toolTipPosition: {
89
+ offsetX,
90
+ offsetY
91
+ }
92
+ });
93
+ };
94
+ this.hiddenTooltip = event => {
95
+ this.setState({
96
+ toolTipPosition: null
97
+ });
98
+ };
99
+ this.draw = (data, mapJson) => {
100
+ const {
101
+ chart,
102
+ globalTheme,
103
+ canvasStyle
104
+ } = this.props;
105
+ const theme = _constants.CHART_THEME_COLOR[globalTheme];
106
+ const {
107
+ data_color,
108
+ legend_size,
109
+ legend_direction,
110
+ summary_method
111
+ } = chart.config;
112
+ const {
113
+ width: chartWidth,
114
+ height: chartHeight,
115
+ insertPadding
116
+ } = this.chartBoundingClientRect;
117
+ const currentColorOption = _colorRules.COLOR_OPTIONS.filter(item => item.name === data_color)[0] || _colorRules.COLOR_OPTIONS[0];
118
+ const {
119
+ exampleColors,
120
+ highlightedBorderColor
121
+ } = currentColorOption;
122
+
123
+ // Color scale
124
+ const minVal = d3.min(data, d => d.value);
125
+ const maxVal = d3.max(data, d => d.value);
126
+ const color = d3.scaleSequential([minVal, maxVal], d3.interpolateRgbBasis(exampleColors)).unknown('#e2e2e2');
127
+
128
+ // 1. Setting up projection
129
+ const projection = d3.geoMercator().fitSize([chartWidth - insertPadding, chartHeight - insertPadding], mapJson);
130
+
131
+ // 2. Generate a path based on projection
132
+ const pathGenerator = d3.geoPath(projection);
133
+
134
+ // 3. Rendering map
135
+ this.chart.append('g').attr('class', 'map-wrapper').on('click', event => {
136
+ const value = Number(event.target.getAttribute('data-value'));
137
+ if (value) {
138
+ const name = event.target.getAttribute('data-name');
139
+ const newData = data.find(item => item.name.includes(name));
140
+ newData && this.props.toggleRecords(newData);
141
+ }
142
+ }).on('mousemove', event => {
143
+ const value = Number(event.target.getAttribute('data-value'));
144
+ if (value) {
145
+ this.curElement = event.target.parentNode;
146
+ this.showTooltip(event);
147
+ this.handleAcitveAndInActiveState('active', event, highlightedBorderColor);
148
+ } else {
149
+ if (this.curElement) {
150
+ this.hiddenTooltip(event);
151
+ this.handleAcitveAndInActiveState('inActive', event, highlightedBorderColor);
152
+ this.curElement = null;
153
+ }
154
+ }
155
+ }).on('mouseleave', event => {
156
+ if (this.curElement) {
157
+ this.hiddenTooltip(event);
158
+ this.handleAcitveAndInActiveState('inActive', event, highlightedBorderColor);
159
+ this.curElement = null;
160
+ }
161
+ }).selectAll().data(mapJson.features).join('g').append('path').attr('d', d => {
162
+ return pathGenerator(d);
163
+ }).attr('stroke-width', 1).attr('stroke', '#bdbdbd').attr('stroke-opacity', 1).attr('opacity', 1).attr('data-name', d => d.properties.name).call(g => {
164
+ g.nodes().forEach(item => {
165
+ const curName = item.getAttribute('data-name');
166
+ const curItemData = data.find(item => item.name.includes(curName));
167
+ if (curItemData) {
168
+ d3.select(item).attr('fill', color(curItemData.value));
169
+ d3.select(item).attr('data-value', curItemData.value);
170
+ } else {
171
+ d3.select(item).attr('fill', color());
172
+ }
173
+ });
174
+ });
175
+ const columnData = this.getColumnData();
176
+ this.setContinuousLegend({
177
+ previewType: canvasStyle.previewType,
178
+ theme,
179
+ colorRange: exampleColors,
180
+ legendDirection: legend_direction,
181
+ legendSize: legend_size,
182
+ legendTextRange: [_utils.BaseUtils.getSummaryValueDisplayString(columnData, minVal, summary_method), _utils.BaseUtils.getSummaryValueDisplayString(columnData, maxVal, summary_method)]
183
+ });
184
+ };
185
+ this.handleAcitveAndInActiveState = (state, event, highlightedBorderColor) => {
186
+ if (state === 'active') {
187
+ const lastElement = Array.from(event.target.parentNode.parentNode.children).at(-1);
188
+
189
+ // Add element to the end
190
+ d3.select(this.curElement).select('path').transition().duration(this.transitionDuration).attr('stroke-opacity', 0.7).attr('stroke-width', 0.5).attr('stroke', highlightedBorderColor);
191
+ lastElement.after(this.curElement);
192
+ return;
193
+ }
194
+ if (this.curElement) {
195
+ d3.select(this.curElement).select('path').transition().duration(this.transitionDuration).attr('stroke-opacity', 1).attr('stroke-width', 1).attr('stroke', '#bdbdbd');
196
+ }
197
+ };
198
+ this.getColumnData = () => {
199
+ const {
200
+ chart
201
+ } = this.props;
202
+ const {
203
+ summary_type,
204
+ summary_column_key,
205
+ table_id
206
+ } = chart.config;
207
+ let columnData = _constants.DEFAULT_NUMBER_FORMAT_OBJECT;
208
+ if (summary_type === _constants.CHART_SUMMARY_TYPE.ADVANCED) {
209
+ const table = (0, _dtableUtils.getTableById)(table_id);
210
+ const summaryColumn = (0, _dtableUtils.getTableColumnByKey)(table, summary_column_key) || {};
211
+ columnData = summaryColumn.data || _constants.DEFAULT_NUMBER_FORMAT_OBJECT;
212
+ }
213
+ return columnData;
214
+ };
215
+ this.chart = null;
216
+ this.state = {
217
+ tooltipData: null,
218
+ toolTipPosition: null
219
+ };
220
+ this.curElement = null; // Regions that currently contain data
221
+ }
222
+ componentDidMount() {
223
+ this.createChart();
224
+ this.drawChart();
225
+ this.debouncedHandleResize = (0, _lodashEs.debounce)(this.handleResize, 300);
226
+ window.addEventListener('resize', this.debouncedHandleResize);
227
+ }
228
+ componentDidUpdate(prevProps) {
229
+ super.componentDidUpdate(prevProps);
230
+ if (_utils.BaseUtils.shouldChartComponentUpdate(prevProps, this.props)) {
231
+ this.createChart();
232
+ this.drawChart();
233
+ }
234
+ }
235
+ componentWillUnmount() {
236
+ this.chart.node() && this.chart.node().remove();
237
+ window.removeEventListener('resize', this.debouncedHandleResize);
238
+ }
239
+ render() {
240
+ const {
241
+ tooltipData,
242
+ toolTipPosition
243
+ } = this.state;
244
+ return /*#__PURE__*/_react.default.createElement("div", {
245
+ ref: ref => this.container = ref,
246
+ className: "sea-chart-container"
247
+ }, /*#__PURE__*/_react.default.createElement(_tooltip.default, {
248
+ tooltipData: tooltipData,
249
+ toolTipPosition: toolTipPosition,
250
+ chart: this.chart
251
+ }));
252
+ }
253
+ }
254
+ Map.propTypes = {
255
+ canvasStyle: _propTypes.default.object,
256
+ chart: _propTypes.default.object,
257
+ groupbyColumn: _propTypes.default.object,
258
+ columnGroupbyColumn: _propTypes.default.object,
259
+ summaryColumn: _propTypes.default.object,
260
+ result: _propTypes.default.array,
261
+ tables: _propTypes.default.array,
262
+ globalTheme: _propTypes.default.string,
263
+ chartColorTheme: _propTypes.default.string,
264
+ toggleRecords: _propTypes.default.func,
265
+ customRender: _propTypes.default.func
266
+ };
267
+ var _default = exports.default = Map;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sea-chart",
3
- "version": "2.0.25",
3
+ "version": "2.0.27",
4
4
  "main": "./dist/index.js",
5
5
  "dependencies": {
6
6
  "@dnd-kit/core": "^6.1.0",
@@ -17,7 +17,8 @@
17
17
  "rc-slider": "^10.5.0",
18
18
  "reactstrap": "~9.2.3",
19
19
  "shallowequal": "^1.1.0",
20
- "slugid": "~5.0.1"
20
+ "slugid": "~5.0.1",
21
+ "topojson": "^3.0.2"
21
22
  },
22
23
  "peerDependencies": {
23
24
  "dtable-ui-component": "~6.0.15",