Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setXAxisMaxLabelCount() not to affect Y Axis in HeatMapCharts #641

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> 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<Object> 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;
Expand Down
76 changes: 76 additions & 0 deletions xchart/src/test/java/org/knowm/xchart/HeatMapChartTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer> xData;
java.util.List<String> yData;
java.util.List<Number[]> 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));
}
}