Apache POI Line Chart colors

后端 未结 2 537
花落未央
花落未央 2021-01-20 11:42

I am creating an Excel file with some statistics, also including line charts. I had succeed in creating charts and filling them with data. But I am not satisfied with defaul

相关标签:
2条回答
  • 2021-01-20 11:45

    Of note is that with line charts, there are several different places where colour options exist. The colour of the line, the fill colour of the markers, and the outline colour of the marker. All can be set independently of each other. In this example I just set them to the same thing, but if you want them to be different just supply different rgb byte arrays when setting them.

    CTChart ctChart = chart.getCTChart();
    CTPlotArea plotArea = ctChart.getPlotArea();
    CTLineChart lineChart = plotArea.addNewLineChart();
    
    lineChart.addNewVaryColors().setVal(false);
    
    CTLineSer lineSeries = lineChart.addNewSer();
    lineSeries.addNewSmooth().setVal(false);
    
    // some rgb color code. in this example it is a light greenish color
    byte[] color = new byte[] {(byte) 195, (byte) 224, (byte) 176};
    
    CTShapeProperties lineProp = lineSeries.addNewSpPr();
    
    //Set the colour of the line connecting points
    CTSRgbColor fill = lineProp.addNewLn().addNewSolidFill().addNewSrgbClr();
    fill.setVal(color);
    
    CTShapeProperties markerProp = lineSeries.addNewMarker().addNewSpPr();
    
    // Set the fill colour of the marker
    markerProp.addNewLn().addNewSolidFill().setSrgbClr(fill);
    
    //Set the outline colour of the marker
    markerProp.addNewSolidFill().setSrgbClr(fill);
    
    0 讨论(0)
  • It works for me. Groovy, PieChart, random colors.

    def ctChart = chart.getCTChart()
    def ctPieChart = ctChart.getPlotArea().addNewPieChart()
    def ctPieSer = ctPieChart.addNewSer()
    
    byte[] b = new byte[3];
    
    (0 .. rows).each {
      random.nextBytes(b)
      def x = ctPieSer.addNewDPt()
      x.addNewIdx().setVal(it)
      x.addNewBubble3D().setVal(false)
      x.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(b)
    }
    
    0 讨论(0)
提交回复
热议问题