@publishfx/publish-chart 2.1.28 → 2.1.29

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.
@@ -1,4 +1,8 @@
1
1
  import lib from "@antv/data-set/lib";
2
+ const parseDataValue = (value, defaultStr = '-')=>{
3
+ if ('' !== value && '-' !== value && null != value && 'NaN' !== value) return +value;
4
+ return defaultStr;
5
+ };
2
6
  class DataAdapter {
3
7
  static transform(data, transformType, config) {
4
8
  if (!data || 0 === data.length) return [];
@@ -24,7 +28,7 @@ class DataAdapter {
24
28
  dv.transform({
25
29
  type: 'map',
26
30
  callback: (row)=>{
27
- row[config.y] = '' !== row[config.y] ? +row[config.y] : '-';
31
+ row[config.y] = parseDataValue(row[config.y], null);
28
32
  return row;
29
33
  }
30
34
  });
@@ -106,7 +110,7 @@ class DataAdapter {
106
110
  if (config.y) dv.transform({
107
111
  type: 'map',
108
112
  callback: (row)=>{
109
- row[config.y] = '' !== row[config.y] && '-' !== row[config.y] && null !== row[config.y] ? +row[config.y] : '-';
113
+ row[config.y] = parseDataValue(row[config.y]);
110
114
  return row;
111
115
  }
112
116
  });
@@ -122,7 +126,7 @@ class DataAdapter {
122
126
  dv.transform({
123
127
  type: 'map',
124
128
  callback: (row)=>{
125
- row[config.y] = '' !== row[config.y] ? +row[config.y] : null;
129
+ row[config.y] = parseDataValue(row[config.y], null);
126
130
  return row;
127
131
  }
128
132
  });
@@ -141,14 +145,14 @@ class DataAdapter {
141
145
  tdv.transform({
142
146
  type: 'map',
143
147
  callback: (row)=>{
144
- row[config.y] = '' !== row[config.y] ? +row[config.y] : null;
148
+ row[config.y] = parseDataValue(row[config.y], null);
145
149
  return row;
146
150
  }
147
151
  });
148
152
  combinedv.transform({
149
153
  type: 'map',
150
154
  callback: (row)=>{
151
- row[config.y] = '' !== row[config.y] ? +row[config.y] : null;
155
+ row[config.y] = parseDataValue(row[config.y], null);
152
156
  return row;
153
157
  }
154
158
  });
@@ -228,6 +228,8 @@ export interface ApplyAxisXOptions {
228
228
  containerWidth?: number;
229
229
  /** 单个标签最大显示字符数,超出截断。默认 6 */
230
230
  maxLabelChars?: number;
231
+ /** 单个标签最小显示宽度,默认 90 */
232
+ minLabelWidth?: number;
231
233
  }
232
234
  /** 配置 X 轴,挂载在 mark 上(如 line / interval) */
233
235
  export declare function applyAxisX(mark: any, options?: ApplyAxisXOptions): void;
@@ -37,12 +37,12 @@ function applyScaleYLinear(view, options = {}) {
37
37
  }
38
38
  });
39
39
  }
40
- function calcAxisInterval(dataCount, containerWidth, maxLabelChars, fontSize) {
40
+ function calcAxisInterval(dataCount, containerWidth, maxLabelChars, fontSize, minLabelWidth = 90) {
41
41
  if (dataCount <= 1) return 1;
42
42
  const charWidth = 0.55 * fontSize;
43
- const ellipsisWidth = Math.min(maxLabelChars * charWidth, 90);
43
+ const ellipsisWidth = Math.min(maxLabelChars * charWidth, minLabelWidth);
44
44
  const slotWidth = ellipsisWidth + 8;
45
- const plotWidth = 0.85 * containerWidth;
45
+ const plotWidth = 0.95 * containerWidth;
46
46
  const maxLabels = Math.max(Math.floor(plotWidth / slotWidth), 2);
47
47
  return Math.max(Math.ceil(dataCount / maxLabels), 1);
48
48
  }
@@ -55,10 +55,10 @@ function createAxisFilter(type, interval, _dataCount) {
55
55
  function applyAxisX(mark, options = {}) {
56
56
  const { title = false, isHorizontal = false, grid = true, gridStroke = '#333', gridLineWidth = 50, gridLineDash = [
57
57
  1000
58
- ], gridFilter, dataCount = 0, containerWidth = 0, maxLabelChars = 30 } = options;
58
+ ], gridFilter, dataCount = 0, containerWidth = 0, maxLabelChars = 30, minLabelWidth = 90 } = options;
59
59
  const fontSize = 12;
60
60
  const needSampling = dataCount > 0 && containerWidth > 0;
61
- const interval = needSampling ? calcAxisInterval(dataCount, containerWidth, maxLabelChars, fontSize) : 1;
61
+ const interval = needSampling ? calcAxisInterval(dataCount, containerWidth, maxLabelChars, fontSize, minLabelWidth) : 1;
62
62
  const axisFilter = needSampling && interval > 1 ? (type)=>createAxisFilter(type, interval, dataCount) : null;
63
63
  mark.axis('x', {
64
64
  title,
@@ -34,6 +34,7 @@ function renderG2BarChart(container, options) {
34
34
  key: 'main'
35
35
  });
36
36
  if (!CLOSE_HIGHLIGHT_DEBUG) applyHighlightDate(view, x, data, highlightDate, isHighlight);
37
+ const minLabelWidth = data.some((d)=>d.groupName.length < 3) ? 30 : 90;
37
38
  const interval = view.interval().encode('x', x).encode('y', y).encode('color', 'groupType').transform({
38
39
  type: 'dodgeX',
39
40
  padding: isHorizontal ? 0.2 : 0
@@ -72,7 +73,8 @@ function renderG2BarChart(container, options) {
72
73
  grid: false,
73
74
  isHorizontal,
74
75
  dataCount: xValueCount,
75
- containerWidth: container.clientWidth
76
+ containerWidth: container.clientWidth,
77
+ minLabelWidth
76
78
  });
77
79
  applyAxisY(view, {
78
80
  labelFormatter: formatAxis
@@ -91,14 +93,14 @@ function renderG2BarChart(container, options) {
91
93
  interval.tooltip({
92
94
  items: [
93
95
  (datum)=>({
94
- value: datum[Y_AXIS_FIELD],
96
+ value: null === datum[Y_AXIS_FIELD] ? '-' : datum[Y_AXIS_FIELD],
95
97
  name: datum['groupType'],
96
98
  indicatorId: datum['groupType'],
97
99
  channel: Y_AXIS_FIELD,
98
100
  color: getColorByGroupType(datum['groupType'], colorOpts) || 'transparent'
99
101
  }),
100
102
  isCompare ? (datum)=>({
101
- value: datum[Y_AXIS_FIELD],
103
+ value: null === datum[Y_AXIS_FIELD] ? '-' : datum[Y_AXIS_FIELD],
102
104
  compareTime: datum?.compareTime,
103
105
  name: datum['groupType'],
104
106
  indicatorId: datum['groupType'].replace('_compare', ''),
@@ -16,6 +16,7 @@ function renderG2CombineChart(container, options) {
16
16
  insetBottom: 0,
17
17
  animate: null
18
18
  });
19
+ const minLabelWidth = data.some((d)=>d.groupName.length < 3) ? 30 : 90;
19
20
  const view = getMainView(chart);
20
21
  view.attr('marginLeft', 0).attr('marginRight', 30).attr('marginTop', 16).attr('marginBottom', 0).animate(false);
21
22
  view.data(data);
@@ -24,7 +25,8 @@ function renderG2CombineChart(container, options) {
24
25
  title: false,
25
26
  grid: false,
26
27
  dataCount: xValueCount,
27
- containerWidth: container.clientWidth
28
+ containerWidth: container.clientWidth,
29
+ minLabelWidth
28
30
  });
29
31
  const barColor = themeColors[0] ?? "#5B8FF9";
30
32
  applyHighlightDate(view, x, data, highlightDate, isHighlight);
@@ -16,6 +16,7 @@ function renderG2LineChart(container, options) {
16
16
  paddingBottom: 0,
17
17
  insetBottom: 0
18
18
  });
19
+ const minLabelWidth = data.some((d)=>d.groupName.length < 3) ? 30 : 90;
19
20
  const view = getMainView(chart);
20
21
  view.attr('marginLeft', 0).attr('marginRight', 30).attr('marginTop', 10).attr('marginBottom', isLegend ? 0 : 16).animate(false);
21
22
  view.data(data);
@@ -42,7 +43,8 @@ function renderG2LineChart(container, options) {
42
43
  applyAxisX(view, {
43
44
  grid: false,
44
45
  dataCount: xValueCount,
45
- containerWidth: container.clientWidth
46
+ containerWidth: container.clientWidth,
47
+ minLabelWidth
46
48
  });
47
49
  applyAxisY(view, {
48
50
  labelFormatter: formatAxis
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@publishfx/publish-chart",
3
- "version": "2.1.28",
3
+ "version": "2.1.29",
4
4
  "description": "A React chart component library for the Publish platform, including BarChart, LineChart, BarLineChart and other visualization components",
5
5
  "type": "module",
6
6
  "keywords": [