Skip to content
Snippets Groups Projects
Verified Commit fbb8b7db authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Added starter code for Lab 11

parent 25139ed3
No related branches found
No related tags found
No related merge requests found
Showing
with 2140 additions and 0 deletions
package org.knowm.xchart.demo;
import org.knowm.xchart.demo.charts.ExampleChart;
public final class ChartInfo {
private final String exampleChartName;
private final ExampleChart exampleChart;
/**
* Constructor
*
* @param exampleChartName
* @param exampleChart
*/
public ChartInfo(String exampleChartName, ExampleChart exampleChart) {
this.exampleChartName = exampleChartName;
this.exampleChart = exampleChart;
}
public String getExampleChartName() {
return exampleChartName;
}
public ExampleChart getExampleChart() {
return exampleChart;
}
@Override
public String toString() {
return this.exampleChartName;
}
}
This diff is collapsed.
package org.knowm.xchart.demo;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.internal.series.Series;
import org.knowm.xchart.style.Styler;
public class DemoChartsUtil {
private static final String DEMO_CHARTS_PACKAGE = "org.knowm.xchart.demo.charts";
public static List<ExampleChart<Chart<Styler, Series>>> getAllDemoCharts() {
List<ExampleChart<Chart<Styler, Series>>> demoCharts = null;
String packagePath = DEMO_CHARTS_PACKAGE.replace(".", "/");
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource(packagePath);
if (url != null) {
try {
demoCharts = getAllDemoCharts(url);
} catch (Exception e) {
e.printStackTrace();
}
}
return demoCharts;
}
@SuppressWarnings("unchecked")
private static List<ExampleChart<Chart<Styler, Series>>> getAllDemoCharts(URL url)
throws Exception {
List<ExampleChart<Chart<Styler, Series>>> demoCharts = new ArrayList<>();
List<Class<?>> classes = getAllAssignedClasses(url);
// sort
Collections.sort(
classes,
new Comparator<Class<?>>() {
@Override
public int compare(Class<?> c1, Class<?> c2) {
return c1.getName().compareTo(c2.getName());
}
});
for (Class<?> c : classes) {
demoCharts.add(((ExampleChart<Chart<Styler, Series>>) c.newInstance()));
}
return demoCharts;
}
private static List<Class<?>> getAllAssignedClasses(URL url)
throws ClassNotFoundException, IOException {
List<Class<?>> classes = null;
String type = url.getProtocol();
if ("file".equals(type)) {
classes = getClassesByFile(new File(url.getFile()), DEMO_CHARTS_PACKAGE);
} else if ("jar".equals(type)) {
classes = getClassesByJar(url.getPath());
}
List<Class<?>> allAssignedClasses = new ArrayList<>();
if (classes != null) {
for (Class<?> c : classes) {
if (ExampleChart.class.isAssignableFrom(c) && !ExampleChart.class.equals(c)) {
allAssignedClasses.add(c);
}
}
}
return allAssignedClasses;
}
private static List<Class<?>> getClassesByFile(File dir, String pk)
throws ClassNotFoundException {
List<Class<?>> classes = new ArrayList<>();
if (!dir.exists()) {
return classes;
}
String fileName = "";
for (File f : dir.listFiles()) {
fileName = f.getName();
if (f.isDirectory()) {
classes.addAll(getClassesByFile(f, pk + "." + fileName));
} else if (fileName.endsWith(".class")) {
classes.add(
Class.forName(pk + "." + fileName.substring(0, fileName.length() - ".class".length())));
}
}
return classes;
}
@SuppressWarnings("resource")
private static List<Class<?>> getClassesByJar(String jarPath)
throws IOException, ClassNotFoundException {
List<Class<?>> classes = new ArrayList<>();
String[] jarInfo = jarPath.split("!");
String jarFilePath = jarInfo[0].substring(jarInfo[0].indexOf("/"));
String packagePath = jarInfo[1].substring(1);
Enumeration<JarEntry> entrys = new JarFile(jarFilePath).entries();
JarEntry jarEntry = null;
String entryName = "";
String className = "";
while (entrys.hasMoreElements()) {
jarEntry = entrys.nextElement();
entryName = jarEntry.getName();
if (entryName.endsWith(".class") && entryName.startsWith(packagePath)) {
className = entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));
classes.add(Class.forName(className));
}
}
return classes;
}
}
package org.knowm.xchart.demo;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.WindowConstants;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.demo.charts.RealtimeExampleChart;
import org.knowm.xchart.demo.charts.area.AreaChart01;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.internal.series.Series;
import org.knowm.xchart.style.Styler;
/** Class containing all XChart example charts */
public class XChartDemo extends JPanel implements TreeSelectionListener {
/** The main split frame */
private final JSplitPane splitPane;
/** The tree */
private final JTree tree;
/** The panel for chart */
protected XChartPanel chartPanel;
Timer timer = new Timer();
/** Constructor */
public XChartDemo() {
super(new GridLayout(1, 0));
// Create the nodes.
DefaultMutableTreeNode top = new DefaultMutableTreeNode("XChart Example Charts");
createNodes(top);
// Create a tree that allows one selection at a time.
tree = new JTree(top);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
// Listen for when the selection changes.
tree.addTreeSelectionListener(this);
// Create the scroll pane and add the tree to it.
JScrollPane treeView = new JScrollPane(tree);
// Create Chart Panel
chartPanel = new XChartPanel(new AreaChart01().getChart());
// Add the scroll panes to a split pane.
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(treeView);
splitPane.setBottomComponent(chartPanel);
Dimension minimumSize = new Dimension(130, 160);
treeView.setMinimumSize(minimumSize);
splitPane.setPreferredSize(new Dimension(700, 700));
// Add the split pane to this panel.
add(splitPane);
}
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node == null) {
return;
}
Object nodeInfo = node.getUserObject();
// tree leaf
if (node.isLeaf()) {
ChartInfo chartInfo = (ChartInfo) nodeInfo;
// displayURL(chartInfo.bookURL);
chartPanel = new XChartPanel(chartInfo.getExampleChart().getChart());
splitPane.setBottomComponent(chartPanel);
// start running a simulated data feed for the sample real-time plot
timer.cancel(); // just in case
if (chartInfo.getExampleChart() instanceof RealtimeExampleChart) {
final RealtimeExampleChart realtimeChart =
(RealtimeExampleChart) chartInfo.getExampleChart();
TimerTask chartUpdaterTask =
new TimerTask() {
@Override
public void run() {
realtimeChart.updateData();
chartPanel.revalidate();
chartPanel.repaint();
}
};
timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
}
}
}
/**
* Create the tree
*
* @param top
*/
private void createNodes(DefaultMutableTreeNode top) {
// categories
DefaultMutableTreeNode category = null;
// leaves
DefaultMutableTreeNode defaultMutableTreeNode;
List<ExampleChart<Chart<Styler, Series>>> exampleList = DemoChartsUtil.getAllDemoCharts();
String categoryName = "";
for (ExampleChart exampleChart : exampleList) {
String name = exampleChart.getClass().getSimpleName();
name = name.substring(0, name.indexOf("Chart"));
if (!categoryName.equals(name)) {
String label = name.equals("") ? "Chart Themes" : (name + " Charts");
if (label.equals("Realtime Charts")) {
label = "Real-time Charts";
}
category = new DefaultMutableTreeNode(label);
top.add(category);
categoryName = name;
}
defaultMutableTreeNode =
new DefaultMutableTreeNode(
new ChartInfo(exampleChart.getExampleChartName(), exampleChart));
category.add(defaultMutableTreeNode);
}
}
/**
* Create the GUI and show it. For thread safety, this method should be invoked from the event
* dispatch thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("XChart Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Add content to the window.
frame.add(new XChartDemo());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
package org.knowm.xchart.demo;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.WindowConstants;
import javax.swing.event.TreeSelectionEvent;
import org.knowm.xchart.XChartPanel;
public class XChartStyleDemo extends XChartDemo {
private JSplitPane styleSplitPane;
private ChartStylePanel stylePanel;
public XChartStyleDemo() {
styleSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
styleSplitPane.setLeftComponent(this);
stylePanel = new ChartStylePanel(chartPanel);
styleSplitPane.setRightComponent(stylePanel);
}
@Override
public void valueChanged(TreeSelectionEvent e) {
XChartPanel oldChartPanel = chartPanel;
super.valueChanged(e);
if (chartPanel != oldChartPanel) {
stylePanel.changeChart(chartPanel);
}
}
/**
* Create the GUI and show it. For thread safety, this method should be invoked from the event
* dispatch thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("XChart Style Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
XChartStyleDemo demo = new XChartStyleDemo();
// Add content to the window.
frame.add(demo.styleSplitPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
package org.knowm.xchart.demo.charts;
import org.knowm.xchart.internal.chartpart.Chart;
public interface ExampleChart<C extends Chart<?, ?>> {
C getChart();
String getExampleChartName();
}
package org.knowm.xchart.demo.charts;
public interface RealtimeExampleChart {
public void updateData();
}
package org.knowm.xchart.demo.charts.area;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Area Chart with 3 series
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Area Chart
* <li>Place legend at Inside-NE position
* <li>ChartBuilder
*/
public class AreaChart01 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new AreaChart01();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart =
new XYChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("X")
.yAxisTitle("Y")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNE);
chart.getStyler().setAxisTitlesVisible(false);
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
// Series
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});
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - 3-Series";
}
}
package org.knowm.xchart.demo.charts.area;
import java.util.ArrayList;
import java.util.List;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Null Y-Axis Data Points
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Area Chart
* <li>null Y-Axis values
* <li>ChartBuilder
*/
public class AreaChart02 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new AreaChart02();
XYChart chart = exampleChart.getChart();
new SwingWrapper<XYChart>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart =
new XYChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("X")
.yAxisTitle("Y")
.build();
// Customize Chart
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
// Series
List<Integer> xData = new ArrayList<Integer>();
List<Integer> yData = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
xData.add(i);
yData.add(i * i);
}
xData.add(5);
yData.add(null);
for (int i = 6; i < 10; i++) {
xData.add(i);
yData.add(i * i);
}
xData.add(10);
yData.add(null);
xData.add(11);
yData.add(100);
xData.add(12);
yData.add(90);
chart.addSeries("a", xData, yData);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Null Y-Axis Data Points";
}
}
package org.knowm.xchart.demo.charts.area;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.AxesChartStyler;
import org.knowm.xchart.style.Styler.LegendPosition;
import org.knowm.xchart.style.markers.SeriesMarkers;
/**
* Combination of Line and Area Chart
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Combination of Line and Area series
* <li>Axis Label Alignment
* <li>Ensuring a chart axis on a tick
* <li>Turning off series markers
*/
public class AreaChart03 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new AreaChart03();
XYChart chart = exampleChart.getChart();
new SwingWrapper<XYChart>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart =
new XYChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Age")
.yAxisTitle("Amount")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);
chart.getStyler().setYAxisLabelAlignment(AxesChartStyler.TextAlignment.Right);
chart.getStyler().setYAxisDecimalPattern("$ #,###.##");
chart.getStyler().setPlotMargin(0);
chart.getStyler().setPlotContentSize(.95);
// Series
// @formatter:off
double[] xAges =
new double[] {
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
};
double[] yLiability =
new double[] {
672234, 691729, 711789, 732431, 753671, 775528, 798018, 821160, 844974, 869478, 907735,
887139, 865486, 843023, 819621, 795398, 770426, 744749, 719011, 693176, 667342, 641609,
616078, 590846, 565385, 540002, 514620, 489380, 465149, 441817, 419513, 398465, 377991,
358784, 340920, 323724, 308114, 293097, 279356, 267008, 254873
};
double[] yPercentile75th =
new double[] {
800000, 878736, 945583, 1004209, 1083964, 1156332, 1248041, 1340801, 1440138, 1550005,
1647728, 1705046, 1705032, 1710672, 1700847, 1683418, 1686522, 1674901, 1680456, 1679164,
1668514, 1672860, 1673988, 1646597, 1641842, 1653758, 1636317, 1620725, 1589985, 1586451,
1559507, 1544234, 1529700, 1507496, 1474907, 1422169, 1415079, 1346929, 1311689, 1256114,
1221034
};
double[] yPercentile50th =
new double[] {
800000, 835286, 873456, 927048, 969305, 1030749, 1101102, 1171396, 1246486, 1329076,
1424666, 1424173, 1421853, 1397093, 1381882, 1364562, 1360050, 1336885, 1340431, 1312217,
1288274, 1271615, 1262682, 1237287, 1211335, 1191953, 1159689, 1117412, 1078875, 1021020,
974933, 910189, 869154, 798476, 744934, 674501, 609237, 524516, 442234, 343960, 257025
};
double[] yPercentile25th =
new double[] {
800000, 791439, 809744, 837020, 871166, 914836, 958257, 1002955, 1054094, 1118934,
1194071, 1185041, 1175401, 1156578, 1132121, 1094879, 1066202, 1054411, 1028619, 987730,
944977, 914929, 880687, 809330, 783318, 739751, 696201, 638242, 565197, 496959, 421280,
358113, 276518, 195571, 109514, 13876, 29, 0, 0, 0, 0
};
// @formatter:on
XYSeries series = chart.addSeries("Liability", xAges, yLiability);
series.setXYSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Area);
series.setMarker(SeriesMarkers.NONE);
chart.addSeries("75th Percentile", xAges, yPercentile75th);
chart.addSeries("50th Percentile", xAges, yPercentile50th);
chart.addSeries("25th Percentile", xAges, yPercentile25th);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Combination Area & Line Chart";
}
}
package org.knowm.xchart.demo.charts.area;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Area Chart with 3 series
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Step Area Chart
* <li>Place legend at Inside-NE position
* <li>ChartBuilder
*/
public class AreaChart04 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new AreaChart04();
XYChart chart = exampleChart.getChart();
new SwingWrapper<XYChart>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart =
new XYChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("X")
.yAxisTitle("Y")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNE);
chart.getStyler().setAxisTitlesVisible(false);
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.StepArea);
// Series
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});
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Step Area Rendering";
}
}
package org.knowm.xchart.demo.charts.area;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Area Chart with 1 series
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Polygon Area Chart
* <li>Place legend at Inside-NE position
* <li>ChartBuilder
*/
public class AreaChart05 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new AreaChart05();
XYChart chart = exampleChart.getChart();
new SwingWrapper<XYChart>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart =
new XYChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("X")
.yAxisTitle("Y")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNE);
chart.getStyler().setAxisTitlesVisible(false);
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.PolygonArea);
// Series
chart.addSeries(
"a",
new double[] {
0, 3, 5, 7, 9, // x coordinates ascending
9, 7, 5, 3, 0
}, // x coordinates descending
new double[] {
-1, 6, 9, 6, 5, // upper y
4, 0, 4, 5, -3
}); // lower y
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Polygon Area Rendering";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.util.Arrays;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Basic Bar Chart
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Integer categories as List
* <li>All positive values
* <li>Single series
* <li>Place legend at Inside-NW position
* <li>Bar Chart Annotations
*/
public class BarChart01 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart01();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Score")
.yAxisTitle("Number")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setLabelsVisible(false);
chart.getStyler().setPlotGridLinesVisible(false);
// Series
chart.addSeries("test 1", Arrays.asList(0, 1, 2, 3, 4), Arrays.asList(4, 5, 9, 6, 5));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Basic Bar Chart";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.awt.Color;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.CategorySeries;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.ChartTheme;
/**
* Date Categories
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Date categories as List
* <li>All negative values
* <li>Single series
* <li>No horizontal plot gridlines
* <li>Change series color
* <li>MATLAB Theme
*/
public class BarChart02 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart02();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.theme(ChartTheme.Matlab)
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Year")
.yAxisTitle("Units Sold")
.build();
// Customize Chart
chart.getStyler().setPlotGridLinesVisible(false);
chart.getStyler().setDatePattern("yyyy");
// Series
List<Date> xData = new ArrayList<Date>();
List<Number> yData = new ArrayList<Number>();
Random random = new Random();
DateFormat sdf = new SimpleDateFormat("yyyy");
Date date = null;
for (int i = 1; i <= 8; i++) {
try {
date = sdf.parse("" + (2000 + i));
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(-1 * 0.00000001 * ((random.nextInt(i) + 1)));
}
CategorySeries series = chart.addSeries("Model 77", xData, yData);
series.setFillColor(new Color(230, 150, 150));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Date Categories";
}
}
package org.knowm.xchart.demo.charts.bar;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
/**
* Stacked Bar Chart
*
* <p>Demonstrates the following:
*
* <ul>
* <li>int categories as array
* <li>Positive and negative values
* <li>data labels and stack sum labels
*/
public class BarChart03 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart03();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Age")
.yAxisTitle("Score")
.build();
// Customize Chart
chart.getStyler().setPlotGridVerticalLinesVisible(false);
chart.getStyler().setStacked(true);
chart.getStyler().setLabelsVisible(true);
chart.getStyler().setShowStackSum(true);
// Series
chart.addSeries("males", new int[] {10, 20, 30, 40}, new int[] {40, -30, -20, -60});
chart.addSeries("females", new int[] {10, 20, 30, 40}, new int[] {45, -35, -25, 65});
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Stacked Bar Chart with Data and Stack Sum Labels";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.util.Arrays;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler;
/**
* Missing Point in Series
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Number categories
* <li>Positive values
* <li>Multiple series
* <li>Missing point in series
* <li>Manually setting y-axis min and max values
* <li>Bar Chart Annotations
* <li>Horizontal Legend OutsideS
*/
public class BarChart04 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart04();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Age")
.yAxisTitle("XFactor")
.build();
// Customize Chart
chart.getStyler().setYAxisMin(5.0);
chart.getStyler().setYAxisMax(70.0);
chart.getStyler().setLabelsVisible(true);
chart.getStyler().setPlotGridVerticalLinesVisible(false);
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
// Series
chart.addSeries("female", Arrays.asList(10, 20, 30, 40, 50), Arrays.asList(50, 10, 20, 40, 35));
chart.addSeries("male", Arrays.asList(10, 20, 30, 40, 50), Arrays.asList(40, 30, 20, null, 60));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Missing Point in Series";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.util.ArrayList;
import java.util.Arrays;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.ChartTheme;
/**
* GGPlot2 Theme Bar chart
*
* <p>Demonstrates the following:
*
* <ul>
* <li>String categories
* <li>Positive and negative values
* <li>Multiple series
*/
public class BarChart05 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart05();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Color")
.yAxisTitle("Temperature")
.theme(ChartTheme.GGPlot2)
.build();
// Customize Chart
chart.getStyler().setPlotGridVerticalLinesVisible(false);
// Series
chart.addSeries(
"fish",
new ArrayList<>(Arrays.asList(new String[] {"Blue", "Red", "Green", "Yellow", "Orange"})),
new ArrayList<Number>(Arrays.asList(new Number[] {-40, 30, 20, 60, 60})));
chart.addSeries(
"worms",
new ArrayList<>(Arrays.asList(new String[] {"Blue", "Red", "Green", "Yellow", "Orange"})),
new ArrayList<Number>(Arrays.asList(new Number[] {50, 10, -20, 40, 60})));
chart.addSeries(
"birds",
new ArrayList<>(Arrays.asList(new String[] {"Blue", "Red", "Green", "Yellow", "Orange"})),
new ArrayList<Number>(Arrays.asList(new Number[] {13, 22, -23, -34, 37})));
chart.addSeries(
"ants",
new ArrayList<>(Arrays.asList(new String[] {"Blue", "Red", "Green", "Yellow", "Orange"})),
new ArrayList<Number>(Arrays.asList(new Number[] {50, 57, -14, -20, 31})));
chart.addSeries(
"slugs",
new ArrayList<>(Arrays.asList(new String[] {"Blue", "Red", "Green", "Yellow", "Orange"})),
new ArrayList<Number>(Arrays.asList(new Number[] {-2, 29, 49, -16, -43})));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - GGPlot2 Theme";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.Histogram;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Histogram Overlapped
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Histogram
* <li>Bar Chart styles - overlapped, bar width
*/
public class BarChart06 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart06();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Mean")
.yAxisTitle("Count")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setAvailableSpaceFill(.96);
chart.getStyler().setOverlapped(true);
chart.getStyler().setPlotGridVerticalLinesVisible(false);
// Series
Histogram histogram1 = new Histogram(getGaussianData(10000), 20, -20, 20);
Histogram histogram2 = new Histogram(getGaussianData(5000), 20, -20, 20);
chart.addSeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
chart.addSeries("histogram 2", histogram2.getxAxisData(), histogram2.getyAxisData());
return chart;
}
private List<Double> getGaussianData(int count) {
List<Double> data = new ArrayList<Double>(count);
Random r = new Random();
for (int i = 0; i < count; i++) {
data.add(r.nextGaussian() * 10);
}
return data;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Histogram Overlapped";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.Histogram;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Histogram Not Overlapped
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Histogram
* <li>Bar Chart styles - not overlapped, bar width
* <li>Integer data values
* <li>Tool Tips
*/
public class BarChart07 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart07();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Mean")
.yAxisTitle("Count")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setAvailableSpaceFill(.96);
chart.getStyler().setPlotGridVerticalLinesVisible(false);
chart.getStyler().setToolTipsEnabled(true);
chart.getStyler().setToolTipType(Styler.ToolTipType.yLabels);
// Series
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());
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));
}
return data;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Histogram Not Overlapped with Tool Tips";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.Histogram;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.ChartTheme;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Histogram with Error Bars
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Histogram
* <li>Bar Chart with error bars
*/
public class BarChart08 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart08();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<CategoryChart>(chart).displayChart();
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Mean")
.theme(ChartTheme.Matlab)
.yAxisTitle("Count")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setAvailableSpaceFill(.96);
// Series
Histogram histogram1 = new Histogram(getGaussianData(10000), 10, -10, 10);
chart.addSeries(
"histogram",
histogram1.getxAxisData(),
histogram1.getyAxisData(),
getFakeErrorData(histogram1.getxAxisData().size()));
return chart;
}
private List<Double> getGaussianData(int count) {
List<Double> data = new ArrayList<Double>(count);
Random r = new Random();
for (int i = 0; i < count; i++) {
data.add(r.nextGaussian() * 5);
// data.add(r.nextDouble() * 60 - 30);
}
return data;
}
private List<Double> getFakeErrorData(int count) {
List<Double> data = new ArrayList<Double>(count);
Random r = new Random();
for (int i = 0; i < count; i++) {
data.add(r.nextDouble() * 200);
}
return data;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Histogram with Error Bars";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment