本帖最后由 Ellia.Duan 于 2022-12-28 15:30 编辑
有很多熟悉GcExcel学习指南的用户发现,在GcExcel中创建图表,发现获取数据源的区域是连续的,如:
- shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
复制代码 首先了解下系列概念
系列是指一组数据点,或仅是图表中绘制的值列表。 使用电子表格时,您可以在图表中绘制一个或多个数据系列。每个系列都有一个图例项,并提供对图表控件系列对象集合的访问。 - // Adding Charts
- IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
- worksheet.getRange("A1:D6").setValue(new Object[][]
- {
- {null, "S1", "S2", "S3"},
- {"Item1", 10, 25, 25},
- {"Item2", -51, -36, 27},
- {"Item3", 52, -85, -30},
- {"Item4", 22, 65, 65},
- {"Item5", 23, 69, 69}
- });
-
- // Detects three series, B2:B6, C2:C6, D2:D6.
- // Does not detect out series labels and category labels, auto generated.
- shape.getChart().getSeriesCollection().add(worksheet.getRange("B2:D6"));
复制代码执行后效果如下所示: 如果此时修改区域为 - shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"));
复制代码执行后效果如下所示: 如果此时类别轴数值不在第一列,回到问题本身,如何实现呢? - IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 300, 10, 300, 300);
- worksheet.getRange("A1:E4").setValue(new Object[][]{
- {"系列一", "系列二", "系列三", "系列四", ""},
- {8, 1, 2, 5, "类别1"},
- {9, 2, 3, 6, "类别2"},
- {10, 3, 4, 7, "类别3"},
- });
- //create chart.
- ISeries series1 = shape.getChart().getSeriesCollection().newSeries();
- ISeries series2 = shape.getChart().getSeriesCollection().newSeries();
- ISeries series3 = shape.getChart().getSeriesCollection().newSeries();
- ISeries series4 = shape.getChart().getSeriesCollection().newSeries();
- series1.setFormula("=SERIES(Sheet1!$B$1,Sheet1!$E$2:$E$4,Sheet1!$B$2:$B$4,1)");
- series2.setFormula("=SERIES(Sheet1!$C$1,Sheet1!$E$2:$E$4,Sheet1!$C$2:$C$4,2)");
- series3.setFormula("=SERIES(Sheet1!$A$1,Sheet1!$E$2:$E$4,Sheet1!$A$2:$A$4,3)");
- series4.setFormula("=SERIES(Sheet1!$D$1,Sheet1!$E$2:$E$4,Sheet1!$D$2:$D$4,3)");
复制代码
此时,可以用SERIES公式,执行后效果如下所示:
|