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

area charts

parent 5453316b
No related branches found
No related tags found
No related merge requests found
Showing with 156 additions and 27 deletions
......@@ -29,6 +29,7 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import com.xeiam.xchart.XChartPanel;
import com.xeiam.xchart.demo.charts.area.AreaChart01;
import com.xeiam.xchart.demo.charts.line.LineChart01;
import com.xeiam.xchart.demo.charts.line.LineChart02;
import com.xeiam.xchart.demo.charts.line.LineChart03;
......@@ -167,7 +168,14 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
category = new DefaultMutableTreeNode("Scatter Charts");
top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart01 - Gaussian Blob Scatter Plot", new ScatterChart01().getChart()));
chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart01 - Gaussian Blob", new ScatterChart01().getChart()));
category.add(chart);
// Second category
category = new DefaultMutableTreeNode("Area Charts");
top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("AreaChart01 - 3-Series", new AreaChart01().getChart()));
category.add(chart);
}
......
/**
* Copyright 2011-2013 Xeiam LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.demo.charts.area;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.ChartBuilder;
import com.xeiam.xchart.ChartBuilder.ChartType;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.line.ExampleChart;
import com.xeiam.xchart.style.StyleManager.LegendPosition;
/**
* 3-Series
*
* @author timmolter
*/
public class AreaChart01 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new AreaChart01();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Area).width(800).height(600).title("LineChart11").xAxisTitle("X").yAxisTitle("Y").build();
chart.addSeries("a", new double[] { 0, 3, 5, 7, 9 }, new double[] { -3, 5, 9, 6, 5 });
chart.addSeries("b", new double[] { 0, 2, 4, 6, 9 }, new double[] { -1, 6, 4, 0, 4 });
chart.addSeries("c", new double[] { 0, 1, 3, 8, 9 }, new double[] { -2, -1, 1, 0, 1 });
// Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
return chart;
}
}
......@@ -29,9 +29,8 @@ import com.xeiam.xchart.style.SeriesMarker;
* <p>
* Demonstrates the following:
* <ul>
* <li>Series data hardcoded, perhaps copied from elsewhere</li>
* <li>LineChart without series markers</li>
* </ul>
* <li>Series data hardcoded, perhaps copied from elsewhere
* <li>LineChart without series markers
*
* @author timmolter
*/
......
......@@ -31,6 +31,11 @@ import com.xeiam.xchart.style.StyleManager.LegendPosition;
/**
* Date Axis
* <p>
* Demonstrates the following:
* <ul>
* <li>Date-tyoe X Axis Data
* <li>Placing legend inside northwest corner of plot area
*/
public class LineChart04 implements ExampleChart {
......
......@@ -25,7 +25,7 @@ import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.line.ExampleChart;
/**
* Gaussian Blob Scatter Plot
* Gaussian Blob
*
* @author timmolter
*/
......@@ -41,7 +41,6 @@ public class ScatterChart01 implements ExampleChart {
@Override
public Chart getChart() {
// generates sine data
List<Number> xData = new ArrayList<Number>();
List<Number> yData = new ArrayList<Number>();
Random random = new Random();
......
/**
* Copyright (C) 2013 Xeiam LLC http://xeiam.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.xeiam.xchart;
/**
* @author timmolter
*/
public class AreaChart extends Chart {
/**
* Constructor
*
* @param width
* @param height
*/
public AreaChart(int width, int height) {
super(width, height);
}
/**
* Constructor
*
* @param chartBuilder
*/
public AreaChart(ChartBuilder chartBuilder) {
super(chartBuilder);
}
}
......@@ -31,7 +31,7 @@ public class ChartBuilder {
public enum ChartType {
Line, Scatter
Line, Scatter, Area
}
protected ChartType chartType = ChartType.Line;
......@@ -96,6 +96,8 @@ public class ChartBuilder {
return new LineChart(this);
case Scatter:
return new ScatterChart(this);
case Area:
return new AreaChart(this);
default:
return new LineChart(this);
}
......
......@@ -25,6 +25,7 @@ import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import com.xeiam.xchart.AreaChart;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.ScatterChart;
import com.xeiam.xchart.internal.chartpart.Axis.AxisType;
......@@ -60,6 +61,7 @@ public class PlotContent implements ChartPart {
public void paint(Graphics2D g) {
boolean isScatterChart = getChart() instanceof ScatterChart;
boolean isAreaChart = getChart() instanceof AreaChart;
Rectangle bounds = plot.getBounds();
......@@ -112,23 +114,21 @@ public class PlotContent implements ChartPart {
eb = ebItr.next().doubleValue();
}
// int xTransform = (int) (xLeftMargin + ((x - xMin) / (xMax - xMin) * xTickSpace));
int xTransform = (int) (xLeftMargin + (x.subtract(xMin).doubleValue() / xMax.subtract(xMin).doubleValue() * xTickSpace));
// int yTransform = (int) (bounds.getHeight() - (yTopMargin + (y - yMin) / (yMax - yMin) * yTickSpace));
int yTransform = (int) (bounds.getHeight() - (yTopMargin + y.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace));
int yBottomOfArea = (int) (bounds.getHeight() - (yTopMargin + y.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace));
// a check if all y data are the exact same values
// a check if all x data are the exact same values
if (Math.abs(xMax.subtract(xMin).doubleValue()) / 5 == 0.0) {
xTransform = (int) (bounds.getWidth() / 2.0);
}
// a check if all y data are the exact same values
if (Math.abs(yMax.subtract(yMin).doubleValue()) / 5 == 0.0) {
yTransform = (int) (bounds.getHeight() / 2.0);
yBottomOfArea = (int) (bounds.getHeight() / 2.0);
}
int xOffset = (int) (bounds.getX() + xTransform - 1);
int yOffset = (int) (bounds.getY() + yTransform);
int yOffset = (int) (bounds.getY() + yBottomOfArea);
// System.out.println(yOffset);
// System.out.println(yTransform);
......@@ -139,10 +139,21 @@ public class PlotContent implements ChartPart {
g.setStroke(series.getStroke());
g.drawLine(previousX, previousY, xOffset, yOffset);
}
previousX = xOffset;
previousY = yOffset;
}
// paint area
if (isAreaChart) {
if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) {
g.setColor(series.getStrokeColor());
yBottomOfArea = (int) (bounds.getY() + bounds.getHeight() - yTopMargin + 1);
g.fillPolygon(new int[] { previousX, xOffset, xOffset, previousX }, new int[] { previousY, yOffset, yBottomOfArea, yBottomOfArea }, 4);
}
}
previousX = xOffset;
previousY = yOffset;
// paint marker
if (series.getMarker() != null) {
g.setColor(series.getMarkerColor());
......
......@@ -25,40 +25,40 @@ import java.awt.Color;
public enum SeriesColor {
/** BLUE */
BLUE(0, new Color(0, 55, 255)),
BLUE(0, new Color(0, 55, 255, 180)),
/** ORANGE */
ORANGE(1, new Color(255, 172, 0)),
ORANGE(1, new Color(255, 172, 0, 180)),
/** PURPLE */
PURPLE(2, new Color(128, 0, 255)),
PURPLE(2, new Color(128, 0, 255, 180)),
/** GREEN */
GREEN(3, new Color(0, 205, 0)),
GREEN(3, new Color(0, 205, 0, 180)),
/** RED */
RED(4, new Color(205, 0, 0)),
RED(4, new Color(205, 0, 0, 180)),
/** YELLOW */
YELLOW(5, new Color(255, 215, 0)),
YELLOW(5, new Color(255, 215, 0, 180)),
/** MAGENTA */
MAGENTA(6, new Color(255, 0, 255)),
MAGENTA(6, new Color(255, 0, 255, 180)),
/** PINK */
PINK(7, new Color(255, 166, 201)),
PINK(7, new Color(255, 166, 201, 180)),
/** LIGHT_GREY */
LIGHT_GREY(8, new Color(207, 207, 207)),
LIGHT_GREY(8, new Color(207, 207, 207, 180)),
/** CYAN */
CYAN(9, new Color(0, 255, 255)),
CYAN(9, new Color(0, 255, 255, 180)),
/** BROWN */
BROWN(10, new Color(102, 56, 10)),
BROWN(10, new Color(102, 56, 10, 180)),
/** BLACK */
BLACK(11, new Color(0, 0, 0));
BLACK(11, new Color(0, 0, 0, 180));
/** The index */
private int index;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment