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

add sample bar chart 10, fix broken y aix tick calculator for bar charts

parent 1b131957
No related branches found
No related tags found
No related merge requests found
......@@ -142,7 +142,7 @@ public class BarChart09 implements ExampleChart {
// Add data series to chart
for (int i = 0; i < seriesNames.length; i++) {
Series series = chart.addSeries(seriesNames[i], xAxisKeys, Arrays.asList(dataPerSeries[i]));
Series series = chart.addCategorySeries(seriesNames[i], xAxisKeys, Arrays.asList(dataPerSeries[i]));
series.setMarker(SeriesMarker.NONE);
series.setSeriesType(SeriesType.Line);
}
......
/**
* Copyright 2015 Knowm Inc. (http://knowm.org) and contributors.
* Copyright 2011-2015 Xeiam LLC (http://xeiam.com) and contributors.
*
* 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 org.knowm.xchart.demo.charts.bar;
import java.util.ArrayList;
import java.util.Arrays;
import org.knowm.xchart.Chart;
import org.knowm.xchart.ChartBuilder;
import org.knowm.xchart.Series;
import org.knowm.xchart.Series.SeriesType;
import org.knowm.xchart.StyleManager.ChartTheme;
import org.knowm.xchart.StyleManager.ChartType;
import org.knowm.xchart.StyleManager.LegendPosition;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
/**
* Category chart with Bar and Line Series
* <p>
* Demonstrates the following:
* <ul>
* <li>Mixed series types - Bar and Line
* <li>Bar Chart styles - overlapped, bar width
*/
public class BarChart10 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new BarChart10();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// 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 })));
series2.setSeriesType(SeriesType.Line);
// Customize Chart
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
chart.getStyleManager().setBarWidthPercentage(.7);
chart.getStyleManager().setBarsOverlapped(true);
return chart;
}
}
......@@ -25,6 +25,8 @@ import java.util.LinkedList;
import java.util.List;
import org.knowm.xchart.StyleManager;
import org.knowm.xchart.StyleManager.ChartType;
import org.knowm.xchart.internal.Utils;
import org.knowm.xchart.internal.chartpart.Axis.Direction;
/**
......@@ -59,16 +61,31 @@ public abstract class AxisTickCalculator {
*/
public AxisTickCalculator(Direction axisDirection, double workingSpace, double minValue, double maxValue, StyleManager styleManager) {
// override min and maxValue if specified
// override min/max value for bar charts' Y-Axis
double overrideMinValue = minValue;
double overrideMaxValue = maxValue;
if (axisDirection == Direction.X && styleManager.getXAxisMin() != null) {
if (styleManager.getChartType() == ChartType.Bar && axisDirection == Direction.Y) { // this is the Y-Axis for a bar chart
if (minValue > 0.0 && maxValue > 0.0) {
overrideMinValue = 0.0;
}
if (minValue < 0.0 && maxValue < 0.0) {
overrideMaxValue = 0.0;
}
}
if (styleManager.getChartType() == ChartType.Bar && styleManager.isYAxisLogarithmic()) {
int logMin = (int) Math.floor(Math.log10(minValue));
overrideMinValue = Utils.pow(10, logMin);
}
// override min and maxValue if specified
if (axisDirection == Direction.X && styleManager.getXAxisMin() != null && styleManager.getChartType() != ChartType.Bar) { // bar chart cannot have a max or min
overrideMinValue = styleManager.getXAxisMin();
}
if (axisDirection == Direction.Y && styleManager.getYAxisMin() != null) {
overrideMinValue = styleManager.getYAxisMin();
}
if (axisDirection == Direction.X && styleManager.getXAxisMax() != null) {
if (axisDirection == Direction.X && styleManager.getXAxisMax() != null && styleManager.getChartType() != ChartType.Bar) { // bar chart cannot have a max or min
overrideMaxValue = styleManager.getXAxisMax();
}
if (axisDirection == Direction.Y && styleManager.getYAxisMax() != null) {
......
......@@ -46,26 +46,6 @@ public class AxisTickCategoryChartCalculator extends AxisTickCalculator {
super(axisDirection, workingSpace, minValue, maxValue, chart.getStyleManager());
// override min/max value for bar charts' Y-Axis
double overrideMinValue = minValue;
double overrideMaxValue = maxValue;
if (axisDirection == Direction.Y) { // this is the Y-Axis for a bar chart
if (minValue > 0.0 && maxValue > 0.0) {
overrideMinValue = 0.0;
}
if (minValue < 0.0 && maxValue < 0.0) {
overrideMaxValue = 0.0;
}
}
if (styleManager.isYAxisLogarithmic()) {
int logMin = (int) Math.floor(Math.log10(minValue));
overrideMinValue = Utils.pow(10, logMin);
}
this.minValue = overrideMinValue;
this.maxValue = overrideMaxValue;
calculate(chart);
}
......
......@@ -105,7 +105,7 @@ public class ChartInternal {
// inspect the series to see what kind of data it contains (Number, Date)
AxisType axisType = setXAxisType(xData);
if (!(axisType == AxisType.Number || axisType == AxisType.Date)) {
throw new IllegalArgumentException("X-Axis data must be of type Number or Date!!!");
throw new IllegalArgumentException("X-Axis data must be of type Number or Date for X-Y chart types!!!");
}
axisPair.getYAxis().setAxisType(AxisType.Number);
......
......@@ -85,6 +85,7 @@ public class PlotContentCategoricalChart extends PlotContent {
double yMin = getChartInternal().getAxisPair().getYAxis().getMin();
double yMax = getChartInternal().getAxisPair().getYAxis().getMax();
// if (styleManager.getChartType() == ChartType.Bar) {
// if min and max positive, set min to zero
if (yMin > 0.0 && yMax > 0.0) {
yMin = 0.0;
......@@ -93,6 +94,7 @@ public class PlotContentCategoricalChart extends PlotContent {
if (yMin < 0.0 && yMax < 0.0) {
yMax = 0.0;
}
// }
// override min and maxValue if specified
if (getChartInternal().getStyleManager().getYAxisMin() != null) {
......@@ -111,6 +113,7 @@ public class PlotContentCategoricalChart extends PlotContent {
else if (getChartInternal().getStyleManager().isYAxisLogarithmic()) {
yMax = Math.log10(yMax);
}
// figure out the general form of the chart
int chartForm = 1; // 1=positive, -1=negative, 0=span
if (yMin > 0.0 && yMax > 0.0) {
......
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