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

fixed major bar chart rendering error

parent 7b715061
No related branches found
Tags
No related merge requests found
......@@ -183,16 +183,16 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart01 - Basic Bar Chart", new BarChart01().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart02 - Basic Bar Chart", new BarChart02().getChart()));
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart02 - Date Categories", new BarChart02().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart03 - Basic Bar Chart", new BarChart03().getChart()));
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart03 - Positive and Negative", new BarChart03().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart04 - Missing Point in Series", new BarChart04().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart05 - Basic Bar Chart", new BarChart05().getChart()));
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart05 - GGPlot2 Theme", new BarChart05().getChart()));
category.add(chart);
// Theme category
......
......@@ -31,12 +31,14 @@ import java.util.Random;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.ChartBuilder;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.Series;
import com.xeiam.xchart.SeriesColor;
import com.xeiam.xchart.StyleManager.ChartType;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.ExampleChart;
/**
* Basic Bar Chart
* Date Categories
* <p>
* Demonstrates the following:
* <ul>
......@@ -44,6 +46,7 @@ import com.xeiam.xchart.demo.charts.ExampleChart;
* <li>All negative values
* <li>Single series
* <li>No horizontal plot gridlines
* <li>Change series color
*/
public class BarChart02 implements ExampleChart {
......@@ -73,9 +76,10 @@ public class BarChart02 implements ExampleChart {
e.printStackTrace();
}
xData.add(date);
yData.add(random.nextInt(i) + 1);
yData.add(-1 * ((random.nextInt(i) + 1)));
}
chart.addDateSeries("Model 77", xData, yData);
Series series = chart.addDateSeries("Model 77", xData, yData);
series.setLineColor(SeriesColor.RED);
chart.getStyleManager().setPlotGridLinesVisible(false);
return chart;
......
......@@ -23,12 +23,12 @@ package com.xeiam.xchart.demo.charts.bar;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.ChartBuilder;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.StyleManager.ChartType;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.ExampleChart;
/**
* Basic Bar Chart
* Positive and Negative
* <p>
* Demonstrates the following:
* <ul>
......
......@@ -32,7 +32,7 @@ import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.ExampleChart;
/**
* Multiple series Bar Chart
* GGPlot2 Theme
* <p>
* Demonstrates the following:
* <ul>
......
......@@ -80,11 +80,16 @@ public class PlotContentBarChart extends PlotContent {
Collection<Number> yData = series.getyData();
BigDecimal yMin = getChartPainter().getAxisPair().getyAxis().getMin();
BigDecimal yMax = getChartPainter().getAxisPair().getyAxis().getMax();
// if min and max positive, set min to zero
if (yMin.compareTo(BigDecimal.ZERO) > 0 && yMax.compareTo(BigDecimal.ZERO) > 0) {
yMin = BigDecimal.ZERO;
} else if (yMin.compareTo(BigDecimal.ZERO) < 0 && yMax.compareTo(BigDecimal.ZERO) < 0) {
}
// if min and max negative, set max to zero
if (yMin.compareTo(BigDecimal.ZERO) < 0 && yMax.compareTo(BigDecimal.ZERO) < 0) {
yMax = BigDecimal.ZERO;
}
// override min and maxValue if specified
if (getChartPainter().getStyleManager().getYAxisMin() != null) {
yMin = new BigDecimal(getChartPainter().getStyleManager().getYAxisMin());
......@@ -92,6 +97,23 @@ public class PlotContentBarChart extends PlotContent {
if (getChartPainter().getStyleManager().getYAxisMax() != null) {
yMax = new BigDecimal(getChartPainter().getStyleManager().getYAxisMax());
}
// figure out the general form of the chart
int chartForm = 1; // 1=positive, -1=negative, 0=span
if (yMin.compareTo(BigDecimal.ZERO) > 0 && yMax.compareTo(BigDecimal.ZERO) > 0) {
// positive chart
chartForm = 1;
System.out.println("positive chart");
} else if (yMin.compareTo(BigDecimal.ZERO) < 0 && yMax.compareTo(BigDecimal.ZERO) < 0) {
// negative chart
chartForm = -1;
System.out.println("negative chart");
} else {
// span chart
chartForm = 0;
System.out.println("span chart");
}
Iterator<?> categoryItr = categories.iterator();
Iterator<Number> yItr = yData.iterator();
......@@ -101,19 +123,42 @@ public class PlotContentBarChart extends PlotContent {
if (xData.contains(categoryItr.next())) {
BigDecimal y = new BigDecimal(yItr.next().doubleValue());
BigDecimal yTop = new BigDecimal(y.doubleValue());
if (yTop.compareTo(yMax) > 0) {
BigDecimal yTop = null;
BigDecimal yBottom = null;
switch (chartForm) {
case 1: // positive chart
yTop = new BigDecimal(y.doubleValue());
yBottom = yMin;
break;
case -1: // negative chart
yTop = yMax;
yBottom = new BigDecimal(y.doubleValue());
break;
case 0: // span chart
if (y.compareTo(BigDecimal.ZERO) >= 0) { // positive
yTop = y;
yBottom = BigDecimal.ZERO;
} else {
yTop = BigDecimal.ZERO;
yBottom = y;
}
break;
default:
break;
}
// if (yTop.compareTo(yMax) > 0) {
// yTop = yMax;
// }
int yTransform = (int) (bounds.getHeight() - (yTopMargin + yTop.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace));
int yOffset = (int) (bounds.getY() + yTransform);
int yOffset = (int) (bounds.getY() + yTransform) + 1;
BigDecimal yBottom = new BigDecimal(y.doubleValue());
if (yBottom.compareTo(yMin) > 0) {
yBottom = yMin;
}
// if (yBottom.compareTo(yMin) > 0) {
// yBottom = yMin;
// }
int zeroTransform = (int) (bounds.getHeight() - (yTopMargin + (yBottom.subtract(yMin).doubleValue()) / (yMax.subtract(yMin).doubleValue()) * yTickSpace));
int zeroOffset = (int) (bounds.getY() + zeroTransform);
int zeroOffset = (int) (bounds.getY() + zeroTransform) + 1;
// paint bar
int barWidth = (int) (gridStep / seriesMap.size() / 1.1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment