更改折线图 Apache POI 的颜色

问题描述 投票:0回答:2

我使用 Apache POI 生成了折线图。我需要更改图表中的默认颜色。我可以使用 RGB 代码来定义每条线的特定颜色吗?

我的代码如下。

Drawing drawing = sheet4.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 17, 22);

Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.RIGHT);

LineChartData data = chart.getChartDataFactory().createLineChartData();

ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 0, 0));
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 1, 1));
ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 3, 3));
ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 4, 4));
ChartDataSource<Number> ys4 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 8, 8));

LineChartSeries series1 = data.addSeries(xs, ys1);
series1.setTitle("Value 1");
LineChartSeries series2 = data.addSeries(xs, ys2);
series2.setTitle("Value 2");
LineChartSeries series3 = data.addSeries(xs, ys3);
series3.setTitle("Value 3");
LineChartSeries series4 = data.addSeries(xs, ys4);
series4.setTitle("Value 4");

chart.plot(data, bottomAxis, leftAxis);

XSSFChart xssfChart = (XSSFChart) chart;
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
plotArea.getLineChartArray()[0].getSmooth();
CTBoolean ctBool = CTBoolean.Factory.newInstance();
ctBool.setVal(false);
plotArea.getLineChartArray()[0].setSmooth(ctBool);
for (CTLineSer ser : plotArea.getLineChartArray()[0].getSerArray()) {
    ser.setSmooth(ctBool);
}
java excel charts apache-poi
2个回答
8
投票

我总是喜欢完整的例子。然后其他人就可以复制这个,而不需要猜测

Excel
工作表可能是什么样子。

以下完整示例以 Office Open XML 格式 (

Excel
) 创建一个新的
*.xlsx
工作簿,其中包含一些示例数据和折线图。

第一个代码部分创建一个折线图,

apache poi
版本
3.17
如何默认创建它。

然后在第二个代码部分,我自定义图表。代码部分被注释以显示它们应该做什么。之后,自定义图表也兼容

OpenOffice
/
Libreoffice
 Calc
apache poi
默认图表与
OpenOffice
/
Libreoffice
 Calc
不兼容。至少达到版本
3.17

为了定制,需要了解内部低级对象

apache poi
用作基本对象。这些包含在 ooxml-schemas-1.3.jar 中。不幸的是,没有更多可用的
API
文档。因此,必须下载
ooxml-schemas-1.3-sources.jar 
,然后自己执行
javadoc
才能获取文档。

还应该知道 Office Open XML 格式 (

*.xlsx
) 的文件只是
ZIP
档案。所以人们可以解压它们并看看
/xl/charts/chart1.xml
。当您在
apache poi
创建图表后执行此操作,并将该 XML 与完成
Excel
中所需更改后重新保存后的 XML 进行比较时,您将看到所需的 XML 更改。

示例:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.util.Units;

public class CreateExcelLineChartAndCustomize {

