Skip to content

Commit e74192d

Browse files
committed
feature(chart): support for combo chart (bar/vert. lines)
1 parent 5aa5e84 commit e74192d

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/helper/modify-chart-helper.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
ChartCategory,
88
ChartSeries, ChartPoint,
99
} from '../types/chart-types';
10+
import { vd } from './general-helper';
11+
import {XmlHelper} from './xml-helper';
1012

1113
export default class ModifyChartHelper {
1214
/**
@@ -101,6 +103,49 @@ export default class ModifyChartHelper {
101103
// XmlHelper.dump(chart)
102104
};
103105

106+
/**
107+
* Set chart data to modify combo charts.
108+
* This type is prepared for
109+
* first series: bar chart (e.g. total)
110+
* other series: vertical lines
111+
* See `__tests__/modify-chart-scatter.test.js`
112+
*/
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+
};
148+
104149
/**
105150
* Set chart data to modify bubble charts.
106151
* See `__tests__/modify-chart-bubbles.test.js`

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const setChartData = ModifyChartHelper.setChartData;
2323
const setChartVerticalLines = ModifyChartHelper.setChartVerticalLines;
2424
const setChartScatter = ModifyChartHelper.setChartScatter;
2525
const setChartBubbles = ModifyChartHelper.setChartBubbles;
26+
const setChartCombo = ModifyChartHelper.setChartCombo;
2627

2728
import { AutomizerSummary } from './types/types';
2829
export type { AutomizerSummary };
@@ -54,5 +55,6 @@ export const modify = {
5455
setChartData,
5556
setChartVerticalLines,
5657
setChartScatter,
58+
setChartCombo,
5759
setChartBubbles,
5860
};

src/modify/modify-chart.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ export class ModifyChart {
6767
// XmlHelper.dump(this.chart.root as XMLDocument)
6868
}
6969

70-
setColumns(slot: ChartSlot[]): ChartColumn[] {
70+
setColumns(slots: ChartSlot[]): ChartColumn[] {
7171
const columns = [] as ChartColumn[];
7272

73-
slot.forEach((slot) => {
73+
slots.forEach((slot) => {
7474
const series = slot.series;
7575
const index = slot.index;
7676
const targetCol = slot.targetCol;
77+
const targetYCol = slot.targetYCol || 1;
7778

7879
const label = slot.label ? slot.label : series.label;
7980

@@ -108,6 +109,7 @@ export class ModifyChart {
108109
category,
109110
slot.tag,
110111
mapData,
112+
targetYCol
111113
);
112114
}
113115
: null;
@@ -167,7 +169,7 @@ export class ModifyChart {
167169
this.series(column.series, {
168170
...this.seriesId(column.series),
169171
...this.seriesLabel(column.label, colId),
170-
...this.seriesStyle(this.data.series[colId]),
172+
...this.seriesStyle(this.data.series[column.series]),
171173
}),
172174
);
173175
}
@@ -334,6 +336,7 @@ export class ModifyChart {
334336
isRequired: false,
335337
children: {
336338
'c:spPr': {
339+
isRequired: false,
337340
modify: ModifyColorHelper.solidFill(series.style.marker?.color),
338341
}
339342
}
@@ -378,10 +381,13 @@ export class ModifyChart {
378381
targetCol: number,
379382
point: number,
380383
category: ChartCategory,
384+
tag: string,
385+
mapData: ChartDataMapper,
386+
targetYCol: number,
381387
): ModificationTags {
382388
return {
383389
'c:xVal': this.point(r, targetCol, point),
384-
'c:yVal': this.point(r, 1, category.y),
390+
'c:yVal': this.point(r, targetYCol, category.y),
385391
};
386392
}
387393

src/types/chart-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export type ChartSlot = {
5858
series?: ChartSeries;
5959
index?: number;
6060
targetCol: number;
61+
targetYCol?: number;
6162
type?: string;
6263
tag?: string;
6364
isStrRef?: boolean;

0 commit comments

Comments
 (0)