From 5dd9753a846ff5cd8de355965c51a86aa58448c3 Mon Sep 17 00:00:00 2001 From: Tim Molter <tim@knowm.org> Date: Wed, 30 Sep 2015 23:59:31 +0200 Subject: [PATCH] Fix for issue #111 - Wrong bars placement when category count over 12 --- .../xchart/standalone/TestForIssue111.java | 41 +++++++++++++ .../chartpart/AxisTickBarChartCalculator.java | 61 ++++++------------- 2 files changed, 61 insertions(+), 41 deletions(-) create mode 100644 xchart-demo/src/main/java/com/xeiam/xchart/standalone/TestForIssue111.java diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/standalone/TestForIssue111.java b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/TestForIssue111.java new file mode 100644 index 00000000..ab3916a9 --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/TestForIssue111.java @@ -0,0 +1,41 @@ +/** + * Copyright 2013 Xeiam LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.xeiam.xchart.standalone; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.ChartBuilder; +import com.xeiam.xchart.StyleManager.ChartType; +import com.xeiam.xchart.SwingWrapper; + +/** + * @author timmolter + */ +public class TestForIssue111 { + + public static void main(String[] args) { + + int[] x = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; + int[] y = new int[] { 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }; + // int[] x = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }; + // int[] y = new int[] { 1, 0, 1, 0, 1, 0, 0, 0 }; + + Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(640).height(480).build(); + chart.addSeries("test", x, y); + chart.getStyleManager().setLegendVisible(false); + new SwingWrapper(chart).displayChart(); + } + +} diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java index 6a44c643..c7d11849 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java @@ -54,7 +54,7 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { int tickSpace = (int) (styleManager.getAxisTickSpacePercentage() * workingSpace); // in plot space // where the tick should begin in the working space in pixels - double margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); + double margin = Utils.getTickStartOffset(workingSpace, tickSpace); // get all categories List<Object> categories = new ArrayList<Object>(); @@ -100,55 +100,34 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { tickLocations.add(tickLabelPosition); } } - else if (categories.size() < 13) { // Number or Date and 12 or less categories. give each category a tick label - double gridStep = (tickSpace / (double) categories.size()); - double firstPosition = gridStep / 2.0; + double gridStep = (tickSpace / (double) categories.size()); + double firstPosition = gridStep / 2.0; - // generate all tickLabels and tickLocations from the first to last position - NumberFormatter numberFormatter = null; - DateFormatter dateFormatter = null; + // generate all tickLabels and tickLocations from the first to last position + NumberFormatter numberFormatter = null; + DateFormatter dateFormatter = null; - if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) { - numberFormatter = new NumberFormatter(styleManager); - } - else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Date) { - dateFormatter = new DateFormatter(chartPainter.getStyleManager()); - } - int counter = 0; - - for (Object category : categories) { - if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) { - tickLabels.add(numberFormatter.formatNumber(new BigDecimal(category.toString()), minValue, maxValue, axisDirection)); - } - else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Date) { - long span = (long) Math.abs(maxValue - minValue); // in data space - long gridStepHint = (long) (span / (double) tickSpace * styleManager.getXAxisTickMarkSpacingHint()); - long timeUnit = dateFormatter.getTimeUnit(gridStepHint); - tickLabels.add(dateFormatter.formatDate(((Number) category).doubleValue(), timeUnit)); - } - double tickLabelPosition = (int) (margin + firstPosition + gridStep * counter++); - tickLocations.add(tickLabelPosition); - } + if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) { + numberFormatter = new NumberFormatter(styleManager); } - else { // Number or Date and more than 12 categories. divide up the axis tick space according to normal date oor number axis layout - - // generate all tickLabels and tickLocations from the first to last position + else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Date) { + dateFormatter = new DateFormatter(chartPainter.getStyleManager()); + } + int counter = 0; + for (Object category : categories) { if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) { - - AxisTickNumericalCalculator axisTickNumericalCalculator = new AxisTickNumericalCalculator(axisDirection, workingSpace, minValue, maxValue, styleManager); - tickLabels = axisTickNumericalCalculator.getTickLabels(); - tickLocations = axisTickNumericalCalculator.getTickLocations(); - + tickLabels.add(numberFormatter.formatNumber(new BigDecimal(category.toString()), minValue, maxValue, axisDirection)); } else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Date) { - - AxisTickDateCalculator axisTickDateCalculator = new AxisTickDateCalculator(axisDirection, workingSpace, minValue, maxValue, styleManager); - tickLabels = axisTickDateCalculator.getTickLabels(); - tickLocations = axisTickDateCalculator.getTickLocations(); + long span = (long) Math.abs(maxValue - minValue); // in data space + long gridStepHint = (long) (span / (double) tickSpace * styleManager.getXAxisTickMarkSpacingHint()); + long timeUnit = dateFormatter.getTimeUnit(gridStepHint); + tickLabels.add(dateFormatter.formatDate(((Number) category).doubleValue(), timeUnit)); } - + double tickLabelPosition = (int) (margin + firstPosition + gridStep * counter++); + tickLocations.add(tickLabelPosition); } } -- GitLab