XChart icon indicating copy to clipboard operation
XChart copied to clipboard

BitmapEncoder saveBitmap no response when XYChart setyAxisTickLabelsFormattingFunction is called before.

Open hegemon33 opened this issue 2 years ago • 2 comments

Now we're using XYChart to draw scatter chart with modified Y-Axis which is the real value of Y-Axis is different from the one shown on label.

For example: real y value = 10, on y-axis label should show 0.01.

// The label should show as y-label
final Map<Double, Double> yLabelMap = getyLabelMap();
chart.setyAxisTickLabelsFormattingFunction(yValue -> {
    final Double yLabel = yLabelMap.get(yValue);
    return yLabel == null ? "" : yLabel.toString();
});

BitmapEncoder.saveBitmap(chart, "output.png", BitmapEncoder.BitmapFormat.PNG);

When running, the program will stuck at the saveBitmap.

java: Java 11 knowm XChart version: 3.8.5

hegemon33 avatar Nov 30 '23 09:11 hegemon33

Ok...found the problem, if the return values of the format function get duplicate value, the saveBitmap method will never stop. Maybe need to handle this case? Even throw an Exception will be nice.

hegemon33 avatar Nov 30 '23 09:11 hegemon33

Good bug report, thanks.

While surely an infinite loop is not a proper behavior of handling input, just making a remark that ensuring the labels are unique seem to be intended functionality: https://github.com/knowm/XChart/blob/ba1fa850cb2e255579c1e0ee36019f60055aa7a7/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_.java#L410 The code seems to assume this is the only desired situation.

So I guess the intended behavior here is to actually throw error.

mccartney avatar Feb 13 '24 20:02 mccartney