From 73b86475cdb604b32c29cad25e08ffbbe5dfaf8a Mon Sep 17 00:00:00 2001 From: Anthony Quiros <quiros.antho@gmail.com> Date: Thu, 3 Dec 2015 09:25:36 +0100 Subject: [PATCH] Remove vertical or horizontal lines in PlotSurface adding a option to disable or unable displaying of vertical or horizontal lines in PlotSurface. --- .../lines/Example0WithoutHorizontalLines.java | 71 +++++++++++++++++++ ...ple0WithoutVerticalAndHorizontalLines.java | 70 ++++++++++++++++++ .../lines/Example0WithoutVerticalLines.java | 67 +++++++++++++++++ .../java/org/knowm/xchart/StyleManager.java | 42 +++++++++-- .../internal/chartpart/PlotSurface.java | 11 +-- .../xchart/internal/style/GGPlot2Theme.java | 14 ++++ .../xchart/internal/style/MatlabTheme.java | 12 ++++ .../knowm/xchart/internal/style/Theme.java | 4 ++ .../xchart/internal/style/XChartTheme.java | 12 ++++ .../org/knowm/xchart/StyleManagerTest.java | 67 +++++++++++++++++ 10 files changed, 362 insertions(+), 8 deletions(-) create mode 100644 xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutHorizontalLines.java create mode 100644 xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutVerticalAndHorizontalLines.java create mode 100644 xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutVerticalLines.java create mode 100644 xchart/src/test/java/org/knowm/xchart/StyleManagerTest.java diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutHorizontalLines.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutHorizontalLines.java new file mode 100644 index 00000000..e88229ef --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutHorizontalLines.java @@ -0,0 +1,71 @@ +/** + * 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.plot.lines; + +import java.util.ArrayList; +import java.util.List; + +import org.knowm.xchart.BitmapEncoder; +import org.knowm.xchart.BitmapEncoder.BitmapFormat; +import org.knowm.xchart.Chart; +import org.knowm.xchart.ChartBuilder; +import org.knowm.xchart.Series; +import org.knowm.xchart.SeriesMarker; +import org.knowm.xchart.SwingWrapper; +import org.knowm.xchart.VectorGraphicsEncoder; +import org.knowm.xchart.VectorGraphicsEncoder.VectorGraphicsFormat; + +/** + * Creates a simple Chart and saves it as a PNG and JPEG image file. + */ +public class Example0WithoutHorizontalLines { + + public static void main(String[] args) { + + int numCharts = 4; + + List<Chart> charts = new ArrayList<Chart>(); + + for (int i = 0; i < numCharts; i++) { + Chart chart = new ChartBuilder().xAxisTitle("X").yAxisTitle("Y").width(600).height(400).build(); + chart.getStyleManager().setYAxisMin(-10); + chart.getStyleManager().setYAxisMax(10); + chart.getStyleManager().setPlotGridHorizontalLinesVisible(false); + Series series = chart.addSeries("" + i, null, getRandomWalk(200)); + series.setMarker(SeriesMarker.NONE); + charts.add(chart); + } + new SwingWrapper(charts).displayChartMatrix(); + } + + /** + * Generates a set of random walk data + * + * @param numPoints + * @return + */ + private static double[] getRandomWalk(int numPoints) { + + double[] y = new double[numPoints]; + y[0] = 0; + for (int i = 1; i < y.length; i++) { + y[i] = y[i - 1] + Math.random() - .5; + } + return y; + } + +} diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutVerticalAndHorizontalLines.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutVerticalAndHorizontalLines.java new file mode 100644 index 00000000..c2342e85 --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutVerticalAndHorizontalLines.java @@ -0,0 +1,70 @@ +/** + * 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.plot.lines; + +import java.util.ArrayList; +import java.util.List; + +import org.knowm.xchart.Chart; +import org.knowm.xchart.ChartBuilder; +import org.knowm.xchart.Series; +import org.knowm.xchart.SeriesMarker; +import org.knowm.xchart.SwingWrapper; + +/** + * Create a Chart matrix + * + * @author timmolter + */ +public class Example0WithoutVerticalAndHorizontalLines { + + public static void main(String[] args) { + + int numCharts = 4; + + List<Chart> charts = new ArrayList<Chart>(); + + for (int i = 0; i < numCharts; i++) { + Chart chart = new ChartBuilder().xAxisTitle("X").yAxisTitle("Y").width(600).height(400).build(); + chart.getStyleManager().setYAxisMin(-10); + chart.getStyleManager().setYAxisMax(10); + chart.getStyleManager().setPlotGridVerticalLinesVisible(false); + chart.getStyleManager().setPlotGridHorizontalLinesVisible(false); + Series series = chart.addSeries("" + i, null, getRandomWalk(200)); + series.setMarker(SeriesMarker.NONE); + charts.add(chart); + } + new SwingWrapper(charts).displayChartMatrix(); + } + + /** + * Generates a set of random walk data + * + * @param numPoints + * @return + */ + private static double[] getRandomWalk(int numPoints) { + + double[] y = new double[numPoints]; + y[0] = 0; + for (int i = 1; i < y.length; i++) { + y[i] = y[i - 1] + Math.random() - .5; + } + return y; + } + +} diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutVerticalLines.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutVerticalLines.java new file mode 100644 index 00000000..645cb6da --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/plot/lines/Example0WithoutVerticalLines.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.plot.lines; + +import java.util.ArrayList; +import java.util.List; + +import org.knowm.xchart.Chart; +import org.knowm.xchart.ChartBuilder; +import org.knowm.xchart.Series; +import org.knowm.xchart.SeriesMarker; +import org.knowm.xchart.SwingWrapper; + +/** + * Creates a simple Chart using QuickChart + */ +public class Example0WithoutVerticalLines { + + public static void main(String[] args) { + + int numCharts = 4; + + List<Chart> charts = new ArrayList<Chart>(); + + for (int i = 0; i < numCharts; i++) { + Chart chart = new ChartBuilder().xAxisTitle("X").yAxisTitle("Y").width(600).height(400).build(); + chart.getStyleManager().setYAxisMin(-10); + chart.getStyleManager().setYAxisMax(10); + chart.getStyleManager().setPlotGridVerticalLinesVisible(false); + Series series = chart.addSeries("" + i, null, getRandomWalk(200)); + series.setMarker(SeriesMarker.NONE); + charts.add(chart); + } + new SwingWrapper(charts).displayChartMatrix(); + } + + /** + * Generates a set of random walk data + * + * @param numPoints + * @return + */ + private static double[] getRandomWalk(int numPoints) { + + double[] y = new double[numPoints]; + y[0] = 0; + for (int i = 1; i < y.length; i++) { + y[i] = y[i - 1] + Math.random() - .5; + } + return y; + } + +} diff --git a/xchart/src/main/java/org/knowm/xchart/StyleManager.java b/xchart/src/main/java/org/knowm/xchart/StyleManager.java index ad084568..a2f4f3ef 100644 --- a/xchart/src/main/java/org/knowm/xchart/StyleManager.java +++ b/xchart/src/main/java/org/knowm/xchart/StyleManager.java @@ -127,7 +127,8 @@ public class StyleManager { private int xAxisLabelRotation = 0; // Chart Plot Area /////////////////////////////// - private boolean isPlotGridLinesVisible; + private boolean isPlotGridHorizontalLinesVisible; + private boolean isPlotGridVerticalLinesVisible; private Color plotBackgroundColor; private Color plotBorderColor; private boolean isPlotBorderVisible; @@ -215,7 +216,8 @@ public class StyleManager { axisTickSpacePercentage = .95; // Chart Plot Area /////////////////////////////// - isPlotGridLinesVisible = theme.isPlotGridLinesVisible(); + isPlotGridVerticalLinesVisible = theme.isPlotGridVerticalLinesVisible(); + isPlotGridHorizontalLinesVisible = theme.isPlotGridHorizontalLinesVisible(); plotBackgroundColor = theme.getPlotBackgroundColor(); plotBorderColor = theme.getPlotBorderColor(); isPlotBorderVisible = theme.isPlotBorderVisible(); @@ -927,12 +929,44 @@ public class StyleManager { */ public void setPlotGridLinesVisible(boolean isPlotGridLinesVisible) { - this.isPlotGridLinesVisible = isPlotGridLinesVisible; + this.isPlotGridHorizontalLinesVisible = isPlotGridLinesVisible; + this.isPlotGridVerticalLinesVisible = isPlotGridLinesVisible; } + @Deprecated public boolean isPlotGridLinesVisible() { - return isPlotGridLinesVisible; + return isPlotGridHorizontalLinesVisible && isPlotGridVerticalLinesVisible; + } + + /** + * sets the visibility of the horizontal gridlines on the plot area + * + * @param isPlotGridLinesVisible + */ + public void setPlotGridHorizontalLinesVisible(boolean isPlotGridHorizontalLinesVisible) { + + this.isPlotGridHorizontalLinesVisible = isPlotGridHorizontalLinesVisible; + } + + public boolean isPlotGridHorizontalLinesVisible() { + + return isPlotGridHorizontalLinesVisible; + } + + /** + * sets the visibility of the vertical gridlines on the plot area + * + * @param isPlotGridLinesVisible + */ + public void setPlotGridVerticalLinesVisible(boolean isPlotGridVerticalLinesVisible) { + + this.isPlotGridVerticalLinesVisible = isPlotGridVerticalLinesVisible; + } + + public boolean isPlotGridVerticalLinesVisible() { + + return isPlotGridVerticalLinesVisible; } /** diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotSurface.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotSurface.java index 3c04fcd1..b21d3226 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotSurface.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotSurface.java @@ -68,7 +68,10 @@ public class PlotSurface implements ChartPart { } // paint grid lines and/or inner plot ticks - if (getChartPainter().getStyleManager().isPlotGridLinesVisible() || getChartPainter().getStyleManager().isPlotTicksMarksVisible()) { + if (getChartPainter().getStyleManager().isPlotGridHorizontalLinesVisible() + || getChartPainter().getStyleManager().isPlotGridVerticalLinesVisible() + || getChartPainter().getStyleManager().isPlotTicksMarksVisible() + ) { // horizontal List<Double> yAxisTickLocations = getChartPainter().getAxisPair().getYAxis().getAxisTickCalculator().getTickLocations(); @@ -79,7 +82,7 @@ public class PlotSurface implements ChartPart { if (yOffset > bounds.getY() && yOffset < bounds.getY() + bounds.getHeight()) { // draw lines - if (getChartPainter().getStyleManager().isPlotGridLinesVisible()) { + if (getChartPainter().getStyleManager().isPlotGridHorizontalLinesVisible()) { g.setColor(getChartPainter().getStyleManager().getPlotGridLinesColor()); g.setStroke(getChartPainter().getStyleManager().getPlotGridLinesStroke()); @@ -103,7 +106,7 @@ public class PlotSurface implements ChartPart { // vertical if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar - && (getChartPainter().getStyleManager().isPlotGridLinesVisible() + && (getChartPainter().getStyleManager().isPlotGridVerticalLinesVisible() || getChartPainter().getStyleManager().isPlotTicksMarksVisible()) @@ -118,7 +121,7 @@ public class PlotSurface implements ChartPart { if (xOffset > bounds.getX() && xOffset < bounds.getX() + bounds.getWidth()) { // draw lines - if (getChartPainter().getStyleManager().isPlotGridLinesVisible()) { + if (getChartPainter().getStyleManager().isPlotGridVerticalLinesVisible()) { g.setColor(getChartPainter().getStyleManager().getPlotGridLinesColor()); g.setStroke(getChartPainter().getStyleManager().getPlotGridLinesStroke()); diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/GGPlot2Theme.java b/xchart/src/main/java/org/knowm/xchart/internal/style/GGPlot2Theme.java index e0461c20..24a6337b 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/GGPlot2Theme.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/GGPlot2Theme.java @@ -244,6 +244,20 @@ public class GGPlot2Theme implements Theme { return true; } + + @Override + public boolean isPlotGridVerticalLinesVisible() { + + return true; + } + + + @Override + public boolean isPlotGridHorizontalLinesVisible() { + + return true; + } + @Override public Color getPlotBackgroundColor() { diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/MatlabTheme.java b/xchart/src/main/java/org/knowm/xchart/internal/style/MatlabTheme.java index cdb2776a..f0b3f780 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/MatlabTheme.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/MatlabTheme.java @@ -244,6 +244,18 @@ public class MatlabTheme implements Theme { return true; } + @Override + public boolean isPlotGridVerticalLinesVisible() { + + return true; + } + + @Override + public boolean isPlotGridHorizontalLinesVisible() { + + return true; + } + @Override public Color getPlotBackgroundColor() { diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme.java b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme.java index c2a4132a..c593bf92 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme.java @@ -104,6 +104,10 @@ public interface Theme { // Chart Plot Area /////////////////////////////// public boolean isPlotGridLinesVisible(); + + public boolean isPlotGridVerticalLinesVisible(); + + public boolean isPlotGridHorizontalLinesVisible(); public Color getPlotBackgroundColor(); diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/XChartTheme.java b/xchart/src/main/java/org/knowm/xchart/internal/style/XChartTheme.java index 56ba4b93..fb69b061 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/XChartTheme.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/XChartTheme.java @@ -244,6 +244,18 @@ public class XChartTheme implements Theme { return true; } + @Override + public boolean isPlotGridVerticalLinesVisible() { + + return true; + } + + @Override + public boolean isPlotGridHorizontalLinesVisible() { + + return true; + } + @Override public Color getPlotBackgroundColor() { diff --git a/xchart/src/test/java/org/knowm/xchart/StyleManagerTest.java b/xchart/src/test/java/org/knowm/xchart/StyleManagerTest.java new file mode 100644 index 00000000..bcaaf767 --- /dev/null +++ b/xchart/src/test/java/org/knowm/xchart/StyleManagerTest.java @@ -0,0 +1,67 @@ +package org.knowm.xchart; + +import org.junit.Before; +import org.junit.Test; + +import static org.fest.assertions.api.Assertions.assertThat; + +public class StyleManagerTest { + + private StyleManager styleManager; + + @Before + public void setUp() { + styleManager = new StyleManager(); + } + + @SuppressWarnings("deprecation") + @Test + public void setPlotGridLinesVisibleTestWithFalse(){ + styleManager.setPlotGridLinesVisible(false); + assertThat(styleManager.isPlotGridLinesVisible()).isFalse(); + assertThat(styleManager.isPlotGridHorizontalLinesVisible()).isFalse(); + assertThat(styleManager.isPlotGridVerticalLinesVisible()).isFalse(); + } + + @SuppressWarnings("deprecation") + @Test + public void setPlotGridLinesVisibleTestWithTrue(){ + styleManager.setPlotGridHorizontalLinesVisible(true); + assertThat(styleManager.isPlotGridLinesVisible()).isTrue(); + assertThat(styleManager.isPlotGridHorizontalLinesVisible()).isTrue(); + assertThat(styleManager.isPlotGridVerticalLinesVisible()).isTrue(); + } + + @SuppressWarnings("deprecation") + @Test + public void setPlotGridHorizontalLinesVisibleTestWithFalse(){ + styleManager.setPlotGridHorizontalLinesVisible(false); + assertThat(styleManager.isPlotGridHorizontalLinesVisible()).isFalse(); + assertThat(styleManager.isPlotGridLinesVisible()).isFalse(); + } + + @SuppressWarnings("deprecation") + @Test + public void setPlotGridHorizontalLinesVisibleTestWithTrue(){ + styleManager.setPlotGridLinesVisible(true); + assertThat(styleManager.isPlotGridHorizontalLinesVisible()).isTrue(); + assertThat(styleManager.isPlotGridLinesVisible()).isTrue(); + } + + @SuppressWarnings("deprecation") + @Test + public void setPlotGridVerticalLinesVisibleTestWithFalse(){ + styleManager.setPlotGridVerticalLinesVisible(false); + assertThat(styleManager.isPlotGridVerticalLinesVisible()).isFalse(); + assertThat(styleManager.isPlotGridLinesVisible()).isFalse(); + } + + @SuppressWarnings("deprecation") + @Test + public void setPlotGridVerticalLinesVisibleTestWithTrue(){ + styleManager.setPlotGridVerticalLinesVisible(true); + assertThat(styleManager.isPlotGridVerticalLinesVisible()).isTrue(); + assertThat(styleManager.isPlotGridLinesVisible()).isTrue(); + } + +} -- GitLab