From c99204a8aa55de0e5e15441b555d16be032bdf97 Mon Sep 17 00:00:00 2001 From: Greg Oledzki Date: Sat, 6 Nov 2021 21:18:56 +0100 Subject: [PATCH] Revert "Revert "setXAxisMaxLabelCount() not to affect Y Axis in HeatMapCharts"" --- .../AxisTickCalculator_Category.java | 56 +++++++------- .../org/knowm/xchart/HeatMapChartTest.java | 76 +++++++++++++++++++ 2 files changed, 105 insertions(+), 27 deletions(-) create mode 100644 xchart/src/test/java/org/knowm/xchart/HeatMapChartTest.java diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_Category.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_Category.java index 1c3e7952b..92d962736 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_Category.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_Category.java @@ -63,36 +63,38 @@ private void calculate(List categories, Series.DataType axisType) { throw new IllegalArgumentException("Unsupported max label count equal to 1"); } - if (0 < xAxisMaxLabelCount && xAxisMaxLabelCount < categories.size()) { - List sparseCategories = new ArrayList<>(); - double step = categories.size() / (double) (xAxisMaxLabelCount - 1); - for (double stepIdx = 0; Math.round(stepIdx) < categories.size(); stepIdx += step) { - int idx = (int) Math.round(stepIdx); - Object label = categories.get(idx); - sparseCategories.add(label); + if (this.axisDirection == Direction.X) { + if (0 < xAxisMaxLabelCount && xAxisMaxLabelCount < categories.size()) { + List sparseCategories = new ArrayList<>(); + double step = categories.size() / (double) (xAxisMaxLabelCount - 1); + for (double stepIdx = 0; Math.round(stepIdx) < categories.size(); stepIdx += step) { + int idx = (int) Math.round(stepIdx); + Object label = categories.get(idx); + sparseCategories.add(label); + } + + Object lastLabel = categories.get(categories.size() - 1); + sparseCategories.add(lastLabel); + categories = sparseCategories; + + gridStep = (tickSpace / (categories.size() - 1)); + firstPosition = 0; } - Object lastLabel = categories.get(categories.size() - 1); - sparseCategories.add(lastLabel); - categories = sparseCategories; - - gridStep = (tickSpace / (categories.size() - 1)); - firstPosition = 0; - } - - // set up String formatters that may be encountered - if (axisType == Series.DataType.String) { - axisFormat = new Formatter_String(); - } else if (axisType == Series.DataType.Number) { - axisFormat = new Formatter_Number(styler, axisDirection, minValue, maxValue); - } else if (axisType == Series.DataType.Date) { - if (styler.getDatePattern() == null) { - throw new RuntimeException("You need to set the Date Formatting Pattern!!!"); + // set up String formatters that may be encountered + if (axisType == Series.DataType.String) { + axisFormat = new Formatter_String(); + } else if (axisType == Series.DataType.Number) { + axisFormat = new Formatter_Number(styler, axisDirection, minValue, maxValue); + } else if (axisType == Series.DataType.Date) { + if (styler.getDatePattern() == null) { + throw new RuntimeException("You need to set the Date Formatting Pattern!!!"); + } + SimpleDateFormat simpleDateformat = + new SimpleDateFormat(styler.getDatePattern(), styler.getLocale()); + simpleDateformat.setTimeZone(styler.getTimezone()); + axisFormat = simpleDateformat; } - SimpleDateFormat simpleDateformat = - new SimpleDateFormat(styler.getDatePattern(), styler.getLocale()); - simpleDateformat.setTimeZone(styler.getTimezone()); - axisFormat = simpleDateformat; } int counter = 0; diff --git a/xchart/src/test/java/org/knowm/xchart/HeatMapChartTest.java b/xchart/src/test/java/org/knowm/xchart/HeatMapChartTest.java new file mode 100644 index 000000000..ce74391c6 --- /dev/null +++ b/xchart/src/test/java/org/knowm/xchart/HeatMapChartTest.java @@ -0,0 +1,76 @@ +package org.knowm.xchart; + +import java.io.ByteArrayOutputStream; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import org.junit.Assert; +import org.junit.Test; +import org.knowm.xchart.internal.chartpart.Axis; +import org.knowm.xchart.internal.chartpart.AxisPair; +import org.knowm.xchart.internal.chartpart.Chart; +import org.knowm.xchart.style.theme.XChartTheme; + +public class HeatMapChartTest { + + java.util.List xData; + java.util.List yData; + java.util.List data; + + { + int N = 12; + xData = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); + yData = + Arrays.asList( + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December"); + data = new ArrayList<>(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + data.add(new Integer[] {i, j, (i * 2 + j * 3) % 7}); + } + } + } + + @Test + public void shouldApplyMaxLabelCountOnlyToXAxis() throws Exception { + // given + HeatMapChart chart = new HeatMapChart(800, 600); + chart.addSeries("only", xData, yData, data); + chart.getStyler().setTheme(new XChartTheme()); + + // when + chart.getStyler().setXAxisMaxLabelCount(5); + BitmapEncoder.saveBitmap(chart, new ByteArrayOutputStream(), BitmapEncoder.BitmapFormat.PNG); + + // test + Assert.assertEquals(5, getXAxis(chart).getAxisTickCalculator().getTickLocations().size()); + Assert.assertEquals(12, getYAxis(chart).getAxisTickCalculator().getTickLocations().size()); + } + + private AxisPair getAxisPair(Chart chart) throws ReflectiveOperationException { + Field f = Chart.class.getDeclaredField("axisPair"); + f.setAccessible(true); + return (AxisPair) f.get(chart); + } + + public Axis getXAxis(Chart chart) throws ReflectiveOperationException { + return getAxisPair(chart).getXAxis(); + } + + public Axis getYAxis(Chart chart) throws ReflectiveOperationException { + Field f = AxisPair.class.getDeclaredField("yAxis"); + f.setAccessible(true); + return (Axis) f.get(getAxisPair(chart)); + } +}