diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/bar/BarChart05.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/bar/BarChart05.java index f1369c290328a1907041735747db40abae45ccc1..a13e8c18fbbc193b7744d5ea77aa30e8df60ebc6 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/bar/BarChart05.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/bar/BarChart05.java @@ -48,8 +48,10 @@ public class BarChart05 implements ExampleChart { // Create Chart Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Temperature vs. Color").xAxisTitle("Color").yAxisTitle("Temperature").theme(ChartTheme.GGPlot2).build(); - chart.addSeries("fish", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { -40, 30, 20, 60 }))); - chart.addSeries("worms", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { 50, 10, -20, 40 }))); + chart.addSeries("fish", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { -40, 30, 20, 60, + 60 }))); + chart.addSeries("worms", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { 50, 10, -20, 40, + 60 }))); return chart; } 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 17ecc718bd8b2578f548f4bebfd5255aedb1df53..9d4918b5c6e6650f0cbdeec0dc926f9be00bd6fa 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 @@ -17,7 +17,6 @@ package com.xeiam.xchart.internal.chartpart; import java.math.BigDecimal; import java.util.Date; -import java.util.Iterator; import java.util.List; import com.xeiam.xchart.Series; @@ -55,83 +54,48 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { // where the tick should begin in the working space in pixels double margin = Utils.getTickStartOffset(workingSpace, tickSpace); - List<?> firstXAxis = (List<?>) chartPainter.getAxisPair().getSeriesMap().values().iterator().next().getXData(); + List<?> categories = (List<?>) chartPainter.getAxisPair().getSeriesMap().values().iterator().next().getXData(); // verify all series have exactly the same xAxis if (chartPainter.getAxisPair().getSeriesMap().size() > 1) { for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) { - Iterator<?> firstSeriesItr = firstXAxis.iterator(); - Iterator<?> xItr = series.getXData().iterator(); - while (xItr.hasNext()) { - - // check matching - Object next = xItr.next(); - Object firstSeriesNext = firstSeriesItr.next(); - if (!firstSeriesNext.equals(next)) { - throw new IllegalArgumentException("X-Axis data must exactly match all other Series X-Axis data for Bar Charts!!"); - } + if (!series.getXData().equals(categories)) { + throw new IllegalArgumentException("X-Axis data must exactly match all other Series X-Axis data for Bar Charts!!"); } } } - for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) { - - Iterator<?> xItr = series.getXData().iterator(); - while (xItr.hasNext()) { - - // check matching - Object next = xItr.next(); - - Object x = null; - if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) { - x = next; - } - else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Date) { - x = (double) (((Date) next).getTime()); - } - else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.String) { - x = next; - } - } - } - - if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.String) { - - double gridStep = (tickSpace / (double) firstXAxis.size()); - double firstPosition = gridStep / 2.0; - int counter = 0; - for (Object category : firstXAxis) { - tickLabels.add(category.toString()); - double tickLabelPosition = margin + firstPosition + gridStep * counter++; - tickLocations.add(tickLabelPosition); - } - } - - double gridStep = (tickSpace / (double) firstXAxis.size()); + // generate all tickLabels and tickLocations from the first to last position + double gridStep = (tickSpace / (double) categories.size()); double firstPosition = gridStep / 2.0; - // generate all tickLabels and tickLocations from the first to last position + // set up String formatters that may be encountered 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 : firstXAxis) { - if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) { + for (Object category : categories) { + if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.String) { + tickLabels.add(category.toString()); + double tickLabelPosition = margin + firstPosition + gridStep * counter++; + tickLocations.add(tickLabelPosition); + } + else 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)); + tickLabels.add(dateFormatter.formatDate(((Number) ((Date) category).getTime()).doubleValue(), timeUnit)); } double tickLabelPosition = (int) (margin + firstPosition + gridStep * counter++); tickLocations.add(tickLabelPosition);