Skip to content
Snippets Groups Projects
Commit 5dd9753a authored by Tim Molter's avatar Tim Molter
Browse files

Fix for issue #111 - Wrong bars placement when category count over 12

parent 670c2e12
No related branches found
No related tags found
No related merge requests found
/**
* 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();
}
}
......@@ -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);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment