本文介绍使用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();
}
}