XChart icon indicating copy to clipboard operation
XChart copied to clipboard

NPE on tooltips with BitmapEncoder.saveBitmap

Open jc7447 opened this issue 8 months ago • 0 comments

When upgrading from 3.6.6 to 3.8.8 , BitmapEncoder.saveBitmap throws NPE when tooltips are used.

Caused by: java.lang.NullPointerException: null
        at org.knowm.xchart.internal.chartpart.PlotContent_XY.doPaint(PlotContent_XY.java:304) ~[xchart-3.8.8.jar:na]
        at org.knowm.xchart.internal.chartpart.PlotContent_.paint(PlotContent_.java:61) ~[xchart-3.8.8.jar:na]
        at org.knowm.xchart.internal.chartpart.Plot_.paint(Plot_.java:37) ~[xchart-3.8.8.jar:na]
        at org.knowm.xchart.internal.chartpart.Plot_AxesChart.paint(Plot_AxesChart.java:34) ~[xchart-3.8.8.jar:na]
        at org.knowm.xchart.XYChart.paint(XYChart.java:417) ~[xchart-3.8.8.jar:na]
        at org.knowm.xchart.BitmapEncoder.getBufferedImage(BitmapEncoder.java:277) ~[xchart-3.8.8.jar:na]
        at org.knowm.xchart.BitmapEncoder.saveBitmap(BitmapEncoder.java:81) ~[xchart-3.8.8.jar:na]
        at org.knowm.xchart.BitmapEncoder.saveBitmap(BitmapEncoder.java:65) ~[xchart-3.8.8.jar:na]
public class LineChart01 {

	private static final String CHART_PREFIX = "chart-";

	private static final String PNG_SUFFIX = ".png";

	private static final String CHARTS = "charts-";

	public void getChart() throws IOException {

		// generates Log data
		List<Integer> xData = new ArrayList<Integer>();
		List<Double> yData = new ArrayList<Double>();
		for (int i = -3; i <= 3; i++) {
			xData.add(i);
			yData.add(Math.pow(10, i));
		}

		// Create Chart
		XYChart chart = new XYChartBuilder().width(800).height(600).title("Powers of Ten").xAxisTitle("Power").yAxisTitle("Value").build();

		// Customize Chart
		chart.getStyler().setChartTitleVisible(true);
		chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
		chart.getStyler().setYAxisLogarithmic(true);
		chart.getStyler().setXAxisLabelRotation(45);

		chart.getStyler().setToolTipsEnabled(true);
		chart.getStyler().setToolTipsAlwaysVisible(true);
		chart.getStyler().setToolTipType(Styler.ToolTipType.yLabels);
		chart.getStyler().setToolTipBorderColor(Color.RED);

		// Series
		chart.addSeries("10^x", xData, yData);

		Path tempFolder = Files.createTempDirectory(CHARTS);

		Path chartFile = Files.createTempFile(tempFolder, CHART_PREFIX, PNG_SUFFIX);

		System.out.println(chartFile);

		BitmapEncoder.saveBitmap(chart, chartFile.toString(), BitmapEncoder.BitmapFormat.PNG);
	}
}

This was working fine in 3.6.6 :

chart-13192250370041411106

jc7447 avatar Jun 18 '24 19:06 jc7447