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

more bar chart stuff

parent 2bf01fa5
Branches
No related tags found
No related merge requests found
......@@ -34,6 +34,7 @@ import com.xeiam.xchart.demo.charts.bar.BarChart01;
import com.xeiam.xchart.demo.charts.bar.BarChart02;
import com.xeiam.xchart.demo.charts.bar.BarChart03;
import com.xeiam.xchart.demo.charts.bar.BarChart04;
import com.xeiam.xchart.demo.charts.bar.BarChart05;
import com.xeiam.xchart.demo.charts.date.DateChart01;
import com.xeiam.xchart.demo.charts.date.DateChart02;
import com.xeiam.xchart.demo.charts.date.DateChart03;
......@@ -181,7 +182,10 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart03 - Basic Bar Chart", new BarChart03().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart04 - Basic Bar Chart", new BarChart04().getChart()));
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()));
category.add(chart);
// Area category
......
......@@ -51,11 +51,10 @@ public class BarChart01 implements ExampleChart {
public Chart getChart() {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart01").xAxisTitle("X").yAxisTitle("Y").build();
chart.addSeries("a", new double[] { 0, 1, 2, 3, 4 }, new double[] { 4, 5, 9, 6, 5 });
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Score Histgram").xAxisTitle("Score").yAxisTitle("Number").build();
chart.addSeries("test 1", new double[] { 0, 1, 2, 3, 4 }, new double[] { 4, 5, 9, 6, 5 });
// Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
return chart;
......
......@@ -43,6 +43,7 @@ import com.xeiam.xchart.style.StyleManager.ChartType;
* <li>Date categories
* <li>All negative values
* <li>Single series
* <li>No horizontal plot gridlines
*/
public class BarChart02 implements ExampleChart {
......@@ -75,6 +76,7 @@ public class BarChart02 implements ExampleChart {
yData.add(random.nextInt(i) + 1);
}
chart.addDateSeries("Model 77", xData, yData);
chart.getStyleManager().setPlotGridLinesVisible(false);
return chart;
}
......
......@@ -49,11 +49,8 @@ public class BarChart03 implements ExampleChart {
public Chart getChart() {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart03").xAxisTitle("X").yAxisTitle("Y").build();
chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { 40, -30, -20, -60 });
// Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Score vs. Age").xAxisTitle("Age").yAxisTitle("Score").build();
chart.addSeries("males", new double[] { 10, 20, 30, 40 }, new double[] { 40, -30, -20, -60 });
return chart;
}
......
......@@ -28,13 +28,14 @@ import com.xeiam.xchart.demo.charts.ExampleChart;
import com.xeiam.xchart.style.StyleManager.ChartType;
/**
* Basic Bar Chart
* Missing Point in Series
* <p>
* Demonstrates the following:
* <ul>
* <li>Number categories
* <li>Positive and negative values
* <li>Positive values
* <li>Multiple series
* <li>Missing point in series
*/
public class BarChart04 implements ExampleChart {
......@@ -49,12 +50,9 @@ public class BarChart04 implements ExampleChart {
public Chart getChart() {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart04").xAxisTitle("X").yAxisTitle("Y").build();
chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { 40, 30, 20, 60 });
chart.addSeries("b", new double[] { 10, 20, 30, 40 }, new double[] { 50, 10, 20, 40 });
// Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("XFactor vs. Age").xAxisTitle("Age").yAxisTitle("XFactor").build();
chart.addSeries("male", new double[] { 10, 20, 30, 50 }, new double[] { 40, 30, 20, 60 });
chart.addSeries("female", new double[] { 10, 20, 30, 40, 50 }, new double[] { 50, 10, 20, 40, 35 });
return chart;
}
......
......@@ -26,9 +26,9 @@ import com.xeiam.xchart.ChartBuilder;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.ExampleChart;
import com.xeiam.xchart.style.StyleManager.ChartType;
import com.xeiam.xchart.style.theme.GGPlot2Theme;
/**
* Basic Bar Chart
* <p>
* Demonstrates the following:
* <ul>
......@@ -49,9 +49,9 @@ public class BarChart05 implements ExampleChart {
public Chart getChart() {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart05").xAxisTitle("X").yAxisTitle("Y").build();
chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { -40, 30, 20, 60 });
chart.addSeries("b", new double[] { 10, 20, 30, 40 }, new double[] { 50, 10, -20, 40 });
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Temperature vs. Length").xAxisTitle("Length").yAxisTitle("Temperature").theme(new GGPlot2Theme()).build();
chart.addSeries("fish", new double[] { 10, 20, 30, 40 }, new double[] { -40, 30, 20, 60 });
chart.addSeries("worms", new double[] { 10, 20, 30, 40 }, new double[] { 50, 10, -20, 40 });
// Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
......
......@@ -58,21 +58,14 @@ public class PlotContentBarChart extends PlotContent {
int yTopMargin = AxisPair.getTickStartOffset((int) bounds.getHeight(), yTickSpace);
// get all categories
Set<BigDecimal> categories = new TreeSet<BigDecimal>();
Set<Object> categories = new TreeSet<Object>();
Map<Integer, Series> seriesMap = getChart().getAxisPair().getSeriesMap();
for (Integer seriesId : seriesMap.keySet()) {
Series series = seriesMap.get(seriesId);
Iterator<?> xItr = series.getxData().iterator();
while (xItr.hasNext()) {
BigDecimal x = null;
if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Number) {
x = new BigDecimal(((Number) xItr.next()).doubleValue());
}
if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Date) {
x = new BigDecimal(((Date) xItr.next()).getTime());
}
categories.add(x);
categories.add(xItr.next());
}
}
int numBars = categories.size();
......@@ -97,21 +90,30 @@ public class PlotContentBarChart extends PlotContent {
yMax = BigDecimal.ZERO;
}
Iterator<?> categoryItr = categories.iterator();
Iterator<?> xItr = xData.iterator();
Iterator<Number> yItr = yData.iterator();
int barCounter = 0;
while (xItr.hasNext()) {
BigDecimal x = null;
if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Number) {
x = new BigDecimal(((Number) xItr.next()).doubleValue());
}
if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Date) {
x = new BigDecimal(((Date) xItr.next()).getTime());
}
if (categories.contains(x)) {
while (categoryItr.hasNext()) {
// BigDecimal category = null;
// if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Number) {
// category = new BigDecimal(((Number) categoryItr.next()).doubleValue());
// }
// if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Date) {
// category = new BigDecimal(((Date) categoryItr.next()).getTime());
// }
if (xData.contains(categoryItr.next())) {
BigDecimal x = null;
if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Number) {
x = new BigDecimal(((Number) xItr.next()).doubleValue());
}
if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Date) {
x = new BigDecimal(((Date) xItr.next()).getTime());
}
BigDecimal y = new BigDecimal(yItr.next().doubleValue());
int yTransform = (int) (bounds.getHeight() - (yTopMargin + y.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace));
int yOffset = (int) (bounds.getY() + yTransform);
......@@ -125,6 +127,8 @@ public class PlotContentBarChart extends PlotContent {
int xOffset = (int) (bounds.getX() + xLeftMargin + gridStep * barCounter++ + seriesCounter * barWidth + barMargin);
g.setColor(series.getStrokeColor());
g.fillPolygon(new int[] { xOffset, xOffset + barWidth, xOffset + barWidth, xOffset }, new int[] { yOffset, yOffset, zeroOffset, zeroOffset }, 4);
} else {
barCounter++;
}
}
seriesCounter++;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment