diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart09.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart09.java index 0343f046a19af7cf5fc1631ad78c1b3013c6851c..112736a390505ced90df9a6339c5615e60f835db 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart09.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart09.java @@ -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); } diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart10.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart10.java new file mode 100644 index 0000000000000000000000000000000000000000..a6955a2b6eda870f887279d24c52f755444e7695 --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart10.java @@ -0,0 +1,67 @@ +/** + * 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; + } + +} diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator.java index 280962c7bdc998dac7701e9cf51133aee1dca8cc..9416ede52cc35fff71bd1e3053a929edc3cb13e9 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator.java @@ -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) { diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCategoryChartCalculator.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCategoryChartCalculator.java index 5cec5523daf436316754eb934c61fa7265f6d8cd..e9d869bac65aedc77d2010d7a7585c40585cebac 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCategoryChartCalculator.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCategoryChartCalculator.java @@ -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); } diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartInternal.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartInternal.java index 3609cbdb6cbb0a43815933d56130ed974e71187f..b2c53e2959e014326b7a563db32048c8575ebf5b 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartInternal.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartInternal.java @@ -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); diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java index af0a421f01294f7849e507949b61e195f3855c33..5a87a81767f1008d2ecb774493bca869ce89964f 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java @@ -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) {