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

added histogram demo charts

parent 2201bc42
Branches
No related tags found
No related merge requests found
......@@ -38,6 +38,8 @@ import com.xeiam.xchart.demo.charts.bar.BarChart02;
import com.xeiam.xchart.demo.charts.bar.BarChart03;
import com.xeiam.xchart.demo.charts.bar.BarChart04;
import com.xeiam.xchart.demo.charts.bar.BarChart05;
import com.xeiam.xchart.demo.charts.bar.BarChart06;
import com.xeiam.xchart.demo.charts.bar.BarChart07;
import com.xeiam.xchart.demo.charts.date.DateChart01;
import com.xeiam.xchart.demo.charts.date.DateChart02;
import com.xeiam.xchart.demo.charts.date.DateChart03;
......@@ -192,7 +194,7 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("AreaChart02 - Null Y-Axis Data Points", new AreaChart02().getChart()));
category.add(defaultMutableTreeNode);
// First category
// Line category
category = new DefaultMutableTreeNode("Line Charts");
top.add(category);
......@@ -249,6 +251,12 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BarChart05 - GGPlot2 Theme", new BarChart05().getChart()));
category.add(defaultMutableTreeNode);
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BarChart06 - Histogram Overlapped", new BarChart06().getChart()));
category.add(defaultMutableTreeNode);
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BarChart07 - Histogram Not Overlapped", new BarChart07().getChart()));
category.add(defaultMutableTreeNode);
// Theme category
category = new DefaultMutableTreeNode("Chart Themes");
top.add(category);
......
......@@ -21,17 +21,19 @@ import java.util.Random;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.ChartBuilder;
import com.xeiam.xchart.Histogram;
import com.xeiam.xchart.StyleManager.ChartType;
import com.xeiam.xchart.StyleManager.LegendPosition;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.ExampleChart;
/**
* Basic Bar Chart
* Histogram Overlapped
* <p>
* Demonstrates the following:
* <ul>
* <li>Histogram
* <li>Bar Chart styles - overlapped, bar width
*/
public class BarChart06 implements ExampleChart {
......@@ -46,15 +48,18 @@ public class BarChart06 implements ExampleChart {
public Chart getChart() {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Score Histogram").xAxisTitle("Score").yAxisTitle("Count").build();
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Score Histogram").xAxisTitle("Mean").yAxisTitle("Count").build();
chart.addSeries("histogram 1", getGaussianData(10000), 30, -30, 30);
chart.addSeries("histogram 2", getGaussianData(5000), 30, -30, 30);
Histogram histogram1 = new Histogram(getGaussianData(10000), 30, -30, 30);
Histogram histogram2 = new Histogram(getGaussianData(5000), 30, -30, 30);
chart.addSeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
chart.addSeries("histogram 2", histogram2.getxAxisData(), histogram2.getyAxisData());
// Customize Chart
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
chart.getStyleManager().setBarWidthPercentage(.96);
chart.getStyleManager().setOverlapped(false);
chart.getStyleManager().setOverlapped(true);
return chart;
}
......
/**
* Copyright 2011 - 2014 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.bar;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.ChartBuilder;
import com.xeiam.xchart.Histogram;
import com.xeiam.xchart.StyleManager.ChartType;
import com.xeiam.xchart.StyleManager.LegendPosition;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.ExampleChart;
/**
* Histogram Not Overlapped
* <p>
* Demonstrates the following:
* <ul>
* <li>Histogram
* <li>Bar Chart styles - not overlapped, bar width
* <li>Integer data values
*/
public class BarChart07 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new BarChart07();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Score Histogram").xAxisTitle("Mean").yAxisTitle("Count").build();
Histogram histogram1 = new Histogram(getGaussianData(1000), 10, -30, 30);
chart.addSeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
Histogram histogram2 = new Histogram(getGaussianData(1000), 10, -30, 30);
chart.addSeries("histogram 2", histogram2.getxAxisData(), histogram2.getyAxisData());
// Customize Chart
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
chart.getStyleManager().setBarWidthPercentage(.96);
return chart;
}
private List<Integer> getGaussianData(int count) {
List<Integer> data = new ArrayList<Integer>(count);
Random r = new Random();
for (int i = 0; i < count; i++) {
data.add((int) (r.nextGaussian() * 10));
// data.add(r.nextDouble() * 60 - 30);
}
return data;
}
}
......@@ -22,7 +22,6 @@ import java.util.List;
import java.util.Map;
import com.xeiam.xchart.StyleManager.ChartTheme;
import com.xeiam.xchart.internal.Histogram;
import com.xeiam.xchart.internal.chartpart.ChartPainter;
import com.xeiam.xchart.internal.style.Theme;
......@@ -222,22 +221,22 @@ public class Chart {
return chartPainter.getAxisPair().addSeries(seriesName, xDataNumber, yDataNumber, errorBarDataNumber);
}
/**
* Add a series for a histogram
*
* @param seriesName
* @param data
* @param numBins
* @param min
* @param max
* @return
*/
public Series addSeries(String seriesName, Collection<? extends Number> data, int numBins, double min, double max) {
Histogram histogram1 = new Histogram(data, numBins, min, max);
return chartPainter.getAxisPair().addSeries(seriesName, histogram1.getxAxisData(), histogram1.getyAxisData(), null);
}
// /**
// * Add a series for a histogram
// *
// * @param seriesName
// * @param data
// * @param numBins
// * @param min
// * @param max
// * @return
// */
// public Series addSeries(String seriesName, Collection<? extends Number> data, int numBins, double min, double max) {
//
// Histogram histogram1 = new Histogram(data, numBins, min, max);
//
// return chartPainter.getAxisPair().addSeries(seriesName, histogram1.getxAxisData(), histogram1.getyAxisData(), null);
// }
/**
* Set the chart title
......
......@@ -13,20 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.internal;
package com.xeiam.xchart;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* This class can be used to create histogram data for histogram bar charts
*
* @author timmolter
*/
public class Histogram {
private final List<Double> xAxisData; // bin centers
private final List<Double> yAxisData; // frequency counts
private List<Double> xAxisData; // bin centers
private List<Double> yAxisData; // frequency counts
private final Collection<? extends Number> originalData;
private final int numBins;
private final double min;
......@@ -34,6 +37,34 @@ public class Histogram {
/**
* Constructor
*
* @param data
* @param numBins
*/
public Histogram(Collection<? extends Number> data, int numBins) {
this.numBins = numBins;
this.originalData = data;
List<Double> dataAsList = new ArrayList<Double>();
Iterator<? extends Number> itr = data.iterator();
while (itr.hasNext()) {
dataAsList.add(((Number) itr.next()).doubleValue());
}
Collections.sort(dataAsList);
this.min = dataAsList.get(0);
this.max = dataAsList.get(dataAsList.size() - 1);
init();
}
/**
* Constructor
*
* @param data
* @param numBins
* @param min
* @param max
*/
public Histogram(Collection<? extends Number> data, int numBins, double min, double max) {
......@@ -45,14 +76,19 @@ public class Histogram {
// this.min = data[0];
// this.max = data[data.length - 1];
init();
}
private void init() {
double[] tempYAxisData = new double[numBins];
final double binSize = (max - min) / numBins;
// y axis data
Iterator<? extends Number> itr = data.iterator();
Iterator<? extends Number> itr = originalData.iterator();
while (itr.hasNext()) {
int bin = (int) (((Double) itr.next() - min) / binSize); // changed this from numBins
int bin = (int) ((((Number) itr.next()).doubleValue() - min) / binSize); // changed this from numBins
if (bin < 0) { /* this data is smaller than min */
}
else if (bin >= numBins) { /* this data point is bigger than max */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment