diff --git a/src/com/xeiam/swing/SwingWrapper.java b/src/com/xeiam/swing/SwingWrapper.java index b2154aaf0ab24aa31b573048b7f8b392f3901508..99dbed8558c8e85a2417a739a7433fddabcd5607 100644 --- a/src/com/xeiam/swing/SwingWrapper.java +++ b/src/com/xeiam/swing/SwingWrapper.java @@ -31,106 +31,115 @@ import com.xeiam.xcharts.Chart; */ public class SwingWrapper { - private Chart[] charts; - private int numRows; - private int numColumns; - - /** - * Constructor - * - * @param chart - */ - public SwingWrapper(Chart chart) { - this.charts = new Chart[1]; - charts[0] = chart; - } + private Chart[] charts; + private int numRows; + private int numColumns; + + /** + * Constructor + * + * @param chart + */ + public SwingWrapper(Chart chart) { + + this.charts = new Chart[1]; + charts[0] = chart; + } + + /** + * Constructor + * + * @param charts + */ + public SwingWrapper(Chart[] charts, int numRows, int numColumns) { + + this.charts = charts; + this.numRows = numRows; + this.numColumns = numColumns; + } + + /** + * Display the chart in a Swing JFrame + */ + public void displayChart() { + + // Schedule a job for the event-dispatching thread: + // creating and showing this application's GUI. + javax.swing.SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + + // Create and set up the window. + JFrame frame = new JFrame("XChart"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); + + JPanel chartPanel = new ChartJPanel(charts[0]); + frame.getContentPane().add(chartPanel); + + // Display the window. + frame.pack(); + frame.setVisible(true); + } + }); + } + + /** + * Display the chart in a Swing JFrame + */ + public void displayChartMatrix() { + + // Schedule a job for the event-dispatching thread: + // creating and showing this application's GUI. + javax.swing.SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + + // Create and set up the window. + JFrame frame = new JFrame("XChart"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.getContentPane().setLayout(new GridLayout(numRows, numColumns)); + + for (int i = 0; i < charts.length; i++) { + + if (charts[i] != null) { + JPanel chartPanel = new ChartJPanel(charts[i]); + frame.getContentPane().add(chartPanel); + } else { + JPanel chartPanel = new JPanel(); + frame.getContentPane().add(chartPanel); + } - /** - * Constructor - * - * @param charts - */ - public SwingWrapper(Chart[] charts, int numRows, int numColumns) { - this.charts = charts; - this.numRows = numRows; - this.numColumns = numColumns; - } + } - /** - * Display the chart in a Swing JFrame - */ - public void displayChart() { - - // Schedule a job for the event-dispatching thread: - // creating and showing this application's GUI. - javax.swing.SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - // Create and set up the window. - JFrame frame = new JFrame("XChart"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); - - JPanel chartPanel = new ChartJPanel(charts[0]); - frame.getContentPane().add(chartPanel); - - // Display the window. - frame.pack(); - frame.setVisible(true); - } - }); - } + // Display the window. + frame.pack(); + frame.setVisible(true); + } + }); + } - /** - * Display the chart in a Swing JFrame - */ - public void displayChartMatrix() { - - // Schedule a job for the event-dispatching thread: - // creating and showing this application's GUI. - javax.swing.SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - // Create and set up the window. - JFrame frame = new JFrame("XChart"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.getContentPane().setLayout(new GridLayout(numRows, numColumns)); - - for (int i = 0; i < charts.length; i++) { - - if (charts[i] != null) { - JPanel chartPanel = new ChartJPanel(charts[i]); - frame.getContentPane().add(chartPanel); - } else { - JPanel chartPanel = new JPanel(); - frame.getContentPane().add(chartPanel); - } - - } - - // Display the window. - frame.pack(); - frame.setVisible(true); - } - }); - } + private class ChartJPanel extends JPanel { - private class ChartJPanel extends JPanel { + private Chart chart; - private Chart chart; + public ChartJPanel(Chart chart) { - public ChartJPanel(Chart chart) { - this.chart = chart; - } + this.chart = chart; + } - @Override - public void paint(Graphics g) { - chart.paint((Graphics2D) g); - } + @Override + public void paint(Graphics g) { - @Override - public Dimension getPreferredSize() { - return new Dimension(chart.getWidth(), chart.getHeight()); - } + chart.paint((Graphics2D) g); + } + + @Override + public Dimension getPreferredSize() { + + return new Dimension(chart.getWidth(), chart.getHeight()); } + } } diff --git a/src/com/xeiam/xcharts/Axis.java b/src/com/xeiam/xcharts/Axis.java index 38888f258bd70c384863124bb11ae31219a74a77..e9ffd04f3099d8fc021043d13601db3fb232ee21 100644 --- a/src/com/xeiam/xcharts/Axis.java +++ b/src/com/xeiam/xcharts/Axis.java @@ -29,207 +29,216 @@ import com.xeiam.xcharts.interfaces.IChartPart; */ public class Axis implements IChartPart { - /** the chart */ - private Chart chart; + /** the chart */ + private Chart chart; - /** the axisPair */ - private AxisPair axisPair; + /** the axisPair */ + private AxisPair axisPair; - /** the axis title */ - private AxisTitle axisTitle; + /** the axis title */ + private AxisTitle axisTitle; - /** the axis tick */ - private AxisTick axisTick; + /** the axis tick */ + private AxisTick axisTick; - /** the grid */ - private AxisLine axisLine; + /** the grid */ + private AxisLine axisLine; - /** the axis direction */ - private Direction direction; + /** the axis direction */ + private Direction direction; - private Double min = null; + private Double min = null; - private Double max = null; + private Double max = null; - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - /** the paint zone */ - private Rectangle paintZone = new Rectangle(); // default all-zero rectangle + /** the paint zone */ + private Rectangle paintZone = new Rectangle(); // default all-zero rectangle - /** An axis direction */ - public enum Direction { + /** An axis direction */ + public enum Direction { - /** the constant to represent X axis */ - X, + /** the constant to represent X axis */ + X, - /** the constant to represent Y axis */ - Y - } + /** the constant to represent Y axis */ + Y + } - /** - * Constructor - * - * @param direction the axis direction (X or Y) - * @param chart the chart - */ - public Axis(Chart chart, AxisPair axisPair, Direction direction) { + /** + * Constructor + * + * @param direction the axis direction (X or Y) + * @param chart the chart + */ + public Axis(Chart chart, AxisPair axisPair, Direction direction) { + + this.chart = chart; + this.axisPair = axisPair; + this.direction = direction; - this.chart = chart; - this.axisPair = axisPair; - this.direction = direction; + axisTitle = new AxisTitle(this); + axisTick = new AxisTick(this); + axisLine = new AxisLine(this); + } - axisTitle = new AxisTitle(this); - axisTick = new AxisTick(this); - axisLine = new AxisLine(this); + /** + * @param min + * @param max + */ + public void addMinMax(double min, double max) { + + // System.out.println(min); + // System.out.println(max); + + if (this.min == null || min < this.min) { + this.min = min; + } + if (this.max == null || max > this.max) { + this.max = max; } - /** - * @param min - * @param max - */ - public void addMinMax(double min, double max) { + // System.out.println(this.min); + // System.out.println(this.max); + } - // System.out.println(min); - // System.out.println(max); + public Direction getDirection() { - if (this.min == null || min < this.min) { - this.min = min; - } - if (this.max == null || max > this.max) { - this.max = max; - } + return direction; + } - // System.out.println(this.min); - // System.out.println(this.max); - } + @Override + public Rectangle getBounds() { - public Direction getDirection() { - return direction; - } + return bounds; + } - @Override - public Rectangle getBounds() { - return bounds; - } + public Rectangle getPaintZone() { - public Rectangle getPaintZone() { - return paintZone; - } + return paintZone; + } - protected AxisTitle getAxisTitle() { - return axisTitle; - } + protected AxisTitle getAxisTitle() { - public void setAxisTitle(String title) { - this.axisTitle.setText(title); - } + return axisTitle; + } - public void setAxisTitle(AxisTitle axisTitle) { - this.axisTitle = axisTitle; - } + public void setAxisTitle(String title) { - public AxisTick getAxisTick() { - return axisTick; - } + this.axisTitle.setText(title); + } - public double getMin() { - return min; - } + public void setAxisTitle(AxisTitle axisTitle) { - public double getMax() { - return max; - } + this.axisTitle = axisTitle; + } + + public AxisTick getAxisTick() { + + return axisTick; + } + + public double getMin() { - /** + return min; + } + + public double getMax() { + + return max; + } + + /** * */ - public int getSizeHint() { - - if (direction == Direction.X) { // X-Axis - - // Axis title - double titleHeight = 0; - if (axisTitle.isVisible) { - TextLayout textLayout = new TextLayout(axisTitle.getText(), axisTitle.getFont(), new FontRenderContext(null, true, false)); - Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - titleHeight = rectangle.getHeight() + AxisTitle.AXIS_TITLE_PADDING; - } - - // Axis tick labels - TextLayout textLayout = new TextLayout("0", axisTick.getAxisTickLabels().getFont(), new FontRenderContext(null, true, false)); - Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - double axisTickLabelsHeight = rectangle.getHeight(); - - double gridStrokeWidth = axisLine.getStroke().getLineWidth(); - return (int) (titleHeight + axisTickLabelsHeight + AxisTick.AXIS_TICK_PADDING + AxisTickMarks.TICK_LENGTH + gridStrokeWidth + Plot.PLOT_PADDING); - } else { // Y-Axis - return 0; // We layout the yAxis first depending in the xAxis height hint. We don't care about the yAxis height hint - } + public int getSizeHint() { + + if (direction == Direction.X) { // X-Axis + + // Axis title + double titleHeight = 0; + if (axisTitle.isVisible) { + TextLayout textLayout = new TextLayout(axisTitle.getText(), axisTitle.getFont(), new FontRenderContext(null, true, false)); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + titleHeight = rectangle.getHeight() + AxisTitle.AXIS_TITLE_PADDING; + } + + // Axis tick labels + TextLayout textLayout = new TextLayout("0", axisTick.getAxisTickLabels().getFont(), new FontRenderContext(null, true, false)); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + double axisTickLabelsHeight = rectangle.getHeight(); + + double gridStrokeWidth = axisLine.getStroke().getLineWidth(); + return (int) (titleHeight + axisTickLabelsHeight + AxisTick.AXIS_TICK_PADDING + AxisTickMarks.TICK_LENGTH + gridStrokeWidth + Plot.PLOT_PADDING); + } else { // Y-Axis + return 0; // We layout the yAxis first depending in the xAxis height hint. We don't care about the yAxis height hint } - - @Override - public void paint(Graphics2D g) { - - // determine Axis bounds - if (direction == Direction.Y) { // Y-Axis - - // calculate paint zone - // ---- - // | - // | - // | - // | - // ---- - int xOffset = Chart.CHART_PADDING; - int yOffset = (int) (axisPair.getChartTitleBounds().getY() + axisPair.getChartTitleBounds().getHeight() + Chart.CHART_PADDING); - int width = 80; // arbitrary, final width depends on Axis tick labels - int height = chart.getHeight() - yOffset - axisPair.getXAxis().getSizeHint() - Chart.CHART_PADDING; - Rectangle yAxisRectangle = new Rectangle(xOffset, yOffset, width, height); - this.paintZone = yAxisRectangle; - // g.setColor(Color.green); - // g.draw(yAxisRectangle); - - // fill in Axis with sub-components - axisTitle.paint(g); - axisTick.paint(g); - axisLine.paint(g); - - xOffset = (int) paintZone.getX(); - yOffset = (int) paintZone.getY(); - width = (int) (axisTitle.isVisible ? axisTitle.getBounds().getWidth() : 0) + (int) axisTick.getBounds().getWidth() + (int) axisLine.getBounds().getWidth(); - height = (int) paintZone.getHeight(); - bounds = new Rectangle(xOffset, yOffset, width, height); - // g.setColor(Color.yellow); - // g.draw(bounds); - - } else { // X-Axis - - // calculate paint zone - // |____________________| - - int xOffset = (int) (axisPair.getYAxis().getBounds().getWidth() + Plot.PLOT_PADDING + Chart.CHART_PADDING - 1); - int yOffset = (int) (axisPair.getYAxis().getBounds().getY() + axisPair.getYAxis().getBounds().getHeight()); - int width = (int) (chart.getWidth() - axisPair.getYAxis().getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - 3 * Chart.CHART_PADDING); - int height = this.getSizeHint(); - Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height); - this.paintZone = xAxisRectangle; - // g.setColor(Color.green); - // g.draw(xAxisRectangle); - - axisTitle.paint(g); - axisTick.paint(g); - axisLine.paint(g); - - xOffset = (int) paintZone.getX(); - yOffset = (int) paintZone.getY(); - width = ((int) paintZone.getWidth()); - height = (int) (axisTitle.isVisible ? axisTitle.getBounds().getHeight() : 0 + axisTick.getBounds().getHeight() + axisLine.getBounds().getHeight()); - bounds = new Rectangle(xOffset, yOffset, width, height); - bounds = new Rectangle(xOffset, yOffset, width, height); - // g.setColor(Color.yellow); - // g.draw(bounds); - } - + } + + @Override + public void paint(Graphics2D g) { + + // determine Axis bounds + if (direction == Direction.Y) { // Y-Axis + + // calculate paint zone + // ---- + // | + // | + // | + // | + // ---- + int xOffset = Chart.CHART_PADDING; + int yOffset = (int) (axisPair.getChartTitleBounds().getY() + axisPair.getChartTitleBounds().getHeight() + Chart.CHART_PADDING); + int width = 80; // arbitrary, final width depends on Axis tick labels + int height = chart.getHeight() - yOffset - axisPair.getXAxis().getSizeHint() - Chart.CHART_PADDING; + Rectangle yAxisRectangle = new Rectangle(xOffset, yOffset, width, height); + this.paintZone = yAxisRectangle; + // g.setColor(Color.green); + // g.draw(yAxisRectangle); + + // fill in Axis with sub-components + axisTitle.paint(g); + axisTick.paint(g); + axisLine.paint(g); + + xOffset = (int) paintZone.getX(); + yOffset = (int) paintZone.getY(); + width = (int) (axisTitle.isVisible ? axisTitle.getBounds().getWidth() : 0) + (int) axisTick.getBounds().getWidth() + (int) axisLine.getBounds().getWidth(); + height = (int) paintZone.getHeight(); + bounds = new Rectangle(xOffset, yOffset, width, height); + // g.setColor(Color.yellow); + // g.draw(bounds); + + } else { // X-Axis + + // calculate paint zone + // |____________________| + + int xOffset = (int) (axisPair.getYAxis().getBounds().getWidth() + Plot.PLOT_PADDING + Chart.CHART_PADDING - 1); + int yOffset = (int) (axisPair.getYAxis().getBounds().getY() + axisPair.getYAxis().getBounds().getHeight()); + int width = (int) (chart.getWidth() - axisPair.getYAxis().getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - 3 * Chart.CHART_PADDING); + int height = this.getSizeHint(); + Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height); + this.paintZone = xAxisRectangle; + // g.setColor(Color.green); + // g.draw(xAxisRectangle); + + axisTitle.paint(g); + axisTick.paint(g); + axisLine.paint(g); + + xOffset = (int) paintZone.getX(); + yOffset = (int) paintZone.getY(); + width = ((int) paintZone.getWidth()); + height = (int) (axisTitle.isVisible ? axisTitle.getBounds().getHeight() : 0 + axisTick.getBounds().getHeight() + axisLine.getBounds().getHeight()); + bounds = new Rectangle(xOffset, yOffset, width, height); + bounds = new Rectangle(xOffset, yOffset, width, height); + // g.setColor(Color.yellow); + // g.draw(bounds); } + + } } diff --git a/src/com/xeiam/xcharts/AxisLine.java b/src/com/xeiam/xcharts/AxisLine.java index 563cd7d3e4d2ea89cbd1e71dde1b33b89a076120..ea08a18be2b3ac89e9d27c63e09a78bbffbc375b 100644 --- a/src/com/xeiam/xcharts/AxisLine.java +++ b/src/com/xeiam/xcharts/AxisLine.java @@ -27,76 +27,79 @@ import com.xeiam.xcharts.interfaces.IChartPart; */ public class AxisLine implements IChartPart { - /** the axis */ - private Axis axis; + /** the axis */ + private Axis axis; - /** the visibility state of grid */ - protected boolean isVisible = true; // default to true + /** the visibility state of grid */ + protected boolean isVisible = true; // default to true - /** the foreground color */ - private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default foreground color + /** the foreground color */ + private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default foreground color - /** the line style */ - private BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + /** the line style */ + private BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - /** - * Constructor - * - * @param axis the axis - */ - public AxisLine(Axis axis) { - this.axis = axis; + /** + * Constructor + * + * @param axis the axis + */ + public AxisLine(Axis axis) { - } + this.axis = axis; - public BasicStroke getStroke() { - return stroke; - } + } - @Override - public void paint(Graphics2D g) { + public BasicStroke getStroke() { - g.setColor(foreground); + return stroke; + } - if (axis.getDirection() == Axis.Direction.Y) { + @Override + public void paint(Graphics2D g) { - int xOffset = (int) (axis.getAxisTick().getBounds().getX() + axis.getAxisTick().getBounds().getWidth()); - int yOffset = (int) (axis.getPaintZone().getY()); + g.setColor(foreground); - g.setColor(foreground); - g.setStroke(stroke); + if (axis.getDirection() == Axis.Direction.Y) { - g.drawLine(xOffset, yOffset, xOffset, (int) (yOffset + axis.getPaintZone().getHeight())); + int xOffset = (int) (axis.getAxisTick().getBounds().getX() + axis.getAxisTick().getBounds().getWidth()); + int yOffset = (int) (axis.getPaintZone().getY()); - // bounds - bounds = new Rectangle(xOffset, yOffset, (int) stroke.getLineWidth(), (int) axis.getPaintZone().getHeight()); - // g.setColor(Color.green); - // g.draw(bounds); + g.setColor(foreground); + g.setStroke(stroke); - } else { + g.drawLine(xOffset, yOffset, xOffset, (int) (yOffset + axis.getPaintZone().getHeight())); - int xOffset = (int) (axis.getPaintZone().getX()); - int yOffset = (int) (axis.getAxisTick().getBounds().getY() - stroke.getLineWidth()); + // bounds + bounds = new Rectangle(xOffset, yOffset, (int) stroke.getLineWidth(), (int) axis.getPaintZone().getHeight()); + // g.setColor(Color.green); + // g.draw(bounds); - g.setColor(foreground); - g.setStroke(stroke); + } else { - g.drawLine(xOffset, yOffset, (int) (xOffset + axis.getPaintZone().getWidth()), yOffset); + int xOffset = (int) (axis.getPaintZone().getX()); + int yOffset = (int) (axis.getAxisTick().getBounds().getY() - stroke.getLineWidth()); - // bounds - bounds = new Rectangle(xOffset, yOffset, (int) axis.getPaintZone().getWidth(), (int) stroke.getLineWidth()); - // g.setColor(Color.green); - // g.draw(bounds); + g.setColor(foreground); + g.setStroke(stroke); - } - } + g.drawLine(xOffset, yOffset, (int) (xOffset + axis.getPaintZone().getWidth()), yOffset); + + // bounds + bounds = new Rectangle(xOffset, yOffset, (int) axis.getPaintZone().getWidth(), (int) stroke.getLineWidth()); + // g.setColor(Color.green); + // g.draw(bounds); - @Override - public Rectangle getBounds() { - // TODO Auto-generated method stub - return bounds; } + } + + @Override + public Rectangle getBounds() { + + // TODO Auto-generated method stub + return bounds; + } } diff --git a/src/com/xeiam/xcharts/AxisPair.java b/src/com/xeiam/xcharts/AxisPair.java index c44579a36a9721842df42a7d0a84ec3f37188dd4..28b8433617ef648066a41431e8880324f72a9b00 100644 --- a/src/com/xeiam/xcharts/AxisPair.java +++ b/src/com/xeiam/xcharts/AxisPair.java @@ -28,142 +28,149 @@ import com.xeiam.xcharts.series.Series; */ public class AxisPair implements IChartPart { - /** the chart */ - private Chart chart; + /** the chart */ + private Chart chart; - private Map<Integer, Series> seriesMap = new LinkedHashMap<Integer, Series>(); + private Map<Integer, Series> seriesMap = new LinkedHashMap<Integer, Series>(); - int seriesCount = 0; + int seriesCount = 0; - Axis xAxis; - Axis yAxis; + Axis xAxis; + Axis yAxis; - /** - * Constructor. - * - * @param chart the chart - */ - public AxisPair(Chart chart) { + /** + * Constructor. + * + * @param chart the chart + */ + public AxisPair(Chart chart) { - this.chart = chart; + this.chart = chart; - // add axes - xAxis = new Axis(chart, this, Axis.Direction.X); - yAxis = new Axis(chart, this, Axis.Direction.Y); - } + // add axes + xAxis = new Axis(chart, this, Axis.Direction.X); + yAxis = new Axis(chart, this, Axis.Direction.Y); + } - /** - * @param xData - * @param yData - */ - public Series addSeries(String seriesName, double[] xData, double[] yData) { - - // Sanity checks - if (seriesName == null) { - throw new RuntimeException("Series Name cannot be null!!!"); - } - if (yData == null) { - throw new RuntimeException("Y-Axis data cannot be null!!!"); - } - if (yData.length == 0) { - throw new RuntimeException("Y-Axis data cannot be empty!!!"); - } - if (xData != null && xData.length == 0) { - throw new RuntimeException("X-Axis data cannot be empty!!!"); - } - if (xData != null && xData.length == 1 && Double.isNaN(yData[0])) { - throw new RuntimeException("X-Axis data cannot contain a single NaN value!!!"); - } - if (yData.length == 1 && Double.isNaN(yData[0])) { - throw new RuntimeException("Y-Axis data cannot contain a single NaN value!!!"); - } - - Series series; - if (xData != null) { - verifyValues(xData); - verifyValues(yData); - series = new Series(seriesName, xData, yData); - } else { // generate xData - double[] generatedXData = new double[yData.length]; - verifyValues(yData); - for (int i = 1; i < yData.length; i++) { - generatedXData[i] = i; - } - series = new Series(seriesName, generatedXData, yData); - } - - // Sanity check - if (xData != null && xData.length != yData.length) { - throw new RuntimeException("X and Y-Axis lengths are not the same!!! "); - } - - seriesMap.put(seriesCount++, series); - - // add min/max to axis - xAxis.addMinMax(series.getxMin(), series.getxMax()); - yAxis.addMinMax(series.getyMin(), series.getyMax()); - - return series; - } + /** + * @param xData + * @param yData + */ + public Series addSeries(String seriesName, double[] xData, double[] yData) { - /** - * Checks for invalid values in data array - * - * @param data - */ - private void verifyValues(double[] data) { - - for (int i = 0; i < data.length; i++) { - if (data[i] == Double.POSITIVE_INFINITY) { - throw new RuntimeException("Axis data cannot contain Double.POSITIVE_INFINITY!!!"); - } else if (data[i] == Double.NEGATIVE_INFINITY) { - throw new RuntimeException("Axis data cannot contain Double.NEGATIVE_INFINITY!!!"); - } - } + // Sanity checks + if (seriesName == null) { + throw new RuntimeException("Series Name cannot be null!!!"); } - - protected Axis getXAxis() { - return xAxis; + if (yData == null) { + throw new RuntimeException("Y-Axis data cannot be null!!!"); } - - protected Axis getYAxis() { - return yAxis; + if (yData.length == 0) { + throw new RuntimeException("Y-Axis data cannot be empty!!!"); } - - protected Rectangle getChartTitleBounds() { - return chart.getTitle().getBounds(); + if (xData != null && xData.length == 0) { + throw new RuntimeException("X-Axis data cannot be empty!!!"); } - - protected Rectangle getChartLegendBounds() { - return chart.getLegend().getBounds(); + if (xData != null && xData.length == 1 && Double.isNaN(yData[0])) { + throw new RuntimeException("X-Axis data cannot contain a single NaN value!!!"); + } + if (yData.length == 1 && Double.isNaN(yData[0])) { + throw new RuntimeException("Y-Axis data cannot contain a single NaN value!!!"); } - protected Map<Integer, Series> getSeriesMap() { - return seriesMap; + Series series; + if (xData != null) { + verifyValues(xData); + verifyValues(yData); + series = new Series(seriesName, xData, yData); + } else { // generate xData + double[] generatedXData = new double[yData.length]; + verifyValues(yData); + for (int i = 1; i < yData.length; i++) { + generatedXData[i] = i; + } + series = new Series(seriesName, generatedXData, yData); } - public static int getTickSpace(int workingSpace) { - int tickSpace = (int) (workingSpace * 0.95); - return tickSpace; + // Sanity check + if (xData != null && xData.length != yData.length) { + throw new RuntimeException("X and Y-Axis lengths are not the same!!! "); } - public static int getMargin(int workingSpace, int tickSpace) { + seriesMap.put(seriesCount++, series); - int marginSpace = workingSpace - tickSpace; - int margin = (int) (marginSpace / 2.0); - return margin; - } + // add min/max to axis + xAxis.addMinMax(series.getxMin(), series.getxMax()); + yAxis.addMinMax(series.getyMin(), series.getyMax()); - @Override - public void paint(Graphics2D g) { + return series; + } - yAxis.paint(g); - xAxis.paint(g); - } + /** + * Checks for invalid values in data array + * + * @param data + */ + private void verifyValues(double[] data) { - @Override - public Rectangle getBounds() { - return null; // should never be called + for (int i = 0; i < data.length; i++) { + if (data[i] == Double.POSITIVE_INFINITY) { + throw new RuntimeException("Axis data cannot contain Double.POSITIVE_INFINITY!!!"); + } else if (data[i] == Double.NEGATIVE_INFINITY) { + throw new RuntimeException("Axis data cannot contain Double.NEGATIVE_INFINITY!!!"); + } } + } + + protected Axis getXAxis() { + + return xAxis; + } + + protected Axis getYAxis() { + + return yAxis; + } + + protected Rectangle getChartTitleBounds() { + + return chart.getTitle().getBounds(); + } + + protected Rectangle getChartLegendBounds() { + + return chart.getLegend().getBounds(); + } + + protected Map<Integer, Series> getSeriesMap() { + + return seriesMap; + } + + public static int getTickSpace(int workingSpace) { + + int tickSpace = (int) (workingSpace * 0.95); + return tickSpace; + } + + public static int getMargin(int workingSpace, int tickSpace) { + + int marginSpace = workingSpace - tickSpace; + int margin = (int) (marginSpace / 2.0); + return margin; + } + + @Override + public void paint(Graphics2D g) { + + yAxis.paint(g); + xAxis.paint(g); + } + + @Override + public Rectangle getBounds() { + + return null; // should never be called + } } diff --git a/src/com/xeiam/xcharts/AxisTick.java b/src/com/xeiam/xcharts/AxisTick.java index 0319132cb836eb7a65886e7b6cc1733711a6018f..785de05a1b9b3c6f484b917ec7235a963720b6e9 100644 --- a/src/com/xeiam/xcharts/AxisTick.java +++ b/src/com/xeiam/xcharts/AxisTick.java @@ -30,222 +30,229 @@ import com.xeiam.xcharts.interfaces.IChartPart; */ public class AxisTick implements IChartPart { - /** the axis */ - private Axis axis; + /** the axis */ + private Axis axis; - /** the axisticklabels */ - private AxisTickLabels axisTickLabels; + /** the axisticklabels */ + private AxisTickLabels axisTickLabels; - /** the axistickmarks */ - private AxisTickMarks axisTickMarks; + /** the axistickmarks */ + private AxisTickMarks axisTickMarks; - /** the arraylist of tick label position in pixels */ - private List<Integer> tickLocations = new LinkedList<Integer>(); + /** the arraylist of tick label position in pixels */ + private List<Integer> tickLocations = new LinkedList<Integer>(); - /** the arraylist of tick label vales */ - private List<String> tickLabels = new LinkedList<String>(); + /** the arraylist of tick label vales */ + private List<String> tickLabels = new LinkedList<String>(); - private int workingSpace; + private int workingSpace; - /** the default tick mark step hint */ - private static final int DEFAULT_TICK_MARK_STEP_HINT = 64; + /** the default tick mark step hint */ + private static final int DEFAULT_TICK_MARK_STEP_HINT = 64; - protected final static int AXIS_TICK_PADDING = 4; + protected final static int AXIS_TICK_PADDING = 4; - /** the normal format for tick labels */ - private Format normalFormat = new DecimalFormat("#.###########"); + /** the normal format for tick labels */ + private Format normalFormat = new DecimalFormat("#.###########"); - /** the scientific format for tick labels */ - private Format scientificFormat = new DecimalFormat("0.###E0"); + /** the scientific format for tick labels */ + private Format scientificFormat = new DecimalFormat("0.###E0"); - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - /** - * Constructor. - * - * @param chart the chart - * @param axis the axis - */ - protected AxisTick(Axis axis) { - this.axis = axis; - axisTickLabels = new AxisTickLabels(axis, this); - axisTickMarks = new AxisTickMarks(axis, this); + /** + * Constructor. + * + * @param chart the chart + * @param axis the axis + */ + protected AxisTick(Axis axis) { - } + this.axis = axis; + axisTickLabels = new AxisTickLabels(axis, this); + axisTickMarks = new AxisTickMarks(axis, this); - public AxisTickLabels getAxisTickLabels() { - return axisTickLabels; - } + } - public AxisTickMarks getAxisTickMarks() { - return axisTickMarks; - } + public AxisTickLabels getAxisTickLabels() { - public List<String> getTickLabels() { - return tickLabels; - } + return axisTickLabels; + } - public List<Integer> getTickLocations() { - return tickLocations; - } + public AxisTickMarks getAxisTickMarks() { - @Override - public Rectangle getBounds() { - return bounds; - } + return axisTickMarks; + } - protected int getWorkingSpace() { - return this.workingSpace; - } + public List<String> getTickLabels() { + + return tickLabels; + } + + public List<Integer> getTickLocations() { + + return tickLocations; + } - @Override - public void paint(Graphics2D g) { - - if (axis.getDirection() == Axis.Direction.Y) { - workingSpace = (int) axis.getPaintZone().getHeight(); // number of pixels the axis has to work with for drawing AxisTicks - // System.out.println("workingspace= " + workingSpace); - } else { - workingSpace = (int) axis.getPaintZone().getWidth(); // number of pixels the axis has to work with for drawing AxisTicks - // System.out.println("workingspace= " + workingSpace); - } - - determineAxisTick(); - - // for (Integer position : tickLocations) { - // System.out.println(position); - // } - // for (String label : tickLabels) { - // System.out.println(label); - // } - - axisTickLabels.paint(g); - axisTickMarks.paint(g); - - if (axis.getDirection() == Axis.Direction.Y) { - bounds = new Rectangle((int) axisTickLabels.getBounds().getX(), (int) (axisTickLabels.getBounds().getY()), (int) (axisTickLabels.getBounds().getWidth() + AXIS_TICK_PADDING + 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() - + AXIS_TICK_PADDING + axisTickLabels.getBounds().getHeight())); - // g.setColor(Color.red); - // g.draw(bounds); - } + @Override + public Rectangle getBounds() { + return bounds; + } + + protected int getWorkingSpace() { + + return this.workingSpace; + } + + @Override + public void paint(Graphics2D g) { + + if (axis.getDirection() == Axis.Direction.Y) { + workingSpace = (int) axis.getPaintZone().getHeight(); // number of pixels the axis has to work with for drawing AxisTicks + // System.out.println("workingspace= " + workingSpace); + } else { + workingSpace = (int) axis.getPaintZone().getWidth(); // number of pixels the axis has to work with for drawing AxisTicks + // System.out.println("workingspace= " + workingSpace); + } + + determineAxisTick(); + + // for (Integer position : tickLocations) { + // System.out.println(position); + // } + // for (String label : tickLabels) { + // System.out.println(label); + // } + + axisTickLabels.paint(g); + axisTickMarks.paint(g); + + if (axis.getDirection() == Axis.Direction.Y) { + bounds = new Rectangle((int) axisTickLabels.getBounds().getX(), (int) (axisTickLabels.getBounds().getY()), (int) (axisTickLabels.getBounds().getWidth() + AXIS_TICK_PADDING + 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() + + AXIS_TICK_PADDING + axisTickLabels.getBounds().getHeight())); + // g.setColor(Color.red); + // g.draw(bounds); } - /** + } + + /** * */ - private void determineAxisTick() { + private void determineAxisTick() { - // System.out.println("workingSpace= " + workingSpace); + // System.out.println("workingSpace= " + workingSpace); - int tickSpace = AxisPair.getTickSpace(workingSpace); - // System.out.println("tickSpace= " + tickSpace); + int tickSpace = AxisPair.getTickSpace(workingSpace); + // System.out.println("tickSpace= " + tickSpace); - int margin = AxisPair.getMargin(workingSpace, tickSpace); + int margin = AxisPair.getMargin(workingSpace, tickSpace); - // a check if all axis data are the exact same values - if (axis.getMax() == axis.getMin()) { - tickLabels.add(format(axis.getMax())); - tickLocations.add((int) (margin + tickSpace / 2.0)); - } else { + // a check if all axis data are the exact same values + if (axis.getMax() == axis.getMin()) { + tickLabels.add(format(axis.getMax())); + tickLocations.add((int) (margin + tickSpace / 2.0)); + } else { - final BigDecimal MIN = new BigDecimal(new Double(axis.getMin()).toString()); - BigDecimal firstPosition; - BigDecimal gridStep = getGridStep(tickSpace); + final BigDecimal MIN = new BigDecimal(new Double(axis.getMin()).toString()); + BigDecimal firstPosition; + BigDecimal gridStep = getGridStep(tickSpace); - double xyz = MIN.remainder(gridStep).doubleValue(); - if (xyz <= 0.0) { - firstPosition = MIN.subtract(MIN.remainder(gridStep)); - } else { - firstPosition = MIN.subtract(MIN.remainder(gridStep)).add(gridStep); - } + double xyz = MIN.remainder(gridStep).doubleValue(); + if (xyz <= 0.0) { + firstPosition = MIN.subtract(MIN.remainder(gridStep)); + } else { + firstPosition = MIN.subtract(MIN.remainder(gridStep)).add(gridStep); + } - for (BigDecimal b = firstPosition; b.doubleValue() <= axis.getMax(); b = b.add(gridStep)) { + for (BigDecimal b = firstPosition; b.doubleValue() <= axis.getMax(); b = b.add(gridStep)) { - // System.out.println("b= " + b); - tickLabels.add(format(b.doubleValue())); - int tickLabelPosition = (int) (margin + ((b.doubleValue() - axis.getMin()) / (axis.getMax() - axis.getMin()) * tickSpace)); - // System.out.println("tickLabelPosition= " + tickLabelPosition); + // System.out.println("b= " + b); + tickLabels.add(format(b.doubleValue())); + int tickLabelPosition = (int) (margin + ((b.doubleValue() - axis.getMin()) / (axis.getMax() - axis.getMin()) * tickSpace)); + // System.out.println("tickLabelPosition= " + tickLabelPosition); - tickLocations.add(tickLabelPosition); - } - } + tickLocations.add(tickLabelPosition); + } } - - private BigDecimal getGridStep(int tickSpace) { - - double length = Math.abs(axis.getMax() - axis.getMin()); - // System.out.println(axis.getMax()); - // System.out.println(axis.getMin()); - // System.out.println(length); - double gridStepHint = length / tickSpace * DEFAULT_TICK_MARK_STEP_HINT; - - // gridStepHint --> mantissa * 10 ** exponent - // e.g. 724.1 --> 7.241 * 10 ** 2 - double mantissa = gridStepHint; - int exponent = 0; - if (mantissa == 0) { - exponent = 1; - } else if (mantissa < 1) { - while (mantissa < 1) { - mantissa *= 10.0; - exponent--; - } - } else { - while (mantissa >= 10) { - mantissa /= 10.0; - exponent++; - } - } - - // calculate the grid step with hint. - BigDecimal gridStep; - if (mantissa > 7.5) { - // gridStep = 10.0 * 10 ** exponent - gridStep = BigDecimal.TEN.multiply(pow(10, exponent)); - } else if (mantissa > 3.5) { - // gridStep = 5.0 * 10 ** exponent - gridStep = new BigDecimal(new Double(5).toString()).multiply(pow(10, exponent)); - } else if (mantissa > 1.5) { - // gridStep = 2.0 * 10 ** exponent - gridStep = new BigDecimal(new Double(2).toString()).multiply(pow(10, exponent)); - } else { - // gridStep = 1.0 * 10 ** exponent - gridStep = pow(10, exponent); - } - return gridStep; + } + + private BigDecimal getGridStep(int tickSpace) { + + double length = Math.abs(axis.getMax() - axis.getMin()); + // System.out.println(axis.getMax()); + // System.out.println(axis.getMin()); + // System.out.println(length); + double gridStepHint = length / tickSpace * DEFAULT_TICK_MARK_STEP_HINT; + + // gridStepHint --> mantissa * 10 ** exponent + // e.g. 724.1 --> 7.241 * 10 ** 2 + double mantissa = gridStepHint; + int exponent = 0; + if (mantissa == 0) { + exponent = 1; + } else if (mantissa < 1) { + while (mantissa < 1) { + mantissa *= 10.0; + exponent--; + } + } else { + while (mantissa >= 10) { + mantissa /= 10.0; + exponent++; + } } - /** - * Calculates the value of the first argument raised to the power of the second argument. - * - * @param base the base - * @param exponent the exponent - * @return the value <tt>a<sup>b</sup></tt> in <tt>BigDecimal</tt> - */ - private BigDecimal pow(double base, int exponent) { - - BigDecimal value; - if (exponent > 0) { - value = new BigDecimal(new Double(base).toString()).pow(exponent); - } else { - value = BigDecimal.ONE.divide(new BigDecimal(new Double(base).toString()).pow(-exponent)); - } - return value; + // calculate the grid step with hint. + BigDecimal gridStep; + if (mantissa > 7.5) { + // gridStep = 10.0 * 10 ** exponent + gridStep = BigDecimal.TEN.multiply(pow(10, exponent)); + } else if (mantissa > 3.5) { + // gridStep = 5.0 * 10 ** exponent + gridStep = new BigDecimal(new Double(5).toString()).multiply(pow(10, exponent)); + } else if (mantissa > 1.5) { + // gridStep = 2.0 * 10 ** exponent + gridStep = new BigDecimal(new Double(2).toString()).multiply(pow(10, exponent)); + } else { + // gridStep = 1.0 * 10 ** exponent + gridStep = pow(10, exponent); + } + return gridStep; + } + + /** + * Calculates the value of the first argument raised to the power of the second argument. + * + * @param base the base + * @param exponent the exponent + * @return the value <tt>a<sup>b</sup></tt> in <tt>BigDecimal</tt> + */ + private BigDecimal pow(double base, int exponent) { + + BigDecimal value; + if (exponent > 0) { + value = new BigDecimal(new Double(base).toString()).pow(exponent); + } else { + value = BigDecimal.ONE.divide(new BigDecimal(new Double(base).toString()).pow(-exponent)); } + return value; + } - private String format(double value) { + private String format(double value) { - if (Math.abs(value) < 9999 && Math.abs(value) > .0001 || value == 0) { - return this.normalFormat.format(value); - } else { - return this.scientificFormat.format(value); - } + if (Math.abs(value) < 9999 && Math.abs(value) > .0001 || value == 0) { + return this.normalFormat.format(value); + } else { + return this.scientificFormat.format(value); } + } } diff --git a/src/com/xeiam/xcharts/AxisTickLabels.java b/src/com/xeiam/xcharts/AxisTickLabels.java index e6254c57f8c70cb1c4c5fef6f868eb3b7db72239..0e63764c98ca148bc82e8a6449fb3e925a32e702 100644 --- a/src/com/xeiam/xcharts/AxisTickLabels.java +++ b/src/com/xeiam/xcharts/AxisTickLabels.java @@ -29,94 +29,96 @@ import com.xeiam.xcharts.interfaces.IChartPart; */ public class AxisTickLabels implements IChartPart { - /** the axis */ - private Axis axis; + /** the axis */ + private Axis axis; - private AxisTick axisTick; + private AxisTick axisTick; - /** the font */ - private Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12); // default font + /** the font */ + private Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12); // default font - /** the foreground color */ - private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY);// default foreground color + /** the foreground color */ + private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY);// default foreground color - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - /** - * Constructor - * - * @param axis the axis - */ - protected AxisTickLabels(Axis axis, AxisTick axisTick) { + /** + * Constructor + * + * @param axis the axis + */ + protected AxisTickLabels(Axis axis, AxisTick axisTick) { - this.axis = axis; - this.axisTick = axisTick; - } + this.axis = axis; + this.axisTick = axisTick; + } - public Font getFont() { - return font; - } + public Font getFont() { - @Override - public Rectangle getBounds() { - return bounds; - } + return font; + } - @Override - public void paint(Graphics2D g) { + @Override + public Rectangle getBounds() { - g.setColor(foreground); + return bounds; + } - if (axis.getDirection() == Axis.Direction.Y) { // Y-Axis + @Override + public void paint(Graphics2D g) { - int xOffset = (int) (axis.getAxisTitle().getBounds().getX() + axis.getAxisTitle().getBounds().getWidth()); - int yOffset = (int) (axis.getPaintZone().getY()); - int maxTickLabelWidth = 0; - for (int i = 0; i < axisTick.getTickLabels().size(); i++) { + g.setColor(foreground); - String tickLabel = axisTick.getTickLabels().get(i); - int tickLocation = axisTick.getTickLocations().get(i); + if (axis.getDirection() == Axis.Direction.Y) { // Y-Axis - TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false)); - Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); - layout.draw(g, xOffset, (int) (yOffset + axis.getPaintZone().getHeight() - tickLocation + tickLabelBounds.getHeight() / 2.0)); + int xOffset = (int) (axis.getAxisTitle().getBounds().getX() + axis.getAxisTitle().getBounds().getWidth()); + int yOffset = (int) (axis.getPaintZone().getY()); + int maxTickLabelWidth = 0; + for (int i = 0; i < axisTick.getTickLabels().size(); i++) { - if (tickLabelBounds.getWidth() > maxTickLabelWidth) { - maxTickLabelWidth = (int) tickLabelBounds.getWidth(); - } - } + String tickLabel = axisTick.getTickLabels().get(i); + int tickLocation = axisTick.getTickLocations().get(i); - // bounds - bounds = new Rectangle(xOffset, yOffset, maxTickLabelWidth, (int) axis.getPaintZone().getHeight()); - // g.setColor(Color.blue); - // g.draw(bounds); + TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false)); + Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); + layout.draw(g, xOffset, (int) (yOffset + axis.getPaintZone().getHeight() - tickLocation + tickLabelBounds.getHeight() / 2.0)); - } else { // X-Axis + if (tickLabelBounds.getWidth() > maxTickLabelWidth) { + maxTickLabelWidth = (int) tickLabelBounds.getWidth(); + } + } - int xOffset = (int) (axis.getPaintZone().getX()); - int yOffset = (int) (axis.getAxisTitle().getBounds().getY()); - int maxTickLabelHeight = 0; - for (int i = 0; i < axisTick.getTickLabels().size(); i++) { + // bounds + bounds = new Rectangle(xOffset, yOffset, maxTickLabelWidth, (int) axis.getPaintZone().getHeight()); + // g.setColor(Color.blue); + // g.draw(bounds); - String tickLabel = axisTick.getTickLabels().get(i); - int tickLocation = axisTick.getTickLocations().get(i); + } else { // X-Axis - TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false)); - Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); - layout.draw(g, (int) (xOffset + tickLocation - tickLabelBounds.getWidth() / 2.0), yOffset); + int xOffset = (int) (axis.getPaintZone().getX()); + int yOffset = (int) (axis.getAxisTitle().getBounds().getY()); + int maxTickLabelHeight = 0; + for (int i = 0; i < axisTick.getTickLabels().size(); i++) { - if (tickLabelBounds.getHeight() > maxTickLabelHeight) { - maxTickLabelHeight = (int) tickLabelBounds.getHeight(); - } - } + String tickLabel = axisTick.getTickLabels().get(i); + int tickLocation = axisTick.getTickLocations().get(i); - // bounds - bounds = new Rectangle(xOffset, yOffset - maxTickLabelHeight, (int) axis.getPaintZone().getWidth(), maxTickLabelHeight); - // g.setColor(Color.blue); - // g.draw(bounds); + TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false)); + Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); + layout.draw(g, (int) (xOffset + tickLocation - tickLabelBounds.getWidth() / 2.0), yOffset); + if (tickLabelBounds.getHeight() > maxTickLabelHeight) { + maxTickLabelHeight = (int) tickLabelBounds.getHeight(); } + } + + // bounds + bounds = new Rectangle(xOffset, yOffset - maxTickLabelHeight, (int) axis.getPaintZone().getWidth(), maxTickLabelHeight); + // g.setColor(Color.blue); + // g.draw(bounds); } + + } } diff --git a/src/com/xeiam/xcharts/AxisTickMarks.java b/src/com/xeiam/xcharts/AxisTickMarks.java index 4e1e4c40160c58c20551d2f3078e5aa06ee6e1fa..e8c2f31c112c1624a7ec0bca628ce505b70179b3 100644 --- a/src/com/xeiam/xcharts/AxisTickMarks.java +++ b/src/com/xeiam/xcharts/AxisTickMarks.java @@ -28,83 +28,85 @@ import com.xeiam.xcharts.interfaces.IChartPart; */ public class AxisTickMarks implements IChartPart { - /** the axis */ - private Axis axis; + /** the axis */ + private Axis axis; - private AxisTick axisTick; + private AxisTick axisTick; - /** the foreground color */ - private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY);// default foreground color + /** the foreground color */ + private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY);// default foreground color - /** the line style */ - private Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + /** the line style */ + private Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); - /** the tick length */ - public static final int TICK_LENGTH = 3; + /** the tick length */ + public static final int TICK_LENGTH = 3; - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - /** - * Constructor - * - * @param axis - * @param axisTick - */ - public AxisTickMarks(Axis axis, AxisTick axisTick) { - this.axis = axis; - this.axisTick = axisTick; - } + /** + * Constructor + * + * @param axis + * @param axisTick + */ + public AxisTickMarks(Axis axis, AxisTick axisTick) { - @Override - public Rectangle getBounds() { - return bounds; - } + this.axis = axis; + this.axisTick = axisTick; + } - @Override - public void paint(Graphics2D g) { + @Override + public Rectangle getBounds() { - g.setColor(foreground); + return bounds; + } - if (axis.getDirection() == Axis.Direction.Y) { // Y-Axis + @Override + public void paint(Graphics2D g) { - int xOffset = (int) (axisTick.getAxisTickLabels().getBounds().getX() + axisTick.getAxisTickLabels().getBounds().getWidth() + AxisTick.AXIS_TICK_PADDING); - int yOffset = (int) (axis.getPaintZone().getY()); - for (int i = 0; i < axisTick.getTickLabels().size(); i++) { + g.setColor(foreground); - int tickLocation = axisTick.getTickLocations().get(i); + if (axis.getDirection() == Axis.Direction.Y) { // Y-Axis - g.setColor(foreground); - g.setStroke(stroke); + int xOffset = (int) (axisTick.getAxisTickLabels().getBounds().getX() + axisTick.getAxisTickLabels().getBounds().getWidth() + AxisTick.AXIS_TICK_PADDING); + int yOffset = (int) (axis.getPaintZone().getY()); + for (int i = 0; i < axisTick.getTickLabels().size(); i++) { - g.drawLine(xOffset, yOffset + (int) (axis.getPaintZone().getHeight() - tickLocation), xOffset + TICK_LENGTH, yOffset + (int) (axis.getPaintZone().getHeight() - tickLocation)); + int tickLocation = axisTick.getTickLocations().get(i); - } + g.setColor(foreground); + g.setStroke(stroke); - // bounds - bounds = new Rectangle(xOffset, yOffset, TICK_LENGTH, (int) axis.getPaintZone().getHeight()); - // g.setColor(Color.blue); - // g.draw(bounds); + g.drawLine(xOffset, yOffset + (int) (axis.getPaintZone().getHeight() - tickLocation), xOffset + TICK_LENGTH, yOffset + (int) (axis.getPaintZone().getHeight() - tickLocation)); - } else { // X-Axis + } - int xOffset = (int) (axis.getPaintZone().getX()); - int yOffset = (int) (axisTick.getAxisTickLabels().getBounds().getY() - AxisTick.AXIS_TICK_PADDING); - for (int i = 0; i < axisTick.getTickLabels().size(); i++) { + // bounds + bounds = new Rectangle(xOffset, yOffset, TICK_LENGTH, (int) axis.getPaintZone().getHeight()); + // g.setColor(Color.blue); + // g.draw(bounds); - int tickLocation = axisTick.getTickLocations().get(i); + } else { // X-Axis - g.setColor(foreground); - g.setStroke(stroke); + int xOffset = (int) (axis.getPaintZone().getX()); + int yOffset = (int) (axisTick.getAxisTickLabels().getBounds().getY() - AxisTick.AXIS_TICK_PADDING); + for (int i = 0; i < axisTick.getTickLabels().size(); i++) { + + int tickLocation = axisTick.getTickLocations().get(i); + + g.setColor(foreground); + g.setStroke(stroke); - g.drawLine(xOffset + tickLocation, yOffset, xOffset + tickLocation, yOffset - TICK_LENGTH); - } + g.drawLine(xOffset + tickLocation, yOffset, xOffset + tickLocation, yOffset - TICK_LENGTH); + } - // bounds - bounds = new Rectangle(xOffset, yOffset - TICK_LENGTH, (int) axis.getPaintZone().getWidth(), TICK_LENGTH); - // g.setColor(Color.blue); - // g.draw(bounds); - } + // bounds + bounds = new Rectangle(xOffset, yOffset - TICK_LENGTH, (int) axis.getPaintZone().getWidth(), TICK_LENGTH); + // g.setColor(Color.blue); + // g.draw(bounds); } + } } diff --git a/src/com/xeiam/xcharts/AxisTitle.java b/src/com/xeiam/xcharts/AxisTitle.java index a4a6d718fd70d2edcab907cdd5c24120513782b6..03de73ed6c87487ea7112ba58e90e4855d1a3951 100644 --- a/src/com/xeiam/xcharts/AxisTitle.java +++ b/src/com/xeiam/xcharts/AxisTitle.java @@ -30,118 +30,123 @@ import com.xeiam.xcharts.interfaces.IHideable; */ public class AxisTitle implements IHideable { - /** the chart */ - private Axis axis; + /** the chart */ + private Axis axis; - /** the title text */ - protected String text = ""; // default to "" + /** the title text */ + protected String text = ""; // default to "" - /** the visibility state of title */ - protected boolean isVisible = false; // default to false + /** the visibility state of title */ + protected boolean isVisible = false; // default to false - /** the font */ - private Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12); // default font + /** the font */ + private Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12); // default font - /** the foreground color */ - private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default foreground color + /** the foreground color */ + private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default foreground color - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - protected final static int AXIS_TITLE_PADDING = 10; + protected final static int AXIS_TITLE_PADDING = 10; - /** - * Constructor. - * - * @param axis the axis - */ - public AxisTitle(Axis axis) { + /** + * Constructor. + * + * @param axis the axis + */ + public AxisTitle(Axis axis) { - this.axis = axis; - } + this.axis = axis; + } - protected String getText() { - return text; - } + protected String getText() { - protected void setText(String text) { - if (text.trim().equalsIgnoreCase("")) { - this.isVisible = false; - } else { - this.isVisible = true; - } - this.text = text; - } + return text; + } - protected Font getFont() { - return font; - } + protected void setText(String text) { - @Override - public void setVisible(boolean isVisible) { - this.isVisible = isVisible; + if (text.trim().equalsIgnoreCase("")) { + this.isVisible = false; + } else { + this.isVisible = true; } + this.text = text; + } - @Override - public Rectangle getBounds() { - return bounds; - } + protected Font getFont() { + + return font; + } + + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + } + + @Override + public Rectangle getBounds() { + + return bounds; + } - @Override - public void paint(Graphics2D g) { + @Override + public void paint(Graphics2D g) { - g.setColor(foreground); + g.setColor(foreground); - if (axis.getDirection() == Axis.Direction.Y) { - if (isVisible && !this.text.trim().equalsIgnoreCase("")) { + if (axis.getDirection() == Axis.Direction.Y) { + if (isVisible && !this.text.trim().equalsIgnoreCase("")) { - FontRenderContext frc = g.getFontRenderContext(); + FontRenderContext frc = g.getFontRenderContext(); - TextLayout nonRotatedTextLayout = new TextLayout(this.text, this.font, frc); - Rectangle nonRotatedRectangle = nonRotatedTextLayout.getPixelBounds(null, 0, 0); - // System.out.println(nonRotatedRectangle); + TextLayout nonRotatedTextLayout = new TextLayout(this.text, this.font, frc); + Rectangle nonRotatedRectangle = nonRotatedTextLayout.getPixelBounds(null, 0, 0); + // System.out.println(nonRotatedRectangle); - TextLayout rotatedTextLayout = new TextLayout(this.text, this.font.deriveFont(AffineTransform.getRotateInstance(Math.PI / -2.0, 0, 0)), frc); - // Rectangle rotatedRectangle = rotatedTextLayout.getPixelBounds(null, 0, 0); - // System.out.println(rotatedRectangle); + TextLayout rotatedTextLayout = new TextLayout(this.text, this.font.deriveFont(AffineTransform.getRotateInstance(Math.PI / -2.0, 0, 0)), frc); + // Rectangle rotatedRectangle = rotatedTextLayout.getPixelBounds(null, 0, 0); + // System.out.println(rotatedRectangle); - int xOffset = (int) (axis.getPaintZone().getX() + nonRotatedRectangle.getHeight()); - int yOffset = (int) ((axis.getPaintZone().getHeight() + nonRotatedRectangle.getWidth()) / 2.0 + axis.getPaintZone().getY()); - rotatedTextLayout.draw(g, xOffset, yOffset); + int xOffset = (int) (axis.getPaintZone().getX() + nonRotatedRectangle.getHeight()); + int yOffset = (int) ((axis.getPaintZone().getHeight() + nonRotatedRectangle.getWidth()) / 2.0 + axis.getPaintZone().getY()); + rotatedTextLayout.draw(g, xOffset, yOffset); - // bounds - bounds = new Rectangle((int) (xOffset - nonRotatedRectangle.getHeight()), (int) (yOffset - nonRotatedRectangle.getWidth()), (int) nonRotatedRectangle.getHeight() + AXIS_TITLE_PADDING, - (int) nonRotatedRectangle.getWidth()); - // g.setColor(Color.blue); - // g.draw(bounds); - } else { - bounds = new Rectangle((int) axis.getPaintZone().getX(), (int) axis.getPaintZone().getY(), 0, (int) axis.getPaintZone().getHeight()); - } + // bounds + bounds = new Rectangle((int) (xOffset - nonRotatedRectangle.getHeight()), (int) (yOffset - nonRotatedRectangle.getWidth()), (int) nonRotatedRectangle.getHeight() + AXIS_TITLE_PADDING, + (int) nonRotatedRectangle.getWidth()); + // g.setColor(Color.blue); + // g.draw(bounds); + } else { + bounds = new Rectangle((int) axis.getPaintZone().getX(), (int) axis.getPaintZone().getY(), 0, (int) axis.getPaintZone().getHeight()); + } - } else { + } else { - if (isVisible) { + if (isVisible) { - FontRenderContext frc = g.getFontRenderContext(); - TextLayout textLayout = new TextLayout(this.text, this.font, frc); - Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - // System.out.println(rectangle); + FontRenderContext frc = g.getFontRenderContext(); + TextLayout textLayout = new TextLayout(this.text, this.font, frc); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + // System.out.println(rectangle); - int xOffset = (int) (axis.getPaintZone().getX() + (axis.getPaintZone().getWidth() - rectangle.getWidth()) / 2.0); - int yOffset = (int) (axis.getPaintZone().getY() + axis.getPaintZone().getHeight() - rectangle.getHeight()); + int xOffset = (int) (axis.getPaintZone().getX() + (axis.getPaintZone().getWidth() - rectangle.getWidth()) / 2.0); + int yOffset = (int) (axis.getPaintZone().getY() + axis.getPaintZone().getHeight() - rectangle.getHeight()); - textLayout.draw(g, xOffset, (float) (yOffset - rectangle.getY())); + textLayout.draw(g, xOffset, (float) (yOffset - rectangle.getY())); - bounds = new Rectangle(xOffset, yOffset - AXIS_TITLE_PADDING, (int) rectangle.getWidth(), (int) rectangle.getHeight() + AXIS_TITLE_PADDING); - // g.setColor(Color.blue); - // g.draw(bounds); + bounds = new Rectangle(xOffset, yOffset - AXIS_TITLE_PADDING, (int) rectangle.getWidth(), (int) rectangle.getHeight() + AXIS_TITLE_PADDING); + // g.setColor(Color.blue); + // g.draw(bounds); - } else { - bounds = new Rectangle((int) axis.getPaintZone().getX(), (int) (axis.getPaintZone().getY() + axis.getPaintZone().getHeight()), (int) axis.getPaintZone().getWidth(), 0); - // g.setColor(Color.blue); - // g.draw(bounds); + } else { + bounds = new Rectangle((int) axis.getPaintZone().getX(), (int) (axis.getPaintZone().getY() + axis.getPaintZone().getHeight()), (int) axis.getPaintZone().getWidth(), 0); + // g.setColor(Color.blue); + // g.draw(bounds); - } - } + } } + } } diff --git a/src/com/xeiam/xcharts/BitmapEncoder.java b/src/com/xeiam/xcharts/BitmapEncoder.java index 44ceaf15fc96c1809ad35d2e5566f5f66ff48402..4f99681b72fc42dd9d27f0660aef773dde80d0f6 100644 --- a/src/com/xeiam/xcharts/BitmapEncoder.java +++ b/src/com/xeiam/xcharts/BitmapEncoder.java @@ -28,37 +28,37 @@ import javax.servlet.ServletOutputStream; */ public class BitmapEncoder { - /** - * Saves a chart as a PNG file - * - * @param chart - * @param pFileName - */ - public static void savePNG(Chart chart, String pFileName) throws Exception { + /** + * Saves a chart as a PNG file + * + * @param chart + * @param pFileName + */ + public static void savePNG(Chart chart, String pFileName) throws Exception { - BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB); - Graphics2D lGraphics2D = lBufferedImage.createGraphics(); - chart.paint(lGraphics2D); + BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics2D lGraphics2D = lBufferedImage.createGraphics(); + chart.paint(lGraphics2D); - // Save chart as PNG - OutputStream out = new FileOutputStream(pFileName); - ImageIO.write(lBufferedImage, "png", out); - out.close(); - } + // Save chart as PNG + OutputStream out = new FileOutputStream(pFileName); + ImageIO.write(lBufferedImage, "png", out); + out.close(); + } - /** - * Streams a chart as a PNG file - * - * @param out - * @param chart - */ - public static void streamPNG(ServletOutputStream out, Chart chart) throws Exception { + /** + * Streams a chart as a PNG file + * + * @param out + * @param chart + */ + public static void streamPNG(ServletOutputStream out, Chart chart) throws Exception { - BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB); - Graphics2D lGraphics2D = lBufferedImage.createGraphics(); - chart.paint(lGraphics2D); + BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics2D lGraphics2D = lBufferedImage.createGraphics(); + chart.paint(lGraphics2D); - ImageIO.write(lBufferedImage, "png", out); - out.close(); - } + ImageIO.write(lBufferedImage, "png", out); + out.close(); + } } diff --git a/src/com/xeiam/xcharts/Chart.java b/src/com/xeiam/xcharts/Chart.java index 1722e90312171fc3ff64ee8f594748418bdbf1b9..b0f6c87eac6d0c46a9bce839f1a2a62f073aff60 100644 --- a/src/com/xeiam/xcharts/Chart.java +++ b/src/com/xeiam/xcharts/Chart.java @@ -28,106 +28,120 @@ import com.xeiam.xcharts.series.SeriesMarker; */ public class Chart { - private int width; - private int height; - - protected final static int CHART_PADDING = 10; - - private ChartTitle chartTitle = new ChartTitle(this); - private ChartLegend chartLegend = new ChartLegend(this); - private AxisPair axisPair = new AxisPair(this); - private Plot plot = new Plot(this); - - /** - * Constructor - * - * @param pWidth - * @param pHeight - */ - public Chart(final int pWidth, final int pHeight) { - width = pWidth; - height = pHeight; + private int width; + private int height; + + protected final static int CHART_PADDING = 10; + + private ChartTitle chartTitle = new ChartTitle(this); + private ChartLegend chartLegend = new ChartLegend(this); + private AxisPair axisPair = new AxisPair(this); + private Plot plot = new Plot(this); + + /** + * Constructor + * + * @param pWidth + * @param pHeight + */ + public Chart(final int pWidth, final int pHeight) { + + width = pWidth; + height = pHeight; + } + + public void paint(final Graphics2D g) { + + // Sanity check + if (axisPair.getSeriesMap().isEmpty()) { + throw new RuntimeException("No series defined for Chart!!!"); } - public void paint(final Graphics2D g) { + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // global rendering hint + g.setColor(ChartColor.getAWTColor(ChartColor.GREY)); + g.fillRect(0, 0, width, height); - // Sanity check - if (axisPair.getSeriesMap().isEmpty()) { - throw new RuntimeException("No series defined for Chart!!!"); - } + chartTitle.paint(g); + chartLegend.paint(g); + axisPair.paint(g); + plot.paint(g); - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // global rendering hint - g.setColor(ChartColor.getAWTColor(ChartColor.GREY)); - g.fillRect(0, 0, width, height); + g.dispose(); - chartTitle.paint(g); - chartLegend.paint(g); - axisPair.paint(g); - plot.paint(g); + // reset static Ids + SeriesColor.resetId(); + SeriesLineStyle.resetId(); + SeriesMarker.resetId(); - g.dispose(); + } - // reset static Ids - SeriesColor.resetId(); - SeriesLineStyle.resetId(); - SeriesMarker.resetId(); + // GETTERS & SETTERS - } + public int getWidth() { - // GETTERS & SETTERS + return width; + } - public int getWidth() { - return width; - } + public int getHeight() { - public int getHeight() { - return height; - } + return height; + } - protected ChartTitle getTitle() { - return chartTitle; - } + protected ChartTitle getTitle() { - protected ChartLegend getLegend() { - return chartLegend; - } + return chartTitle; + } - protected AxisPair getAxisPair() { - return axisPair; - } + protected ChartLegend getLegend() { - protected Plot getPlot() { - return plot; - } + return chartLegend; + } - // EXTERNAL GETTERS & SETTERS + protected AxisPair getAxisPair() { - public Series addSeries(String seriesName, double[] xData, double[] yData) { - return axisPair.addSeries(seriesName, xData, yData); - } + return axisPair; + } - public void setChartTitle(String title) { - this.chartTitle.setText(title); - } + protected Plot getPlot() { - public void setChartTitleVisible(boolean isVisible) { - this.chartTitle.setVisible(isVisible); - } + return plot; + } - public void setXAxisTitle(String title) { - this.axisPair.getXAxis().setAxisTitle(title); - } + // EXTERNAL GETTERS & SETTERS - public void setYAxisTitle(String title) { - this.axisPair.getYAxis().setAxisTitle(title); - } + public Series addSeries(String seriesName, double[] xData, double[] yData) { - public void setAxisTitlesVisible(boolean isVisible) { - this.axisPair.getXAxis().getAxisTitle().setVisible(isVisible); - this.axisPair.getYAxis().getAxisTitle().setVisible(isVisible); - } + return axisPair.addSeries(seriesName, xData, yData); + } - public void setChartLegendVisible(boolean isVisible) { - this.chartLegend.setVisible(isVisible); - } + public void setChartTitle(String title) { + + this.chartTitle.setText(title); + } + + public void setChartTitleVisible(boolean isVisible) { + + this.chartTitle.setVisible(isVisible); + } + + public void setXAxisTitle(String title) { + + this.axisPair.getXAxis().setAxisTitle(title); + } + + public void setYAxisTitle(String title) { + + this.axisPair.getYAxis().setAxisTitle(title); + } + + public void setAxisTitlesVisible(boolean isVisible) { + + this.axisPair.getXAxis().getAxisTitle().setVisible(isVisible); + this.axisPair.getYAxis().getAxisTitle().setVisible(isVisible); + } + + public void setChartLegendVisible(boolean isVisible) { + + this.chartLegend.setVisible(isVisible); + } } diff --git a/src/com/xeiam/xcharts/ChartColor.java b/src/com/xeiam/xcharts/ChartColor.java index 9f8292bb823b9b317546fea79c43ce62e8bec60f..7e582c6412625d16fca44925e826d728e9a78422 100644 --- a/src/com/xeiam/xcharts/ChartColor.java +++ b/src/com/xeiam/xcharts/ChartColor.java @@ -22,34 +22,36 @@ import java.awt.Color; */ public enum ChartColor { - /** BLACK */ - BLACK(new Color(0, 0, 0)), + /** BLACK */ + BLACK(new Color(0, 0, 0)), - /** WHITE */ - WHITE(new Color(255, 255, 255)), + /** WHITE */ + WHITE(new Color(255, 255, 255)), - /** DARK_GREY */ - DARK_GREY(new Color(22, 22, 22)), + /** DARK_GREY */ + DARK_GREY(new Color(22, 22, 22)), - /** GREY */ - GREY(new Color(200, 200, 200)), + /** GREY */ + GREY(new Color(200, 200, 200)), - /** LIGHT_GREY */ - LIGHT_GREY(new Color(252, 252, 252)); + /** LIGHT_GREY */ + LIGHT_GREY(new Color(252, 252, 252)); - Color color; + Color color; - protected static Color getAWTColor(ChartColor chartColor) { - return chartColor.color; - } + protected static Color getAWTColor(ChartColor chartColor) { - /** - * Constructor - * - * @param color - */ - private ChartColor(Color color) { - this.color = color; - } + return chartColor.color; + } + + /** + * Constructor + * + * @param color + */ + private ChartColor(Color color) { + + this.color = color; + } } diff --git a/src/com/xeiam/xcharts/ChartLegend.java b/src/com/xeiam/xcharts/ChartLegend.java index 8d23c51cb7490d35d572b39bba41ffee2b2717b7..c67c56733ad87bb099acbdc68ee00405b75a4d6d 100644 --- a/src/com/xeiam/xcharts/ChartLegend.java +++ b/src/com/xeiam/xcharts/ChartLegend.java @@ -32,118 +32,121 @@ import com.xeiam.xcharts.series.markers.Marker; */ public class ChartLegend implements IHideable { - /** the chart */ - private Chart chart; + /** the chart */ + private Chart chart; - /** the visibility state of legend */ - private boolean isVisible = true; // default to true + /** the visibility state of legend */ + private boolean isVisible = true; // default to true - /** the font */ - private Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 11); // default font + /** the font */ + private Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 11); // default font - /** the border color */ - private Color border = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default border color + /** the border color */ + private Color border = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default border color - /** the background color */ - private Color background = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default background color + /** the background color */ + private Color background = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default background color - /** the foreground color */ - private Color foreground = ChartColor.getAWTColor(ChartColor.BLACK); // default foreground color - private final int LEGEND_PADDING = 10; + /** the foreground color */ + private Color foreground = ChartColor.getAWTColor(ChartColor.BLACK); // default foreground color + private final int LEGEND_PADDING = 10; - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - /** - * Constructor - */ - public ChartLegend(Chart pChart) { - this.chart = pChart; - } + /** + * Constructor + */ + public ChartLegend(Chart pChart) { - @Override - public void setVisible(boolean isVisible) { - this.isVisible = isVisible; - } + this.chart = pChart; + } + + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + } + + @Override + public void paint(Graphics2D g) { + + if (isVisible) { + + Map<Integer, Series> seriesMap = chart.getAxisPair().getSeriesMap(); - @Override - public void paint(Graphics2D g) { - - if (isVisible) { - - Map<Integer, Series> seriesMap = chart.getAxisPair().getSeriesMap(); - - // determine legend text content max width - int legendTextContentMaxWidth = 0; - int legendTextContentMaxHeight = 0; - - for (Integer seriesId : seriesMap.keySet()) { - Series series = seriesMap.get(seriesId); - TextLayout textLayout = new TextLayout(series.getName(), font, new FontRenderContext(null, true, false)); - Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - // System.out.println(rectangle); - if (rectangle.getWidth() > legendTextContentMaxWidth) { - legendTextContentMaxWidth = (int) rectangle.getWidth(); - } - if (rectangle.getHeight() > legendTextContentMaxHeight) { - legendTextContentMaxHeight = (int) rectangle.getHeight(); - } - } - - // determine legend content height - int legendContentHeight = 0; - int maxContentHeight = Math.max(legendTextContentMaxHeight, Marker.SIZE); - legendContentHeight = maxContentHeight * seriesMap.size() + LEGEND_PADDING * (seriesMap.size() - 1); - - // determine legend content width - int legendContentWidth = (int) (3.0 * Marker.SIZE + LEGEND_PADDING + legendTextContentMaxWidth); - - // Draw Legend Box - int legendBoxWidth = legendContentWidth + 2 * LEGEND_PADDING; - int legendBoxHeight = legendContentHeight + 2 * LEGEND_PADDING; - int xOffset = (chart.getWidth() - legendBoxWidth - Chart.CHART_PADDING); - int yOffset = (int) ((chart.getHeight() - legendBoxHeight) / 2.0 + chart.getTitle().getBounds().getY() + chart.getTitle().getBounds().getHeight()); - - g.setColor(border); - g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight); - g.setColor(background); - g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1); - - // Draw legend content inside legend box - int startx = xOffset + LEGEND_PADDING; - int starty = yOffset + LEGEND_PADDING; - for (Integer seriesId : seriesMap.keySet()) { - Series series = seriesMap.get(seriesId); - // paint line - if (series.getLineStyle() != null) { - g.setColor(series.getLineColor()); - g.setStroke(series.getLineStyle()); - g.drawLine(startx, starty - Marker.Y_OFFSET, (int) (startx + Marker.SIZE * 3.0), starty - Marker.Y_OFFSET); - } - // paint marker - if (series.getMarker() != null) { - g.setColor(series.getMarkerColor()); - series.getMarker().paint(g, (int) (startx + (Marker.SIZE * 1.5)), starty - Marker.Y_OFFSET); - } - - // paint series name - g.setColor(foreground); - TextLayout layout = new TextLayout(series.getName(), font, new FontRenderContext(null, true, false)); - layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + LEGEND_PADDING), (starty + Marker.SIZE)); - starty = starty + legendTextContentMaxHeight + LEGEND_PADDING; - } - - // bounds - bounds = new Rectangle(xOffset, yOffset, legendBoxWidth, legendBoxHeight); - // g.setColor(Color.blue); - // g.draw(bounds); + // determine legend text content max width + int legendTextContentMaxWidth = 0; + int legendTextContentMaxHeight = 0; + + for (Integer seriesId : seriesMap.keySet()) { + Series series = seriesMap.get(seriesId); + TextLayout textLayout = new TextLayout(series.getName(), font, new FontRenderContext(null, true, false)); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + // System.out.println(rectangle); + if (rectangle.getWidth() > legendTextContentMaxWidth) { + legendTextContentMaxWidth = (int) rectangle.getWidth(); + } + if (rectangle.getHeight() > legendTextContentMaxHeight) { + legendTextContentMaxHeight = (int) rectangle.getHeight(); + } + } + + // determine legend content height + int legendContentHeight = 0; + int maxContentHeight = Math.max(legendTextContentMaxHeight, Marker.SIZE); + legendContentHeight = maxContentHeight * seriesMap.size() + LEGEND_PADDING * (seriesMap.size() - 1); + + // determine legend content width + int legendContentWidth = (int) (3.0 * Marker.SIZE + LEGEND_PADDING + legendTextContentMaxWidth); + + // Draw Legend Box + int legendBoxWidth = legendContentWidth + 2 * LEGEND_PADDING; + int legendBoxHeight = legendContentHeight + 2 * LEGEND_PADDING; + int xOffset = (chart.getWidth() - legendBoxWidth - Chart.CHART_PADDING); + int yOffset = (int) ((chart.getHeight() - legendBoxHeight) / 2.0 + chart.getTitle().getBounds().getY() + chart.getTitle().getBounds().getHeight()); + + g.setColor(border); + g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight); + g.setColor(background); + g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1); + + // Draw legend content inside legend box + int startx = xOffset + LEGEND_PADDING; + int starty = yOffset + LEGEND_PADDING; + for (Integer seriesId : seriesMap.keySet()) { + Series series = seriesMap.get(seriesId); + // paint line + if (series.getLineStyle() != null) { + g.setColor(series.getLineColor()); + g.setStroke(series.getLineStyle()); + g.drawLine(startx, starty - Marker.Y_OFFSET, (int) (startx + Marker.SIZE * 3.0), starty - Marker.Y_OFFSET); + } + // paint marker + if (series.getMarker() != null) { + g.setColor(series.getMarkerColor()); + series.getMarker().paint(g, (int) (startx + (Marker.SIZE * 1.5)), starty - Marker.Y_OFFSET); } + // paint series name + g.setColor(foreground); + TextLayout layout = new TextLayout(series.getName(), font, new FontRenderContext(null, true, false)); + layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + LEGEND_PADDING), (starty + Marker.SIZE)); + starty = starty + legendTextContentMaxHeight + LEGEND_PADDING; + } + + // bounds + bounds = new Rectangle(xOffset, yOffset, legendBoxWidth, legendBoxHeight); + // g.setColor(Color.blue); + // g.draw(bounds); } - @Override - public Rectangle getBounds() { - return bounds; - } + } + + @Override + public Rectangle getBounds() { + + return bounds; + } } diff --git a/src/com/xeiam/xcharts/ChartTitle.java b/src/com/xeiam/xcharts/ChartTitle.java index 7176fd6dd2270bf66de716470466ffd93f8c2152..8f2b8ad61de1165859505d6674068d8218ade0e5 100644 --- a/src/com/xeiam/xcharts/ChartTitle.java +++ b/src/com/xeiam/xcharts/ChartTitle.java @@ -29,69 +29,73 @@ import com.xeiam.xcharts.interfaces.IHideable; */ public class ChartTitle implements IHideable { - /** the chart */ - protected Chart chart; + /** the chart */ + protected Chart chart; - /** the title text */ - protected String text = ""; // default to "" + /** the title text */ + protected String text = ""; // default to "" - /** the visibility state of title */ - protected boolean isVisible = false; // default to false + /** the visibility state of title */ + protected boolean isVisible = false; // default to false - /** the font */ - private Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14); // default font + /** the font */ + private Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14); // default font - /** the foreground color */ - private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default foreground color + /** the foreground color */ + private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default foreground color - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - /** - * Constructor - */ - public ChartTitle(Chart pChart) { - this.chart = pChart; - } + /** + * Constructor + */ + public ChartTitle(Chart pChart) { - public void setText(String text) { - if (text.trim().equalsIgnoreCase("")) { - this.isVisible = false; - } else { - this.isVisible = true; - } - this.text = text; - } + this.chart = pChart; + } - @Override - public void setVisible(boolean isVisible) { - this.isVisible = isVisible; + public void setText(String text) { + + if (text.trim().equalsIgnoreCase("")) { + this.isVisible = false; + } else { + this.isVisible = true; } + this.text = text; + } - @Override - public void paint(Graphics2D g) { + @Override + public void setVisible(boolean isVisible) { - if (isVisible) { + this.isVisible = isVisible; + } - FontRenderContext frc = g.getFontRenderContext(); - TextLayout textLayout = new TextLayout(this.text, this.font, frc); - Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - // System.out.println(rectangle); - int xOffset = (int) ((chart.getWidth() - rectangle.getWidth()) / 2.0); - int yOffset = (int) (Chart.CHART_PADDING - rectangle.getY()); + @Override + public void paint(Graphics2D g) { - g.setColor(foreground); - textLayout.draw(g, xOffset, yOffset); + if (isVisible) { - bounds = new Rectangle(xOffset, (int) (yOffset + rectangle.getY()), (int) rectangle.getWidth(), (int) rectangle.getHeight()); - // g.setColor(Color.green); - // g.draw(bounds); - } + FontRenderContext frc = g.getFontRenderContext(); + TextLayout textLayout = new TextLayout(this.text, this.font, frc); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + // System.out.println(rectangle); + int xOffset = (int) ((chart.getWidth() - rectangle.getWidth()) / 2.0); + int yOffset = (int) (Chart.CHART_PADDING - rectangle.getY()); - } + g.setColor(foreground); + textLayout.draw(g, xOffset, yOffset); - @Override - public Rectangle getBounds() { - return bounds; + bounds = new Rectangle(xOffset, (int) (yOffset + rectangle.getY()), (int) rectangle.getWidth(), (int) rectangle.getHeight()); + // g.setColor(Color.green); + // g.draw(bounds); } + + } + + @Override + public Rectangle getBounds() { + + return bounds; + } } \ No newline at end of file diff --git a/src/com/xeiam/xcharts/Plot.java b/src/com/xeiam/xcharts/Plot.java index 1d8159086bacf43add5f772b5b2398600e21be09..129ce74994eb314bd5f105d6ea33ea3d1fcfef46 100644 --- a/src/com/xeiam/xcharts/Plot.java +++ b/src/com/xeiam/xcharts/Plot.java @@ -25,43 +25,45 @@ import com.xeiam.xcharts.interfaces.IChartPart; */ public class Plot implements IChartPart { - private Chart chart; + private Chart chart; - private PlotSurface plotSurface; + private PlotSurface plotSurface; - private PlotContent plotContent; + private PlotContent plotContent; - public static final int PLOT_PADDING = 5; + public static final int PLOT_PADDING = 5; - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the bounds */ + private Rectangle bounds = new Rectangle(); // default all-zero rectangle - public Plot(Chart chart) { - this.chart = chart; - this.plotSurface = new PlotSurface(chart, this); - this.plotContent = new PlotContent(chart, this); - } + public Plot(Chart chart) { - @Override - public Rectangle getBounds() { - return bounds; - } + this.chart = chart; + this.plotSurface = new PlotSurface(chart, this); + this.plotContent = new PlotContent(chart, this); + } - @Override - public void paint(Graphics2D g) { + @Override + public Rectangle getBounds() { - // calculate bounds - int xOffset = (int) (chart.getAxisPair().getYAxis().getBounds().getX() + chart.getAxisPair().getYAxis().getBounds().getWidth() + PLOT_PADDING); - int yOffset = (int) (chart.getAxisPair().getYAxis().getBounds().getY()); - int width = (int) chart.getAxisPair().getXAxis().getBounds().getWidth(); - int height = (int) chart.getAxisPair().getYAxis().getBounds().getHeight(); - bounds = new Rectangle(xOffset, yOffset, width, height); - // g.setColor(Color.green); - // g.draw(bounds); + return bounds; + } - plotSurface.paint(g); - plotContent.paint(g); + @Override + public void paint(Graphics2D g) { - } + // calculate bounds + int xOffset = (int) (chart.getAxisPair().getYAxis().getBounds().getX() + chart.getAxisPair().getYAxis().getBounds().getWidth() + PLOT_PADDING); + int yOffset = (int) (chart.getAxisPair().getYAxis().getBounds().getY()); + int width = (int) chart.getAxisPair().getXAxis().getBounds().getWidth(); + int height = (int) chart.getAxisPair().getYAxis().getBounds().getHeight(); + bounds = new Rectangle(xOffset, yOffset, width, height); + // g.setColor(Color.green); + // g.draw(bounds); + + plotSurface.paint(g); + plotContent.paint(g); + + } } diff --git a/src/com/xeiam/xcharts/PlotContent.java b/src/com/xeiam/xcharts/PlotContent.java index 9446d921af7c09e2e5c0c8acf7cbac4d051915e2..633c6955913707e59c3e99a9b807b65f9fa7bf66 100644 --- a/src/com/xeiam/xcharts/PlotContent.java +++ b/src/com/xeiam/xcharts/PlotContent.java @@ -27,95 +27,97 @@ import com.xeiam.xcharts.series.Series; */ public class PlotContent implements IChartPart { - private Chart chart; + private Chart chart; - private Plot plot; + private Plot plot; - public PlotContent(Chart chart, Plot plot) { - this.chart = chart; - this.plot = plot; - } + public PlotContent(Chart chart, Plot plot) { - @Override - public Rectangle getBounds() { - return plot.getBounds(); - } + this.chart = chart; + this.plot = plot; + } - @Override - public void paint(Graphics2D g) { + @Override + public Rectangle getBounds() { - Rectangle bounds = plot.getBounds(); + return plot.getBounds(); + } - Map<Integer, Series> seriesMap = chart.getAxisPair().getSeriesMap(); - for (Integer seriesId : seriesMap.keySet()) { + @Override + public void paint(Graphics2D g) { - Series series = seriesMap.get(seriesId); + Rectangle bounds = plot.getBounds(); - // X-Axis - int xTickSpace = AxisPair.getTickSpace((int) bounds.getWidth()); - int xLeftMargin = AxisPair.getMargin((int) bounds.getWidth(), xTickSpace); + Map<Integer, Series> seriesMap = chart.getAxisPair().getSeriesMap(); + for (Integer seriesId : seriesMap.keySet()) { - // Y-Axis - int yTickSpace = AxisPair.getTickSpace((int) bounds.getHeight()); - int yTopMargin = AxisPair.getMargin((int) bounds.getHeight(), yTickSpace); + Series series = seriesMap.get(seriesId); - // data points - double[] xData = series.getxData(); - double xMin = chart.getAxisPair().getXAxis().getMin(); - double xMax = chart.getAxisPair().getXAxis().getMax(); - double[] yData = series.getyData(); - double yMin = chart.getAxisPair().getYAxis().getMin(); - double yMax = chart.getAxisPair().getYAxis().getMax(); + // X-Axis + int xTickSpace = AxisPair.getTickSpace((int) bounds.getWidth()); + int xLeftMargin = AxisPair.getMargin((int) bounds.getWidth(), xTickSpace); - int previousX = Integer.MIN_VALUE; - int previousY = Integer.MIN_VALUE; + // Y-Axis + int yTickSpace = AxisPair.getTickSpace((int) bounds.getHeight()); + int yTopMargin = AxisPair.getMargin((int) bounds.getHeight(), yTickSpace); - for (int i = 0; i < xData.length; i++) { + // data points + double[] xData = series.getxData(); + double xMin = chart.getAxisPair().getXAxis().getMin(); + double xMax = chart.getAxisPair().getXAxis().getMax(); + double[] yData = series.getyData(); + double yMin = chart.getAxisPair().getYAxis().getMin(); + double yMax = chart.getAxisPair().getYAxis().getMax(); - if (Double.isInfinite(xData[i])) { - throw new RuntimeException("Infinite values in xAxis Data not allowed!!!"); - } + int previousX = Integer.MIN_VALUE; + int previousY = Integer.MIN_VALUE; - if (Double.isInfinite(yData[i])) { - throw new RuntimeException("Infinite values in yAxis Data not allowed!!!"); - } + for (int i = 0; i < xData.length; i++) { - if (!Double.isNaN(xData[i]) && !Double.isNaN(yData[i])) { + if (Double.isInfinite(xData[i])) { + throw new RuntimeException("Infinite values in xAxis Data not allowed!!!"); + } + + if (Double.isInfinite(yData[i])) { + throw new RuntimeException("Infinite values in yAxis Data not allowed!!!"); + } - int xTransform = (int) (xLeftMargin + ((xData[i] - xMin) / (xMax - xMin) * xTickSpace)); - int yTransform = (int) (bounds.getHeight() - (yTopMargin + (yData[i] - yMin) / (yMax - yMin) * yTickSpace)); + if (!Double.isNaN(xData[i]) && !Double.isNaN(yData[i])) { - // a check if all y data are the exact same values - if (Math.abs(xMax - xMin) / 5 == 0.0) { - xTransform = (int) (bounds.getWidth() / 2.0); - } + int xTransform = (int) (xLeftMargin + ((xData[i] - xMin) / (xMax - xMin) * xTickSpace)); + int yTransform = (int) (bounds.getHeight() - (yTopMargin + (yData[i] - yMin) / (yMax - yMin) * yTickSpace)); - // a check if all y data are the exact same values - if (Math.abs(yMax - yMin) / 5 == 0.0) { - yTransform = (int) (bounds.getHeight() / 2.0); - } + // a check if all y data are the exact same values + if (Math.abs(xMax - xMin) / 5 == 0.0) { + xTransform = (int) (bounds.getWidth() / 2.0); + } - int xOffset = (int) (bounds.getX() + xTransform - 1); - int yOffset = (int) (bounds.getY() + yTransform); + // a check if all y data are the exact same values + if (Math.abs(yMax - yMin) / 5 == 0.0) { + yTransform = (int) (bounds.getHeight() / 2.0); + } - // paint line - if (series.getLineStyle() != null) { - if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) { - g.setColor(series.getLineColor()); - g.setStroke(series.getLineStyle()); - g.drawLine(previousX, previousY, xOffset, yOffset); - } - previousX = xOffset; - previousY = yOffset; - } + int xOffset = (int) (bounds.getX() + xTransform - 1); + int yOffset = (int) (bounds.getY() + yTransform); - // paint marker - if (series.getMarker() != null) { - g.setColor(series.getMarkerColor()); - series.getMarker().paint(g, xOffset, yOffset); - } - } + // paint line + if (series.getLineStyle() != null) { + if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) { + g.setColor(series.getLineColor()); + g.setStroke(series.getLineStyle()); + g.drawLine(previousX, previousY, xOffset, yOffset); } + previousX = xOffset; + previousY = yOffset; + } + + // paint marker + if (series.getMarker() != null) { + g.setColor(series.getMarkerColor()); + series.getMarker().paint(g, xOffset, yOffset); + } } + } } + } } diff --git a/src/com/xeiam/xcharts/PlotSurface.java b/src/com/xeiam/xcharts/PlotSurface.java index c62301c5d26b203f3bce6c8c9e05afe2da6d339a..f77b5076b4e5d51464f531fdce11df066567d8de 100644 --- a/src/com/xeiam/xcharts/PlotSurface.java +++ b/src/com/xeiam/xcharts/PlotSurface.java @@ -28,65 +28,67 @@ import com.xeiam.xcharts.interfaces.IChartPart; */ public class PlotSurface implements IChartPart { - private Chart chart; + private Chart chart; - private Plot plot; + private Plot plot; - /** the foreground color */ - private Color foreground = ChartColor.getAWTColor(ChartColor.GREY); // default foreground color + /** the foreground color */ + private Color foreground = ChartColor.getAWTColor(ChartColor.GREY); // default foreground color - /** the background color */ - private Color background = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default background color + /** the background color */ + private Color background = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default background color - /** the line style */ - private BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f); + /** the line style */ + private BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f); - public PlotSurface(Chart chart, Plot plot) { - this.chart = chart; - this.plot = plot; - } + public PlotSurface(Chart chart, Plot plot) { - @Override - public Rectangle getBounds() { - return plot.getBounds(); - } + this.chart = chart; + this.plot = plot; + } + + @Override + public Rectangle getBounds() { - @Override - public void paint(Graphics2D g) { + return plot.getBounds(); + } - Rectangle bounds = plot.getBounds(); + @Override + public void paint(Graphics2D g) { - // paint background - Rectangle backgroundRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); - g.setColor(background); - g.fill(backgroundRectangle); - Rectangle borderRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); - g.setColor(ChartColor.getAWTColor(ChartColor.DARK_GREY)); - g.draw(borderRectangle); + Rectangle bounds = plot.getBounds(); - // paint grid lines - // horizontal - List<Integer> yAxisTickLocations = chart.getAxisPair().getYAxis().getAxisTick().getTickLocations(); - for (int i = 0; i < yAxisTickLocations.size(); i++) { + // paint background + Rectangle backgroundRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); + g.setColor(background); + g.fill(backgroundRectangle); + Rectangle borderRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); + g.setColor(ChartColor.getAWTColor(ChartColor.DARK_GREY)); + g.draw(borderRectangle); - int tickLocation = yAxisTickLocations.get(i); + // paint grid lines + // horizontal + List<Integer> yAxisTickLocations = chart.getAxisPair().getYAxis().getAxisTick().getTickLocations(); + for (int i = 0; i < yAxisTickLocations.size(); i++) { - g.setColor(foreground); - 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), (int) (bounds.getY() + bounds.getHeight() - tickLocation)); - } + int tickLocation = yAxisTickLocations.get(i); + + g.setColor(foreground); + 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), (int) (bounds.getY() + bounds.getHeight() - tickLocation)); + } - // vertical - List<Integer> xAxisTickLocations = chart.getAxisPair().getXAxis().getAxisTick().getTickLocations(); - for (int i = 0; i < xAxisTickLocations.size(); i++) { + // vertical + List<Integer> xAxisTickLocations = chart.getAxisPair().getXAxis().getAxisTick().getTickLocations(); + for (int i = 0; i < xAxisTickLocations.size(); i++) { - int tickLocation = xAxisTickLocations.get(i); + int tickLocation = xAxisTickLocations.get(i); - g.setColor(foreground); - g.setStroke(stroke); + g.setColor(foreground); + g.setStroke(stroke); - g.drawLine((int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + 1), (int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + bounds.getHeight() - 1)); - } + g.drawLine((int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + 1), (int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + bounds.getHeight() - 1)); } + } } diff --git a/src/com/xeiam/xcharts/QuickChart.java b/src/com/xeiam/xcharts/QuickChart.java index 8c668f7fe5f5509b65ab216614d17df8650368c4..84c76f3a9274c5a4e22d8c14db97bf0a3eaecfad 100644 --- a/src/com/xeiam/xcharts/QuickChart.java +++ b/src/com/xeiam/xcharts/QuickChart.java @@ -25,57 +25,57 @@ import com.xeiam.xcharts.series.SeriesMarker; */ public class QuickChart { - /** - * @param chartTitle - * @param xTitle - * @param yTitle - * @param seriesName - * @param xData - * @param yData - * @return a Chart Object - */ - public static Chart getChart(String chartTitle, String xTitle, String yTitle, String seriesName, double[] xData, double[] yData) { + /** + * @param chartTitle + * @param xTitle + * @param yTitle + * @param seriesName + * @param xData + * @param yData + * @return a Chart Object + */ + public static Chart getChart(String chartTitle, String xTitle, String yTitle, String seriesName, double[] xData, double[] yData) { - double[][] yData2d = { yData }; - if (seriesName == null) { - return getChart(chartTitle, xTitle, yTitle, null, xData, yData2d); - } else { - return getChart(chartTitle, xTitle, yTitle, new String[] { seriesName }, xData, yData2d); - } + double[][] yData2d = { yData }; + if (seriesName == null) { + return getChart(chartTitle, xTitle, yTitle, null, xData, yData2d); + } else { + return getChart(chartTitle, xTitle, yTitle, new String[] { seriesName }, xData, yData2d); } + } - /** - * @param chartTitle - * @param xTitle - * @param yTitle - * @param seriesNames - * @param xData - * @param yData - * @return a Chart Object - */ - public static Chart getChart(String chartTitle, String xTitle, String yTitle, String[] seriesNames, double[] xData, double[][] yData) { + /** + * @param chartTitle + * @param xTitle + * @param yTitle + * @param seriesNames + * @param xData + * @param yData + * @return a Chart Object + */ + public static Chart getChart(String chartTitle, String xTitle, String yTitle, String[] seriesNames, double[] xData, double[][] yData) { - // Create Chart - Chart chart = new Chart(400, 280); + // Create Chart + Chart chart = new Chart(400, 280); - // Customize Chart - chart.setChartTitle(chartTitle); - chart.setXAxisTitle(xTitle); - chart.setYAxisTitle(yTitle); + // Customize Chart + chart.setChartTitle(chartTitle); + chart.setXAxisTitle(xTitle); + chart.setYAxisTitle(yTitle); - // Series - for (int i = 0; i < yData.length; i++) { - Series series; - if (seriesNames != null) { - series = chart.addSeries(seriesNames[i], xData, yData[i]); - } else { - chart.setChartLegendVisible(false); - series = chart.addSeries(" " + i, xData, yData[i]); - } - series.setMarker(SeriesMarker.NONE); - } - - return chart; + // Series + for (int i = 0; i < yData.length; i++) { + Series series; + if (seriesNames != null) { + series = chart.addSeries(seriesNames[i], xData, yData[i]); + } else { + chart.setChartLegendVisible(false); + series = chart.addSeries(" " + i, xData, yData[i]); + } + series.setMarker(SeriesMarker.NONE); } + return chart; + } + } diff --git a/src/com/xeiam/xcharts/interfaces/IChartPart.java b/src/com/xeiam/xcharts/interfaces/IChartPart.java index cfeef1e373e242c85f60a9a731abd11ded0a06bd..0dc9d370b0646391c9070d930ab2791a18ac90c5 100644 --- a/src/com/xeiam/xcharts/interfaces/IChartPart.java +++ b/src/com/xeiam/xcharts/interfaces/IChartPart.java @@ -23,8 +23,8 @@ import java.awt.Rectangle; */ public interface IChartPart { - public Rectangle getBounds(); + public Rectangle getBounds(); - public void paint(final Graphics2D g); + public void paint(final Graphics2D g); } diff --git a/src/com/xeiam/xcharts/interfaces/IHideable.java b/src/com/xeiam/xcharts/interfaces/IHideable.java index 78094ad87790c750a272dc141415f68f6fad952a..6890efa0fb4a1cc6f5d30635bfb19ed02756752f 100644 --- a/src/com/xeiam/xcharts/interfaces/IHideable.java +++ b/src/com/xeiam/xcharts/interfaces/IHideable.java @@ -13,6 +13,6 @@ package com.xeiam.xcharts.interfaces; */ public interface IHideable extends IChartPart { - public void setVisible(boolean visible); + public void setVisible(boolean visible); } \ No newline at end of file diff --git a/src/com/xeiam/xcharts/series/Series.java b/src/com/xeiam/xcharts/series/Series.java index 4e1b47cfd2851219fed16c248ba71d8e75fcd217..816174364350a5fb6f4155d8e70c18d7aab85c19 100644 --- a/src/com/xeiam/xcharts/series/Series.java +++ b/src/com/xeiam/xcharts/series/Series.java @@ -25,162 +25,182 @@ import com.xeiam.xcharts.series.markers.Marker; */ public class Series { - private String name = ""; + private String name = ""; - protected double[] xData; + protected double[] xData; - protected double[] yData; + protected double[] yData; - /** the minimum value of axis range */ - private double xMin; + /** the minimum value of axis range */ + private double xMin; - /** the maximum value of axis range */ - private double xMax; + /** the maximum value of axis range */ + private double xMax; - /** the minimum value of axis range */ - private double yMin; + /** the minimum value of axis range */ + private double yMin; - /** the maximum value of axis range */ - private double yMax; + /** the maximum value of axis range */ + private double yMax; - /** Line Style */ - private BasicStroke stroke; + /** Line Style */ + private BasicStroke stroke; - /** Line Color */ - private Color strokeColor; + /** Line Color */ + private Color strokeColor; - /** Marker Style */ - private Marker marker; + /** Marker Style */ + private Marker marker; - /** Marker Color */ - private Color markerColor; + /** Marker Color */ + private Color markerColor; - /** - * Constructor - * - * @param name - * @param xData - * @param yData - */ - public Series(String name, double[] xData, double[] yData) { + /** + * Constructor + * + * @param name + * @param xData + * @param yData + */ + public Series(String name, double[] xData, double[] yData) { - this.name = name; - this.xData = xData; - this.yData = yData; + this.name = name; + this.xData = xData; + this.yData = yData; - // xData - double[] xMinMax = findMinMax(xData); - this.xMin = xMinMax[0]; - this.xMax = xMinMax[1]; + // xData + double[] xMinMax = findMinMax(xData); + this.xMin = xMinMax[0]; + this.xMax = xMinMax[1]; - // yData - double[] yMinMax = findMinMax(yData); - this.yMin = yMinMax[0]; - this.yMax = yMinMax[1]; - // System.out.println(yMin); - // System.out.println(yMax); + // yData + double[] yMinMax = findMinMax(yData); + this.yMin = yMinMax[0]; + this.yMax = yMinMax[1]; + // System.out.println(yMin); + // System.out.println(yMax); - Color color = SeriesColor.getNextAWTColor(); - this.strokeColor = color; - this.markerColor = color; + Color color = SeriesColor.getNextAWTColor(); + this.strokeColor = color; + this.markerColor = color; - this.marker = SeriesMarker.getNextMarker(); - this.stroke = SeriesLineStyle.getNextBasicStroke(); + this.marker = SeriesMarker.getNextMarker(); + this.stroke = SeriesLineStyle.getNextBasicStroke(); - } + } + + private double[] findMinMax(double[] data) { - private double[] findMinMax(double[] data) { - Double min = null; - Double max = null; - for (int i = 0; i < data.length; i++) { - if (min == null || data[i] < min) { - if (!Double.isNaN(data[i])) { - min = data[i]; - } - } - if (max == null || data[i] > max) { - if (!Double.isNaN(data[i])) { - max = data[i]; - } - } + Double min = null; + Double max = null; + for (int i = 0; i < data.length; i++) { + if (min == null || data[i] < min) { + if (!Double.isNaN(data[i])) { + min = data[i]; + } + } + if (max == null || data[i] > max) { + if (!Double.isNaN(data[i])) { + max = data[i]; } - return new double[] { min, max }; + } } + return new double[] { min, max }; + } - public String getName() { - return name; - } + public String getName() { - public double[] getxData() { - return xData; - } + return name; + } - public double[] getyData() { - return yData; - } + public double[] getxData() { - public double getxMin() { - return xMin; - } + return xData; + } - public double getxMax() { - return xMax; - } + public double[] getyData() { - public double getyMin() { - return yMin; - } + return yData; + } - public double getyMax() { - return yMax; - } + public double getxMin() { - public BasicStroke getLineStyle() { - return stroke; - } + return xMin; + } - public void setLineStyle(SeriesLineStyle lineStyle) { - this.stroke = SeriesLineStyle.getBasicStroke(lineStyle); - } + public double getxMax() { - public void setLineStyle(BasicStroke lineStyle) { - this.stroke = lineStyle; - } + return xMax; + } - public Color getLineColor() { - return strokeColor; - } + public double getyMin() { - public void setLineColor(SeriesColor lineColor) { - this.strokeColor = SeriesColor.getAWTColor(lineColor); - } + return yMin; + } - public void setLineColor(java.awt.Color lineColor) { - this.strokeColor = lineColor; - } + public double getyMax() { - public Marker getMarker() { - return marker; - } + return yMax; + } - public void setMarker(SeriesMarker marker) { - this.marker = SeriesMarker.getMarker(marker); - } + public BasicStroke getLineStyle() { - public void setMarker(Marker marker) { - this.marker = marker; - } + return stroke; + } - public Color getMarkerColor() { - return markerColor; - } + public void setLineStyle(SeriesLineStyle lineStyle) { - public void setMarkerColor(SeriesColor lineColor) { - this.markerColor = SeriesColor.getAWTColor(lineColor); - } + this.stroke = SeriesLineStyle.getBasicStroke(lineStyle); + } - public void setMarkerColor(java.awt.Color lineColor) { - this.markerColor = lineColor; - } + public void setLineStyle(BasicStroke lineStyle) { + + this.stroke = lineStyle; + } + + public Color getLineColor() { + + return strokeColor; + } + + public void setLineColor(SeriesColor lineColor) { + + this.strokeColor = SeriesColor.getAWTColor(lineColor); + } + + public void setLineColor(java.awt.Color lineColor) { + + this.strokeColor = lineColor; + } + + public Marker getMarker() { + + return marker; + } + + public void setMarker(SeriesMarker marker) { + + this.marker = SeriesMarker.getMarker(marker); + } + + public void setMarker(Marker marker) { + + this.marker = marker; + } + + public Color getMarkerColor() { + + return markerColor; + } + + public void setMarkerColor(SeriesColor lineColor) { + + this.markerColor = SeriesColor.getAWTColor(lineColor); + } + + public void setMarkerColor(java.awt.Color lineColor) { + + this.markerColor = lineColor; + } } diff --git a/src/com/xeiam/xcharts/series/SeriesColor.java b/src/com/xeiam/xcharts/series/SeriesColor.java index 230521ca3d4f0e8db88c73172f86f82faaa9b8b5..ac058714b935c21991633b6ee1c224d9f9472b7b 100644 --- a/src/com/xeiam/xcharts/series/SeriesColor.java +++ b/src/com/xeiam/xcharts/series/SeriesColor.java @@ -25,88 +25,92 @@ import java.util.Map; */ public enum SeriesColor { - /** BLUE */ - BLUE(0, new Color(0, 55, 255)), + /** BLUE */ + BLUE(0, new Color(0, 55, 255)), - /** ORANGE */ - ORANGE(1, new Color(255, 172, 0)), + /** ORANGE */ + ORANGE(1, new Color(255, 172, 0)), - /** PURPLE */ - PURPLE(2, new Color(128, 0, 255)), + /** PURPLE */ + PURPLE(2, new Color(128, 0, 255)), - /** GREEN */ - GREEN(3, new Color(0, 205, 0)), + /** GREEN */ + GREEN(3, new Color(0, 205, 0)), - /** RED */ - RED(4, new Color(205, 0, 0)), + /** RED */ + RED(4, new Color(205, 0, 0)), - /** YELLOW */ - YELLOW(5, new Color(255, 215, 0)), + /** YELLOW */ + YELLOW(5, new Color(255, 215, 0)), - /** MAGENTA */ - MAGENTA(6, new Color(255, 0, 255)), + /** MAGENTA */ + MAGENTA(6, new Color(255, 0, 255)), - /** PINK */ - PINK(7, new Color(255, 166, 201)), + /** PINK */ + PINK(7, new Color(255, 166, 201)), - /** LIGHT_GREY */ - LIGHT_GREY(8, new Color(207, 207, 207)), + /** LIGHT_GREY */ + LIGHT_GREY(8, new Color(207, 207, 207)), - /** CYAN */ - CYAN(9, new Color(0, 255, 255)), + /** CYAN */ + CYAN(9, new Color(0, 255, 255)), - /** BROWN */ - BROWN(10, new Color(150, 74, 0)), + /** BROWN */ + BROWN(10, new Color(150, 74, 0)), - /** BLACK */ - BLACK(11, new Color(0, 0, 0)), + /** BLACK */ + BLACK(11, new Color(0, 0, 0)), - /** RANDOM */ - RANDOM(12, new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255))); + /** RANDOM */ + RANDOM(12, new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255))); - int id; - Color color; + int id; + Color color; - private static int nextId = 0; + private static int nextId = 0; - private static final Map<Integer, SeriesColor> idLookup = new HashMap<Integer, SeriesColor>(); - static { - for (SeriesColor seriesColor : EnumSet.allOf(SeriesColor.class)) { - idLookup.put(seriesColor.getId(), seriesColor); - } + private static final Map<Integer, SeriesColor> idLookup = new HashMap<Integer, SeriesColor>(); + static { + for (SeriesColor seriesColor : EnumSet.allOf(SeriesColor.class)) { + idLookup.put(seriesColor.getId(), seriesColor); } + } - private Integer getId() { - return id; - } + private Integer getId() { - public static void resetId() { - nextId = 0; - } + return id; + } - protected static Color getAWTColor(SeriesColor seriesColor) { - return seriesColor.color; - } + public static void resetId() { - protected static Color getNextAWTColor() { + nextId = 0; + } - SeriesColor seriesColor = idLookup.get(nextId); - if (seriesColor == null) { - // rotate thru from beginning - resetId(); - } - return idLookup.get(nextId++).color; - } + protected static Color getAWTColor(SeriesColor seriesColor) { + + return seriesColor.color; + } + + protected static Color getNextAWTColor() { - /** - * Constructor - * - * @param id - * @param color - */ - private SeriesColor(int id, Color color) { - this.id = id; - this.color = color; + SeriesColor seriesColor = idLookup.get(nextId); + if (seriesColor == null) { + // rotate thru from beginning + resetId(); } + return idLookup.get(nextId++).color; + } + + /** + * Constructor + * + * @param id + * @param color + */ + private SeriesColor(int id, Color color) { + + this.id = id; + this.color = color; + } } diff --git a/src/com/xeiam/xcharts/series/SeriesLineStyle.java b/src/com/xeiam/xcharts/series/SeriesLineStyle.java index d4784264fa5ae9c41331ef2d508b09488ba0d437..3c1aabc7c14152d8ffc08363fee47454569e57b7 100644 --- a/src/com/xeiam/xcharts/series/SeriesLineStyle.java +++ b/src/com/xeiam/xcharts/series/SeriesLineStyle.java @@ -25,62 +25,66 @@ import java.util.Map; */ public enum SeriesLineStyle { - /** NONE */ - NONE(-1, null), + /** NONE */ + NONE(-1, null), - /** SOLID */ - SOLID(0, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)), + /** SOLID */ + SOLID(0, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)), - /** DASH_DOT */ - DASH_DOT(1, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 1.0f }, 0.0f)), + /** DASH_DOT */ + DASH_DOT(1, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 1.0f }, 0.0f)), - /** DASH_DASH */ - DASH_DASH(2, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f)), + /** DASH_DASH */ + DASH_DASH(2, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f)), - /** DOT_DOT */ - DOT_DOT(3, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 1.0f, 1.0f }, 0.0f)); + /** DOT_DOT */ + DOT_DOT(3, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 1.0f, 1.0f }, 0.0f)); - int id; - BasicStroke basicStroke; - private static int nextId = 0; + int id; + BasicStroke basicStroke; + private static int nextId = 0; - private static final Map<Integer, SeriesLineStyle> idLookup = new HashMap<Integer, SeriesLineStyle>(); - static { - for (SeriesLineStyle seriesLineStyle : EnumSet.allOf(SeriesLineStyle.class)) { - idLookup.put(seriesLineStyle.getId(), seriesLineStyle); - } + private static final Map<Integer, SeriesLineStyle> idLookup = new HashMap<Integer, SeriesLineStyle>(); + static { + for (SeriesLineStyle seriesLineStyle : EnumSet.allOf(SeriesLineStyle.class)) { + idLookup.put(seriesLineStyle.getId(), seriesLineStyle); } + } - private Integer getId() { - return id; - } + private Integer getId() { - public static void resetId() { - nextId = 0; - } + return id; + } - protected static BasicStroke getBasicStroke(SeriesLineStyle seriesMarker) { - return seriesMarker.basicStroke; - } + public static void resetId() { - protected static BasicStroke getNextBasicStroke() { + nextId = 0; + } - SeriesLineStyle seriesLineStyle = idLookup.get(nextId); - if (seriesLineStyle == null) { - // rotate thru from beginning - resetId(); - } - return idLookup.get(nextId++).basicStroke; - } + protected static BasicStroke getBasicStroke(SeriesLineStyle seriesMarker) { - /** - * Constructor - * - * @param id - * @param color - */ - private SeriesLineStyle(int id, BasicStroke basicStroke) { - this.id = id; - this.basicStroke = basicStroke; + return seriesMarker.basicStroke; + } + + protected static BasicStroke getNextBasicStroke() { + + SeriesLineStyle seriesLineStyle = idLookup.get(nextId); + if (seriesLineStyle == null) { + // rotate thru from beginning + resetId(); } + return idLookup.get(nextId++).basicStroke; + } + + /** + * Constructor + * + * @param id + * @param color + */ + private SeriesLineStyle(int id, BasicStroke basicStroke) { + + this.id = id; + this.basicStroke = basicStroke; + } } diff --git a/src/com/xeiam/xcharts/series/SeriesMarker.java b/src/com/xeiam/xcharts/series/SeriesMarker.java index 6767fe988d9f3831c204dec003cae379f72f4e83..38b9e8d25c99564391b65b185a16b4a9ad9dc88d 100644 --- a/src/com/xeiam/xcharts/series/SeriesMarker.java +++ b/src/com/xeiam/xcharts/series/SeriesMarker.java @@ -31,66 +31,70 @@ import com.xeiam.xcharts.series.markers.TriangleUp; */ public enum SeriesMarker { - /** NONE */ - NONE(-1, null), + /** NONE */ + NONE(-1, null), - /** CIRCLE */ - CIRCLE(0, new Circle()), + /** CIRCLE */ + CIRCLE(0, new Circle()), - /** DIAMOND */ - DIAMOND(1, new Diamond()), + /** DIAMOND */ + DIAMOND(1, new Diamond()), - /** SQUARE */ - SQUARE(2, new Square()), + /** SQUARE */ + SQUARE(2, new Square()), - /** TRIANGLE_DOWN */ - TRIANGLE_DOWN(3, new TriangleDown()), + /** TRIANGLE_DOWN */ + TRIANGLE_DOWN(3, new TriangleDown()), - /** TRIANGLE_UP */ - TRIANGLE_UP(4, new TriangleUp()); + /** TRIANGLE_UP */ + TRIANGLE_UP(4, new TriangleUp()); - int id; - Marker marker; - private static int nextId = 0; + int id; + Marker marker; + private static int nextId = 0; - private static final Map<Integer, SeriesMarker> idLookup = new HashMap<Integer, SeriesMarker>(); - static { - for (SeriesMarker seriesMarker : EnumSet.allOf(SeriesMarker.class)) { - idLookup.put(seriesMarker.getId(), seriesMarker); - } + private static final Map<Integer, SeriesMarker> idLookup = new HashMap<Integer, SeriesMarker>(); + static { + for (SeriesMarker seriesMarker : EnumSet.allOf(SeriesMarker.class)) { + idLookup.put(seriesMarker.getId(), seriesMarker); } + } - private Integer getId() { - return id; - } + private Integer getId() { - public static void resetId() { - nextId = 0; - } + return id; + } - protected static Marker getMarker(SeriesMarker seriesMarker) { - return seriesMarker.marker; - } + public static void resetId() { - protected static Marker getNextMarker() { + nextId = 0; + } - SeriesMarker seriesMarker = idLookup.get(nextId); - if (seriesMarker == null) { - // rotate thru from beginning - resetId(); - } - return idLookup.get(nextId++).marker; - } + protected static Marker getMarker(SeriesMarker seriesMarker) { + + return seriesMarker.marker; + } + + protected static Marker getNextMarker() { - /** - * Constructor - * - * @param id - * @param color - */ - private SeriesMarker(int id, Marker marker) { - this.id = id; - this.marker = marker; + SeriesMarker seriesMarker = idLookup.get(nextId); + if (seriesMarker == null) { + // rotate thru from beginning + resetId(); } + return idLookup.get(nextId++).marker; + } + + /** + * Constructor + * + * @param id + * @param color + */ + private SeriesMarker(int id, Marker marker) { + + this.id = id; + this.marker = marker; + } } diff --git a/src/com/xeiam/xcharts/series/markers/Circle.java b/src/com/xeiam/xcharts/series/markers/Circle.java index 744b5a87d801109591f26b3f232f6e293f2999b4..a17cfd4792fca7cbe662ec7a01b4a645fbf3ea83 100644 --- a/src/com/xeiam/xcharts/series/markers/Circle.java +++ b/src/com/xeiam/xcharts/series/markers/Circle.java @@ -22,12 +22,12 @@ import java.awt.Graphics2D; */ public class Circle extends Marker { - @Override - public void paint(Graphics2D g, int xOffset, int yOffset) { + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { - g.setStroke(stroke); - g.fillOval(xOffset + Marker.X_OFFSET, yOffset + Marker.Y_OFFSET, Marker.SIZE, Marker.SIZE); + g.setStroke(stroke); + g.fillOval(xOffset + Marker.X_OFFSET, yOffset + Marker.Y_OFFSET, Marker.SIZE, Marker.SIZE); - } + } } diff --git a/src/com/xeiam/xcharts/series/markers/Diamond.java b/src/com/xeiam/xcharts/series/markers/Diamond.java index 35787c56c252507cbe86cf815c67b138f392047c..167eb5d8ce2f6345a1aaab24b3dd1ab8db8d1030 100644 --- a/src/com/xeiam/xcharts/series/markers/Diamond.java +++ b/src/com/xeiam/xcharts/series/markers/Diamond.java @@ -23,30 +23,30 @@ import java.awt.Polygon; */ public class Diamond extends Marker { - @Override - public void paint(Graphics2D g, int xOffset, int yOffset) { + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { - g.setStroke(stroke); + g.setStroke(stroke); - int[] x = new int[4]; - int[] y = new int[4]; - int n = 4; + int[] x = new int[4]; + int[] y = new int[4]; + int n = 4; - // Make a diamond - int halfSize = (int) (Math.ceil((Marker.SIZE + 3) / 2.0)); - x[0] = xOffset - halfSize + 0; - x[1] = xOffset - halfSize + halfSize; - x[2] = xOffset - halfSize + Marker.SIZE + 3; - x[3] = xOffset - halfSize + halfSize; + // Make a diamond + int halfSize = (int) (Math.ceil((Marker.SIZE + 3) / 2.0)); + x[0] = xOffset - halfSize + 0; + x[1] = xOffset - halfSize + halfSize; + x[2] = xOffset - halfSize + Marker.SIZE + 3; + x[3] = xOffset - halfSize + halfSize; - y[0] = 1 + yOffset - halfSize + halfSize; - y[1] = 1 + yOffset - halfSize + Marker.SIZE + 3; - y[2] = 1 + yOffset - halfSize + halfSize; - y[3] = 1 + yOffset - halfSize + 0; + y[0] = 1 + yOffset - halfSize + halfSize; + y[1] = 1 + yOffset - halfSize + Marker.SIZE + 3; + y[2] = 1 + yOffset - halfSize + halfSize; + y[3] = 1 + yOffset - halfSize + 0; - Polygon diamond = new Polygon(x, y, n); - g.fillPolygon(diamond); + Polygon diamond = new Polygon(x, y, n); + g.fillPolygon(diamond); - } + } } diff --git a/src/com/xeiam/xcharts/series/markers/Marker.java b/src/com/xeiam/xcharts/series/markers/Marker.java index 87d0094dd336d629d6cbe0f3a01cb807f04866a9..5f6fd5ef41e5d946fa650481f98cd43927e17b1e 100644 --- a/src/com/xeiam/xcharts/series/markers/Marker.java +++ b/src/com/xeiam/xcharts/series/markers/Marker.java @@ -23,12 +23,12 @@ import java.awt.Graphics2D; */ public abstract class Marker { - protected BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + protected BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); - public static final int SIZE = 7; // make this an odd number! + public static final int SIZE = 7; // make this an odd number! - public static final int X_OFFSET = (int) (-1.0 * (SIZE / 2.0)); - public static final int Y_OFFSET = (int) (-1.0 * (SIZE / 2.0)); + public static final int X_OFFSET = (int) (-1.0 * (SIZE / 2.0)); + public static final int Y_OFFSET = (int) (-1.0 * (SIZE / 2.0)); - public abstract void paint(Graphics2D g, int xOffset, int yOffset); + public abstract void paint(Graphics2D g, int xOffset, int yOffset); } diff --git a/src/com/xeiam/xcharts/series/markers/Square.java b/src/com/xeiam/xcharts/series/markers/Square.java index aba4a1ecb48a18beb4b48d856c967cdd71d0e8b0..a63a03f5e52fe5b4911e5bc251506a78c2665301 100644 --- a/src/com/xeiam/xcharts/series/markers/Square.java +++ b/src/com/xeiam/xcharts/series/markers/Square.java @@ -23,12 +23,12 @@ import java.awt.Rectangle; */ public class Square extends Marker { - @Override - public void paint(Graphics2D g, int xOffset, int yOffset) { + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { - g.setStroke(stroke); - g.fill(new Rectangle(xOffset + Marker.X_OFFSET, yOffset + Marker.Y_OFFSET, Marker.SIZE, Marker.SIZE)); + g.setStroke(stroke); + g.fill(new Rectangle(xOffset + Marker.X_OFFSET, yOffset + Marker.Y_OFFSET, Marker.SIZE, Marker.SIZE)); - } + } } diff --git a/src/com/xeiam/xcharts/series/markers/TriangleDown.java b/src/com/xeiam/xcharts/series/markers/TriangleDown.java index 5c38f968351dd8faf80d89e2ce8a97c40e318c93..f792bc97eff84d08b8ff0452a402e88f6fec504c 100644 --- a/src/com/xeiam/xcharts/series/markers/TriangleDown.java +++ b/src/com/xeiam/xcharts/series/markers/TriangleDown.java @@ -23,27 +23,27 @@ import java.awt.Polygon; */ public class TriangleDown extends Marker { - @Override - public void paint(Graphics2D g, int xOffset, int yOffset) { + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { - g.setStroke(stroke); + g.setStroke(stroke); - int[] x = new int[3]; - int[] y = new int[3]; - int n = 3; + int[] x = new int[3]; + int[] y = new int[3]; + int n = 3; - // Make a triangle - int halfSize = (int) (Math.ceil((Marker.SIZE + 1) / 2.0)); - x[0] = xOffset - halfSize + 0; - x[1] = xOffset - halfSize + halfSize; - x[2] = xOffset - halfSize + Marker.SIZE + 1; + // Make a triangle + int halfSize = (int) (Math.ceil((Marker.SIZE + 1) / 2.0)); + x[0] = xOffset - halfSize + 0; + x[1] = xOffset - halfSize + halfSize; + x[2] = xOffset - halfSize + Marker.SIZE + 1; - y[0] = 1 + yOffset - halfSize + 0; - y[1] = 1 + yOffset - halfSize + Marker.SIZE + 1; - y[2] = 1 + yOffset - halfSize + 0; + y[0] = 1 + yOffset - halfSize + 0; + y[1] = 1 + yOffset - halfSize + Marker.SIZE + 1; + y[2] = 1 + yOffset - halfSize + 0; - Polygon triangle = new Polygon(x, y, n); - g.fillPolygon(triangle); + Polygon triangle = new Polygon(x, y, n); + g.fillPolygon(triangle); - } + } } diff --git a/src/com/xeiam/xcharts/series/markers/TriangleUp.java b/src/com/xeiam/xcharts/series/markers/TriangleUp.java index 8a35f12738a51d9a305db338f32c9df0affae56b..8ac5a60e9879b73d4a7ac12d54c52391872e8855 100644 --- a/src/com/xeiam/xcharts/series/markers/TriangleUp.java +++ b/src/com/xeiam/xcharts/series/markers/TriangleUp.java @@ -23,27 +23,27 @@ import java.awt.Polygon; */ public class TriangleUp extends Marker { - @Override - public void paint(Graphics2D g, int xOffset, int yOffset) { + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { - g.setStroke(stroke); + g.setStroke(stroke); - int[] x = new int[3]; - int[] y = new int[3]; - int n = 3; + int[] x = new int[3]; + int[] y = new int[3]; + int n = 3; - // Make a triangle - int halfSize = (int) (Math.ceil((Marker.SIZE + 1) / 2.0)); - x[0] = xOffset - halfSize + 0; - x[1] = xOffset - halfSize + Marker.SIZE + 1; - x[2] = xOffset - halfSize + halfSize; + // Make a triangle + int halfSize = (int) (Math.ceil((Marker.SIZE + 1) / 2.0)); + x[0] = xOffset - halfSize + 0; + x[1] = xOffset - halfSize + Marker.SIZE + 1; + x[2] = xOffset - halfSize + halfSize; - y[0] = yOffset - halfSize + Marker.SIZE + 1; - y[1] = yOffset - halfSize + Marker.SIZE + 1; - y[2] = yOffset - halfSize + 0; + y[0] = yOffset - halfSize + Marker.SIZE + 1; + y[1] = yOffset - halfSize + Marker.SIZE + 1; + y[2] = yOffset - halfSize + 0; - Polygon triangle = new Polygon(x, y, n); - g.fillPolygon(triangle); + Polygon triangle = new Polygon(x, y, n); + g.fillPolygon(triangle); - } + } }