首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何设置Apache POI创建的excel折线图的颜色和系列名称?

如何设置Apache POI创建的excel折线图的颜色和系列名称?
EN

Stack Overflow用户
提问于 2017-06-28 13:45:46
回答 1查看 1.3K关注 0票数 0

我将使用Apache POI中的折线图设置三个系列的颜色和名称。根据下面的示例,每行的名称只有series 1series 2series 3,颜色是自动分配的。如何在图表中修改颜色和名称?

http://thinktibits.blogspot.hk/2014/07/apache-poi-xlsx-line-chart-java-example.html

代码语言:javascript
运行
复制
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.charts.*;
/* Line Chart Example in Apache POI */

public class LineChart{

   public static void main(String[] args) throws Exception {
   /* Create a Workbook object that will hold the final chart */

       XSSFWorkbook my_workbook = new XSSFWorkbook();
       /* Create a worksheet object for the line chart.
       This worksheet will contain the chart */
       XSSFSheet my_worksheet = my_workbook.createSheet("LineChart_Example");

       /* Let us now create some test data for the chart */
       /* Later we can see how to get this test data from a CSV File or SQL Table */
       /* We use a 4 Row chart input with 5 columns each */

       for (int rowIndex = 0; rowIndex < 4; rowIndex++){

            /* Add a row that contains the chart data */
            XSSFRow my_row = my_worksheet.createRow((short)rowIndex);

            for (int colIndex = 0; colIndex < 5; colIndex++){

                 /* Define column values for the row that is created */
                 XSSFCell cell = my_row.createCell((short)colIndex);
                 cell.setCellValue(colIndex * (rowIndex + 1));
            }
        }               
        /* At the end of this step, we have a worksheet with test data, that we want to write into a chart */
        /* Create a drawing canvas on the worksheet */
        XSSFDrawing xlsx_drawing = my_worksheet.createDrawingPatriarch();
        /* Define anchor points in the worksheet to position the chart */
        XSSFClientAnchor anchor = xlsx_drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
        /* Create the chart object based on the anchor point */
        XSSFChart my_line_chart = xlsx_drawing.createChart(anchor);
        /* Define legends for the line chart and set the position of the legend */
        XSSFChartLegend legend = my_line_chart.getOrCreateLegend();
        legend.setPosition(LegendPosition.BOTTOM);     
        /* Create data for the chart */
        LineChartData data = my_line_chart.getChartDataFactory().createLineChartData();     
        /* Define chart AXIS */
        ChartAxis bottomAxis = my_line_chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        ValueAxis leftAxis = my_line_chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);     
        /* Define Data sources for the chart */
        /* Set the right cell range that contain values for the chart */
        /* Pass the worksheet and cell range address as inputs */
        /* Cell Range Address is defined as First row, last row, first column, last column */
        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(my_worksheet, new CellRangeAddress(0, 0, 0, 4));
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(my_worksheet, new CellRangeAddress(1, 1, 0, 4));
        ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(my_worksheet, new CellRangeAddress(2, 2, 0, 4));
        ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(my_worksheet, new CellRangeAddress(3, 3, 0, 4));
        /* Add chart data sources as data to the chart */
        data.addSerie(xs, ys1);
        data.addSerie(xs, ys2);
        data.addSerie(xs, ys3);
        /* Plot the chart with the inputs from data and chart axis */
        my_line_chart.plot(data, new ChartAxis[] { bottomAxis, leftAxis });
        /* Finally define FileOutputStream and write chart information */               
        FileOutputStream fileOut = new FileOutputStream("xlsx-line-chart.xlsx");
        my_workbook.write(fileOut);
        fileOut.close();
    }
}
EN

Stack Overflow用户

发布于 2017-06-28 14:37:05

绘制完图表后,您可以更改线条颜色:

代码语言:javascript
运行
复制
/* Plot the chart with the inputs from data and chart axis */
my_line_chart.plot(data, new ChartAxis[]{bottomAxis, leftAxis});

Color col1 = new Color(233, 87, 162);
CTSolidColorFillProperties fillProp = CTSolidColorFillProperties.Factory.newInstance();
CTSRgbColor rgb = CTSRgbColor.Factory.newInstance();
rgb.setVal(new byte[]{(byte) col1.getRed(), (byte) col1.getGreen(), (byte) col1.getBlue()});
fillProp.setSrgbClr(rgb);

CTLineProperties lineProp = CTLineProperties.Factory.newInstance();
lineProp.setSolidFill(fillProp);
CTShapeProperties ctShapeProperties = CTShapeProperties.Factory.newInstance();
ctShapeProperties.setLn(lineProp);

my_line_chart.getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(0).setSpPr(ctShapeProperties);
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44794475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档