简体   繁体   English

如何使用 Java 在 Excel 图表中删除类别轴线并添加水平网格线?

[英]How to remove the Category axis lines and add Horizontal grid lines in Excel Chart using Java?

I wanted to remove the lines that indicate a specific value on category axis, with lesser number of values it won't be much of a issue but with large amount of records a black strip is formed around the Horizontal axis.我想删除在类别轴上指示特定值的行,值的数量较少不会有太大问题,但如果记录量很大,则会在水平轴周围形成黑色条带。 I also wanted to add the Horizontal gridlines to the chart.我还想将水平网格线添加到图表中。 I am using Java and Apache POI for the Excel chart.我正在为 Excel 海图使用 Java 和 Apache POI。 I have used 200 values, but I have to plot values sometimes more than thousand in that case it forms a thick black strip.我已经使用了 200 个值,但我必须 plot 个值,有时超过 1000 个值,在那种情况下,它 forms 是一条粗黑条。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.MarkerStyle;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFLineChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class EasyChart {

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

        String path = ".\\ExcelFile";

        FileInputStream inputStream = new FileInputStream(path);

        XSSFWorkbook wb = new XSSFWorkbook(inputStream);

        XSSFSheet sheet = wb.getSheetAt(0);

        XSSFDrawing drawing = sheet.createDrawingPatriarch();
        XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 7, 2, 12 ,18); //

        XSSFChart chart = drawing.createChart(anchor);
        chart.setTitleText("Trend");
        chart.setTitleOverlay(false);

        XDDFChartLegend legend = chart.getOrAddLegend();
        legend.setPosition(LegendPosition.BOTTOM);

        XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
        XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
        
        int range= 200;
                
        XDDFDataSource<String> dates = XDDFDataSourcesFactory.fromStringCellRange(sheet,
                new CellRangeAddress(1, range, 0, 0));

        XDDFNumericalDataSource<Double> readIOP = XDDFDataSourcesFactory.fromNumericCellRange(sheet,new CellRangeAddress(2, range, 2, 2));


        XDDFNumericalDataSource<Double> writeIOP = XDDFDataSourcesFactory.fromNumericCellRange(sheet,new CellRangeAddress(2, range, 4, 4));

        XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);

        XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(dates, readIOP);
        series1.setTitle("SP");
        series1.setSmooth(false);
        series1.setMarkerStyle(MarkerStyle.NONE);

        XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(dates, writeIOP);
        series2.setTitle("FP");
        series2.setSmooth(true);
        series2.setMarkerStyle(MarkerStyle.NONE);

        chart.plot(data);

        save(wb);
    }

    static void save(XSSFWorkbook wb) throws IOException {
        String path = "D:\\Trends";
        FileOutputStream file = new FileOutputStream(new File(path + "\\output.xlsx"));
        wb.write(file);
        System.out.println("File Saved Successfully");
    }

}

The resulting plot I desire结果 plot 我想要

What you want set are properties of the chart axes.您想要设置的是图表轴的属性。

To get rid of the tick marks on category axis, do set major tick marks of XDDFCategoryAxis bottomAxis to org.apache.poi.xddf.usermodel.chart.AxisTickMark.NONE .要摆脱类别轴上的刻度线, XDDFCategoryAxis bottomAxis的主要刻度线设置为org.apache.poi.xddf.usermodel.chart.AxisTickMark.NONE

...
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
// set axis tick marks none
bottomAxis.setMajorTickMark(org.apache.poi.xddf.usermodel.chart.AxisTickMark.NONE);
...

To set the grid lines, do set org.apache.poi.xddf.usermodel.XDDFLineProperties for XDDFValueAxis leftAxis .要设置网格线,请为XDDFValueAxis leftAxis org.apache.poi.xddf.usermodel.XDDFLineProperties

To get rid of the axis line of XDDFValueAxis leftAxis , do set line properties having org.apache.poi.xddf.usermodel.XDDFNoFillProperties .要摆脱XDDFValueAxis leftAxis的轴线,请设置具有org.apache.poi.xddf.usermodel.XDDFNoFillProperties的线属性。

...
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
// set major grid properties
org.apache.poi.xddf.usermodel.XDDFLineProperties lineProperties = new org.apache.poi.xddf.usermodel.XDDFLineProperties();
lineProperties.setWidth(1d);
lineProperties.setFillProperties(new org.apache.poi.xddf.usermodel.XDDFSolidFillProperties(org.apache.poi.xddf.usermodel.XDDFColor.from(org.apache.poi.xddf.usermodel.PresetColor.LIGHT_GRAY)));
leftAxis.getOrAddMajorGridProperties().setLineProperties(lineProperties);
// set axis line to no fill 
lineProperties.setFillProperties(new org.apache.poi.xddf.usermodel.XDDFNoFillProperties());
leftAxis.getOrAddShapeProperties().setLineProperties(lineProperties);
...

Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM