Skip to content

Commit adeae8b

Browse files
committed
chore(axis): modify value axis minor/major unit
1 parent 197fe39 commit adeae8b

File tree

3 files changed

+194
-161
lines changed

3 files changed

+194
-161
lines changed

src/helper/modify-chart-helper.ts

Lines changed: 189 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -5,103 +5,111 @@ import {
55
ChartBubble,
66
ChartSlot,
77
ChartCategory,
8-
ChartSeries, ChartPoint, ChartAxisRange,
8+
ChartSeries,
9+
ChartPoint,
10+
ChartAxisRange,
911
} from '../types/chart-types';
1012
import { vd } from './general-helper';
11-
import {XmlHelper} from './xml-helper';
13+
import { XmlHelper } from './xml-helper';
1214

1315
export default class ModifyChartHelper {
1416
/**
1517
* Set chart data to modify default chart types.
1618
* See `__tests__/modify-existing-chart.test.js`
1719
*/
18-
static setChartData = (data: ChartData) => (
19-
element: XMLDocument | Element,
20-
chart?: Document,
21-
workbook?: Workbook,
22-
): void => {
23-
const slots = [] as ChartSlot[];
24-
data.series.forEach((series: ChartSeries, s: number) => {
25-
slots.push({
26-
index: s,
27-
series: series,
28-
targetCol: s + 1,
29-
type: 'defaultSeries',
20+
static setChartData =
21+
(data: ChartData) =>
22+
(
23+
element: XMLDocument | Element,
24+
chart?: Document,
25+
workbook?: Workbook,
26+
): void => {
27+
const slots = [] as ChartSlot[];
28+
data.series.forEach((series: ChartSeries, s: number) => {
29+
slots.push({
30+
index: s,
31+
series: series,
32+
targetCol: s + 1,
33+
type: 'defaultSeries',
34+
});
3035
});
31-
});
3236

33-
new ModifyChart(chart, workbook, data, slots).modify();
37+
new ModifyChart(chart, workbook, data, slots).modify();
3438

35-
// XmlHelper.dump(chart)
36-
// XmlHelper.dump(workbook.table)
37-
};
39+
// XmlHelper.dump(chart)
40+
// XmlHelper.dump(workbook.table)
41+
};
3842

3943
/**
4044
* Set chart data to modify vertical line charts.
4145
* See `__tests__/modify-chart-vertical-lines.test.js`
4246
*/
43-
static setChartVerticalLines = (data: ChartData) => (
44-
element: XMLDocument | Element,
45-
chart?: Document,
46-
workbook?: Workbook,
47-
): void => {
48-
const slots = [] as ChartSlot[];
49-
50-
slots.push({
51-
label: `Y-Values`,
52-
mapData: (point: number, category: ChartCategory) => category.y,
53-
targetCol: 1,
54-
});
55-
56-
data.series.forEach((series: ChartSeries, s: number) => {
47+
static setChartVerticalLines =
48+
(data: ChartData) =>
49+
(
50+
element: XMLDocument | Element,
51+
chart?: Document,
52+
workbook?: Workbook,
53+
): void => {
54+
const slots = [] as ChartSlot[];
55+
5756
slots.push({
58-
index: s,
59-
series: series,
60-
targetCol: s + 2,
61-
type: 'xySeries',
57+
label: `Y-Values`,
58+
mapData: (point: number, category: ChartCategory) => category.y,
59+
targetCol: 1,
6260
});
63-
});
6461

65-
new ModifyChart(chart, workbook, data, slots).modify();
66-
};
62+
data.series.forEach((series: ChartSeries, s: number) => {
63+
slots.push({
64+
index: s,
65+
series: series,
66+
targetCol: s + 2,
67+
type: 'xySeries',
68+
});
69+
});
70+
71+
new ModifyChart(chart, workbook, data, slots).modify();
72+
};
6773

6874
/**
6975
* Set chart data to modify scatter charts.
7076
* See `__tests__/modify-chart-scatter.test.js`
7177
*/
72-
static setChartScatter = (data: ChartData) => (
73-
element: XMLDocument | Element,
74-
chart?: Document,
75-
workbook?: Workbook,
76-
): void => {
77-
const slots = [] as ChartSlot[];
78-
79-
data.series.forEach((series: ChartSeries, s: number) => {
80-
const colId = s * 2;
81-
slots.push({
82-
index: s,
83-
series: series,
84-
targetCol: colId + 1,
85-
type: 'customSeries',
86-
tag: 'c:xVal',
87-
mapData: (point: ChartPoint): number => point.x,
88-
});
89-
slots.push({
90-
label: `${series.label}-Y-Value`,
91-
index: s,
92-
series: series,
93-
targetCol: colId + 2,
94-
type: 'customSeries',
95-
tag: 'c:yVal',
96-
mapData: (point: ChartPoint): number => point.y,
97-
isStrRef: false,
78+
static setChartScatter =
79+
(data: ChartData) =>
80+
(
81+
element: XMLDocument | Element,
82+
chart?: Document,
83+
workbook?: Workbook,
84+
): void => {
85+
const slots = [] as ChartSlot[];
86+
87+
data.series.forEach((series: ChartSeries, s: number) => {
88+
const colId = s * 2;
89+
slots.push({
90+
index: s,
91+
series: series,
92+
targetCol: colId + 1,
93+
type: 'customSeries',
94+
tag: 'c:xVal',
95+
mapData: (point: ChartPoint): number => point.x,
96+
});
97+
slots.push({
98+
label: `${series.label}-Y-Value`,
99+
index: s,
100+
series: series,
101+
targetCol: colId + 2,
102+
type: 'customSeries',
103+
tag: 'c:yVal',
104+
mapData: (point: ChartPoint): number => point.y,
105+
isStrRef: false,
106+
});
98107
});
99-
});
100108

101-
new ModifyChart(chart, workbook, data, slots).modify();
109+
new ModifyChart(chart, workbook, data, slots).modify();
102110

103-
// XmlHelper.dump(chart)
104-
};
111+
// XmlHelper.dump(chart)
112+
};
105113

106114
/**
107115
* Set chart data to modify combo charts.
@@ -110,102 +118,124 @@ export default class ModifyChartHelper {
110118
* other series: vertical lines
111119
* See `__tests__/modify-chart-scatter.test.js`
112120
*/
113-
static setChartCombo = (data: ChartData) => (
114-
element: XMLDocument | Element,
115-
chart?: Document,
116-
workbook?: Workbook,
117-
): void => {
118-
const slots = [] as ChartSlot[];
119-
120-
slots.push({
121-
index: 0,
122-
series: data.series[0],
123-
targetCol: 1,
124-
type: 'defaultSeries',
125-
});
126-
127-
slots.push({
128-
index: 1,
129-
label: `Y-Values`,
130-
mapData: (point: number, category: ChartCategory) => category.y,
131-
targetCol: 2,
132-
});
133-
134-
data.series.forEach((series: ChartSeries, s: number) => {
135-
if(s > 0)
136-
slots.push({
137-
index: s,
138-
series: series,
139-
targetCol: s + 2,
140-
targetYCol: 2,
141-
type: 'xySeries',
142-
});
143-
}
144-
);
145-
146-
new ModifyChart(chart, workbook, data, slots).modify();
147-
};
121+
static setChartCombo =
122+
(data: ChartData) =>
123+
(
124+
element: XMLDocument | Element,
125+
chart?: Document,
126+
workbook?: Workbook,
127+
): void => {
128+
const slots = [] as ChartSlot[];
148129

149-
/**
150-
* Set chart data to modify bubble charts.
151-
* See `__tests__/modify-chart-bubbles.test.js`
152-
*/
153-
static setChartBubbles = (data: ChartData) => (
154-
element: XMLDocument | Element,
155-
chart?: Document,
156-
workbook?: Workbook,
157-
): void => {
158-
const slots = [] as ChartSlot[];
159-
160-
data.series.forEach((series: ChartSeries, s: number) => {
161-
const colId = s * 3;
162-
slots.push({
163-
index: s,
164-
series: series,
165-
targetCol: colId + 1,
166-
type: 'customSeries',
167-
tag: 'c:xVal',
168-
mapData: (point: ChartBubble): number => point.x,
169-
});
170130
slots.push({
171-
label: `${series.label}-Y-Value`,
172-
index: s,
173-
series: series,
174-
targetCol: colId + 2,
175-
type: 'customSeries',
176-
tag: 'c:yVal',
177-
mapData: (point: ChartBubble): number => point.y,
178-
isStrRef: false,
131+
index: 0,
132+
series: data.series[0],
133+
targetCol: 1,
134+
type: 'defaultSeries',
179135
});
136+
180137
slots.push({
181-
label: `${series.label}-Size`,
182-
index: s,
183-
series: series,
184-
targetCol: colId + 3,
185-
type: 'customSeries',
186-
tag: 'c:bubbleSize',
187-
mapData: (point: ChartBubble): number => point.size,
188-
isStrRef: false,
138+
index: 1,
139+
label: `Y-Values`,
140+
mapData: (point: number, category: ChartCategory) => category.y,
141+
targetCol: 2,
189142
});
190-
});
191-
192-
new ModifyChart(chart, workbook, data, slots).modify();
193-
194-
// XmlHelper.dump(chart)
195-
};
196143

197-
static setAxisRange = (range: ChartAxisRange) => (
198-
chart: XMLDocument,
199-
): void => {
200-
const axis = chart.getElementsByTagName('c:valAx')[range.axisIndex || 0]
201-
if(!axis) return
144+
data.series.forEach((series: ChartSeries, s: number) => {
145+
if (s > 0)
146+
slots.push({
147+
index: s,
148+
series: series,
149+
targetCol: s + 2,
150+
targetYCol: 2,
151+
type: 'xySeries',
152+
});
153+
});
202154

203-
const scaling = axis.getElementsByTagName('c:scaling')[0]
155+
new ModifyChart(chart, workbook, data, slots).modify();
156+
};
204157

205-
scaling.getElementsByTagName('c:min')[0]
206-
.setAttribute('val', String(range.min))
207-
scaling.getElementsByTagName('c:max')[0]
208-
.setAttribute('val', String(range.max))
209-
}
158+
/**
159+
* Set chart data to modify bubble charts.
160+
* See `__tests__/modify-chart-bubbles.test.js`
161+
*/
162+
static setChartBubbles =
163+
(data: ChartData) =>
164+
(
165+
element: XMLDocument | Element,
166+
chart?: Document,
167+
workbook?: Workbook,
168+
): void => {
169+
const slots = [] as ChartSlot[];
170+
171+
data.series.forEach((series: ChartSeries, s: number) => {
172+
const colId = s * 3;
173+
slots.push({
174+
index: s,
175+
series: series,
176+
targetCol: colId + 1,
177+
type: 'customSeries',
178+
tag: 'c:xVal',
179+
mapData: (point: ChartBubble): number => point.x,
180+
});
181+
slots.push({
182+
label: `${series.label}-Y-Value`,
183+
index: s,
184+
series: series,
185+
targetCol: colId + 2,
186+
type: 'customSeries',
187+
tag: 'c:yVal',
188+
mapData: (point: ChartBubble): number => point.y,
189+
isStrRef: false,
190+
});
191+
slots.push({
192+
label: `${series.label}-Size`,
193+
index: s,
194+
series: series,
195+
targetCol: colId + 3,
196+
type: 'customSeries',
197+
tag: 'c:bubbleSize',
198+
mapData: (point: ChartBubble): number => point.size,
199+
isStrRef: false,
200+
});
201+
});
210202

203+
new ModifyChart(chart, workbook, data, slots).modify();
204+
205+
// XmlHelper.dump(chart)
206+
};
207+
208+
static setAxisRange =
209+
(range: ChartAxisRange) =>
210+
(chart: XMLDocument): void => {
211+
const axis = chart.getElementsByTagName('c:valAx')[range.axisIndex || 0];
212+
if (!axis) return;
213+
ModifyChartHelper.setAxisRangeAttribute(
214+
axis,
215+
'c:majorUnit',
216+
range.majorUnit,
217+
);
218+
ModifyChartHelper.setAxisRangeAttribute(
219+
axis,
220+
'c:minorUnit',
221+
range.minorUnit,
222+
);
223+
224+
const scaling = axis.getElementsByTagName('c:scaling')[0];
225+
226+
ModifyChartHelper.setAxisRangeAttribute(scaling, 'c:min', range.min);
227+
ModifyChartHelper.setAxisRangeAttribute(scaling, 'c:max', range.max);
228+
};
229+
230+
static setAxisRangeAttribute = (
231+
axis: Element,
232+
tag: string,
233+
value: number,
234+
) => {
235+
if (value === undefined || !axis) return;
236+
const target = axis.getElementsByTagName(tag);
237+
if (target.length > 0) {
238+
target[0].setAttribute('val', String(value));
239+
}
240+
};
211241
}

0 commit comments

Comments
 (0)