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

clean up bar chart code

parent d4bd17e1
No related branches found
No related tags found
No related merge requests found
......@@ -34,7 +34,7 @@ import org.knowm.xchart.demo.charts.ExampleChart;
* <p>
* Demonstrates the following:
* <ul>
* <li>Mixed series types - Bar and Line
* <li>Mixed series types - Bar, Line and Scatter
* <li>Bar Chart styles - overlapped, bar width
*/
public class BarChart10 implements ExampleChart {
......@@ -52,9 +52,12 @@ public class BarChart10 implements ExampleChart {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Value vs. Letter").xAxisTitle("Letter").yAxisTitle("Value").theme(ChartTheme.GGPlot2).build();
chart.addCategorySeries("China", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 11, 23, 20, 36, 5 })));
Series series2 = chart.addCategorySeries("World Ave.", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 13, 25,
22, 38, 7 })));
Series series2 = chart.addCategorySeries("Korea", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 13, 25, 22, 38,
7 })), new ArrayList<Number>(Arrays.asList(new Number[] { 1, 3, 2, 1, 2 })));
series2.setSeriesType(SeriesType.Line);
Series series3 = chart.addCategorySeries("World Ave.", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 20, 22,
18, 36, 32 })));
series3.setSeriesType(SeriesType.Scatter);
// Customize Chart
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
......
......@@ -930,7 +930,6 @@ public class StyleManager {
this.isPlotGridVerticalLinesVisible = isPlotGridLinesVisible;
}
@Deprecated
public boolean isPlotGridLinesVisible() {
return isPlotGridHorizontalLinesVisible && isPlotGridVerticalLinesVisible;
......
......@@ -20,6 +20,7 @@ import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import org.knowm.xchart.StyleManager.ChartType;
import org.knowm.xchart.internal.chartpart.ChartInternal.ChartInternalType;
/**
* @author timmolter
......@@ -75,8 +76,14 @@ public class Plot implements ChartPart {
// g.draw(bounds);
plotSurface.paint(g);
if (getChartInternal().getStyleManager().getChartType() == ChartType.Bar) {
this.plotContent = new PlotContentCategoricalChart(this);
if (getChartInternal().getChartInternalType() == ChartInternalType.Category) {
if (getChartInternal().getStyleManager().getChartType() == ChartType.Bar) {
this.plotContent = new PlotContentCategoricalChart_Bar(this);
}
else {
this.plotContent = new PlotContentCategoricalChart_Line_Area_Scatter(this);
}
}
else {
this.plotContent = new PlotContentNumericalChart(this);
......
......@@ -32,14 +32,14 @@ import org.knowm.xchart.internal.Utils;
/**
* @author timmolter
*/
public class PlotContentCategoricalChart extends PlotContent {
public class PlotContentCategoricalChart_Bar extends PlotContent {
/**
* Constructor
*
* @param plot
*/
protected PlotContentCategoricalChart(Plot plot) {
protected PlotContentCategoricalChart_Bar(Plot plot) {
super(plot);
}
......@@ -98,6 +98,11 @@ public class PlotContentCategoricalChart extends PlotContent {
int seriesCounter = 0;
for (Series series : getChartInternal().getSeriesMap().values()) {
// sanity check
if (Series.SeriesType.Area.equals(series.getSeriesType())) {
throw new RuntimeException("Category-Bar charts only accept Bar, Line and Scatter series types!!!");
}
// for line series
double previousX = -Double.MAX_VALUE;
double previousY = -Double.MAX_VALUE;
......@@ -114,7 +119,6 @@ public class PlotContentCategoricalChart extends PlotContent {
double y = yItr.next().doubleValue();
// TODO only for bar charts necessary
double yTop = 0.0;
double yBottom = 0.0;
switch (chartForm) {
......@@ -172,6 +176,8 @@ public class PlotContentCategoricalChart extends PlotContent {
double barMargin = gridStep * (1 - barWidthPercentage) / 2;
xOffset = bounds.getX() + xLeftMargin + gridStep * categoryCounter++ + seriesCounter * barWidth + barMargin;
}
// paint series
if (series.getSeriesType() == SeriesType.Bar) {
// paint bar
g.setColor(series.getStrokeColor());
......@@ -189,16 +195,19 @@ public class PlotContentCategoricalChart extends PlotContent {
g.draw(path);
}
}
else if (series.getSeriesType() == SeriesType.Line) { // line series
else {
// paint line
if (series.getStroke() != null) {
if (Series.SeriesType.Line.equals(series.getSeriesType())) {
if (previousX != -Double.MAX_VALUE && previousY != -Double.MAX_VALUE) {
g.setColor(series.getStrokeColor());
g.setStroke(series.getStroke());
Shape line = new Line2D.Double(previousX, previousY, xOffset + barWidth / 2, yOffset);
g.draw(line);
if (series.getStroke() != null) {
if (previousX != -Double.MAX_VALUE && previousY != -Double.MAX_VALUE) {
g.setColor(series.getStrokeColor());
g.setStroke(series.getStroke());
Shape line = new Line2D.Double(previousX, previousY, xOffset + barWidth / 2, yOffset);
g.draw(line);
}
}
}
previousX = xOffset + barWidth / 2;
......@@ -211,10 +220,6 @@ public class PlotContentCategoricalChart extends PlotContent {
}
}
else {
// TODO probably add this earlier as a sanity check when series are added to charts
throw new RuntimeException("Category charts only accept Bar and Line series types!!!");
}
// paint error bars
......
......@@ -98,6 +98,11 @@ public class PlotContentNumericalChart extends PlotContent {
for (Series series : getChartInternal().getSeriesMap().values()) {
// sanity check
if (Series.SeriesType.Bar.equals(series.getSeriesType())) {
throw new RuntimeException("X-Y charts only accept Line, Scatter, and Area series types!!!");
}
// data points
Collection<?> xData = series.getXData();
Collection<? extends Number> yData = series.getYData();
......
......@@ -22,6 +22,9 @@ import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import org.knowm.xchart.StyleManager.ChartType;
import org.knowm.xchart.internal.chartpart.ChartInternal.ChartInternalType;
/**
* Draws the plot background, the plot border and the horizontal and vertical grid lines
*
......@@ -100,34 +103,38 @@ public class PlotSurface implements ChartPart {
}
// vertical
if (getChartInternal().getStyleManager().isPlotGridVerticalLinesVisible() || getChartInternal().getStyleManager().isPlotTicksMarksVisible()) {
if ((getChartInternal().getChartInternalType() == ChartInternalType.XY || (getChartInternal().getChartInternalType() == ChartInternalType.Category && getChartInternal().getStyleManager()
.getChartType() != ChartType.Bar))) {
List<Double> xAxisTickLocations = getChartInternal().getAxisPair().getXAxis().getAxisTickCalculator().getTickLocations();
for (int i = 0; i < xAxisTickLocations.size(); i++) {
if ((getChartInternal().getStyleManager().isPlotGridVerticalLinesVisible() || getChartInternal().getStyleManager().isPlotTicksMarksVisible())) {
double tickLocation = xAxisTickLocations.get(i);
double xOffset = bounds.getX() + tickLocation;
List<Double> xAxisTickLocations = getChartInternal().getAxisPair().getXAxis().getAxisTickCalculator().getTickLocations();
for (int i = 0; i < xAxisTickLocations.size(); i++) {
if (xOffset > bounds.getX() && xOffset < bounds.getX() + bounds.getWidth()) {
double tickLocation = xAxisTickLocations.get(i);
double xOffset = bounds.getX() + tickLocation;
// draw lines
if (getChartInternal().getStyleManager().isPlotGridVerticalLinesVisible()) {
g.setColor(getChartInternal().getStyleManager().getPlotGridLinesColor());
g.setStroke(getChartInternal().getStyleManager().getPlotGridLinesStroke());
if (xOffset > bounds.getX() && xOffset < bounds.getX() + bounds.getWidth()) {
Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + bounds.getHeight());
g.draw(line);
}
// tick marks
if (getChartInternal().getStyleManager().isPlotTicksMarksVisible()) {
// draw lines
if (getChartInternal().getStyleManager().isPlotGridVerticalLinesVisible()) {
g.setColor(getChartInternal().getStyleManager().getPlotGridLinesColor());
g.setStroke(getChartInternal().getStyleManager().getPlotGridLinesStroke());
g.setColor(getChartInternal().getStyleManager().getAxisTickMarksColor());
g.setStroke(getChartInternal().getStyleManager().getAxisTickMarksStroke());
Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + bounds.getHeight());
g.draw(line);
}
// tick marks
if (getChartInternal().getStyleManager().isPlotTicksMarksVisible()) {
Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + getChartInternal().getStyleManager().getAxisTickMarkLength());
g.draw(line);
line = new Line2D.Double(xOffset, bounds.getY() + bounds.getHeight(), xOffset, bounds.getY() + bounds.getHeight() - getChartInternal().getStyleManager().getAxisTickMarkLength());
g.draw(line);
g.setColor(getChartInternal().getStyleManager().getAxisTickMarksColor());
g.setStroke(getChartInternal().getStyleManager().getAxisTickMarksStroke());
Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + getChartInternal().getStyleManager().getAxisTickMarkLength());
g.draw(line);
line = new Line2D.Double(xOffset, bounds.getY() + bounds.getHeight(), xOffset, bounds.getY() + bounds.getHeight() - getChartInternal().getStyleManager().getAxisTickMarkLength());
g.draw(line);
}
}
}
}
......
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