Repository: knowm/XChart (java)

Issue:
Chart has one label too many when specifying setXAxisMaxLabelCount 
`setXAxisMaxLabelCount` is honored but not precisely. It seems like whatever you specify as the label count, XChart takes this and adds one more.

e.g.
```
    java.util.List<String> xData = Arrays.asList("one", "two", "three", "four", "five", "six");
    java.util.List<Double> yData = Arrays.asList(2.0, 1.0, 0.0, -1.0, 2.0, 4.0);

    CategoryChart chart = new CategoryChart(800, 600);
    chart.addSeries("first", xData, yData);
    chart.getStyler().setXAxisMaxLabelCount(3);

    BitmapEncoder.saveBitmap(chart, "/tmp/actual.png", BitmapEncoder.BitmapFormat.PNG);
```
yields
![actual](https://user-images.githubusercontent.com/762437/106948406-f2d17500-672b-11eb-8726-1a0ec4a68a31.png)


Pull request description:
-1 added to maxAxisLabelCount
Fixes #562.

Patch:
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 4a80f164..1c3e7952 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
@@ -59,9 +59,13 @@ class AxisTickCalculator_Category extends AxisTickCalculator_ {
 
     int xAxisMaxLabelCount = styler.getXAxisMaxLabelCount();
 
+    if (xAxisMaxLabelCount == 1) {
+      throw new IllegalArgumentException("Unsupported max label count equal to 1");
+    }
+
     if (0 < xAxisMaxLabelCount && xAxisMaxLabelCount < categories.size()) {
       List<Object> sparseCategories = new ArrayList<>();
-      double step = categories.size() / (double) xAxisMaxLabelCount;
+      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);


Test patch:
diff --git a/xchart/src/test/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_CategoryTest.java b/xchart/src/test/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_CategoryTest.java
new file mode 100644
index 00000000..d7587e04
--- /dev/null
+++ b/xchart/src/test/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_CategoryTest.java
@@ -0,0 +1,62 @@
+package org.knowm.xchart.internal.chartpart;
+
+import static org.junit.Assert.assertThrows;
+
+import java.util.Arrays;
+import java.util.List;
+import org.junit.Assert;
+import org.junit.Test;
+import org.knowm.xchart.internal.series.Series;
+import org.knowm.xchart.style.CategoryStyler;
+
+public class AxisTickCalculator_CategoryTest {
+
+  @Test
+  public void shouldHonorMaxAxisLabelCount() {
+    // given
+    List<String> categories = Arrays.asList("one", "two", "three", "four", "five", "six");
+    CategoryStyler styler = new CategoryStyler();
+    styler.setXAxisMaxLabelCount(3);
+
+    // when
+    AxisTickCalculator_Category calculator =
+        new AxisTickCalculator_Category(
+            Axis.Direction.X, 900, categories, Series.DataType.String, styler);
+
+    // test
+    Assert.assertEquals(3, calculator.tickLabels.size());
+  }
+
+  @Test
+  public void shouldFailIfMaxAxisLabelCountIsOne() {
+    // given
+    List<String> categories = Arrays.asList("one", "two", "three", "four", "five", "six");
+    CategoryStyler styler = new CategoryStyler();
+    styler.setXAxisMaxLabelCount(1);
+
+    // when & test
+    assertThrows(
+        IllegalArgumentException.class,
+        () -> {
+          new AxisTickCalculator_Category(
+              Axis.Direction.X, 900, categories, Series.DataType.String, styler);
+        });
+  }
+
+  @Test
+  public void shouldAllowAllLabelsIfThereisEnoughSpace() {
+    // given
+    List<String> categories = Arrays.asList("one", "two", "three", "four", "five", "six");
+    CategoryStyler styler = new CategoryStyler();
+
+    // when
+    AxisTickCalculator_Category calculator =
+        new AxisTickCalculator_Category(
+            Axis.Direction.X, 900, categories, Series.DataType.String, styler);
+
+    // test
+    Assert.assertEquals(6, calculator.tickLabels.size());
+    Assert.assertEquals(
+        Arrays.asList(105.0, 243.0, 381.0, 519.0, 657.0, 795.0), calculator.tickLocations);
+  }
+}