sea-chart 2.0.7 → 2.0.9

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.
@@ -48,14 +48,31 @@ class ChartComponent extends _react.Component {
48
48
  const column = this.getColumn(tables, tableId, columnKey);
49
49
  return column.name || '';
50
50
  };
51
- this.initChart = container => {
51
+ this.initChart = function (container, id) {
52
+ let initConfig = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
52
53
  const {
53
54
  width,
54
55
  height
55
56
  } = container.getBoundingClientRect();
56
- this.chart = d3.create('svg').attr('width', width).attr('height', height).attr('viewBox', [0, 0, width, height]);
57
- this.container.appendChild(this.chart.node());
58
- this.chartBoundingClientRect = this.chart.node().getBoundingClientRect();
57
+ _this.chart = d3.create('svg').attr('id', id).attr('class', "svg-wrapper-".concat(id)).attr('width', width).attr('height', height).attr('viewBox', [0, 0, width, height]);
58
+ _this.container.appendChild(_this.chart.node());
59
+ _this.chartBoundingClientRect = {
60
+ ...JSON.parse(JSON.stringify(_this.chart.node().getBoundingClientRect())),
61
+ ...initConfig
62
+ };
63
+ };
64
+ this.initDefs = () => {
65
+ if (this.container) {
66
+ // add Mask
67
+ const {
68
+ previewType
69
+ } = this.props.canvasStyle || {};
70
+ const {
71
+ width,
72
+ height
73
+ } = this.container.getBoundingClientRect();
74
+ this.chart.append('defs').append('mask').attr('id', "mask-wrapper-".concat(previewType, "-").concat(this.chart.node().id)).append('rect').attr('x', 0).attr('y', 0).attr('width', width).attr('height', height - 30).attr('fill', 'white');
75
+ }
59
76
  };
60
77
  this.getChartDisplayLabels = (containerWidth, minItemWidth, charts) => {
61
78
  let labels = [];
@@ -198,7 +215,7 @@ class ChartComponent extends _react.Component {
198
215
  });
199
216
  }
200
217
  };
201
- this.renderAxisLabel = (chart, tables, container) => {
218
+ this.renderAxisLabel = (chart, tables, chartContainer) => {
202
219
  var _this$chart2;
203
220
  if (!this.chart || !chart) return;
204
221
  let {
@@ -226,7 +243,6 @@ class ChartComponent extends _react.Component {
226
243
  let textColor;
227
244
  this.globalTheme === _constants.THEME_NAME_MAP.DARK ? textColor = '#fff' : textColor = '#999';
228
245
  const xAxisID = "chart-x-axis-label_".concat(chart.id);
229
- const chartContainer = container || this.chart.getCanvas().get('container');
230
246
  const xLabel = chartContainer === null || chartContainer === void 0 ? void 0 : chartContainer.querySelector("#".concat(xAxisID));
231
247
  if (!xLabel && x_axis_show_label) {
232
248
  const div = document.createElement('div');
@@ -456,13 +472,101 @@ class ChartComponent extends _react.Component {
456
472
  }
457
473
  };
458
474
  };
475
+ this.formatterLegendName = name => {
476
+ if (!name && typeof name !== 'number' || name.trim() === 'undefined' || name.trim() === 'null') {
477
+ return _intl.default.get(_constants.EMPTY_NAME);
478
+ } else if (name === '_Others') {
479
+ return _intl.default.get('Others');
480
+ } else {
481
+ return _intl.default.get(name) || name;
482
+ }
483
+ };
484
+ this.getOthersLegendItemWidth = (curIndex, allLegendItems, direction) => {
485
+ let width = 0;
486
+ let otherItems = [];
487
+ if (direction === 1) {
488
+ otherItems = allLegendItems.slice(0, curIndex);
489
+ }
490
+ if (direction === -1) {
491
+ otherItems = allLegendItems.slice(curIndex);
492
+ }
493
+ otherItems.forEach(item => {
494
+ const {
495
+ width: legendItemWidth
496
+ } = item.getBoundingClientRect();
497
+ width = width + legendItemWidth;
498
+ });
499
+ return width;
500
+ };
459
501
  // set legend
460
502
  this.setLegend = function (legendName) {
503
+ var _this$chart$node;
461
504
  let theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _constants.CHART_THEME_COLOR['light'];
462
- let legendPosition = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'bottom';
505
+ let legendPosition = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'top-left';
506
+ let data = arguments.length > 3 ? arguments[3] : undefined;
507
+ let colorScale = arguments.length > 4 ? arguments[4] : undefined;
463
508
  if (!_this.chart) return;
464
- const basicLegendConfig = _this.getLegendConfig(theme, legendPosition);
465
- _this.chart.legend(legendName, basicLegendConfig);
509
+ const {
510
+ width: chartWidth,
511
+ insertPadding
512
+ } = _this.chartBoundingClientRect;
513
+ const legendItemPaddingTop = 8;
514
+ const legendItemSpacing = 30;
515
+ const legendRectWidth = 18;
516
+ const legendRectHeight = 6;
517
+ const r = 3;
518
+ let translateX = 0;
519
+ let legendData = [];
520
+ let direction = 0; // 1 means left, -1 means right
521
+ if (legendPosition === 'top-left') {
522
+ translateX = insertPadding;
523
+ legendData = d3.group(data, d => d[legendName]);
524
+ direction = 1;
525
+ }
526
+ if (legendPosition === 'top-right') {
527
+ translateX = chartWidth;
528
+ legendData = d3.group(data, d => d[legendName]);
529
+ direction = -1;
530
+ }
531
+ const legendWrapper = _this.chart.append('g').attr('class', "legend-wrapper-".concat((_this$chart$node = _this.chart.node()) === null || _this$chart$node === void 0 ? void 0 : _this$chart$node.id)).attr('transform', "translate(".concat(translateX, ", 0)"));
532
+ legendWrapper.selectAll().data(legendData).join('g').append('rect').attr('width', legendRectWidth).attr('height', legendRectHeight).attr('rx', r).attr('fill', _ref => {
533
+ let [groupName] = _ref;
534
+ if (colorScale) return colorScale(groupName);
535
+ return _this.colorMap[groupName] || _constants.CHART_STYLE_COLORS[0];
536
+ }).attr('data-text', _ref2 => {
537
+ let [groupName] = _ref2;
538
+ return groupName;
539
+ }).call(g => {
540
+ const legendItems = Array.from(legendWrapper.node().children);
541
+ const legendItemCount = legendItems.length;
542
+
543
+ // Add text
544
+ g.nodes().forEach(rect => {
545
+ const parentNode = rect.parentNode;
546
+ let textHeight = 0;
547
+ let textWidth = 0;
548
+ d3.select(parentNode).append('text').attr('x', legendRectWidth).attr('width', 30).attr('fill', theme.legendTextColor).attr('font-size', theme.legendFontSize).text(_this.formatterLegendName(rect.dataset.text)).attr('data-text', rect.dataset.text).call(g => {
549
+ // set text y position
550
+ const {
551
+ height,
552
+ width
553
+ } = g.node().getBoundingClientRect();
554
+ textHeight = height;
555
+ textWidth = width;
556
+ g.attr('y', textHeight / 2);
557
+ });
558
+ });
559
+
560
+ // set g position
561
+ legendItems.forEach((item, index) => {
562
+ const otherslegendItemWidth = _this.getOthersLegendItemWidth(index, legendItems, direction);
563
+ d3.select(item).attr('transform', () => {
564
+ const gaps = direction === 1 ? index : legendItemCount - index;
565
+ const translateX = direction * (otherslegendItemWidth + gaps * legendItemSpacing);
566
+ return "translate(".concat(translateX, ", ").concat(legendItemPaddingTop, ")");
567
+ });
568
+ });
569
+ });
466
570
  };
467
571
  // theta is pie or ring chart
468
572
  this.setLegendForTheta = function (legendName) {
@@ -586,47 +690,6 @@ class ChartComponent extends _react.Component {
586
690
  const settings = this.getToolTipSettings(isGroup, summaryColumn, y_axis_summary_method, useSingleSelectColumnColor);
587
691
  this.chart.tooltip(settings);
588
692
  };
589
- this.getToolTipContainer = (tooltipData, position) => {
590
- const {
591
- offsetX,
592
- offsetY
593
- } = position || {
594
- offsetX: -9999,
595
- offsetY: -9999
596
- };
597
- const {
598
- title,
599
- items
600
- } = tooltipData || {
601
- title: '',
602
- items: []
603
- };
604
- const dom = /*#__PURE__*/_react.default.createElement("div", {
605
- className: "sea-chart-d3-tooltip-container",
606
- style: {
607
- transform: "translate(".concat(offsetX + 50, "px, ").concat(offsetY, "px)")
608
- }
609
- }, /*#__PURE__*/_react.default.createElement("div", {
610
- className: "sea-chart-d3-tooltip-title"
611
- }, title), /*#__PURE__*/_react.default.createElement("ul", {
612
- className: "sea-chart-d3-tooltip-list"
613
- }, items.map((item, index) => {
614
- return /*#__PURE__*/_react.default.createElement("li", {
615
- className: "sea-chart-d3-tooltip-list-item",
616
- key: index
617
- }, /*#__PURE__*/_react.default.createElement("span", {
618
- className: "sea-chart-d3-tooltip-marker",
619
- style: {
620
- backgroundColor: item.color
621
- }
622
- }), /*#__PURE__*/_react.default.createElement("span", {
623
- className: "sea-chart-d3-tooltip-name"
624
- }, item.name), /*#__PURE__*/_react.default.createElement("span", {
625
- className: "sea-chart-d3-tooltip-value"
626
- }, item.value));
627
- })));
628
- return dom;
629
- };
630
693
  this.setToolTipForLine = () => {
631
694
  const settings = this.getToolTipSettings();
632
695
  const lineToolTipSettings = {
@@ -693,30 +756,81 @@ class ChartComponent extends _react.Component {
693
756
  }
694
757
  return themeColors || _constants.CHART_THEME_COLOR[_constants.THEME_NAME_MAP.LIGHT];
695
758
  };
696
- this.setDispalyGoalLine = (goalLabel, goalValue, chartWidth) => {
697
- this.chart.annotation().line({
698
- // start of the annotation line start
699
- // x start is -0.5
700
- start: [-0.5, goalValue],
701
- // x end is infinite
702
- end: [99999, goalValue],
703
- style: {
704
- stroke: '#aaa',
705
- // dash color
706
- lineDash: [8, 3] // dash style
707
- },
708
- text: {
709
- content: goalLabel,
710
- offsetX: chartWidth - 70,
711
- offsetY: -5,
712
- style: {
713
- fill: '#666'
714
- }
759
+ this.setDispalyGoalLine = (goal_label, goal_value, insertPadding, yScale) => {
760
+ var _this$chart$node2;
761
+ const {
762
+ width: chartWidth
763
+ } = this.chartBoundingClientRect;
764
+ const annotationWrapper = this.chart.append('g').attr('class', "annotation-wrapper-".concat((_this$chart$node2 = this.chart.node()) === null || _this$chart$node2 === void 0 ? void 0 : _this$chart$node2.id));
765
+ annotationWrapper.append('line').attr('stroke', '#aaa').attr('x1', insertPadding).attr('y1', yScale(goal_value)).attr('x2', chartWidth - insertPadding).attr('y2', yScale(goal_value)).call(g => {
766
+ annotationWrapper.append('text').attr('x', chartWidth - insertPadding - 30).attr('y', yScale(goal_value) - 10).attr('fill', '#666').text(goal_label);
767
+ });
768
+ };
769
+ this.addLabelToRectTop = _ref3 => {
770
+ let {
771
+ container,
772
+ x,
773
+ y,
774
+ xWidth,
775
+ theme,
776
+ label_font_size,
777
+ text
778
+ } = _ref3;
779
+ d3.select(container).append('text').attr('y', Number(y) - 10).attr('fill', theme.labelColor).attr('font-size', _utils.BaseUtils.getLabelFontSize(label_font_size)).text(text).call(g => {
780
+ const {
781
+ width
782
+ } = g.node().getBoundingClientRect();
783
+ g.attr('x', Number(x) + Number(xWidth) / 2 - width / 2);
784
+ });
785
+ };
786
+ this.addLabelToRectCenter = _ref4 => {
787
+ let {
788
+ chartType,
789
+ container,
790
+ x,
791
+ y,
792
+ xWidth,
793
+ yheight,
794
+ overflowHeight,
795
+ theme,
796
+ label_font_size,
797
+ text
798
+ } = _ref4;
799
+ d3.select(container).append('text').attr('fill', theme.labelColor).attr('font-size', _utils.BaseUtils.getLabelFontSize(label_font_size)).text(text).call(g => {
800
+ const {
801
+ width,
802
+ height
803
+ } = g.node().getBoundingClientRect();
804
+ const translateX = Number(x) + xWidth / 2 - width / 2;
805
+ let translateY = Number(y) + Number(yheight / 2) + height / 2;
806
+ if (chartType === _constants.CHART_TYPE.BAR_STACK) {
807
+ translateY = translateY - overflowHeight / 2;
715
808
  }
809
+ g.attr('transform', "translate(".concat(translateX, ", ").concat(translateY, ")"));
716
810
  });
717
811
  };
812
+ this.setActiveAndInActiveState = (state, allGroup, curGroupName) => {
813
+ if (state === 'active') {
814
+ allGroup.forEach(g => {
815
+ const rects = Array.from(g.children);
816
+ rects.forEach(item => {
817
+ d3.select(item).attr('opacity', 1);
818
+ });
819
+ });
820
+ }
821
+ if (state === 'inActive') {
822
+ allGroup.forEach(g => {
823
+ const rects = Array.from(g.children);
824
+ rects.forEach(item => {
825
+ if (item.getAttribute('data-groupName') !== curGroupName) {
826
+ d3.select(item).attr('opacity', 0.3);
827
+ }
828
+ });
829
+ });
830
+ }
831
+ };
718
832
  this.initLabelStroke(props === null || props === void 0 ? void 0 : props.globalTheme);
719
- this.chartBoundingClientRect = null;
833
+ this.chartBoundingClientRect = {};
720
834
  }
721
835
  componentDidUpdate(prevProps) {
722
836
  if (prevProps.globalTheme !== this.props.globalTheme) {
@@ -11,6 +11,10 @@ var _constants = require("../../constants");
11
11
  var _statisticRecordDialog = _interopRequireDefault(require("../../components/statistic-record-dialog"));
12
12
  var _table = _interopRequireDefault(require("./table"));
13
13
  var _bar = _interopRequireDefault(require("./bar"));
14
+ var _barGroup = _interopRequireDefault(require("./bar-group"));
15
+ var _barStack = _interopRequireDefault(require("./bar-stack"));
16
+ var _barCompare = _interopRequireDefault(require("./bar-compare"));
17
+ var _barCustomStack = _interopRequireDefault(require("./bar-custom-stack"));
14
18
  var _basicNumberCard = _interopRequireDefault(require("./basic-number-card"));
15
19
  var _trend = _interopRequireDefault(require("./trend"));
16
20
  var _tableElement = _interopRequireDefault(require("./table-element"));
@@ -94,6 +98,37 @@ const Wrapper = _ref => {
94
98
  canvasStyle: canvasStyle
95
99
  }));
96
100
  }
101
+ case _constants.CHART_TYPE.BAR_GROUP:
102
+ {
103
+ return /*#__PURE__*/_react.default.createElement(_barGroup.default, Object.assign({}, baseProps, {
104
+ canvasStyle: canvasStyle
105
+ }));
106
+ }
107
+ case _constants.CHART_TYPE.BAR_STACK:
108
+ {
109
+ const {
110
+ config
111
+ } = chart;
112
+ const {
113
+ column_groupby_multiple_numeric_column
114
+ } = config;
115
+ const BarGroupComponent = column_groupby_multiple_numeric_column ? _barStack.default : _barGroup.default;
116
+ return /*#__PURE__*/_react.default.createElement(BarGroupComponent, Object.assign({}, baseProps, {
117
+ canvasStyle: canvasStyle
118
+ }));
119
+ }
120
+ case _constants.CHART_TYPE.COMPARE_BAR:
121
+ {
122
+ return /*#__PURE__*/_react.default.createElement(_barCompare.default, Object.assign({}, baseProps, {
123
+ canvasStyle: canvasStyle
124
+ }));
125
+ }
126
+ case _constants.CHART_TYPE.BAR_CUSTOM:
127
+ {
128
+ return /*#__PURE__*/_react.default.createElement(_barCustomStack.default, Object.assign({}, baseProps, {
129
+ canvasStyle: canvasStyle
130
+ }));
131
+ }
97
132
  case _constants.CHART_TYPE.BASIC_NUMBER_CARD:
98
133
  {
99
134
  return /*#__PURE__*/_react.default.createElement(_basicNumberCard.default, Object.assign({}, baseProps, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sea-chart",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "main": "./dist/index.js",
5
5
  "dependencies": {
6
6
  "@dnd-kit/core": "^6.1.0",
@@ -135,6 +135,7 @@
135
135
  "react-cookies": "0.1.1",
136
136
  "react-dev-utils": "^12.0.1",
137
137
  "react-dom": "~18.3.1",
138
+ "react-i18next": "12.1.4",
138
139
  "regenerator-runtime": "^0.14.1",
139
140
  "release-it": "16.2.1",
140
141
  "resolve": "1.12.0",