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. I have used 200 values, but I have to plot values sometimes more than thousand in that case it forms a thick black strip.
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");
}
}
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 = 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
.
To get rid of the axis line of XDDFValueAxis leftAxis
, do set line properties having 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);
...