Skip to content

Commit 6d0244c

Browse files
committed
2 parents 3b12fb5 + bb1832f commit 6d0244c

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/modules/Exports.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class Exports {
363363
columns.push('maximum')
364364
} else {
365365
series.map((s, sI) => {
366-
const sname = s.name ? s.name : `series-${sI}`
366+
const sname = (s.name ? s.name : `series-${sI}`) + ''
367367
if (w.globals.axisCharts) {
368368
columns.push(
369369
sname.split(columnDelimiter).join('')

tests/unit/download-csv.spec.js

+52
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,42 @@ describe('Export Csv', () => {
5353
expect.stringContaining('.csv')
5454
)
5555
})
56+
it('export csv from simple bar chart with numeric series names should call triggerDownload with csv encoded file data', () => {
57+
var options = {
58+
chart: {
59+
height: 380,
60+
width: "100%",
61+
type: "bar"
62+
},
63+
series: [
64+
{
65+
name: 2022,
66+
data: [1,2]
67+
},
68+
{
69+
name: 2023,
70+
data: [2,3]
71+
}
72+
],
73+
xaxis: {
74+
categories: ["Category 1","Category 2"]
75+
}
76+
};
77+
const csvData = "category,2022,2023\n" +
78+
"Category 1,,2\n" +
79+
"Category 2,,3"
80+
const chart = createChartWithOptions(options)
81+
chart.w.globals.collapsedSeriesIndices = [0]
82+
const exports = new Exports(chart.ctx)
83+
jest.spyOn(Exports.prototype,'triggerDownload')
84+
exports.exportToCSV(chart.w.config.series,'fileName')
85+
expect(Exports.prototype.triggerDownload).toHaveBeenCalledTimes(1)
86+
expect(Exports.prototype.triggerDownload).toHaveBeenCalledWith(
87+
expect.stringContaining(encodeURIComponent(csvData)),
88+
expect.toBeUndefined,
89+
expect.stringContaining('.csv')
90+
)
91+
})
5692
it('export csv from simple line chart with two series should call triggerDownload with csv encoded file data', () => {
5793
const series = [{name: 'series 1', data: [0,1]},{name: 'series 2', data: [1,2]}]
5894
const csvData = "category,series 1,series 2\n" +
@@ -69,6 +105,22 @@ describe('Export Csv', () => {
69105
expect.stringContaining('.csv')
70106
)
71107
})
108+
it('export csv from simple line chart with numeric series names should call triggerDownload with csv encoded file data', () => {
109+
const series = [{name: 2022, data: [0,1]},{name: 2023, data: [1,2]}]
110+
const csvData = "category,2022,2023\n" +
111+
"1,0,1\n" +
112+
"2,1,2"
113+
const chart = createChart('line', series)
114+
const exports = new Exports(chart.ctx)
115+
jest.spyOn(Exports.prototype,'triggerDownload')
116+
exports.exportToCSV(chart.w.config.series,'fileName')
117+
expect(Exports.prototype.triggerDownload).toHaveBeenCalledTimes(1)
118+
expect(Exports.prototype.triggerDownload).toHaveBeenCalledWith(
119+
expect.stringContaining(encodeURIComponent(csvData)),
120+
expect.toBeUndefined,
121+
expect.stringContaining('.csv')
122+
)
123+
})
72124
it("export csv from simple line chart with two x y series should call triggerDownload with csv encoded file data", () => {
73125
const series = [{
74126
name: 'series 1',

0 commit comments

Comments
 (0)