From 0ce95ecfa845c4f0facc75be46588db4a6e914cd Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Mon, 4 Mar 2013 17:45:38 +0100 Subject: [PATCH] moved chart painting code out of Chart and into ChartPainter to hide internal stuff from users of lib --- .../java/com/xeiam/xchart/BitmapEncoder.java | 8 +- .../src/main/java/com/xeiam/xchart/Chart.java | 130 +++----------- .../xeiam/xchart/internal/chartpart/Axis.java | 43 +++-- .../xchart/internal/chartpart/AxisPair.java | 13 +- .../xchart/internal/chartpart/AxisTick.java | 25 ++- .../chartpart/AxisTickBarChartCalculator.java | 5 +- .../internal/chartpart/AxisTickLabels.java | 14 +- .../internal/chartpart/AxisTickMarks.java | 32 ++-- .../xchart/internal/chartpart/AxisTitle.java | 28 ++- .../internal/chartpart/ChartPainter.java | 163 ++++++++++++++++++ .../xchart/internal/chartpart/ChartPart.java | 4 +- .../xchart/internal/chartpart/ChartTitle.java | 52 +++--- .../xchart/internal/chartpart/Legend.java | 85 +++++---- .../xeiam/xchart/internal/chartpart/Plot.java | 25 ++- .../internal/chartpart/PlotContent.java | 6 +- .../chartpart/PlotContentBarChart.java | 19 +- .../chartpart/PlotContentLineChart.java | 44 ++--- .../internal/chartpart/PlotSurface.java | 25 ++- 18 files changed, 389 insertions(+), 332 deletions(-) create mode 100644 xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPainter.java diff --git a/xchart/src/main/java/com/xeiam/xchart/BitmapEncoder.java b/xchart/src/main/java/com/xeiam/xchart/BitmapEncoder.java index eb8b2973..9d148eff 100644 --- a/xchart/src/main/java/com/xeiam/xchart/BitmapEncoder.java +++ b/xchart/src/main/java/com/xeiam/xchart/BitmapEncoder.java @@ -54,8 +54,8 @@ public final class BitmapEncoder { public static void savePNG(Chart chart, String fileName) throws IOException { BufferedImage bufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB); - Graphics2D lGraphics2D = bufferedImage.createGraphics(); - chart.paint(lGraphics2D); + Graphics2D graphics2D = bufferedImage.createGraphics(); + chart.paint(graphics2D); // Save chart as PNG OutputStream out = new FileOutputStream(fileName); @@ -75,8 +75,8 @@ public final class BitmapEncoder { public static void saveJPG(Chart chart, String fileName, float quality) throws FileNotFoundException, IOException { BufferedImage bufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB); - Graphics2D lGraphics2D = bufferedImage.createGraphics(); - chart.paint(lGraphics2D); + Graphics2D graphics2D = bufferedImage.createGraphics(); + chart.paint(graphics2D); Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = iter.next(); diff --git a/xchart/src/main/java/com/xeiam/xchart/Chart.java b/xchart/src/main/java/com/xeiam/xchart/Chart.java index 8edcd3f3..a2884272 100644 --- a/xchart/src/main/java/com/xeiam/xchart/Chart.java +++ b/xchart/src/main/java/com/xeiam/xchart/Chart.java @@ -16,16 +16,11 @@ package com.xeiam.xchart; import java.awt.Graphics2D; -import java.awt.RenderingHints; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import java.util.Date; -import com.xeiam.xchart.internal.chartpart.AxisPair; -import com.xeiam.xchart.internal.chartpart.ChartTitle; -import com.xeiam.xchart.internal.chartpart.Legend; -import com.xeiam.xchart.internal.chartpart.Plot; +import com.xeiam.xchart.internal.chartpart.ChartPainter; import com.xeiam.xchart.internal.style.Theme; /** @@ -35,16 +30,7 @@ import com.xeiam.xchart.internal.style.Theme; */ public class Chart { - private int width; - private int height; - - private final StyleManager styleManager; - - // Chart Parts - private Legend chartLegend; - private AxisPair axisPair; - private Plot plot; - private ChartTitle chartTitle; + private final ChartPainter chartPainter; /** * Constructor @@ -54,14 +40,7 @@ public class Chart { */ public Chart(int width, int height) { - styleManager = new StyleManager(); - chartLegend = new Legend(this); - axisPair = new AxisPair(this); - plot = new Plot(this); - chartTitle = new ChartTitle(this); - this.width = width; - this.height = height; - + chartPainter = new ChartPainter(width, height); } /** @@ -86,42 +65,19 @@ public class Chart { */ public void paint(Graphics2D g, int width, int height) { - this.width = width; - this.height = height; - - paint(g); + chartPainter.paint(g, width, height); } /** * @param g + * @param width + * @param height */ public void paint(Graphics2D g) { - // Sanity checks - if (axisPair.getSeriesMap().isEmpty()) { - throw new RuntimeException("No series defined for Chart!!!"); - } - if (getStyleManager().isXAxisLogarithmic() && axisPair.getxAxis().getMin().compareTo(BigDecimal.ZERO) <= 0) { - throw new IllegalArgumentException("Series data cannot be less or equal to zero for a logarithmic X-Axis!!!"); - } - if (getStyleManager().isYAxisLogarithmic() && axisPair.getyAxis().getMin().compareTo(BigDecimal.ZERO) <= 0) { - throw new IllegalArgumentException("Series data cannot be less or equal to zero for a logarithmic Y-Axis!!!"); - } - - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // global rendering hint - g.setColor(styleManager.getChartBackgroundColor()); - g.fillRect(0, 0, width, height); - - axisPair.paint(g); - plot.paint(g); - chartTitle.paint(g); - chartLegend.paint(g); - - g.dispose(); + chartPainter.paint(g); } - // PUBLIC SETTERS - /** * Add a Category series to the chart * @@ -132,7 +88,7 @@ public class Chart { */ public Series addCategorySeries(String seriesName, Collection<String> xData, Collection<Number> yData) { - return axisPair.addSeries(seriesName, xData, yData, null); + return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, null); } /** @@ -145,7 +101,7 @@ public class Chart { */ public Series addDateSeries(String seriesName, Collection<Date> xData, Collection<Number> yData) { - return axisPair.addSeries(seriesName, xData, yData, null); + return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, null); } /** @@ -159,7 +115,7 @@ public class Chart { */ public Series addDateSeries(String seriesName, Collection<Date> xData, Collection<Number> yData, Collection<Number> errorBars) { - return axisPair.addSeries(seriesName, xData, yData, errorBars); + return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, errorBars); } /** @@ -172,7 +128,7 @@ public class Chart { */ public Series addSeries(String seriesName, Collection<Number> xData, Collection<Number> yData) { - return axisPair.addSeries(seriesName, xData, yData, null); + return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, null); } /** @@ -186,7 +142,7 @@ public class Chart { */ public Series addSeries(String seriesName, Collection<Number> xData, Collection<Number> yData, Collection<Number> errorBars) { - return axisPair.addSeries(seriesName, xData, yData, errorBars); + return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, errorBars); } /** @@ -232,7 +188,7 @@ public class Chart { } } - return axisPair.addSeries(seriesName, xDataNumber, yDataNumber, errorBarDataNumber); + return chartPainter.getAxisPair().addSeries(seriesName, xDataNumber, yDataNumber, errorBarDataNumber); } /** @@ -242,7 +198,7 @@ public class Chart { */ public void setChartTitle(String title) { - this.chartTitle.setText(title); + chartPainter.getChartTitle().setText(title); } /** @@ -252,7 +208,7 @@ public class Chart { */ public void setXAxisTitle(String title) { - this.axisPair.getxAxis().getAxisTitle().setText(title); + chartPainter.getAxisPair().getxAxis().getAxisTitle().setText(title); } /** @@ -263,11 +219,11 @@ public class Chart { public void setYAxisTitle(String title) { if (title == null || title.trim().equalsIgnoreCase("")) { - styleManager.setyAxisTitleVisible(false); + chartPainter.getStyleManager().setyAxisTitleVisible(false); } else { - styleManager.setyAxisTitleVisible(true); + chartPainter.getStyleManager().setyAxisTitleVisible(true); } - this.axisPair.getyAxis().getAxisTitle().setText(title); + chartPainter.getAxisPair().getyAxis().getAxisTitle().setText(title); } /** @@ -277,7 +233,7 @@ public class Chart { */ public StyleManager getStyleManager() { - return styleManager; + return chartPainter.getStyleManager(); } /** @@ -287,59 +243,17 @@ public class Chart { */ public void setTheme(Theme theme) { - styleManager.setTheme(theme); - } - - // / Internal ///////////////////////////////////////// - - /** - * for internal usage - * - * @return - */ - public ChartTitle getChartTitle() { - - return chartTitle; - } - - /** - * for internal usage - * - * @return - */ - public Legend getChartLegend() { - - return chartLegend; - } - - /** - * for internal usage - * - * @return - */ - public AxisPair getAxisPair() { - - return axisPair; - } - - /** - * for internal usage - * - * @return - */ - public Plot getPlot() { - - return plot; + chartPainter.getStyleManager().setTheme(theme); } public int getWidth() { - return width; + return chartPainter.getWidth(); } public int getHeight() { - return height; + return chartPainter.getHeight(); } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java index 7375ad5c..af8608de 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java @@ -21,7 +21,6 @@ import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.math.BigDecimal; -import com.xeiam.xchart.Chart; import com.xeiam.xchart.StyleManager.LegendPosition; /** @@ -81,8 +80,8 @@ public class Axis implements ChartPart { this.axisPair = axisPair; this.direction = direction; - axisTitle = new AxisTitle(this, direction == Direction.X ? getChart().getStyleManager().isxAxisTitleVisible() : getChart().getStyleManager().isyAxisTitleVisible()); - axisTick = new AxisTick(this, direction == Direction.X ? getChart().getStyleManager().isxAxisTicksVisible() : getChart().getStyleManager().isyAxisTicksVisible()); + axisTitle = new AxisTitle(this, direction == Direction.X ? getChartPainter().getStyleManager().isxAxisTitleVisible() : getChartPainter().getStyleManager().isyAxisTitleVisible()); + axisTick = new AxisTick(this, direction == Direction.X ? getChartPainter().getStyleManager().isxAxisTicksVisible() : getChartPainter().getStyleManager().isyAxisTicksVisible()); } /** @@ -127,19 +126,19 @@ public class Axis implements ChartPart { // Axis title double titleHeight = 0.0; - if (getChart().getStyleManager().isxAxisTitleVisible()) { - TextLayout textLayout = new TextLayout(axisTitle.getText(), getChart().getStyleManager().getAxisTitleFont(), new FontRenderContext(null, true, false)); + if (getChartPainter().getStyleManager().isxAxisTitleVisible()) { + TextLayout textLayout = new TextLayout(axisTitle.getText(), getChartPainter().getStyleManager().getAxisTitleFont(), new FontRenderContext(null, true, false)); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - titleHeight = rectangle.getHeight() + getChart().getStyleManager().getAxisTitlePadding(); + titleHeight = rectangle.getHeight() + getChartPainter().getStyleManager().getAxisTitlePadding(); } // Axis tick labels double axisTickLabelsHeight = 0.0; - if (getChart().getStyleManager().isxAxisTicksVisible()) { - TextLayout textLayout = new TextLayout("0", getChart().getStyleManager().getAxisTickLabelsFont(), new FontRenderContext(null, true, false)); + if (getChartPainter().getStyleManager().isxAxisTicksVisible()) { + TextLayout textLayout = new TextLayout("0", getChartPainter().getStyleManager().getAxisTickLabelsFont(), new FontRenderContext(null, true, false)); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - axisTickLabelsHeight = rectangle.getHeight() + getChart().getStyleManager().getAxisTickPadding() + getChart().getStyleManager().getAxisTickMarkLength() - + getChart().getStyleManager().getPlotPadding(); + axisTickLabelsHeight = rectangle.getHeight() + getChartPainter().getStyleManager().getAxisTickPadding() + getChartPainter().getStyleManager().getAxisTickMarkLength() + + getChartPainter().getStyleManager().getPlotPadding(); } return (int) (titleHeight + axisTickLabelsHeight); } else { // Y-Axis @@ -163,10 +162,10 @@ public class Axis implements ChartPart { // | // | // ---- - int xOffset = getChart().getStyleManager().getChartPadding(); - int yOffset = getChart().getChartTitle().getSizeHint(); + int xOffset = getChartPainter().getStyleManager().getChartPadding(); + int yOffset = getChartPainter().getChartTitle().getSizeHint(); int width = 80; // arbitrary, final width depends on Axis tick labels - int height = getChart().getHeight() - yOffset - axisPair.getxAxis().getSizeHint() - getChart().getStyleManager().getChartPadding(); + int height = getChartPainter().getHeight() - yOffset - axisPair.getxAxis().getSizeHint() - getChartPainter().getStyleManager().getChartPadding(); Rectangle yAxisRectangle = new Rectangle(xOffset, yOffset, width, height); this.paintZone = yAxisRectangle; // g.setColor(Color.green); @@ -178,7 +177,7 @@ public class Axis implements ChartPart { xOffset = (int) paintZone.getX(); yOffset = (int) paintZone.getY(); - width = (int) (getChart().getStyleManager().isyAxisTitleVisible() ? axisTitle.getBounds().getWidth() : 0) + (int) axisTick.getBounds().getWidth(); + width = (int) (getChartPainter().getStyleManager().isyAxisTitleVisible() ? axisTitle.getBounds().getWidth() : 0) + (int) axisTick.getBounds().getWidth(); height = (int) paintZone.getHeight(); bounds = new Rectangle(xOffset, yOffset, width, height); // g.setColor(Color.yellow); @@ -189,17 +188,17 @@ public class Axis implements ChartPart { // calculate paint zone // |____________________| - int xOffset = (int) (axisPair.getyAxis().getBounds().getWidth() + (getChart().getStyleManager().isyAxisTicksVisible() ? getChart().getStyleManager().getPlotPadding() : 0) + getChart() + int xOffset = (int) (axisPair.getyAxis().getBounds().getWidth() + (getChartPainter().getStyleManager().isyAxisTicksVisible() ? getChartPainter().getStyleManager().getPlotPadding() : 0) + getChartPainter() .getStyleManager().getChartPadding()); int yOffset = (int) (axisPair.getyAxis().getBounds().getY() + axisPair.getyAxis().getBounds().getHeight()); int chartLegendWidth = 0; - if (getChart().getStyleManager().getLegendPosition() == LegendPosition.OutsideW) { - chartLegendWidth = getChart().getChartLegend().getSizeHint()[0]; + if (getChartPainter().getStyleManager().getLegendPosition() == LegendPosition.OutsideW) { + chartLegendWidth = getChartPainter().getChartLegend().getSizeHint()[0]; } - int width = (int) (getChart().getWidth() - axisPair.getyAxis().getBounds().getWidth() - chartLegendWidth - (getChart().getStyleManager().isLegendVisible() ? 3 : 2) - * getChart().getStyleManager().getChartPadding() - getChart().getStyleManager().getPlotPadding()); + int width = (int) (getChartPainter().getWidth() - axisPair.getyAxis().getBounds().getWidth() - chartLegendWidth - (getChartPainter().getStyleManager().isLegendVisible() ? 3 : 2) + * getChartPainter().getStyleManager().getChartPadding() - getChartPainter().getStyleManager().getPlotPadding()); int height = this.getSizeHint(); Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height); this.paintZone = xAxisRectangle; @@ -212,7 +211,7 @@ public class Axis implements ChartPart { xOffset = (int) paintZone.getX(); yOffset = (int) paintZone.getY(); width = (int) paintZone.getWidth(); - height = (int) ((getChart().getStyleManager().isxAxisTitleVisible() ? axisTitle.getBounds().getHeight() : 0) + (int) axisTick.getBounds().getHeight()); + height = (int) ((getChartPainter().getStyleManager().isxAxisTitleVisible() ? axisTitle.getBounds().getHeight() : 0) + (int) axisTick.getBounds().getHeight()); bounds = new Rectangle(xOffset, yOffset, width, height); bounds = new Rectangle(xOffset, yOffset, width, height); // g.setColor(Color.yellow); @@ -222,9 +221,9 @@ public class Axis implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return axisPair.getChart(); + return axisPair.getChartPainter(); } // Getters ///////////////////////////////////////////////// diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java index bc69d438..b7fa1d22 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java @@ -24,7 +24,6 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; -import com.xeiam.xchart.Chart; import com.xeiam.xchart.Series; import com.xeiam.xchart.internal.chartpart.Axis.AxisType; import com.xeiam.xchart.internal.style.SeriesColorMarkerLineStyleCycler; @@ -35,7 +34,7 @@ import com.xeiam.xchart.internal.style.SeriesColorMarkerLineStyleCycler; public class AxisPair implements ChartPart { /** parent */ - private final Chart chart; + private final ChartPainter chartPainter; private Map<Integer, Series> seriesMap = new LinkedHashMap<Integer, Series>(); @@ -49,11 +48,11 @@ public class AxisPair implements ChartPart { /** * Constructor * - * @param the parent chart + * @param the parent chartPainter */ - public AxisPair(Chart chart) { + public AxisPair(ChartPainter chartPainter) { - this.chart = chart; + this.chartPainter = chartPainter; // add axes xAxis = new Axis(this, Axis.Direction.X); @@ -136,9 +135,9 @@ public class AxisPair implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return chart; + return chartPainter; } // Getters ///////////////////////////////////////////////// diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java index dd212559..2fd2bf84 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java @@ -19,7 +19,6 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.List; -import com.xeiam.xchart.Chart; import com.xeiam.xchart.StyleManager.ChartType; import com.xeiam.xchart.internal.chartpart.Axis.AxisType; @@ -79,25 +78,25 @@ public class AxisTick implements ChartPart { // System.out.println("workingspace= " + workingSpace); } - if (axis.getDirection() == Axis.Direction.X && getChart().getStyleManager().getChartType() == ChartType.Bar) { + if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().getChartType() == ChartType.Bar) { - gridStep = new AxisTickBarChartCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChart()); + gridStep = new AxisTickBarChartCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter()); - } else if (axis.getDirection() == Axis.Direction.X && getChart().getStyleManager().isXAxisLogarithmic() && axis.getAxisType() != AxisType.Date) { + } else if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().isXAxisLogarithmic() && axis.getAxisType() != AxisType.Date) { - gridStep = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChart().getStyleManager()); + gridStep = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); - } else if (axis.getDirection() == Axis.Direction.Y && getChart().getStyleManager().isYAxisLogarithmic() && axis.getAxisType() != AxisType.Date) { + } else if (axis.getDirection() == Axis.Direction.Y && getChartPainter().getStyleManager().isYAxisLogarithmic() && axis.getAxisType() != AxisType.Date) { - gridStep = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChart().getStyleManager()); + gridStep = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); } else if (axis.getAxisType() == AxisType.Number) { - gridStep = new AxisTickNumericalCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChart().getStyleManager()); + gridStep = new AxisTickNumericalCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); } else if (axis.getAxisType() == AxisType.Date) { - gridStep = new AxisTickDateCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChart().getStyleManager()); + gridStep = new AxisTickDateCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); } @@ -108,13 +107,13 @@ public class AxisTick implements ChartPart { if (axis.getDirection() == Axis.Direction.Y) { bounds = new Rectangle((int) axisTickLabels.getBounds().getX(), (int) (axisTickLabels.getBounds().getY()), (int) (axisTickLabels.getBounds().getWidth() - + getChart().getStyleManager().getAxisTickPadding() + axisTickMarks.getBounds().getWidth()), (int) (axisTickMarks.getBounds().getHeight())); + + getChartPainter().getStyleManager().getAxisTickPadding() + axisTickMarks.getBounds().getWidth()), (int) (axisTickMarks.getBounds().getHeight())); // g.setColor(Color.red); // g.draw(bounds); } else { bounds = new Rectangle((int) axisTickMarks.getBounds().getX(), (int) (axisTickMarks.getBounds().getY()), (int) axisTickLabels.getBounds().getWidth(), (int) (axisTickMarks.getBounds() .getHeight() - + getChart().getStyleManager().getAxisTickPadding() + axisTickLabels.getBounds().getHeight())); + + getChartPainter().getStyleManager().getAxisTickPadding() + axisTickLabels.getBounds().getHeight())); // g.setColor(Color.red); // g.draw(bounds); } @@ -123,9 +122,9 @@ public class AxisTick implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return axis.getChart(); + return axis.getChartPainter(); } // Getters ///////////////////////////////////////////////// diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java index d3c3aca9..da7b1e99 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; -import com.xeiam.xchart.Chart; import com.xeiam.xchart.Series; import com.xeiam.xchart.internal.chartpart.Axis.AxisType; import com.xeiam.xchart.internal.chartpart.Axis.Direction; @@ -49,13 +48,13 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { * @param maxValue * @param styleManager */ - public AxisTickBarChartCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, Chart chart) { + public AxisTickBarChartCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, ChartPainter chart) { super(axisDirection, workingSpace, minValue, maxValue, chart.getStyleManager()); calculate(chart); } - private void calculate(Chart chart) { + private void calculate(ChartPainter chart) { // tick space - a percentage of the working space available for ticks, i.e. 95% int tickSpace = AxisPair.getTickSpace(workingSpace); // in plot space diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java index 75cb3f68..90f642c3 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java @@ -20,8 +20,6 @@ import java.awt.Rectangle; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; -import com.xeiam.xchart.Chart; - /** * Axis tick labels */ @@ -53,9 +51,9 @@ public class AxisTickLabels implements ChartPart { public void paint(Graphics2D g) { bounds = new Rectangle(); - g.setFont(getChart().getStyleManager().getAxisTickLabelsFont()); + g.setFont(getChartPainter().getStyleManager().getAxisTickLabelsFont()); - g.setColor(getChart().getStyleManager().getAxisTickLabelsColor()); + g.setColor(getChartPainter().getStyleManager().getAxisTickLabelsColor()); if (axisTick.getAxis().getDirection() == Axis.Direction.Y) { // Y-Axis @@ -71,7 +69,7 @@ public class AxisTickLabels implements ChartPart { if (tickLabel != null) { // some are null for logarithmic axes FontRenderContext frc = g.getFontRenderContext(); // TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false)); - TextLayout layout = new TextLayout(tickLabel, getChart().getStyleManager().getAxisTickLabelsFont(), frc); + TextLayout layout = new TextLayout(tickLabel, getChartPainter().getStyleManager().getAxisTickLabelsFont(), frc); Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); layout.draw(g, xOffset, (int) (yOffset + axisTick.getAxis().getPaintZone().getHeight() - tickLocation + tickLabelBounds.getHeight() / 2.0)); @@ -98,7 +96,7 @@ public class AxisTickLabels implements ChartPart { if (tickLabel != null) { // some are null for logarithmic axes FontRenderContext frc = g.getFontRenderContext(); - TextLayout layout = new TextLayout(tickLabel, getChart().getStyleManager().getAxisTickLabelsFont(), frc); + TextLayout layout = new TextLayout(tickLabel, getChartPainter().getStyleManager().getAxisTickLabelsFont(), frc); Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); layout.draw(g, (int) (xOffset + tickLocation - tickLabelBounds.getWidth() / 2.0), yOffset); @@ -118,8 +116,8 @@ public class AxisTickLabels implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return axisTick.getChart(); + return axisTick.getChartPainter(); } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java index 09ce4d72..5e1c668f 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java @@ -18,8 +18,6 @@ package com.xeiam.xchart.internal.chartpart; import java.awt.Graphics2D; import java.awt.Rectangle; -import com.xeiam.xchart.Chart; - /** * Axis tick marks. */ @@ -52,12 +50,12 @@ public class AxisTickMarks implements ChartPart { bounds = new Rectangle(); - g.setColor(getChart().getStyleManager().getAxisTickMarksColor()); - g.setStroke(getChart().getStyleManager().getAxisTickMarksStroke()); + g.setColor(getChartPainter().getStyleManager().getAxisTickMarksColor()); + g.setStroke(getChartPainter().getStyleManager().getAxisTickMarksStroke()); if (axisTick.getAxis().getDirection() == Axis.Direction.Y) { // Y-Axis - int xOffset = (int) (axisTick.getAxisTickLabels().getBounds().getX() + axisTick.getAxisTickLabels().getBounds().getWidth() + getChart().getStyleManager().getAxisTickPadding()); + int xOffset = (int) (axisTick.getAxisTickLabels().getBounds().getX() + axisTick.getAxisTickLabels().getBounds().getWidth() + getChartPainter().getStyleManager().getAxisTickPadding()); int yOffset = (int) (axisTick.getAxis().getPaintZone().getY()); // tick marks @@ -68,19 +66,19 @@ public class AxisTickMarks implements ChartPart { // g.setColor(getChart().getStyleManager().getChartBordersColor()); // g.setStroke(stroke); - g.drawLine(xOffset, yOffset + (int) (axisTick.getAxis().getPaintZone().getHeight() - tickLocation), xOffset + getChart().getStyleManager().getAxisTickMarkLength(), yOffset + g.drawLine(xOffset, yOffset + (int) (axisTick.getAxis().getPaintZone().getHeight() - tickLocation), xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset + (int) (axisTick.getAxis().getPaintZone().getHeight() - tickLocation)); } // Line - if (getChart().getStyleManager().isAxisTicksLineVisible()) { - g.drawLine(xOffset + getChart().getStyleManager().getAxisTickMarkLength(), yOffset, xOffset + getChart().getStyleManager().getAxisTickMarkLength(), yOffset + if (getChartPainter().getStyleManager().isAxisTicksLineVisible()) { + g.drawLine(xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset, xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset + (int) axisTick.getAxis().getPaintZone().getHeight()); } // bounds - bounds = new Rectangle(xOffset, yOffset, getChart().getStyleManager().getAxisTickMarkLength(), (int) axisTick.getAxis().getPaintZone().getHeight()); + bounds = new Rectangle(xOffset, yOffset, getChartPainter().getStyleManager().getAxisTickMarkLength(), (int) axisTick.getAxis().getPaintZone().getHeight()); // g.setColor(Color.yellow); // g.draw(bounds); @@ -88,7 +86,7 @@ public class AxisTickMarks implements ChartPart { int xOffset = (int) (axisTick.getAxis().getPaintZone().getX()); // int yOffset = (int) (axisTick.getAxisTickLabels().getBounds().getY() - getChart().getStyleManager().getAxisTickPadding()); - int yOffset = (int) (axisTick.getAxisTickLabels().getBounds().getY() - getChart().getStyleManager().getAxisTickPadding()); + int yOffset = (int) (axisTick.getAxisTickLabels().getBounds().getY() - getChartPainter().getStyleManager().getAxisTickPadding()); // tick marks for (int i = 0; i < axisTick.getTickLabels().size(); i++) { @@ -98,18 +96,18 @@ public class AxisTickMarks implements ChartPart { // g.setColor(getChart().getStyleManager().getChartBordersColor()); // g.setStroke(stroke); - g.drawLine(xOffset + tickLocation, yOffset, xOffset + tickLocation, yOffset - getChart().getStyleManager().getAxisTickMarkLength()); + g.drawLine(xOffset + tickLocation, yOffset, xOffset + tickLocation, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength()); } // Line - if (getChart().getStyleManager().isAxisTicksLineVisible()) { + if (getChartPainter().getStyleManager().isAxisTicksLineVisible()) { - g.drawLine(xOffset, yOffset - getChart().getStyleManager().getAxisTickMarkLength(), xOffset + (int) axisTick.getAxis().getPaintZone().getWidth(), yOffset - - getChart().getStyleManager().getAxisTickMarkLength()); + g.drawLine(xOffset, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength(), xOffset + (int) axisTick.getAxis().getPaintZone().getWidth(), yOffset + - getChartPainter().getStyleManager().getAxisTickMarkLength()); } // bounds - bounds = new Rectangle(xOffset, yOffset - getChart().getStyleManager().getAxisTickMarkLength(), (int) axisTick.getAxis().getPaintZone().getWidth(), getChart().getStyleManager() + bounds = new Rectangle(xOffset, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength(), (int) axisTick.getAxis().getPaintZone().getWidth(), getChartPainter().getStyleManager() .getAxisTickMarkLength()); // g.setColor(Color.yellow); // g.draw(bounds); @@ -117,8 +115,8 @@ public class AxisTickMarks implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return axisTick.getChart(); + return axisTick.getChartPainter(); } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java index 3d0e7554..7e5cf2de 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java @@ -21,8 +21,6 @@ import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.AffineTransform; -import com.xeiam.xchart.Chart; - /** * AxisTitle */ @@ -58,14 +56,14 @@ public class AxisTitle implements ChartPart { bounds = new Rectangle(); - g.setColor(getChart().getStyleManager().getChartFontColor()); - g.setFont(getChart().getStyleManager().getAxisTitleFont()); + g.setColor(getChartPainter().getStyleManager().getChartFontColor()); + g.setFont(getChartPainter().getStyleManager().getAxisTitleFont()); if (axis.getDirection() == Axis.Direction.Y) { - if (getChart().getStyleManager().isyAxisTitleVisible()) { + if (getChartPainter().getStyleManager().isyAxisTitleVisible()) { FontRenderContext frc = g.getFontRenderContext(); - TextLayout nonRotatedTextLayout = new TextLayout(text, getChart().getStyleManager().getAxisTitleFont(), frc); + TextLayout nonRotatedTextLayout = new TextLayout(text, getChartPainter().getStyleManager().getAxisTitleFont(), frc); Rectangle nonRotatedRectangle = nonRotatedTextLayout.getPixelBounds(null, 0, 0); // System.out.println(nonRotatedRectangle); @@ -94,7 +92,7 @@ public class AxisTitle implements ChartPart { // bounds bounds = new Rectangle((int) (xOffset - nonRotatedRectangle.getHeight()), (int) (yOffset - nonRotatedRectangle.getWidth()), (int) nonRotatedRectangle.getHeight() - + getChart().getStyleManager().getAxisTitlePadding(), (int) nonRotatedRectangle.getWidth()); + + getChartPainter().getStyleManager().getAxisTitlePadding(), (int) nonRotatedRectangle.getWidth()); // g.setColor(Color.blue); // g.draw(bounds); } else { @@ -103,10 +101,10 @@ public class AxisTitle implements ChartPart { } else { - if (getChart().getStyleManager().isxAxisTitleVisible()) { + if (getChartPainter().getStyleManager().isxAxisTitleVisible()) { FontRenderContext frc = g.getFontRenderContext(); - TextLayout textLayout = new TextLayout(text, getChart().getStyleManager().getAxisTitleFont(), frc); + TextLayout textLayout = new TextLayout(text, getChartPainter().getStyleManager().getAxisTitleFont(), frc); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); // System.out.println(rectangle); @@ -115,8 +113,8 @@ public class AxisTitle implements ChartPart { textLayout.draw(g, xOffset, (float) (yOffset - rectangle.getY())); - bounds = new Rectangle(xOffset, yOffset - getChart().getStyleManager().getAxisTitlePadding(), (int) rectangle.getWidth(), (int) rectangle.getHeight() - + getChart().getStyleManager().getAxisTitlePadding()); + bounds = new Rectangle(xOffset, yOffset - getChartPainter().getStyleManager().getAxisTitlePadding(), (int) rectangle.getWidth(), (int) rectangle.getHeight() + + getChartPainter().getStyleManager().getAxisTitlePadding()); // g.setColor(Color.blue); // g.draw(bounds); @@ -130,9 +128,9 @@ public class AxisTitle implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return axis.getChart(); + return axis.getChartPainter(); } // Getters ///////////////////////////////////////////////// @@ -145,9 +143,9 @@ public class AxisTitle implements ChartPart { public void setText(String text) { if (text == null || text.trim().equalsIgnoreCase("")) { - getChart().getStyleManager().setxAxisTitleVisible(false); + getChartPainter().getStyleManager().setxAxisTitleVisible(false); } else { - getChart().getStyleManager().setxAxisTitleVisible(true); + getChartPainter().getStyleManager().setxAxisTitleVisible(true); } this.text = text; } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPainter.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPainter.java new file mode 100644 index 00000000..1005e30b --- /dev/null +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPainter.java @@ -0,0 +1,163 @@ +/** + * Copyright (C) 2013 Xeiam LLC http://xeiam.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.xeiam.xchart.internal.chartpart; + +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.math.BigDecimal; + +import com.xeiam.xchart.StyleManager; + +/** + * @author timmolter + */ +public class ChartPainter { + + private int width; + private int height; + + private final StyleManager styleManager; + + // Chart Parts + private Legend chartLegend; + private AxisPair axisPair; + private Plot plot; + private ChartTitle chartTitle; + + /** + * Constructor + * + * @param chart + */ + public ChartPainter(int width, int height) { + + this.width = width; + this.height = height; + + styleManager = new StyleManager(); + + chartLegend = new Legend(this); + axisPair = new AxisPair(this); + plot = new Plot(this); + chartTitle = new ChartTitle(this); + } + + /** + * @param g + * @param width + * @param height + */ + public void paint(Graphics2D g, int width, int height) { + + this.width = width; + this.height = height; + paint(g); + } + + /** + * @param g + */ + public void paint(Graphics2D g) { + + // Sanity checks + if (axisPair.getSeriesMap().isEmpty()) { + throw new RuntimeException("No series defined for Chart!!!"); + } + if (getStyleManager().isXAxisLogarithmic() && axisPair.getxAxis().getMin().compareTo(BigDecimal.ZERO) <= 0) { + throw new IllegalArgumentException("Series data cannot be less or equal to zero for a logarithmic X-Axis!!!"); + } + if (getStyleManager().isYAxisLogarithmic() && axisPair.getyAxis().getMin().compareTo(BigDecimal.ZERO) <= 0) { + throw new IllegalArgumentException("Series data cannot be less or equal to zero for a logarithmic Y-Axis!!!"); + } + + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // global rendering hint + g.setColor(styleManager.getChartBackgroundColor()); + g.fillRect(0, 0, width, height); + + axisPair.paint(g); + plot.paint(g); + chartTitle.paint(g); + chartLegend.paint(g); + + g.dispose(); + } + + /** + * for internal usage + * + * @return + */ + public ChartTitle getChartTitle() { + + return chartTitle; + } + + /** + * for internal usage + * + * @return + */ + public Legend getChartLegend() { + + return chartLegend; + } + + /** + * for internal usage + * + * @return + */ + public AxisPair getAxisPair() { + + return axisPair; + } + + /** + * for internal usage + * + * @return + */ + public Plot getPlot() { + + return plot; + } + + public int getWidth() { + + return width; + } + + public int getHeight() { + + return height; + } + + /** + * Gets the Chart's style manager, which can be used to customize the Chart's appearance + * + * @return the style manager + */ + public StyleManager getStyleManager() { + + return styleManager; + } +} diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPart.java index 971bb47f..934af8e6 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPart.java @@ -18,8 +18,6 @@ package com.xeiam.xchart.internal.chartpart; import java.awt.Graphics2D; import java.awt.Rectangle; -import com.xeiam.xchart.Chart; - /** * All components of a chart that need to be painted should implement this interface * @@ -31,6 +29,6 @@ public interface ChartPart { public void paint(final Graphics2D g); - public Chart getChart(); + public ChartPainter getChartPainter(); } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java index d9582865..bc1ccbfa 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java @@ -21,15 +21,13 @@ import java.awt.Rectangle; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; -import com.xeiam.xchart.Chart; - /** * Chart Title */ public class ChartTitle implements ChartPart { /** parent */ - private final Chart chart; + private final ChartPainter chartPainter; /** the bounds */ private Rectangle bounds; @@ -40,11 +38,11 @@ public class ChartTitle implements ChartPart { /** * Constructor * - * @param chart + * @param chartPainter */ - public ChartTitle(Chart chart) { + public ChartTitle(ChartPainter chartPainter) { - this.chart = chart; + this.chartPainter = chartPainter; } /** @@ -55,9 +53,9 @@ public class ChartTitle implements ChartPart { public void setText(String text) { if (text.trim().equalsIgnoreCase("")) { - chart.getStyleManager().setChartTitleVisible(false); + chartPainter.getStyleManager().setChartTitleVisible(false); } else { - chart.getStyleManager().setChartTitleVisible(true); + chartPainter.getStyleManager().setChartTitleVisible(true); } this.text = text; } @@ -69,14 +67,14 @@ public class ChartTitle implements ChartPart { */ protected int getSizeHint() { - if (chart.getStyleManager().isChartTitleVisible()) { + if (chartPainter.getStyleManager().isChartTitleVisible()) { - TextLayout textLayout = new TextLayout(text, chart.getStyleManager().getChartTitleFont(), new FontRenderContext(null, true, false)); + TextLayout textLayout = new TextLayout(text, chartPainter.getStyleManager().getChartTitleFont(), new FontRenderContext(null, true, false)); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - int titleHeight = (int) ((chart.getStyleManager().isChartTitleVisible() ? rectangle.getHeight() : 0)); - return chart.getStyleManager().getChartPadding() + 2 * chart.getStyleManager().getChartTitlePadding() + titleHeight; + int titleHeight = (int) ((chartPainter.getStyleManager().isChartTitleVisible() ? rectangle.getHeight() : 0)); + return chartPainter.getStyleManager().getChartPadding() + 2 * chartPainter.getStyleManager().getChartTitlePadding() + titleHeight; } else { - return chart.getStyleManager().getChartPadding(); + return chartPainter.getStyleManager().getChartPadding(); } } @@ -84,36 +82,36 @@ public class ChartTitle implements ChartPart { public void paint(Graphics2D g) { // bounds = new Rectangle(); - g.setFont(chart.getStyleManager().getChartTitleFont()); + g.setFont(chartPainter.getStyleManager().getChartTitleFont()); - if (chart.getStyleManager().isChartTitleVisible()) { + if (chartPainter.getStyleManager().isChartTitleVisible()) { // create rectangle first for sizing FontRenderContext frc = g.getFontRenderContext(); - TextLayout textLayout = new TextLayout(text, chart.getStyleManager().getChartTitleFont(), frc); + TextLayout textLayout = new TextLayout(text, chartPainter.getStyleManager().getChartTitleFont(), frc); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); // paint the chart title box - int chartTitleBoxWidth = (int) chart.getPlot().getBounds().getWidth(); - int chartTitleBoxHeight = (int) (rectangle.getHeight() + 2 * chart.getStyleManager().getChartTitlePadding()); - int xOffset = (int) chart.getPlot().getBounds().getX(); - int yOffset = chart.getStyleManager().getChartPadding(); + int chartTitleBoxWidth = (int) chartPainter.getPlot().getBounds().getWidth(); + int chartTitleBoxHeight = (int) (rectangle.getHeight() + 2 * chartPainter.getStyleManager().getChartTitlePadding()); + int xOffset = (int) chartPainter.getPlot().getBounds().getX(); + int yOffset = chartPainter.getStyleManager().getChartPadding(); g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); - g.setColor(chart.getStyleManager().getChartTitleBorderColor()); + g.setColor(chartPainter.getStyleManager().getChartTitleBorderColor()); g.drawRect(xOffset - 1, yOffset, chartTitleBoxWidth - 1, chartTitleBoxHeight - 1); - g.setColor(chart.getStyleManager().getChartTitleBackgroundColor()); + g.setColor(chartPainter.getStyleManager().getChartTitleBackgroundColor()); g.fillRect(xOffset - 1, yOffset + 1, chartTitleBoxWidth, chartTitleBoxHeight - 1); // paint title - xOffset = (int) (chart.getPlot().getBounds().getX() + (chart.getPlot().getBounds().getWidth() - rectangle.getWidth()) / 2.0); - yOffset = (int) (chart.getStyleManager().getChartPadding() - rectangle.getY() + chart.getStyleManager().getChartTitlePadding()); + xOffset = (int) (chartPainter.getPlot().getBounds().getX() + (chartPainter.getPlot().getBounds().getWidth() - rectangle.getWidth()) / 2.0); + yOffset = (int) (chartPainter.getStyleManager().getChartPadding() - rectangle.getY() + chartPainter.getStyleManager().getChartTitlePadding()); // bounds = new Rectangle(xOffset, yOffset + ((int) rectangle.getY()), (int) rectangle.getWidth(), (int) (rectangle.getHeight())); // g.setColor(Color.green); // g.draw(bounds); - g.setColor(chart.getStyleManager().getChartFontColor()); + g.setColor(chartPainter.getStyleManager().getChartFontColor()); textLayout.draw(g, xOffset, yOffset); } @@ -126,8 +124,8 @@ public class ChartTitle implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return chart; + return chartPainter; } } \ No newline at end of file diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java index 4a35168b..71a8517e 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java @@ -22,7 +22,6 @@ import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.util.Map; -import com.xeiam.xchart.Chart; import com.xeiam.xchart.Series; import com.xeiam.xchart.StyleManager.ChartType; import com.xeiam.xchart.internal.markers.Marker; @@ -36,7 +35,7 @@ public class Legend implements ChartPart { private static final int BOX_SIZE = 20; /** parent */ - private final Chart chart; + private final ChartPainter chartPainter; /** the bounds */ private Rectangle bounds; @@ -44,11 +43,11 @@ public class Legend implements ChartPart { /** * Constructor * - * @param chart + * @param chartPainter */ - public Legend(Chart chart) { + public Legend(ChartPainter chartPainter) { - this.chart = chart; + this.chartPainter = chartPainter; } /** @@ -58,9 +57,9 @@ public class Legend implements ChartPart { */ protected int[] getSizeHint() { - if (chart.getStyleManager().isLegendVisible()) { + if (chartPainter.getStyleManager().isLegendVisible()) { - Map<Integer, Series> seriesMap = chart.getAxisPair().getSeriesMap(); + Map<Integer, Series> seriesMap = chartPainter.getAxisPair().getSeriesMap(); // determine legend text content max width int legendTextContentMaxWidth = 0; @@ -68,7 +67,7 @@ public class Legend implements ChartPart { for (Integer seriesId : seriesMap.keySet()) { Series series = seriesMap.get(seriesId); - TextLayout textLayout = new TextLayout(series.getName(), chart.getStyleManager().getLegendFont(), new FontRenderContext(null, true, false)); + TextLayout textLayout = new TextLayout(series.getName(), chartPainter.getStyleManager().getLegendFont(), new FontRenderContext(null, true, false)); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); // System.out.println(rectangle); if (rectangle.getWidth() > legendTextContentMaxWidth) { @@ -81,23 +80,23 @@ public class Legend implements ChartPart { // determine legend content height int maxContentHeight = 0; - if (getChart().getStyleManager().getChartType() != ChartType.Bar) { + if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar) { maxContentHeight = Math.max(legendTextContentMaxHeight, Marker.SIZE); } else { maxContentHeight = Math.max(legendTextContentMaxHeight, BOX_SIZE); } - int legendContentHeight = maxContentHeight * seriesMap.size() + chart.getStyleManager().getLegendPadding() * (seriesMap.size() - 1); + int legendContentHeight = maxContentHeight * seriesMap.size() + chartPainter.getStyleManager().getLegendPadding() * (seriesMap.size() - 1); // determine legend content width int legendContentWidth = 0; - if (getChart().getStyleManager().getChartType() != ChartType.Bar) { - legendContentWidth = (int) (3.0 * Marker.SIZE + chart.getStyleManager().getLegendPadding() + legendTextContentMaxWidth); + if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar) { + legendContentWidth = (int) (3.0 * Marker.SIZE + chartPainter.getStyleManager().getLegendPadding() + legendTextContentMaxWidth); } else { - legendContentWidth = BOX_SIZE + chart.getStyleManager().getLegendPadding() + legendTextContentMaxWidth; + legendContentWidth = BOX_SIZE + chartPainter.getStyleManager().getLegendPadding() + legendTextContentMaxWidth; } // Legend Box - int legendBoxWidth = legendContentWidth + 2 * chart.getStyleManager().getLegendPadding(); - int legendBoxHeight = legendContentHeight + 2 * chart.getStyleManager().getLegendPadding(); + int legendBoxWidth = legendContentWidth + 2 * chartPainter.getStyleManager().getLegendPadding(); + int legendBoxHeight = legendContentHeight + 2 * chartPainter.getStyleManager().getLegendPadding(); return new int[] { legendBoxWidth, legendBoxHeight, maxContentHeight }; } else { return new int[] { 0, 0, 0 }; @@ -108,11 +107,11 @@ public class Legend implements ChartPart { public void paint(Graphics2D g) { bounds = new Rectangle(); - g.setFont(chart.getStyleManager().getLegendFont()); + g.setFont(chartPainter.getStyleManager().getLegendFont()); - if (chart.getStyleManager().isLegendVisible()) { + if (chartPainter.getStyleManager().isLegendVisible()) { - Map<Integer, Series> seriesMap = chart.getAxisPair().getSeriesMap(); + Map<Integer, Series> seriesMap = chartPainter.getAxisPair().getSeriesMap(); int legendBoxWidth = getSizeHint()[0]; int legendBoxHeight = getSizeHint()[1]; @@ -121,47 +120,47 @@ public class Legend implements ChartPart { // legend draw position int xOffset = 0; int yOffset = 0; - switch (chart.getStyleManager().getLegendPosition()) { + switch (chartPainter.getStyleManager().getLegendPosition()) { case OutsideW: - xOffset = chart.getWidth() - legendBoxWidth - chart.getStyleManager().getChartPadding(); - yOffset = (int) (chart.getPlot().getBounds().getY() + (chart.getPlot().getBounds().getHeight() - legendBoxHeight) / 2.0); + xOffset = chartPainter.getWidth() - legendBoxWidth - chartPainter.getStyleManager().getChartPadding(); + yOffset = (int) (chartPainter.getPlot().getBounds().getY() + (chartPainter.getPlot().getBounds().getHeight() - legendBoxHeight) / 2.0); break; case InsideNW: - xOffset = (int) (chart.getPlot().getBounds().getX() + LEGEND_MARGIN); - yOffset = (int) (chart.getPlot().getBounds().getY() + LEGEND_MARGIN); + xOffset = (int) (chartPainter.getPlot().getBounds().getX() + LEGEND_MARGIN); + yOffset = (int) (chartPainter.getPlot().getBounds().getY() + LEGEND_MARGIN); break; case InsideNE: - xOffset = (int) (chart.getPlot().getBounds().getX() + chart.getPlot().getBounds().getWidth() - legendBoxWidth - LEGEND_MARGIN); - yOffset = (int) (chart.getPlot().getBounds().getY() + LEGEND_MARGIN); + xOffset = (int) (chartPainter.getPlot().getBounds().getX() + chartPainter.getPlot().getBounds().getWidth() - legendBoxWidth - LEGEND_MARGIN); + yOffset = (int) (chartPainter.getPlot().getBounds().getY() + LEGEND_MARGIN); break; case InsideSE: - xOffset = (int) (chart.getPlot().getBounds().getX() + chart.getPlot().getBounds().getWidth() - legendBoxWidth - LEGEND_MARGIN); - yOffset = (int) (chart.getPlot().getBounds().getY() + chart.getPlot().getBounds().getHeight() - legendBoxHeight - LEGEND_MARGIN); + xOffset = (int) (chartPainter.getPlot().getBounds().getX() + chartPainter.getPlot().getBounds().getWidth() - legendBoxWidth - LEGEND_MARGIN); + yOffset = (int) (chartPainter.getPlot().getBounds().getY() + chartPainter.getPlot().getBounds().getHeight() - legendBoxHeight - LEGEND_MARGIN); break; case InsideSW: - xOffset = (int) (chart.getPlot().getBounds().getX() + LEGEND_MARGIN); - yOffset = (int) (chart.getPlot().getBounds().getY() + chart.getPlot().getBounds().getHeight() - legendBoxHeight - LEGEND_MARGIN); + xOffset = (int) (chartPainter.getPlot().getBounds().getX() + LEGEND_MARGIN); + yOffset = (int) (chartPainter.getPlot().getBounds().getY() + chartPainter.getPlot().getBounds().getHeight() - legendBoxHeight - LEGEND_MARGIN); break; default: break; } g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); - g.setColor(chart.getStyleManager().getLegendBorderColor()); + g.setColor(chartPainter.getStyleManager().getLegendBorderColor()); g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight); - g.setColor(chart.getStyleManager().getLegendBackgroundColor()); + g.setColor(chartPainter.getStyleManager().getLegendBackgroundColor()); g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1); // Draw legend content inside legend box - int startx = xOffset + chart.getStyleManager().getLegendPadding(); - int starty = yOffset + chart.getStyleManager().getLegendPadding(); + int startx = xOffset + chartPainter.getStyleManager().getLegendPadding(); + int starty = yOffset + chartPainter.getStyleManager().getLegendPadding(); for (Integer seriesId : seriesMap.keySet()) { Series series = seriesMap.get(seriesId); - if (getChart().getStyleManager().getChartType() != ChartType.Bar) { + if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar) { // paint line - if (getChart().getStyleManager().getChartType() != ChartType.Scatter && series.getStroke() != null) { + if (getChartPainter().getStyleManager().getChartType() != ChartType.Scatter && series.getStroke() != null) { g.setColor(series.getStrokeColor()); g.setStroke(series.getStroke()); g.drawLine(startx, starty + (int) (maxContentHeight / 2.0), (int) (startx + Marker.SIZE * 3.0), starty + (int) (maxContentHeight / 2.0)); @@ -183,15 +182,15 @@ public class Legend implements ChartPart { } // paint series name - g.setColor(chart.getStyleManager().getChartFontColor()); - TextLayout layout = new TextLayout(series.getName(), chart.getStyleManager().getLegendFont(), new FontRenderContext(null, true, false)); - if (getChart().getStyleManager().getChartType() != ChartType.Bar) { - layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + chart.getStyleManager().getLegendPadding()), starty + g.setColor(chartPainter.getStyleManager().getChartFontColor()); + TextLayout layout = new TextLayout(series.getName(), chartPainter.getStyleManager().getLegendFont(), new FontRenderContext(null, true, false)); + if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar) { + layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + chartPainter.getStyleManager().getLegendPadding()), starty + (int) ((maxContentHeight + layout.getPixelBounds(null, 0, 0).getHeight()) / 2.0)); } else { - layout.draw(g, startx + BOX_SIZE + chart.getStyleManager().getLegendPadding(), starty + (int) ((maxContentHeight + layout.getPixelBounds(null, 0, 0).getHeight()) / 2.0)); + layout.draw(g, startx + BOX_SIZE + chartPainter.getStyleManager().getLegendPadding(), starty + (int) ((maxContentHeight + layout.getPixelBounds(null, 0, 0).getHeight()) / 2.0)); } - starty = starty + maxContentHeight + chart.getStyleManager().getLegendPadding(); + starty = starty + maxContentHeight + chartPainter.getStyleManager().getLegendPadding(); } // bounds @@ -209,9 +208,9 @@ public class Legend implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return chart; + return chartPainter; } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Plot.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Plot.java index 8fde77da..986538ff 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Plot.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Plot.java @@ -18,7 +18,6 @@ package com.xeiam.xchart.internal.chartpart; import java.awt.Graphics2D; import java.awt.Rectangle; -import com.xeiam.xchart.Chart; import com.xeiam.xchart.StyleManager.ChartType; /** @@ -27,7 +26,7 @@ import com.xeiam.xchart.StyleManager.ChartType; public class Plot implements ChartPart { /** parent */ - private final Chart chart; + private final ChartPainter chartPainter; /** the bounds */ private Rectangle bounds; @@ -39,11 +38,11 @@ public class Plot implements ChartPart { /** * Constructor * - * @param chart + * @param chartPainter */ - public Plot(Chart chart) { + public Plot(ChartPainter chartPainter) { - this.chart = chart; + this.chartPainter = chartPainter; this.plotSurface = new PlotSurface(this); } @@ -60,17 +59,17 @@ public class Plot implements ChartPart { bounds = new Rectangle(); // calculate bounds - int xOffset = (int) (chart.getAxisPair().getyAxis().getBounds().getX() + chart.getAxisPair().getyAxis().getBounds().getWidth() + (chart.getStyleManager().isyAxisTicksVisible() ? (chart - .getStyleManager().getPlotPadding() + 1) : 0)); - int yOffset = (int) (chart.getAxisPair().getyAxis().getBounds().getY()); - int width = (int) chart.getAxisPair().getxAxis().getBounds().getWidth(); - int height = (int) chart.getAxisPair().getyAxis().getBounds().getHeight(); + int xOffset = (int) (chartPainter.getAxisPair().getyAxis().getBounds().getX() + chartPainter.getAxisPair().getyAxis().getBounds().getWidth() + (chartPainter.getStyleManager() + .isyAxisTicksVisible() ? (chartPainter.getStyleManager().getPlotPadding() + 1) : 0)); + int yOffset = (int) (chartPainter.getAxisPair().getyAxis().getBounds().getY()); + int width = (int) chartPainter.getAxisPair().getxAxis().getBounds().getWidth(); + int height = (int) chartPainter.getAxisPair().getyAxis().getBounds().getHeight(); bounds = new Rectangle(xOffset, yOffset, width, height); // g.setColor(Color.green); // g.draw(bounds); plotSurface.paint(g); - if (getChart().getStyleManager().getChartType() == ChartType.Bar) { + if (getChartPainter().getStyleManager().getChartType() == ChartType.Bar) { this.plotContent = new PlotContentBarChart(this); } else { this.plotContent = new PlotContentLineChart(this); @@ -80,8 +79,8 @@ public class Plot implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return chart; + return chartPainter; } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java index 286a7557..db7a4a8f 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java @@ -19,8 +19,6 @@ import java.awt.BasicStroke; import java.awt.Rectangle; import java.awt.Stroke; -import com.xeiam.xchart.Chart; - /** * @author timmolter */ @@ -48,9 +46,9 @@ public abstract class PlotContent implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return plot.getChart(); + return plot.getChartPainter(); } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java index f653040a..c600f13f 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java @@ -24,7 +24,6 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; -import com.xeiam.xchart.Chart; import com.xeiam.xchart.Series; /** @@ -57,7 +56,7 @@ public class PlotContentBarChart extends PlotContent { // get all categories Set<Object> categories = new TreeSet<Object>(); - Map<Integer, Series> seriesMap = getChart().getAxisPair().getSeriesMap(); + Map<Integer, Series> seriesMap = getChartPainter().getAxisPair().getSeriesMap(); for (Integer seriesId : seriesMap.keySet()) { Series series = seriesMap.get(seriesId); @@ -79,19 +78,19 @@ public class PlotContentBarChart extends PlotContent { Collection<?> xData = series.getxData(); Collection<Number> yData = series.getyData(); - BigDecimal yMin = getChart().getAxisPair().getyAxis().getMin(); - BigDecimal yMax = getChart().getAxisPair().getyAxis().getMax(); + BigDecimal yMin = getChartPainter().getAxisPair().getyAxis().getMin(); + BigDecimal yMax = getChartPainter().getAxisPair().getyAxis().getMax(); if (yMin.compareTo(BigDecimal.ZERO) > 0 && yMax.compareTo(BigDecimal.ZERO) > 0) { yMin = BigDecimal.ZERO; } else if (yMin.compareTo(BigDecimal.ZERO) < 0 && yMax.compareTo(BigDecimal.ZERO) < 0) { yMax = BigDecimal.ZERO; } // override min and maxValue if specified - if (getChart().getStyleManager().getyAxisMin() != null) { - yMin = new BigDecimal(getChart().getStyleManager().getyAxisMin()); + if (getChartPainter().getStyleManager().getyAxisMin() != null) { + yMin = new BigDecimal(getChartPainter().getStyleManager().getyAxisMin()); } - if (getChart().getStyleManager().getyAxisMax() != null) { - yMax = new BigDecimal(getChart().getStyleManager().getyAxisMax()); + if (getChartPainter().getStyleManager().getyAxisMax() != null) { + yMax = new BigDecimal(getChartPainter().getStyleManager().getyAxisMax()); } Iterator<?> categoryItr = categories.iterator(); Iterator<Number> yItr = yData.iterator(); @@ -132,9 +131,9 @@ public class PlotContentBarChart extends PlotContent { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return plot.getChart(); + return plot.getChartPainter(); } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java index c46ec745..3db20db8 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java @@ -55,39 +55,39 @@ public class PlotContentLineChart extends PlotContent { int yTickSpace = AxisPair.getTickSpace((int) bounds.getHeight()); int yTopMargin = AxisPair.getTickStartOffset((int) bounds.getHeight(), yTickSpace); - Map<Integer, Series> seriesMap = getChart().getAxisPair().getSeriesMap(); + Map<Integer, Series> seriesMap = getChartPainter().getAxisPair().getSeriesMap(); for (Integer seriesId : seriesMap.keySet()) { Series series = seriesMap.get(seriesId); // data points Collection<?> xData = series.getxData(); - BigDecimal xMin = getChart().getAxisPair().getxAxis().getMin(); - BigDecimal xMax = getChart().getAxisPair().getxAxis().getMax(); - if (getChart().getStyleManager().isXAxisLogarithmic()) { + BigDecimal xMin = getChartPainter().getAxisPair().getxAxis().getMin(); + BigDecimal xMax = getChartPainter().getAxisPair().getxAxis().getMax(); + if (getChartPainter().getStyleManager().isXAxisLogarithmic()) { xMin = new BigDecimal(Math.log10(xMin.doubleValue())); xMax = new BigDecimal(Math.log10(xMax.doubleValue())); } Collection<Number> yData = series.getyData(); - BigDecimal yMin = getChart().getAxisPair().getyAxis().getMin(); - BigDecimal yMax = getChart().getAxisPair().getyAxis().getMax(); - if (getChart().getStyleManager().isYAxisLogarithmic()) { + BigDecimal yMin = getChartPainter().getAxisPair().getyAxis().getMin(); + BigDecimal yMax = getChartPainter().getAxisPair().getyAxis().getMax(); + if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { yMin = new BigDecimal(Math.log10(yMin.doubleValue())); yMax = new BigDecimal(Math.log10(yMax.doubleValue())); } // override min and maxValue if specified - if (getChart().getStyleManager().getxAxisMin() != null) { - xMin = new BigDecimal(getChart().getStyleManager().getxAxisMin()); + if (getChartPainter().getStyleManager().getxAxisMin() != null) { + xMin = new BigDecimal(getChartPainter().getStyleManager().getxAxisMin()); } - if (getChart().getStyleManager().getyAxisMin() != null) { - yMin = new BigDecimal(getChart().getStyleManager().getyAxisMin()); + if (getChartPainter().getStyleManager().getyAxisMin() != null) { + yMin = new BigDecimal(getChartPainter().getStyleManager().getyAxisMin()); } - if (getChart().getStyleManager().getxAxisMax() != null) { - xMin = new BigDecimal(getChart().getStyleManager().getxAxisMax()); + if (getChartPainter().getStyleManager().getxAxisMax() != null) { + xMin = new BigDecimal(getChartPainter().getStyleManager().getxAxisMax()); } - if (getChart().getStyleManager().getyAxisMax() != null) { - yMax = new BigDecimal(getChart().getStyleManager().getyAxisMax()); + if (getChartPainter().getStyleManager().getyAxisMax() != null) { + yMax = new BigDecimal(getChartPainter().getStyleManager().getyAxisMax()); } Collection<Number> errorBars = series.getErrorBars(); @@ -104,21 +104,21 @@ public class PlotContentLineChart extends PlotContent { while (xItr.hasNext()) { BigDecimal x = null; - if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Number) { + if (getChartPainter().getAxisPair().getxAxis().getAxisType() == AxisType.Number) { x = new BigDecimal(((Number) xItr.next()).doubleValue()); } - if (getChart().getAxisPair().getxAxis().getAxisType() == AxisType.Date) { + if (getChartPainter().getAxisPair().getxAxis().getAxisType() == AxisType.Date) { x = new BigDecimal(((Date) xItr.next()).getTime()); // System.out.println(x); } - if (getChart().getStyleManager().isXAxisLogarithmic()) { + if (getChartPainter().getStyleManager().isXAxisLogarithmic()) { x = new BigDecimal(Math.log10(x.doubleValue())); } BigDecimal y = new BigDecimal(yItr.next().doubleValue()); - if (getChart().getStyleManager().isYAxisLogarithmic()) { + if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { y = new BigDecimal(Math.log10(y.doubleValue())); } @@ -147,7 +147,7 @@ public class PlotContentLineChart extends PlotContent { // System.out.println(yTransform); // paint line - if (series.getStroke() != null && getChart().getStyleManager().getChartType() != ChartType.Scatter) { + if (series.getStroke() != null && getChartPainter().getStyleManager().getChartType() != ChartType.Scatter) { if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) { g.setColor(series.getStrokeColor()); g.setStroke(series.getStroke()); @@ -156,7 +156,7 @@ public class PlotContentLineChart extends PlotContent { } // paint area - if (getChart().getStyleManager().getChartType() == ChartType.Area) { + if (getChartPainter().getStyleManager().getChartType() == ChartType.Area) { if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) { g.setColor(series.getStrokeColor()); int yBottomOfArea = (int) (bounds.getY() + bounds.getHeight() - yTopMargin + 1); @@ -175,7 +175,7 @@ public class PlotContentLineChart extends PlotContent { // paint errorbar if (errorBars != null) { - g.setColor(getChart().getStyleManager().getErrorBarsColor()); + g.setColor(getChartPainter().getStyleManager().getErrorBarsColor()); g.setStroke(errorBarStroke); int bottom = (int) (-1 * bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue())); int top = (int) (bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue())); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java index 6a25cfa9..9123c72c 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java @@ -20,7 +20,6 @@ import java.awt.Rectangle; import java.awt.Stroke; import java.util.List; -import com.xeiam.xchart.Chart; import com.xeiam.xchart.StyleManager.ChartType; /** @@ -54,28 +53,28 @@ public class PlotSurface implements ChartPart { // paint plot background Rectangle backgroundRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); - g.setColor(getChart().getStyleManager().getPlotBackgroundColor()); + g.setColor(getChartPainter().getStyleManager().getPlotBackgroundColor()); g.fill(backgroundRectangle); // paint plot border - if (getChart().getStyleManager().isPlotBorderVisible()) { + if (getChartPainter().getStyleManager().isPlotBorderVisible()) { Rectangle borderRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); - g.setColor(getChart().getStyleManager().getPlotBorderColor()); + g.setColor(getChartPainter().getStyleManager().getPlotBorderColor()); g.draw(borderRectangle); } - Stroke stroke = getChart().getStyleManager().getPlotGridLinesStroke(); + Stroke stroke = getChartPainter().getStyleManager().getPlotGridLinesStroke(); // paint grid lines - if (getChart().getStyleManager().isPlotGridLinesVisible()) { + if (getChartPainter().getStyleManager().isPlotGridLinesVisible()) { // horizontal - List<Integer> yAxisTickLocations = getChart().getAxisPair().getyAxis().getAxisTick().getTickLocations(); + List<Integer> yAxisTickLocations = getChartPainter().getAxisPair().getyAxis().getAxisTick().getTickLocations(); for (int i = 0; i < yAxisTickLocations.size(); i++) { int tickLocation = yAxisTickLocations.get(i); - g.setColor(getChart().getStyleManager().getPlotGridLinesColor()); + g.setColor(getChartPainter().getStyleManager().getPlotGridLinesColor()); g.setStroke(stroke); // System.out.println("bounds.getY()= " + bounds.getY()); g.drawLine((int) bounds.getX(), (int) (bounds.getY() + bounds.getHeight() - tickLocation), (int) (bounds.getX() + bounds.getWidth() - 2), @@ -83,14 +82,14 @@ public class PlotSurface implements ChartPart { } // vertical - if (getChart().getStyleManager().getChartType() != ChartType.Bar) { + if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar) { - List<Integer> xAxisTickLocations = getChart().getAxisPair().getxAxis().getAxisTick().getTickLocations(); + List<Integer> xAxisTickLocations = getChartPainter().getAxisPair().getxAxis().getAxisTick().getTickLocations(); for (int i = 0; i < xAxisTickLocations.size(); i++) { int tickLocation = xAxisTickLocations.get(i); - g.setColor(getChart().getStyleManager().getPlotGridLinesColor()); + g.setColor(getChartPainter().getStyleManager().getPlotGridLinesColor()); g.setStroke(stroke); g.drawLine((int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY()), (int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + bounds.getHeight() - 1)); @@ -100,9 +99,9 @@ public class PlotSurface implements ChartPart { } @Override - public Chart getChart() { + public ChartPainter getChartPainter() { - return plot.getChart(); + return plot.getChartPainter(); } } -- GitLab