 public static void main(String[] args) throws Exception {

  Workbook wb = new XSSFWorkbook();
  Sheet sheet = wb.createSheet("linechart");
  final int NUM_OF_ROWS = 100;
  final int NUM_OF_COLUMNS = 5;

  // create some data
  Row row;
  Cell cell;
  String[] headings = new String []{"x", "sin(x)", "cos(x)", "random", "random/x"};
  for (int rowIndex = 0; rowIndex < 1; rowIndex++) {
   row = sheet.createRow(rowIndex);
   for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
    cell = row.createCell(colIndex);
    cell.setCellValue(headings[colIndex]);
   }
  }
  for (int rowIndex = 1; rowIndex < NUM_OF_ROWS; rowIndex++) {
   row = sheet.createRow(rowIndex);
   for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
    cell = row.createCell(colIndex);
    switch (colIndex) {
     case 0:
      cell.setCellValue(rowIndex/10d);
      break;
     case 1:
      cell.setCellFormula("SIN(A" + (rowIndex+1) + ")");
      break;
     case 2:
      cell.setCellFormula("COS(A" + (rowIndex+1) + ")");
      break;
     case 3:
      cell.setCellFormula("RAND()");
      break;
     case 4:
      cell.setCellFormula("-D"+(rowIndex+1));
      break;
     default:
    }
   }
  }

  // create default chart
  Drawing<?> drawing = sheet.createDrawingPatriarch();
  ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 6, 1, 15, 21);

  Chart chart = drawing.createChart(anchor);
  ChartLegend legend = chart.getOrCreateLegend();
  legend.setPosition(LegendPosition.RIGHT);

  if (chart instanceof XSSFChart) ((XSSFChart)chart).setTitleText("Some lines");

  LineChartData data = chart.getChartDataFactory().createLineChartData();

  ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
  ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
  leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

  ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 0, 0));
  ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 1, 1));
  ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 2, 2));
  ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 3, 3));
  ChartDataSource<Number> ys4 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 4, 4));

  LineChartSeries series1 = data.addSeries(xs, ys1);
  series1.setTitle("Value 1");
  LineChartSeries series2 = data.addSeries(xs, ys2);
  series2.setTitle("Value 2");
  LineChartSeries series3 = data.addSeries(xs, ys3);
  series3.setTitle("Value 3");
  LineChartSeries series4 = data.addSeries(xs, ys4);
  series4.setTitle("Value 4");

  chart.plot(data, bottomAxis, leftAxis);

  // customize the chart, this also makes it OpenOffice/Libreoffice Calc compatible
  if (chart instanceof XSSFChart) { 

   XSSFChart xssfChart = (XSSFChart)chart;

   // do not auto delete the title
   if (xssfChart.getCTChart().getAutoTitleDeleted() == null) xssfChart.getCTChart().addNewAutoTitleDeleted();
   xssfChart.getCTChart().getAutoTitleDeleted().setVal(false);

   // plot area background and border line
   if (xssfChart.getCTChartSpace().getSpPr() == null) xssfChart.getCTChartSpace().addNewSpPr();
   if (xssfChart.getCTChartSpace().getSpPr().getSolidFill() == null) 
    xssfChart.getCTChartSpace().getSpPr().addNewSolidFill();
   if (xssfChart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr() == null)
    xssfChart.getCTChartSpace().getSpPr().getSolidFill().addNewSrgbClr();
   xssfChart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)255,(byte)255,(byte)255});
   if (xssfChart.getCTChartSpace().getSpPr().getLn() == null) xssfChart.getCTChartSpace().getSpPr().addNewLn();
   xssfChart.getCTChartSpace().getSpPr().getLn().setW(Units.pixelToEMU(1));
   if (xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill() == null)
    xssfChart.getCTChartSpace().getSpPr().getLn().addNewSolidFill();
   if (xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr() == null)
    xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().addNewSrgbClr();
   xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)0,(byte)0,(byte)0});

   // line style of cat axis
   if (xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr() == null)
    xssfChart.getCTChart().getPlotArea().getCatAxArray(0).addNewSpPr();
   if (xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn() == null)
    xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().addNewLn();
   xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().setW(Units.pixelToEMU(1));
   if (xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().getSolidFill() == null)
    xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().addNewSolidFill();
   if (xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().getSolidFill().getSrgbClr() == null)
    xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().getSolidFill().addNewSrgbClr();
   xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().getSolidFill().getSrgbClr()
    .setVal(new byte[]{(byte)0,(byte)0,(byte)0});

   //line style of val axis
   if (xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr() == null)
    xssfChart.getCTChart().getPlotArea().getValAxArray(0).addNewSpPr();
   if (xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn() == null)
    xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().addNewLn();
   xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().setW(Units.pixelToEMU(1));
   if (xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().getSolidFill() == null)
    xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().addNewSolidFill();
   if (xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().getSolidFill().getSrgbClr() == null)
    xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().getSolidFill().addNewSrgbClr();
   xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().getSolidFill().getSrgbClr()
    .setVal(new byte[]{(byte)0,(byte)0,(byte)0});

   // line style of the series
   for (int i = 0; i < 4; i++) {
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).addNewSpPr();
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().addNewLn();
    xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i)
     .getSpPr().getLn().setW(Units.pixelToEMU(3));
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn().getSolidFill() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn().addNewSolidFill();
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn().getSolidFill().getSrgbClr() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn().getSolidFill().addNewSrgbClr();
   }

   // first series red
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0)
    .getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)255,(byte)0,(byte)0});

   // second series green
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1)
    .getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)0,(byte)255,(byte)0});

   // third series blue
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(2)
    .getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)0,(byte)0,(byte)255});

   // fourth series yellow
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(3)
    .getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)255,(byte)255,(byte)0});


   // set line data series to not smooth the line
   for (int i = 0; i < 4; i++) {
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSmooth() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).addNewSmooth();
    xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSmooth().setVal(false);
   }

   // set or unset tick marks
   for (int i = 0; i < 4; i++) {
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getMarker() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).addNewMarker();
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getMarker().getSymbol() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getMarker().addNewSymbol();
   }
   // no tick marks
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getMarker().getSymbol().setVal(
    org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle.NONE);
   // diamond tick marks
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1).getMarker().getSymbol().setVal(
    org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle.DIAMOND);
   // auto tick marks = marker set but no symbol given
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(2).getMarker().unsetSymbol();
   // no tick marks
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(3).getMarker().getSymbol().setVal(
    org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle.NONE);

  }


  wb.write(new FileOutputStream("CreateExcelLineChartAndCustomize.xlsx"));
  wb.close();

 }

}

