 |
Showing chart with dates
Source code name: "xchart_date2.java"
Programming language: Java
Topic: Plots/2D
DMelt Version 1.4. Last modified: 04/05/1977. License: Pro
https://datamelt.org/code/cache/xchart_date2_5294.java
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
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;
/** Year scale */
public class xchart_date2 {
public static void main(String[] args) {
XYChart chart = getChart();
new SwingWrapper<>(chart).displayChart();
}
static 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 xData = new ArrayList<>();
List 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;
}
}
You see the box below because you did not login.