Skip to content

Commit

Permalink
fix for issue #826. Reverted behavior added for issue #653. The decis…
Browse files Browse the repository at this point in the history
…ion was made to anchor min y axis to 0 for bar and stick category charts, but not for other category chart types.
  • Loading branch information
timmolter committed May 10, 2024
1 parent 7c20368 commit 8f92a7a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void main(String[] args) throws Exception {

styler.setAxisTickLabelsColor(Color.darkGray);
styler.setDatePattern("MM-dd");
styler.setYAxisMin(1.0);
styler.setYAxisMin(2.0);
styler.setYAxisMax(10.0);
styler.setXAxisTickMarkSpacingHint(200);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static CategoryChart getCategoryChart() {
builder.title("Sample Chart").xAxisTitle("X").yAxisTitle("Y").theme(Styler.ChartTheme.Matlab);
CategoryChart chart = builder.build();
chart.getStyler().setDefaultSeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Line);
// .setYAxisMin(0.0);
// .setYAxisMin(0.0);

chart.addSeries("y(x)", xData, yData);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.knowm.xchart.standalone.issues;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.SwingWrapper;

public class TestForIssue826 {

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

CategoryChart chart = getChart();
new SwingWrapper(chart).displayChart();
}

public static CategoryChart getChart() {
final CategoryChart chart =
new CategoryChartBuilder().width(600).height(400).xAxisTitle("X").yAxisTitle("Y").build();

ArrayList<String> years =
new ArrayList<String>(
Arrays.asList(
new String[] {
"2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021",
"2022"
}));

ArrayList<Number> yAData =
new ArrayList<Number>(
Arrays.asList(
new Number[] {
4438887, 4365843, 4050498, 4757380, 4429130, 4692889, 4354087, 4530343, 4572770,
4150489, 4487793
}));

ArrayList<Number> yBData =
new ArrayList<Number>(
Arrays.asList(
new Number[] {
3198714, 3144079, 2859215, 3430605, 3839149, 4042579, 3741823, 3890162, 3731367,
3751216, 4008249
}));

chart.getStyler().setOverlapped(true);
chart.getStyler().setYAxisDecimalPattern("###,###.##");

chart.addSeries("A", years, yAData);
chart.addSeries("B", years, yBData);

// chart.getStyler().setYAxisMin(2600000.0);
chart.setTitle(Double.toString(2600000.0));

return chart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,21 @@ private void overrideMinMaxForYAxis(Axis yAxis) {
}

// override min and maxValue if specified
if (chart.getStyler().getYAxisMin(yAxis.getYIndex()) != null
&& !(chart.getStyler() instanceof BoxStyler)) {
overrideYAxisMinValue = chart.getStyler().getYAxisMin(yAxis.getYIndex());
} else if (chart.getStyler().getYAxisMin() != null
&& !(chart.getStyler() instanceof BoxStyler)) {
overrideYAxisMinValue = chart.getStyler().getYAxisMin();
}

if (chart.getStyler().getYAxisMax(yAxis.getYIndex()) != null
&& !(chart.getStyler() instanceof BoxStyler)) {
overrideYAxisMaxValue = chart.getStyler().getYAxisMax(yAxis.getYIndex());
} else if (chart.getStyler().getYAxisMax() != null
&& !(chart.getStyler() instanceof BoxStyler)) {
overrideYAxisMaxValue = chart.getStyler().getYAxisMax();
if (!(chart.getStyler() instanceof BoxStyler)) {

// min
if (chart.getStyler().getYAxisMin(yAxis.getYIndex()) != null) {
overrideYAxisMinValue = chart.getStyler().getYAxisMin(yAxis.getYIndex());
} else if (chart.getStyler().getYAxisMin() != null) {
overrideYAxisMinValue = chart.getStyler().getYAxisMin();
}
// max
if (chart.getStyler().getYAxisMax(yAxis.getYIndex()) != null) {
overrideYAxisMaxValue = chart.getStyler().getYAxisMax(yAxis.getYIndex());
} else if (chart.getStyler().getYAxisMax() != null) {
overrideYAxisMaxValue = chart.getStyler().getYAxisMax();
}
}

yAxis.setMin(overrideYAxisMinValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public abstract class AxisTickCalculator_ implements AxisTickCalculator {

this.axisDirection = axisDirection;
this.workingSpace = workingSpace;
this.minValue = getAxisMinValue(styler, axisDirection, minValue);
this.maxValue = getAxisMaxValue(styler, axisDirection, maxValue);
this.minValue = minValue;
this.maxValue = maxValue;
this.styler = styler;
}

Expand All @@ -79,8 +79,8 @@ public abstract class AxisTickCalculator_ implements AxisTickCalculator {
axisValuesWithMinMax.addAll(axisValues);
axisValuesWithMinMax.add(maxValue);
this.axisValues = new ArrayList<>(axisValuesWithMinMax);
this.minValue = getAxisMinValue(styler, axisDirection, minValue);
this.maxValue = getAxisMaxValue(styler, axisDirection, maxValue);
this.minValue = minValue;
this.maxValue = maxValue;
this.styler = styler;
}

Expand Down Expand Up @@ -192,7 +192,7 @@ protected void calculate() {
// same value i.e. "#0.00" for 0.0, 0.0001, 0.0002
// issue #582
if (isNumberFormatChoppingDecimals(maxValue, minValue)) {
System.out.println("returning");
// System.out.println("returning");
return;
}

Expand Down Expand Up @@ -421,48 +421,6 @@ boolean areAllTickLabelsUnique(List<?> tickLabels) {
return new LinkedHashSet<>(tickLabels).size() == tickLabels.size();
}

/**
* Determines the axis min value, which may differ from the min value of the respective data (e.g.
* for bar charts).
*
* @param styler the chart {@link Styler}
* @param axisDirection the axis {@link Direction}
* @param dataMinValue the minimum value of the data corresponding with the axis.
* @return the axis min value
*/
private static double getAxisMinValue(
Styler styler, Direction axisDirection, double dataMinValue) {

// special case for category charts
if (styler instanceof CategoryStyler) {
CategoryStyler categoryStyler = (CategoryStyler) styler;
if ((categoryStyler.getDefaultSeriesRenderStyle()
== CategorySeries.CategorySeriesRenderStyle.Bar
|| categoryStyler.getDefaultSeriesRenderStyle()
== CategorySeries.CategorySeriesRenderStyle.Stick)
&& dataMinValue > 0) {
return 0;
}
}
return dataMinValue;
}

/**
* Determines the axis max value, which may differ from the max value of the respective data (e.g.
* for bar charts).
*
* @param styler the chart {@link Styler}
* @param axisDirection the axis {@link Direction}
* @param dataMaxValue the maximum value of the data corresponding with the axis.
* @return the axis max value
*/
private static double getAxisMaxValue(
Styler styler, Direction axisDirection, double dataMaxValue) {
if (Direction.Y.equals(axisDirection) && styler instanceof CategoryStyler && dataMaxValue < 0)
return 0;
return dataMaxValue;
}

private boolean isNumberFormatChoppingDecimals(double axisMax, double axisMin) {

// System.out.println("axisMax = " + axisMax);
Expand Down

0 comments on commit 8f92a7a

Please sign in to comment.