结果:

enter image description here


上面的完整代码适用于 Apache POI 版本 3.17 (2018)。 当前的 Apache POI 版本使用

XDDF
来显示图表。

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.util.Units;

public class CreateExcelLineChartAndCustomize {

 public static void main(String[] args) throws Exception {

  XSSFWorkbook wb = new XSSFWorkbook();
  XSSFSheet sheet = wb.createSheet("linechart");
  final int NUM_OF_ROWS = 100;
  final int NUM_OF_COLUMNS = 5;

  // create some data
  Row row;
  Cell cell;
  String[] headings = new String []{"x", "sin(x)", "cos(x)", "random", "random/x"};
  for (int rowIndex = 0; rowIndex < 1; rowIndex++) {
   row = sheet.createRow(rowIndex);
   for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
    cell = row.createCell(colIndex);
    cell.setCellValue(headings[colIndex]);
   }
  }
  for (int rowIndex = 1; rowIndex < NUM_OF_ROWS; rowIndex++) {
   row = sheet.createRow(rowIndex);
   for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
    cell = row.createCell(colIndex);
    switch (colIndex) {
     case 0:
      cell.setCellValue(rowIndex/10d);
      break;
     case 1:
      cell.setCellFormula("SIN(A" + (rowIndex+1) + ")");
      break;
     case 2:
      cell.setCellFormula("COS(A" + (rowIndex+1) + ")");
      break;
     case 3:
      cell.setCellFormula("RAND()");
      break;
     case 4:
      cell.setCellFormula("-D"+(rowIndex+1));
      break;
     default:
    }
   }
  }

  // create default chart
  XSSFDrawing drawing = sheet.createDrawingPatriarch();
  XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 6, 1, 15, 21);

  XSSFChart chart = drawing.createChart(anchor);
  XDDFChartLegend legend = chart.getOrAddLegend();
  legend.setPosition(LegendPosition.RIGHT);

  chart.setTitleText("Some lines");

  XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
  XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
  leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  
  XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);

  XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 0, 0));
  XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 1, 1));
  XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 2, 2));
  XDDFNumericalDataSource<Double> ys3 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 3, 3));
  XDDFNumericalDataSource<Double> ys4 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 99, 4, 4));

  XDDFChartData.Series series1 = data.addSeries(xs, ys1);
  series1.setTitle("Value 1");
  XDDFChartData.Series series2 = data.addSeries(xs, ys2);
  series2.setTitle("Value 2");
  XDDFChartData.Series series3 = data.addSeries(xs, ys3);
  series3.setTitle("Value 3");
  XDDFChartData.Series series4 = data.addSeries(xs, ys4);
  series4.setTitle("Value 4");

  chart.plot(data);

  // customize the chart, this also makes it OpenOffice/Libreoffice Calc compatible
  if (chart instanceof XSSFChart) { 

   XSSFChart xssfChart = (XSSFChart)chart;

   // do not auto delete the title
   if (xssfChart.getCTChart().getAutoTitleDeleted() == null) xssfChart.getCTChart().addNewAutoTitleDeleted();
   xssfChart.getCTChart().getAutoTitleDeleted().setVal(false);

   // plot area background and border line
   if (xssfChart.getCTChartSpace().getSpPr() == null) xssfChart.getCTChartSpace().addNewSpPr();
   if (xssfChart.getCTChartSpace().getSpPr().getSolidFill() == null) 
    xssfChart.getCTChartSpace().getSpPr().addNewSolidFill();
   if (xssfChart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr() == null)
    xssfChart.getCTChartSpace().getSpPr().getSolidFill().addNewSrgbClr();
   xssfChart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)255,(byte)255,(byte)255});
   if (xssfChart.getCTChartSpace().getSpPr().getLn() == null) xssfChart.getCTChartSpace().getSpPr().addNewLn();
   xssfChart.getCTChartSpace().getSpPr().getLn().setW(Units.pixelToEMU(1));
   if (xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill() == null)
    xssfChart.getCTChartSpace().getSpPr().getLn().addNewSolidFill();
   if (xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr() == null)
    xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().addNewSrgbClr();
   xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)0,(byte)0,(byte)0});

   // line style of cat axis
   if (xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr() == null)
    xssfChart.getCTChart().getPlotArea().getCatAxArray(0).addNewSpPr();
   if (xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn() == null)
    xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().addNewLn();
   xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().setW(Units.pixelToEMU(1));
   if (xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().getSolidFill() == null)
    xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().addNewSolidFill();
   if (xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().getSolidFill().getSrgbClr() == null)
    xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().getSolidFill().addNewSrgbClr();
   xssfChart.getCTChart().getPlotArea().getCatAxArray(0).getSpPr().getLn().getSolidFill().getSrgbClr()
    .setVal(new byte[]{(byte)0,(byte)0,(byte)0});

   //line style of val axis
   if (xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr() == null)
    xssfChart.getCTChart().getPlotArea().getValAxArray(0).addNewSpPr();
   if (xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn() == null)
    xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().addNewLn();
   xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().setW(Units.pixelToEMU(1));
   if (xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().getSolidFill() == null)
    xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().addNewSolidFill();
   if (xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().getSolidFill().getSrgbClr() == null)
    xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().getSolidFill().addNewSrgbClr();
   xssfChart.getCTChart().getPlotArea().getValAxArray(0).getSpPr().getLn().getSolidFill().getSrgbClr()
    .setVal(new byte[]{(byte)0,(byte)0,(byte)0});

   // line style of the series
   for (int i = 0; i < 4; i++) {
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).addNewSpPr();
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().addNewLn();
    xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i)
     .getSpPr().getLn().setW(Units.pixelToEMU(3));
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn().getSolidFill() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn().addNewSolidFill();
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn().getSolidFill().getSrgbClr() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSpPr().getLn().getSolidFill().addNewSrgbClr();
   }

   // first series red
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0)
    .getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)255,(byte)0,(byte)0});

   // second series green
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1)
    .getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)0,(byte)255,(byte)0});

   // third series blue
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(2)
    .getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)0,(byte)0,(byte)255});

   // fourth series yellow
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(3)
    .getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)255,(byte)255,(byte)0});


   // set line data series to not smooth the line
   for (int i = 0; i < 4; i++) {
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSmooth() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).addNewSmooth();
    xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getSmooth().setVal(false);
   }

   // set or unset tick marks
   for (int i = 0; i < 4; i++) {
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getMarker() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).addNewMarker();
    if (xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getMarker().getSymbol() == null)
     xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getMarker().addNewSymbol();
   }
   // no tick marks
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getMarker().getSymbol().setVal(
    org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle.NONE);
   // diamond tick marks
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1).getMarker().getSymbol().setVal(
    org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle.DIAMOND);
   // auto tick marks = marker set but no symbol given
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(2).getMarker().unsetSymbol();
   // no tick marks
   xssfChart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(3).getMarker().getSymbol().setVal(
    org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle.NONE);

  }


  wb.write(new FileOutputStream("CreateExcelLineChartAndCustomize.xlsx"));
  wb.close();

 }

}

2
投票

阿帕奇 POI 4

    private void lineSeriesColor(XDDFChartData.Series series, XDDFColor color) {
        XDDFSolidFillProperties fill = new XDDFSolidFillProperties(color);
        XDDFLineProperties line = new XDDFLineProperties();
        line.setFillProperties(fill);
        XDDFShapeProperties properties = series.getShapeProperties();
        if (properties == null) {
            properties = new XDDFShapeProperties();
        } 
        properties.setLineProperties(line);
        series.setShapeProperties(properties);
    }

    private byte[] hex2Rgb(String colorStr) {
        int r = Integer.valueOf(colorStr.substring(1, 3), 16);
        int g = Integer.valueOf(colorStr.substring(3, 5), 16);
        int b = Integer.valueOf(colorStr.substring(5, 7), 16);      
        return new byte[]{(byte) r, (byte) g, (byte) b};
    }

然后

String color = "#0C1111";
lineSeriesColor(series, XDDFColor.from(hex2Rgb(color)));
© www.soinside.com 2019 - 2024. All rights reserved.