本文介绍使用Spire.XLS for Java在Excel中创建分离型饼图及环形图的方法。
分离型饼图
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import java.awt.*;
public class ExplodedPieChart {
public static void main(String[] args) {
//创建Workbook对象
Workbook workbook = new Workbook();
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//将图表数据写入工作表
sheet.getCellRange("A1").setValue("年份");
sheet.getCellRange("A2").setValue("2017");
sheet.getCellRange("A3").setValue("2018");
sheet.getCellRange("A4").setValue("2019");
sheet.getCellRange("A5").setValue("2020");
sheet.getCellRange("B1").setValue("销售额");
sheet.getCellRange("B2").setNumberValue(4000);
sheet.getCellRange("B3").setNumberValue(6000);
sheet.getCellRange("B4").setNumberValue(7000);
sheet.getCellRange("B5").setNumberValue(8500);
//设置单元格样式
sheet.getCellRange("A1:B1").setRowHeight(15);
sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("\"¥\"#,##0");
//添加整体分离型饼图
Chart piechart = sheet.getCharts().add(ExcelChartType.Pie);
//设置图表数据区域
piechart.setDataRange(sheet.getCellRange("B2:B5"));
piechart.setSeriesDataFromRange(false);
//设置图表位置
piechart.setLeftColumn(4);
piechart.setTopRow(1);
piechart.setRightColumn(10);
piechart.setBottomRow(20);
//设置图表标题
piechart.setChartTitle("年销售额");
piechart.getChartTitleArea().isBold(true);
piechart.getChartTitleArea().setSize(12);
//设置系列标签
ChartSerie cs = piechart.getSeries().get(0);
cs.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs.setValues(sheet.getCellRange("B2:B5"));
cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
piechart.getPlotArea().getFill().setVisible(false);
//设置饼图整体分离
for (int i = 0; i < piechart.getSeries().getCount(); i++)
{
piechart.getSeries().get(i).getDataFormat().setPercent(20);
}
//添加局部分离型饼图
Chart piechart1 = sheet.getCharts().add(ExcelChartType.Pie);
piechart1.setDataRange(sheet.getCellRange("B2:B5"));
piechart1.setSeriesDataFromRange(false);
piechart1.setLeftColumn(11);
piechart1.setTopRow(1);
piechart1.setRightColumn(17);
piechart1.setBottomRow(20);
piechart1.setChartTitle("年销售额");
piechart1.getChartTitleArea().isBold(true);
piechart1.getChartTitleArea().setSize(12);
ChartSerie cs1 = piechart1.getSeries().get(0);
cs1.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs1.setValues(sheet.getCellRange("B2:B5"));
cs1.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
piechart1.getPlotArea().getFill().setVisible(false);
//设置饼图局部分离
piechart1.getSeries().get(0).getDataPoints().get(1).getDataFormat().setPercent(20);
//保存文档
workbook.saveToFile("ExplodedPieChart.xlsx", ExcelVersion.Version2016);
workbook.dispose();
}
}
分离型环形图
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import java.awt.*;
public class ExplodedDoughnutChart {
public static void main(String[] args) {
//创建Workbook对象
Workbook workbook = new Workbook();
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//将图表数据写入工作表
sheet.getCellRange("A1").setValue("年份");
sheet.getCellRange("A2").setValue("2017");
sheet.getCellRange("A3").setValue("2018");
sheet.getCellRange("A4").setValue("2019");
sheet.getCellRange("A5").setValue("2020");
sheet.getCellRange("B1").setValue("销售额");
sheet.getCellRange("B2").setNumberValue(4000);
sheet.getCellRange("B3").setNumberValue(6000);
sheet.getCellRange("B4").setNumberValue(7000);
sheet.getCellRange("B5").setNumberValue(8500);
//设置单元格样式
sheet.getCellRange("A1:B1").setRowHeight(15);
sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("\"¥\"#,##0");
//添加环形图
Chart chart = sheet.getCharts().add(ExcelChartType.Doughnut);
//设置图表数据区域
chart.setDataRange(sheet.getCellRange("B2:B5"));
chart.setSeriesDataFromRange(false);
//设置图表位置
chart.setLeftColumn(4);
chart.setTopRow(1);
chart.setRightColumn(10);
chart.setBottomRow(20);
//设置图表标题
chart.setChartTitle("年销售额");
chart.getChartTitleArea().isBold(true);
chart.getChartTitleArea().setSize(12);
//设置系列标签
ChartSerie cs = chart.getSeries().get(0);
cs.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs.setValues(sheet.getCellRange("B2:B5"));
cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
chart.getPlotArea().getFill().setVisible(false);
//添加分离型环形图
Chart chart1 = sheet.getCharts().add(ExcelChartType.DoughnutExploded);
chart1.setDataRange(sheet.getCellRange("B2:B5"));
chart1.setSeriesDataFromRange(false);
chart1.setLeftColumn(11);
chart1.setTopRow(1);
chart1.setRightColumn(17);
chart1.setBottomRow(20);
chart1.setChartTitle("年销售额");
chart1.getChartTitleArea().isBold(true);
chart1.getChartTitleArea().setSize(12);
ChartSerie cs1 = chart1.getSeries().get(0);
cs1.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs1.setValues(sheet.getCellRange("B2:B5"));
cs1.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
chart1.getPlotArea().getFill().setVisible(false);
//保存文档
workbook.saveToFile("ExplodedDoughnutChart.xlsx",FileFormat.Version2013);
workbook.dispose();
}
}