1
0
mirror of https://github.com/QtExcel/QXlsx.git synced 2025-01-30 05:02:52 +08:00
QXlsx/TestExcel/chart.cpp

92 lines
2.7 KiB
C++
Raw Permalink Normal View History

2017-09-16 16:34:45 +09:00
// chart.cpp
2018-12-19 13:50:17 +09:00
#include <QtGlobal>
2017-09-16 12:21:23 +09:00
#include <QtCore>
2018-12-19 13:50:17 +09:00
#include <QDebug>
2019-01-02 20:56:00 +09:00
2017-09-16 12:21:23 +09:00
#include "xlsxdocument.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
2020-06-05 22:20:08 +09:00
QXLSX_USE_NAMESPACE
2017-09-16 12:21:23 +09:00
2017-09-16 16:34:45 +09:00
int chart()
2017-09-16 12:21:23 +09:00
{
//![0]
Document xlsx;
2019-09-15 00:45:12 +09:00
for (int i=1; i<10; ++i)
{
2017-09-16 12:21:23 +09:00
xlsx.write(i, 1, i*i*i); //A1:A9
xlsx.write(i, 2, i*i); //B1:B9
xlsx.write(i, 3, i*i-1); //C1:C9
}
//![0]
//![1]
Chart *pieChart = xlsx.insertChart(3, 3, QSize(300, 300));
2019-02-26 16:59:31 +09:00
pieChart->setChartType(Chart::CT_PieChart);
2017-09-16 12:21:23 +09:00
pieChart->addSeries(CellRange("A1:A9"));
pieChart->addSeries(CellRange("B1:B9"));
pieChart->addSeries(CellRange("C1:C9"));
Chart *pie3DChart = xlsx.insertChart(3, 9, QSize(300, 300));
2019-02-26 16:59:31 +09:00
pie3DChart->setChartType(Chart::CT_Pie3DChart);
2017-09-16 12:21:23 +09:00
pie3DChart->addSeries(CellRange("A1:C9"));
Chart *barChart = xlsx.insertChart(23, 3, QSize(300, 300));
2019-02-26 16:59:31 +09:00
barChart->setChartType(Chart::CT_BarChart);
2017-09-16 12:21:23 +09:00
barChart->addSeries(CellRange("A1:C9"));
Chart *bar3DChart = xlsx.insertChart(23, 9, QSize(300, 300));
2019-02-26 16:59:31 +09:00
bar3DChart->setChartType(Chart::CT_Bar3DChart);
2017-09-16 12:21:23 +09:00
bar3DChart->addSeries(CellRange("A1:C9"));
2018-12-19 13:50:17 +09:00
// issue #10 is fixed. https://github.com/j2doll/QXlsx/issues/10
// {{
2017-09-16 12:21:23 +09:00
Chart *lineChart = xlsx.insertChart(43, 3, QSize(300, 300));
2019-02-26 16:59:31 +09:00
lineChart->setChartType(Chart::CT_LineChart);
2017-09-16 12:21:23 +09:00
lineChart->addSeries(CellRange("A1:C9"));
Chart *line3DChart = xlsx.insertChart(43, 9, QSize(300, 300));
2019-02-26 16:59:31 +09:00
line3DChart->setChartType(Chart::CT_Line3DChart);
2017-09-16 12:21:23 +09:00
line3DChart->addSeries(CellRange("A1:C9"));
Chart *areaChart = xlsx.insertChart(63, 3, QSize(300, 300));
2019-02-26 16:59:31 +09:00
areaChart->setChartType(Chart::CT_AreaChart);
2017-09-16 12:21:23 +09:00
areaChart->addSeries(CellRange("A1:C9"));
Chart *area3DChart = xlsx.insertChart(63, 9, QSize(300, 300));
2019-02-26 16:59:31 +09:00
area3DChart->setChartType(Chart::CT_Area3DChart);
2017-09-16 12:21:23 +09:00
area3DChart->addSeries(CellRange("A1:C9"));
2018-12-19 13:50:17 +09:00
// }}
2017-09-16 12:21:23 +09:00
Chart *scatterChart = xlsx.insertChart(83, 3, QSize(300, 300));
2019-02-26 16:59:31 +09:00
scatterChart->setChartType(Chart::CT_ScatterChart);
2017-09-16 12:21:23 +09:00
//Will generate three lines.
scatterChart->addSeries(CellRange("A1:A9"));
scatterChart->addSeries(CellRange("B1:B9"));
scatterChart->addSeries(CellRange("C1:C9"));
Chart *scatterChart_2 = xlsx.insertChart(83, 9, QSize(300, 300));
2019-02-26 16:59:31 +09:00
scatterChart_2->setChartType(Chart::CT_ScatterChart);
2017-09-16 12:21:23 +09:00
//Will generate two lines.
scatterChart_2->addSeries(CellRange("A1:C9"));
Chart *doughnutChart = xlsx.insertChart(103, 3, QSize(300, 300));
2019-02-26 16:59:31 +09:00
doughnutChart->setChartType(Chart::CT_DoughnutChart);
2017-09-16 12:21:23 +09:00
doughnutChart->addSeries(CellRange("A1:C9"));
//![1]
//![2]
2017-09-23 11:12:59 +09:00
xlsx.saveAs("chart1.xlsx");
2017-09-16 12:21:23 +09:00
//![2]
2017-09-23 11:12:59 +09:00
Document xlsx2("chart1.xlsx");
2019-02-26 16:59:31 +09:00
if ( xlsx2.load() )
{
xlsx2.saveAs("chart2.xlsx");
}
2017-09-16 12:21:23 +09:00
return 0;
}