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

Added starter code for Week 11 lab

parent 776f9984
Branches
Tags
No related merge requests found
Showing
with 1569 additions and 0 deletions
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.CategorySeries;
import org.knowm.xchart.CategorySeries.CategorySeriesRenderStyle;
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;
/**
* Category chart with Bar, Line and Scatter Series
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Mixed series types - Bar, Line and Scatter
* <li>Bar Chart styles - overlapped, bar width
*/
public class BarChart09 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart09();
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("Letter")
.yAxisTitle("Value")
.theme(ChartTheme.GGPlot2)
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setAvailableSpaceFill(.55);
chart.getStyler().setOverlapped(true);
// Series
chart.addSeries(
"China",
new ArrayList<>(Arrays.asList(new String[] {"A", "B", "C", "D", "E"})),
new ArrayList<>(Arrays.asList(new Number[] {11, 23, 20, 36, 5})));
CategorySeries series2 =
chart.addSeries(
"Korea",
new ArrayList<>(Arrays.asList(new String[] {"A", "B", "C", "D", "E"})),
new ArrayList<>(Arrays.asList(new Number[] {13, 25, 22, 38, 7})),
new ArrayList<>(Arrays.asList(new Number[] {1, 3, 2, 1, 2})));
series2.setChartCategorySeriesRenderStyle(CategorySeriesRenderStyle.Line);
CategorySeries series3 =
chart.addSeries(
"World Ave.",
new ArrayList<>(Arrays.asList(new String[] {"A", "B", "C", "D", "E"})),
new ArrayList<>(Arrays.asList(new Number[] {20, 22, 18, 36, 32})));
series3.setChartCategorySeriesRenderStyle(CategorySeriesRenderStyle.Scatter);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Category chart with Bar, Line and Scatter Series";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.awt.BasicStroke;
import java.awt.Color;
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.CategorySeries;
import org.knowm.xchart.CategorySeries.CategorySeriesRenderStyle;
import org.knowm.xchart.Histogram;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
* Stepped Chart Histogram
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Histogram
* <li>Bar Chart styles - overlapped
* <li>Custom Line Style
* <li>Render style - Stepped Bars
*/
public class BarChart10 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart10();
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")
.yAxisTitle("Count")
.build();
// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setAvailableSpaceFill(.96);
chart.getStyler().setPlotGridVerticalLinesVisible(false);
// While supported, SteppedBars in anything but overlapped mode are fairly useless.
chart.getStyler().setOverlapped(true);
// Series
Histogram histogram1 = new Histogram(getGaussianData(10000), 20, -20, 20);
Histogram histogram2 = new Histogram(getGaussianData(5000), 20, -20, 20);
CategorySeries series1 =
chart.addSeries("histogram 2", histogram2.getxAxisData(), histogram2.getyAxisData());
CategorySeries series2 =
chart.addSeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
// Set both series to SteppedBar
series2.setChartCategorySeriesRenderStyle(CategorySeriesRenderStyle.SteppedBar);
series1.setChartCategorySeriesRenderStyle(CategorySeriesRenderStyle.SteppedBar);
// Remove the outline from the first series
series1.setLineColor(new Color(0, 0, 0, 0));
// Make the fill of the second series transparent, leaving us with only the outline
series2.setFillColor(new Color(0, 0, 0, 0));
// Also give it a nice dotted-line apperance
BasicStroke baseLineStyle = new BasicStroke();
BasicStroke newLineStyle =
new BasicStroke(
2f,
baseLineStyle.getEndCap(),
baseLineStyle.getLineJoin(),
baseLineStyle.getMiterLimit(),
new float[] {5, 5},
baseLineStyle.getDashPhase());
series2.setLineStyle(newLineStyle);
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() + " - Stepped Bars with Line Styling";
}
}
package org.knowm.xchart.demo.charts.bar;
import java.awt.Color;
import java.awt.Font;
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;
/**
* Stacked Bar Chart
*
* <p>Demonstrates the following:
*
* <ul>
* <li>int categories as array
* <li>customized data labels
*/
public class BarChart11 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart11();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
private static int[] getRandomValues(int startRange, int endRange, int count) {
int[] values = new int[count];
Random rand = new Random();
for (int i = 0; i < count; i++) {
values[i] = rand.nextInt(endRange - startRange) + startRange;
}
return values;
}
private static int[] getLinearValues(int startRange, int endRange, int count) {
int[] values = new int[count];
int step = (endRange - startRange) / count;
for (int i = 0; i < count; i++) {
values[i] = step * i;
}
return values;
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Speed")
.yAxisTitle("Spin")
.build();
// Customize Chart
chart.getStyler().setPlotGridVerticalLinesVisible(false);
chart.getStyler().setLegendVisible(false);
chart.getStyler().setStacked(true);
chart.getStyler().setLabelsVisible(true);
chart.getStyler().setLabelsFont(new Font(Font.MONOSPACED, Font.BOLD, 13));
chart.getStyler().setLabelsFontColor(Color.WHITE);
chart.getStyler().setLabelsPosition(.5);
chart.getStyler().setLabelsFontColorAutomaticEnabled(false);
chart.getStyler().setLabelsRotation(45);
// Series
CategorySeries series1 =
chart.addSeries("series1", getLinearValues(0, 200, 6), getRandomValues(10, 50, 6));
CategorySeries series2 =
chart.addSeries("series2", getLinearValues(0, 200, 6), getRandomValues(10, 50, 6));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Stacked Stepped Bars with Customized Data Labels";
}
}
/*
* Copyright (c) 2024 Energía Plus. All rights reserved.
*/
package org.knowm.xchart.demo.charts.bar;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
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;
/**
* Stacked Bars with Overlapped Line Chart
*
* <p>Demonstrates the following:
*
* <ul>
* <li>bar series are stacked
* <li>line series is overlapped
*/
public class BarChart12 implements ExampleChart<CategoryChart> {
public static void main(String[] args) {
ExampleChart<CategoryChart> exampleChart = new BarChart12();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
private static List<String> getMonths() {
return Arrays.asList(
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
}
private static List<Double> getRandomValues(int count) {
List<Double> values = new ArrayList<>(count);
Random rand = new Random();
for (int i = 0; i < count; i++) {
values.add(rand.nextDouble() * 1000);
}
return values;
}
@Override
public CategoryChart getChart() {
// Create Chart
CategoryChart chart =
new CategoryChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("Month")
.yAxisTitle("Consumption")
.build();
// Customize Chart
chart.getStyler().setPlotGridVerticalLinesVisible(false);
chart.getStyler().setLegendVisible(true);
chart.getStyler().setStacked(true);
chart
.getStyler()
.setSeriesColors(
new Color[] {
Color.decode("#2133D0"),
Color.decode("#FF3B47"),
Color.decode("#FFBD00"),
Color.DARK_GRAY
});
List<String> months = getMonths();
List<Double> period1Values = getRandomValues(12);
List<Double> period2Values = getRandomValues(12);
List<Double> period3Values = getRandomValues(12);
List<Double> averageValues = new ArrayList<>();
for (int i = 0; i < 12; i++) {
averageValues.add((period1Values.get(i) + period2Values.get(i) + period3Values.get(i)) / 3);
}
// Series
CategorySeries staked1 = chart.addSeries("Period 1", months, period1Values);
CategorySeries staked2 = chart.addSeries("Period 2", months, period2Values);
CategorySeries staked3 = chart.addSeries("Period 3", months, period3Values);
CategorySeries overlappedLine = chart.addSeries("Average", months, averageValues);
overlappedLine.setOverlapped(true);
overlappedLine.setChartCategorySeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Line);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Stacked Bars with Overlapped Line Chart";
}
}
package org.knowm.xchart.demo.charts.box;
import java.util.Arrays;
import org.knowm.xchart.BoxChart;
import org.knowm.xchart.BoxChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.ChartTheme;
/*
* Box Chart with 3 series
*/
public class BoxChart01 implements ExampleChart<BoxChart> {
public static void main(String[] args) {
ExampleChart<BoxChart> exampleChart = new BoxChart01();
BoxChart chart = exampleChart.getChart();
new SwingWrapper<BoxChart>(chart).displayChart();
}
@Override
public BoxChart getChart() {
// Create Chart
BoxChart chart =
new BoxChartBuilder()
.title("box plot demo")
.xAxisTitle("X")
.yAxisTitle("Y")
.theme(ChartTheme.GGPlot2)
.build();
// Series
chart.addSeries("aaa", Arrays.asList(40, 30, 20, 60, 50));
chart.addSeries("bbb", Arrays.asList(-20, -10, -30, -15, -25));
chart.addSeries("ccc", Arrays.asList(50, -20));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + "- 3 Boxes";
}
}
package org.knowm.xchart.demo.charts.box;
import java.util.Arrays;
import org.knowm.xchart.BoxChart;
import org.knowm.xchart.BoxChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.BoxStyler.BoxplotCalCulationMethod;
import org.knowm.xchart.style.Styler.ChartTheme;
/*
* Box Plot with 3 series
* plot data points
*/
public class BoxChart02 implements ExampleChart<BoxChart> {
public static void main(String[] args) {
ExampleChart<BoxChart> exampleChart = new BoxChart02();
BoxChart chart = exampleChart.getChart();
new SwingWrapper<BoxChart>(chart).displayChart();
}
@Override
public BoxChart getChart() {
// Create Chart
BoxChart chart =
new BoxChartBuilder()
.title("box plot show all points")
.xAxisTitle("X")
.yAxisTitle("Y")
.theme(ChartTheme.Matlab)
.build();
// Customize Chart
chart.getStyler().setBoxplotCalCulationMethod(BoxplotCalCulationMethod.N_LESS_1_PLUS_1);
// Series
chart.addSeries("aaa", Arrays.asList(1, 2, 3, 4, 5, 6));
chart.addSeries("bbb", Arrays.asList(1, 2, 3, 4, 5, 6, 17));
chart.addSeries("ccc", Arrays.asList(-10, -8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21));
chart.getStyler().setShowWithinAreaPoint(true);
chart.getStyler().setToolTipsEnabled(true);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + "- Show normal points and abnormal points";
}
}
package org.knowm.xchart.demo.charts.box;
import java.util.Arrays;
import org.knowm.xchart.BoxChart;
import org.knowm.xchart.BoxChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.ChartTheme;
/*
* Box Plot with 1 series
* and show ToolTips
* and Y-Axis is logarithmic
*/
public class BoxChart03 implements ExampleChart<BoxChart> {
public static void main(String[] args) {
ExampleChart<BoxChart> exampleChart = new BoxChart03();
BoxChart chart = exampleChart.getChart();
new SwingWrapper<BoxChart>(chart).displayChart();
}
@Override
public BoxChart getChart() {
// Create Chart
BoxChart chart =
new BoxChartBuilder()
.width(600)
.height(450)
.title("Y Axis Logarithmic-box plot demo")
.xAxisTitle("X")
.yAxisTitle("Y")
.theme(ChartTheme.XChart)
.build();
// Customize Chart
chart.getStyler().setToolTipsEnabled(true);
chart.getStyler().setYAxisLogarithmic(true);
// Series
chart.addSeries("aaa", Arrays.asList(10, 40, 80, 120, 350));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Logarithmic Y-Axis";
}
}
package org.knowm.xchart.demo.charts.bubble;
import org.knowm.xchart.BubbleChart;
import org.knowm.xchart.BubbleChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler;
/**
* Basic Bubble Chart
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Bubble Chart
* <li>Legend Inside North with Horizontal Layout
*/
public class BubbleChart01 implements ExampleChart<BubbleChart> {
public static void main(String[] args) {
ExampleChart<BubbleChart> exampleChart = new BubbleChart01();
BubbleChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public BubbleChart getChart() {
// Create Chart
BubbleChart chart =
new BubbleChartBuilder()
.width(800)
.height(600)
.title(getClass().getSimpleName())
.xAxisTitle("X")
.yAxisTitle("Y")
.build();
chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideN);
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
chart.getStyler().setToolTipsEnabled(true);
// Customize Chart
// Series
double[] xData = new double[] {1.5, 2.6, 3.3, 4.9, 5.5, 6.3, 1, 2.0, 3.0, 4.0, 5, 6};
double[] yData = new double[] {10, 4, 7, 7.7, 7, 5.5, 10, 4, 7, 1, 7, 9};
double[] bubbleData = new double[] {17, 40, 50, 51, 26, 20, 66, 35, 80, 27, 29, 44};
double[] xData2 = new double[] {1, 2.0, 3.0, 4.0, 5, 6, 1.5, 2.6, 3.3, 4.9, 5.5, 6.3};
double[] yData2 = new double[] {1, 2, 3, 4, 5, 6, 10, 8.5, 4, 1, 4.7, 9};
double[] bubbleData2 = new double[] {37, 35, 80, 27, 29, 44, 57, 40, 50, 33, 26, 20};
chart.addSeries("A", xData, yData, bubbleData);
chart.addSeries("B", xData2, yData2, bubbleData2);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Basic Bubble Chart";
}
}
package org.knowm.xchart.demo.charts.date;
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 java.util.TimeZone;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler;
import org.knowm.xchart.style.markers.SeriesMarkers;
/**
* Millisecond Scale
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Millisecond Scale
* <li>LegendPosition.OutsideS
* <li>Two YAxis Groups - both on left
* <li>Zooming by dragging a selection box over area of interest
*/
public class DateChart01 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart01();
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()).build();
// Customize Chart
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
chart.getStyler().setZoomEnabled(true);
// chart.getStyler().setZoomResetButtomPosition(Styler.CardinalPosition.InsideS);
// chart.getStyler().setZoomResetByDoubleClick(false);
// chart.getStyler().setZoomResetByButton(true);
// chart.getStyler().setZoomSelectionColor(new Color(0, 0, 192, 128));
// Series
Random random = new Random();
// generate data
List<Date> xData1 = new ArrayList<>();
List<Double> yData1 = new ArrayList<>();
List<Date> xData2 = new ArrayList<>();
List<Double> yData2 = new ArrayList<>();
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
for (int i = 1; i <= 14; i++) {
try {
date = sdf.parse("23:45:31." + (100 * i + random.nextInt(20)));
} catch (ParseException e) {
e.printStackTrace();
}
xData1.add(date);
xData2.add(date);
yData1.add(Math.random() * i);
yData2.add(Math.random() * i * 100);
}
XYSeries series = chart.addSeries("series 1", xData1, yData1);
series.setMarker(SeriesMarkers.NONE);
chart.addSeries("series 2", xData2, yData2).setMarker(SeriesMarkers.NONE).setYAxisGroup(1);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Millisecond Scale with Two Separate Y Axis Groups";
}
}
package org.knowm.xchart.demo.charts.date;
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 java.util.TimeZone;
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;
/** Second Scale */
public class DateChart02 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart02();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart = new XYChartBuilder().width(800).height(600).title("Second Scale").build();
// Customize Chart
chart.getStyler().setLegendVisible(false);
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
// Series
List<Date> xData = new ArrayList<>();
List<Double> yData = new ArrayList<>();
Random random = new Random();
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
for (int i = 1; i <= 14; i++) {
try {
date = sdf.parse("23:45:" + (5 * i + random.nextInt(2)) + "." + random.nextInt(1000));
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(Math.random() * i);
}
chart.addSeries("blah", xData, yData);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Second Scale";
}
}
package org.knowm.xchart.demo.charts.date;
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 java.util.TimeZone;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler;
/**
* Minute Scale *
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Minute Scale
* <li>10^9 formatting
* <li>LegendPosition.InsideS
* <li>Two YAxis Groups - one on left, one on right
*/
public class DateChart03 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart03();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart = new XYChartBuilder().width(800).height(600).title("Minute Scale").build();
// Customize Chart
chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideS);
chart.getStyler().setYAxisGroupPosition(1, Styler.YAxisPosition.Right);
// Series
List<Date> xData1 = new ArrayList<>();
List<Double> yData1 = new ArrayList<>();
List<Date> xData2 = new ArrayList<>();
List<Double> yData2 = new ArrayList<>();
Random random = new Random();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
for (int i = 1; i <= 14; i++) {
try {
date =
sdf.parse(
"2013-07-22-08:"
+ (5 * i + random.nextInt(2))
+ ":"
+ (random.nextInt(2))
+ "."
+ random.nextInt(1000));
} catch (ParseException e) {
e.printStackTrace();
}
// System.out.println(date.getTime());
// System.out.println(date.toString());
xData1.add(date);
xData2.add(date);
yData1.add(Math.random() * i * 1000000000);
yData2.add(Math.random() * i * 10);
}
chart.addSeries("series1", xData1, yData1).setYAxisGroup(1);
chart.addSeries("series2", xData2, yData2);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Minute Scale with Two Separate Y Axis Groups";
}
}
package org.knowm.xchart.demo.charts.date;
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.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
/**
* Hour Scale
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Hiding Y-Axis Axis Ticks (labels, tick marks, tick line)
*/
public class DateChart04 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart04();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart = new XYChartBuilder().width(800).height(600).title("Hour Scale").build();
// Customize Chart
chart.getStyler().setLegendVisible(false);
chart.getStyler().setYAxisTicksVisible(false);
// Series
List<Date> xData = new ArrayList<>();
List<Double> yData = new ArrayList<>();
Random random = new Random();
DateFormat sdf = new SimpleDateFormat("dd-HH");
Date date = null;
for (int i = 1; i <= 14; i++) {
try {
date = sdf.parse("25-" + (2 * i + random.nextInt(2)));
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(Math.random() * i / 10000000000.0);
}
chart.addSeries("blah", xData, yData);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Hour Scale";
}
}
package org.knowm.xchart.demo.charts.date;
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 java.util.TimeZone;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
/** Day Scale */
public class DateChart05 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart05();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart = new XYChartBuilder().width(800).height(600).title("Day Scale").build();
// Customize Chart
chart.getStyler().setLegendVisible(false);
// Series
List<Date> xData = new ArrayList<>();
List<Double> yData = new ArrayList<>();
Random random = new Random();
DateFormat sdf = new SimpleDateFormat("MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
for (int i = 1; i <= 14; i++) {
try {
date = sdf.parse("02-" + (4 * i + random.nextInt(2)));
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(Math.random() * i / -100000000);
}
chart.addSeries("blah", xData, yData);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Day Scale";
}
}
package org.knowm.xchart.demo.charts.date;
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 java.util.TimeZone;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
/**
* Month scale
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Setting custom Y-Axis tick labels
*/
public class DateChart06 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart06();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart = new XYChartBuilder().width(800).height(600).title("Month Scale").build();
// Customize Chart
chart.getStyler().setLegendVisible(false);
// Series
List<Date> xData = new ArrayList<>();
List<Double> yData = new ArrayList<>();
Random random = new Random();
DateFormat sdf = new SimpleDateFormat("yyyy-MM");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
for (int i = 1; i <= 14; i++) {
try {
date = sdf.parse("2013-" + (2 * i + random.nextInt(1)));
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(Math.random() * i);
}
chart.addSeries("blah", xData, yData);
chart
.getStyler()
.setyAxisTickLabelsFormattingFunction(x -> NumberWordConverter.convert(x.intValue()));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Month Scale with custom Y-Axis tick labels";
}
static class NumberWordConverter {
public static final String[] units = {
"",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
};
public static final String[] tens = {
"", // 0
"", // 1
"twenty", // 2
"thirty", // 3
"forty", // 4
"fifty", // 5
"sixty", // 6
"seventy", // 7
"eighty", // 8
"ninety" // 9
};
public static String convert(final int n) {
// System.out.println("n = " + n);
if (n == 0) {
return "zero";
}
if (n < 0) {
return "minus " + convert(-n);
}
if (n < 20) {
return units[n];
}
if (n < 100) {
return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
}
if (n < 1000) {
return units[n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
}
if (n < 1000000) {
return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
}
if (n < 1000000000) {
return convert(n / 1000000)
+ " million"
+ ((n % 1000000 != 0) ? " " : "")
+ convert(n % 1000000);
}
return convert(n / 1000000000)
+ " billion"
+ ((n % 1000000000 != 0) ? " " : "")
+ convert(n % 1000000000);
}
// public static void main(final String[] args) {
// final Random generator = new Random();
//
// int n;
// for (int i = 0; i < 20; i++) {
// n = generator.nextInt(Integer.MAX_VALUE);
//
// System.out.printf("%10d = '%s'%n", n, convert(n));
// }
//
// n = 1000;
// System.out.printf("%10d = '%s'%n", n, convert(n));
//
// n = 2000;
// System.out.printf("%10d = '%s'%n", n, convert(n));
//
// n = 10000;
// System.out.printf("%10d = '%s'%n", n, convert(n));
//
// n = 11000;
// System.out.printf("%10d = '%s'%n", n, convert(n));
//
// n = 999999999;
// System.out.printf("%10d = '%s'%n", n, convert(n));
//
// n = Integer.MAX_VALUE;
// System.out.printf("%10d = '%s'%n", n, convert(n));
// }
}
}
package org.knowm.xchart.demo.charts.date;
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 java.util.TimeZone;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
/** Year scale */
public class DateChart07 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart07();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
}
@Override
public XYChart getChart() {
// Create Chart
XYChart chart = new XYChartBuilder().width(800).height(600).title("Year Scale").build();
// Customize Chart
chart.getStyler().setLegendVisible(false);
// Series
List<Date> xData = new ArrayList<>();
List<Double> yData = new ArrayList<>();
Random random = new Random();
DateFormat sdf = new SimpleDateFormat("yyyy-MM");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
for (int i = 1; i <= 14; i++) {
try {
date = sdf.parse("" + (2001 + i) + "-" + random.nextInt(12));
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(Math.random() * i);
}
chart.addSeries("blah", xData, yData);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Year Scale";
}
}
package org.knowm.xchart.demo.charts.date;
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 java.util.TimeZone;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
/**
* Year scale
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Rotated X-Axis labels
* <li>Setting a custom date formatter String
* <li>Smooth series
*/
public class DateChart08 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart08();
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()).build();
// Customize Chart
chart.getStyler().setLegendVisible(false);
chart.getStyler().setXAxisLabelRotation(60);
chart.getStyler().setDatePattern("yyyy-MM-dd");
// Series
List<Date> xData = new ArrayList<>();
List<Double> yData = new ArrayList<>();
Random random = new Random();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
for (int i = 1; i <= 14; i++) {
try {
date = sdf.parse("" + (2001 + i) + "-" + random.nextInt(12) + "-" + random.nextInt(28));
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(Math.random() * i);
}
chart.addSeries("blah", xData, yData).setSmooth(true);
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Rotated Tick Labels";
}
}
package org.knowm.xchart.demo.charts.date;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
/**
* Year scale
*
* <p>Demonstrates the following:
*
* <ul>
* <li>Rotated 90 degrees X-Axis labels
* <li>Setting custom X-Axis tick labels
* <li>Setting custom cursor tool tip text
*/
public class DateChart09 implements ExampleChart<XYChart> {
public static void main(String[] args) {
ExampleChart<XYChart> exampleChart = new DateChart09();
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()).build();
// Customize Chart
chart.getStyler().setLegendVisible(false);
chart.getStyler().setXAxisLabelRotation(90);
// Series
List<Integer> xData = IntStream.range(0, 365).boxed().collect(Collectors.toList());
Random random = new Random();
List<Double> yData =
IntStream.range(0, xData.size())
.mapToDouble(x -> random.nextDouble())
.boxed()
.collect(Collectors.toList());
chart.addSeries("blah", xData, yData);
// set custom X-Axis tick labels
LocalDateTime startTime = LocalDateTime.of(2001, Month.JANUARY, 1, 0, 0, 0);
DateTimeFormatter xTickFormatter = DateTimeFormatter.ofPattern("LLL");
chart
.getStyler()
.setxAxisTickLabelsFormattingFunction(
x -> startTime.plusDays(x.longValue()).format(xTickFormatter));
// set custom cursor tool tip text
chart.getStyler().setCursorEnabled(true);
DateTimeFormatter cursorXFormatter = DateTimeFormatter.ofPattern("LLL dd");
chart
.getStyler()
.setCustomCursorXDataFormattingFunction(
x -> startTime.plusDays(x.longValue()).format(cursorXFormatter));
return chart;
}
@Override
public String getExampleChartName() {
return getClass().getSimpleName() + " - Custom Date Formatter Without Years";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment