sea-chart 2.0.11 → 2.0.12

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.
@@ -77,6 +77,7 @@ class BarCompare extends _chartComponent.default {
77
77
  g.selectAll('.tick line').attr('stroke', theme.XAxisColor);
78
78
  g.selectAll('text').attr('font-size', theme.fontSize);
79
79
  g.selectAll('text').attr('fill', theme.textColor);
80
+ this.checkTickOverlap(g);
80
81
  });
81
82
 
82
83
  // Rect group
@@ -156,6 +156,7 @@ class BarCustom extends _chartComponent.default {
156
156
  g.selectAll('.tick line').attr('stroke', theme.XAxisColor);
157
157
  g.selectAll('text').attr('font-size', theme.fontSize);
158
158
  g.selectAll('text').attr('fill', theme.textColor);
159
+ this.checkTickOverlap(g);
159
160
  });
160
161
 
161
162
  // Rect group
@@ -109,7 +109,7 @@ class BarGroup extends _chartComponent.default {
109
109
 
110
110
  // Y axis
111
111
  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));
112
- const fx = d3.scaleBand().domain(new Set(data.map(d => d.name))).range([insertPadding + this.horizontalOverflowOffset, chartWidth - insertPadding]).paddingInner(0.1);
112
+ const fx = d3.scaleBand().domain(new Set(data.map(d => d.name))).range([insertPadding + this.horizontalOverflowOffset, chartWidth - insertPadding]).paddingInner(0.1).paddingOuter(0.1);
113
113
  const x = d3.scaleBand().domain(new Set(data.map(d => d.group_name))).range([0, fx.bandwidth()]);
114
114
 
115
115
  // X axis
@@ -118,6 +118,7 @@ class BarGroup extends _chartComponent.default {
118
118
  g.selectAll('.tick line').attr('stroke', theme.XAxisColor);
119
119
  g.selectAll('text').attr('font-size', theme.fontSize);
120
120
  g.selectAll('text').attr('fill', theme.textColor);
121
+ this.checkTickOverlap(g);
121
122
  });
122
123
 
123
124
  // Rect group
@@ -119,6 +119,7 @@ class BarStack extends _chartComponent.default {
119
119
  g.selectAll('.tick line').attr('stroke', theme.XAxisColor);
120
120
  g.selectAll('text').attr('font-size', theme.fontSize);
121
121
  g.selectAll('text').attr('fill', theme.textColor);
122
+ this.checkTickOverlap(g);
122
123
  });
123
124
 
124
125
  // Rect group
@@ -161,6 +161,7 @@ class Bar extends _chartComponent.default {
161
161
  g.selectAll('.tick line').attr('stroke', theme.XAxisColor);
162
162
  g.selectAll('text').attr('font-size', theme.fontSize);
163
163
  g.selectAll('text').attr('fill', theme.textColor);
164
+ this.checkTickOverlap(g);
164
165
  });
165
166
  const rectsWrapper = this.chart.append('g').attr('class', "rects-wrapper-".concat(chart === null || chart === void 0 ? void 0 : chart.id));
166
167
  rectsWrapper.selectAll().data(data).join('rect').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('data-groupName', d => d.name).attr('data-slugid', d => d.slugId).attr('x', d => x(d.name)).attr('y', d => y(d.value)).attr('width', x.bandwidth()).attr('height', d => y(0) - y(d.value)).call(g => {
@@ -740,25 +740,26 @@ class ChartComponent extends _react.Component {
740
740
  } = this.getLegendBoundary(legendPosition);
741
741
  legendItems.forEach((item, index) => {
742
742
  let translateX = start;
743
- const lastItem = legendItems[index - 1];
743
+ const prevItem = legendItems[index - 1];
744
744
  if (index > 0) {
745
745
  const {
746
746
  width
747
- } = lastItem.getBoundingClientRect();
748
- const prevTranslateX = Number(lastItem.getAttribute('data-translateX'));
747
+ } = prevItem.getBoundingClientRect();
748
+ const prevTranslateX = Number(prevItem.getAttribute('data-translateX'));
749
749
  translateX = prevTranslateX + width + legendItemMargin;
750
750
  }
751
+ d3.select(item).attr('transform', "translate(".concat(translateX, ",0)")).attr('data-translateX', translateX);
751
752
 
752
753
  // legend items add offset
753
754
  if (legendPosition === 'top-right') {
754
755
  d3.select(item).attr('opacity', 0);
756
+ // Execute only once
755
757
  if (index === legendItems.length - 1) {
756
758
  queueMicrotask(() => {
757
759
  this.setLegendItemOffset(legendItems, end);
758
760
  });
759
761
  }
760
762
  }
761
- d3.select(item).attr('transform', "translate(".concat(translateX, ",0)")).attr('data-translateX', translateX);
762
763
  });
763
764
  });
764
765
  };
@@ -1202,6 +1203,66 @@ class ChartComponent extends _react.Component {
1202
1203
  this.horizontalOverflowOffset = offset;
1203
1204
  }
1204
1205
  };
1206
+ this.checkTickOverlap = g => {
1207
+ const allTicks = g.selectAll('.tick').nodes();
1208
+ // Hide overlapping ticks
1209
+ for (let i = 0; i < allTicks.length; i++) {
1210
+ const curText = d3.select(allTicks[i]).select('text').node();
1211
+ const {
1212
+ right: curRight
1213
+ } = curText.getBoundingClientRect();
1214
+ for (let j = i + 1; j < allTicks.length; j++) {
1215
+ const nextText = d3.select(allTicks[j]).select('text').node();
1216
+ if (curText && nextText) {
1217
+ const {
1218
+ left: nextLeft
1219
+ } = nextText.getBoundingClientRect();
1220
+ if (curRight >= nextLeft) {
1221
+ i = j;
1222
+ d3.select(nextText.parentNode).attr('opacity', 0);
1223
+ }
1224
+ }
1225
+ }
1226
+ }
1227
+ const {
1228
+ width: maxWidth
1229
+ } = g.node().getBoundingClientRect();
1230
+ let allWidth = 0;
1231
+ // Can't fit tilted text
1232
+ for (let tick of allTicks) {
1233
+ const {
1234
+ width
1235
+ } = tick.getBoundingClientRect();
1236
+ allWidth = allWidth + width;
1237
+ if (allWidth > maxWidth) break;
1238
+ }
1239
+ if (allWidth > maxWidth) {
1240
+ allTicks.forEach(tick => {
1241
+ const opacityVal = tick.getAttribute('opacity');
1242
+ if (Number(opacityVal) !== 0) {
1243
+ // roate
1244
+ d3.select(tick).select('text').attr('transform', 'rotate(45)').style('text-anchor', 'start').attr('dy', '0.4em').attr('dx', '0.4em');
1245
+ }
1246
+ });
1247
+ }
1248
+
1249
+ // Text overflow
1250
+ const {
1251
+ height
1252
+ } = g.node().getBoundingClientRect();
1253
+ const {
1254
+ insertPadding
1255
+ } = this.chartBoundingClientRect;
1256
+ if (height > insertPadding) {
1257
+ const viewBox = this.chart.node().getAttribute('viewBox').split(',');
1258
+ const viewHeight = viewBox[viewBox.length - 1];
1259
+ const newViewHeight = Number(viewHeight) + (height - insertPadding);
1260
+ viewBox[viewBox.length - 1] = "".concat(newViewHeight);
1261
+ queueMicrotask(() => {
1262
+ this.chart.attr('viewBox', viewBox.join(','));
1263
+ });
1264
+ }
1265
+ };
1205
1266
  this.initLabelStroke(props === null || props === void 0 ? void 0 : props.globalTheme);
1206
1267
  this.chartBoundingClientRect = {};
1207
1268
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sea-chart",
3
- "version": "2.0.11",
3
+ "version": "2.0.12",
4
4
  "main": "./dist/index.js",
5
5
  "dependencies": {
6
6
  "@dnd-kit/core": "^6.1.0",