Skip to content

Commit

Permalink
fix for issue #582
Browse files Browse the repository at this point in the history
  • Loading branch information
timmolter committed May 10, 2024
1 parent ad55dc1 commit c9d01c6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.knowm.xchart.standalone.issues;

import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;

import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;

public class TestForIssue582 {

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

XYChart chart = getXYChart();
new SwingWrapper(chart).displayChart();
}

public static XYChart getXYChart() {
XYChart chart = new XYChartBuilder()
.width(720)
.height(480)
.title("Deadlock Example")
.xAxisTitle("Count")
.yAxisTitle("Value")
.build();

// If the decimal places in the pattern is fewer than the largest data value, the deadlock occurs.
// chart.getStyler().setDecimalPattern("#0.0000");
// chart.getStyler().setDecimalPattern("#,###.00");

// chart.getStyler().setDecimalPattern("$ #0.00");
// chart.getStyler().setDecimalPattern("$ #0.000");

chart.getStyler().setLocale(Locale.ITALIAN);
chart.getStyler().setDecimalPattern("#0.000");

double[] xValues = new double[]{1, 2, 3, 4, 5, 6, 7, 8};

// The only value here is 0.001 which is one decimal place below the pattern.
double[] yValues = new double[] { 0.0, 0.0, 0.0, 0.0, 0.001, 0.0, 0.0, 0.0};
// double[] yValues = new double[] { 0.0, 0.0, 0.0, 0.0, -0.001, 0.0, 0.0, 0.0};
// double[] yValues = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
// double[] yValues = new double[] { 0.0001, 0.0, 0.0, 0.0, -0.001, 0.0, 0.0, 0.0};
// double[] yValues = new double[] { -0.0002, -0.0002, -0.0002, -0.0002, -0.001, -0.0002, -0.0002, -0.0002};
// double[] yValues = new double[]{0.0002, 0.0002, 0.0002, 0.0002, 0.0001, 0.0002, 0.0002, 0.0002};
// double[] yValues = new double[] { 20.0002, 20.0002, 20.0002, 20.0002, 20.001, 20.0002, 20.0002, 20.0002};

chart.addSeries("main", xValues, yValues);


return chart;
}
}
2 changes: 1 addition & 1 deletion xchart/src/main/java/org/knowm/xchart/XYChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public XYChart(int width, int height) {
*
* @param width
* @param height
* @param theme - pass in a instance of Theme class, probably a custom Theme.
* @param theme - pass in an instance of Theme class, probably a custom Theme.
*/
public XYChart(int width, int height, Theme theme) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.Format;
import java.text.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -161,6 +161,8 @@ public Format getAxisFormat() {

protected void calculate() {

// System.out.println("calculate");

// a check if all axis data are the exact same values
if (minValue == maxValue) {
tickLabels.add(getAxisFormat().format(BigDecimal.valueOf(maxValue).doubleValue()));
Expand All @@ -186,7 +188,14 @@ protected void calculate() {
return;
}

// where the tick should begin in the working space in pixels
// this prevents an infinite loop when the axis number formatter "rounds" all axis ticks to the same value i.e. "#0.00" for 0.0, 0.0001, 0.0002
// issue #582
if(isNumberFormatChoppingDecimals(maxValue, minValue)){
System.out.println("returning");
return;
}

// where the tick should begin in the working space in pixels
double margin =
Utils.getTickStartOffset(
workingSpace,
Expand Down Expand Up @@ -216,7 +225,7 @@ protected void calculate() {

do {

// System.out.println("calculating ticks...");
// System.out.println("calculating ticks...");
tickLabels.clear();
tickLocations.clear();

Expand Down Expand Up @@ -342,6 +351,7 @@ protected void calculate() {
}
} while (!areAllTickLabelsUnique(tickLabels)
|| !willLabelsFitInTickSpaceHint(tickLabels, gridStepInChartSpace));

}

private boolean areValuesEquallySpaced(List<Double> values) {
Expand Down Expand Up @@ -452,4 +462,19 @@ private static double getAxisMaxValue(
return 0;
return dataMaxValue;
}

private boolean isNumberFormatChoppingDecimals(double axisMax, double axisMin){

// System.out.println("axisMax = " + axisMax);
// System.out.println("axisMin = " + axisMin);
String formattedMaxValue = getAxisFormat().format(axisMax);
// System.out.println("formattedMaxValue = " + formattedMaxValue);
String formattedMinValue = getAxisFormat().format(axisMin);
// System.out.println("formattedMinValue = " + formattedMinValue);
// if formatted number lost its decimals due to formatter
if(formattedMaxValue.equals(formattedMinValue)){
return true;
}
return false;
}
}

0 comments on commit c9d01c6

Please sign in to comment.