📄 jfreechartdemobase.java
字号:
}
/**
* Creates a horizontally combined chart.
*
* @return a horizontally combined chart.
*/
public JFreeChart createHorizontallyCombinedChart() {
// create a default chart based on some sample data...
String title = this.resources.getString("combined.horizontal.title");
String subtitleStr = this.resources.getString("combined.horizontal.subtitle");
String[] domains = this.resources.getStringArray("combined.horizontal.domains");
String rangeAxisLabel = this.resources.getString("combined.horizontal.range");
TimeSeriesCollection dataset0 = new TimeSeriesCollection();
TimeSeries eur = DemoDatasetFactory.createEURTimeSeries();
dataset0.addSeries(eur);
TimeSeriesCollection dataset1 = new TimeSeriesCollection();
TimeSeries mav = MovingAverage.createMovingAverage(eur, "EUR/GBP (30 Day MA)", 30, 30);
dataset1.addSeries(eur);
dataset1.addSeries(mav);
TimeSeriesCollection dataset2 = new TimeSeriesCollection();
dataset2.addSeries(eur);
// make a combined range plot
NumberAxis valueAxis = new NumberAxis(rangeAxisLabel);
valueAxis.setAutoRangeIncludesZero(false); // override default
CombinedRangeXYPlot parent = new CombinedRangeXYPlot(valueAxis);
parent.setRenderer(new StandardXYItemRenderer());
// add subplots
int[] weight = {1, 1, 1}; // controls space assigned to each subplot
// add subplot 1...
XYPlot subplot1 = new XYPlot(dataset0, new DateAxis(domains[0]), null,
new StandardXYItemRenderer());
parent.add(subplot1, weight[0]);
// add subplot 2...
XYPlot subplot2 = new XYPlot(dataset1, new DateAxis(domains[1]), null,
new StandardXYItemRenderer());
parent.add(subplot2, weight[1]);
// add subplot 3...
XYPlot subplot3 = new XYPlot(dataset2, new DateAxis(domains[2]),
null, new XYBarRenderer(0.20));
parent.add(subplot3, weight[2]);
// now make the top level JFreeChart
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, parent, true);
// then customise it a little...
TextTitle subtitle = new TextTitle(subtitleStr, new Font("SansSerif", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
return chart;
}
/**
* Creates and returns a sample vertically combined chart.
*
* @return a sample vertically combined chart.
*/
public JFreeChart createVerticallyCombinedChart() {
// create a default chart based on some sample data...
String title = this.resources.getString("combined.vertical.title");
String subtitleStr = this.resources.getString("combined.vertical.subtitle");
String domain = this.resources.getString("combined.vertical.domain");
String[] ranges = this.resources.getStringArray("combined.vertical.ranges");
TimeSeriesCollection dataset0 = new TimeSeriesCollection();
TimeSeries eur = DemoDatasetFactory.createEURTimeSeries();
dataset0.addSeries(eur);
TimeSeriesCollection dataset1 = new TimeSeriesCollection();
TimeSeries jpy = DemoDatasetFactory.createJPYTimeSeries();
TimeSeries mav = MovingAverage.createMovingAverage(jpy, "JPY/GBP (30 Day MA)", 30, 30);
dataset1.addSeries(jpy);
dataset1.addSeries(mav);
XYDataset dataset2 = DemoDatasetFactory.createHighLowDataset();
TimeSeriesCollection dataset3 = new TimeSeriesCollection();
dataset3.addSeries(eur);
// make one shared horizontal axis
ValueAxis timeAxis = new DateAxis(domain);
// make a vertically CombinedPlot that will contain the sub-plots
CombinedDomainXYPlot multiPlot = new CombinedDomainXYPlot(timeAxis);
int[] weight = {1, 1, 1, 1}; // control vertical space allocated to each sub-plot
// add subplot1...
XYPlot subplot1 = new XYPlot(dataset0, null, new NumberAxis(ranges[0]),
new StandardXYItemRenderer());
NumberAxis range1 = (NumberAxis) subplot1.getRangeAxis();
range1.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7));
range1.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
range1.setAutoRangeIncludesZero(false);
multiPlot.add(subplot1, weight[0]);
// add subplot2...
XYPlot subplot2 = new XYPlot(dataset1, null, new NumberAxis(ranges[1]),
new StandardXYItemRenderer());
NumberAxis range2 = (NumberAxis) subplot2.getRangeAxis();
range2.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7));
range2.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
range2.setAutoRangeIncludesZero(false);
multiPlot.add(subplot2, weight[1]);
// add subplot3...
XYPlot subplot3 = new XYPlot(dataset2, null, new NumberAxis(ranges[2]), null);
XYItemRenderer renderer3 = new HighLowRenderer();
subplot3.setRenderer(renderer3);
NumberAxis range3 = (NumberAxis) subplot3.getRangeAxis();
range3.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7));
range3.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
range3.setAutoRangeIncludesZero(false);
multiPlot.add(subplot3, weight[2]);
// add subplot4...
XYPlot subplot4 = new XYPlot(dataset3, null, new NumberAxis(ranges[3]), null);
XYItemRenderer renderer4 = new XYBarRenderer();
subplot4.setRenderer(renderer4);
NumberAxis range4 = (NumberAxis) subplot4.getRangeAxis();
range4.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7));
range4.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
range4.setAutoRangeIncludesZero(false);
multiPlot.add(subplot4, weight[3]);
// now make the top level JFreeChart that contains the CombinedPlot
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, multiPlot, true);
// then customise it a little...
TextTitle subtitle = new TextTitle(subtitleStr, new Font("SansSerif", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
return chart;
}
/**
* Creates a combined and overlaid chart.
* <p>
* Note: from version 0.9.10, the overlaid chart is no longer supported (you can achieve
* the same result using a regular XYPlot with multiple datasets and renderers).
*
* @return a combined and overlaid chart.
*/
public JFreeChart createCombinedAndOverlaidChart1() {
// create a default chart based on some sample data...
String title = this.resources.getString("combined.combined-overlaid.title");
String subtitleStr = this.resources.getString("combined.combined-overlaid.subtitle");
String domain = this.resources.getString("combined.combined-overlaid.domain");
String[] ranges = this.resources.getStringArray("combined.combined-overlaid.ranges");
TimeSeries jpy = DemoDatasetFactory.createJPYTimeSeries();
TimeSeries mav = MovingAverage.createMovingAverage(jpy, "30 Day Moving Average", 30, 30);
TimeSeriesCollection dataset0 = new TimeSeriesCollection();
dataset0.addSeries(jpy);
TimeSeriesCollection dataset1 = new TimeSeriesCollection();
dataset1.addSeries(jpy);
dataset1.addSeries(mav);
HighLowDataset highLowDataset = DemoDatasetFactory.createHighLowDataset();
XYDataset highLowDatasetMA = MovingAverage.createMovingAverage(
highLowDataset,
" (MA)",
5 * 24 * 60 * 60 * 1000L,
5 * 24 * 60 * 60 * 1000L
);
// make one vertical axis for each (vertical) chart
NumberAxis[] valueAxis = new NumberAxis[3];
for (int i = 0; i < valueAxis.length; i++) {
valueAxis[i] = new NumberAxis(ranges[i]);
if (i <= 1) {
valueAxis[i].setAutoRangeIncludesZero(false); // override default
}
}
// create CombinedPlot...
CombinedDomainXYPlot parent = new CombinedDomainXYPlot(new DateAxis(domain));
int[] weight = {1, 2, 2};
// add subplot1...
XYItemRenderer renderer1 = new StandardXYItemRenderer();
XYPlot subplot1 = new XYPlot(dataset0, null, new NumberAxis(ranges[0]), renderer1);
NumberAxis axis1 = (NumberAxis) subplot1.getRangeAxis();
axis1.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7));
axis1.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
axis1.setAutoRangeIncludesZero(false);
parent.add(subplot1, weight[0]);
// add subplot2 (an overlaid plot)...
XYPlot subplot2 = new XYPlot(dataset0, null, new NumberAxis(ranges[1]),
new StandardXYItemRenderer());
NumberAxis axis2 = (NumberAxis) subplot2.getRangeAxis();
axis2.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7));
axis2.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
axis2.setAutoRangeIncludesZero(false);
subplot2.setSecondaryDataset(0, dataset1);
subplot2.setSecondaryRenderer(0, new StandardXYItemRenderer());
parent.add(subplot2, weight[1]);
// add subplot3 (an overlaid plot)...
XYItemRenderer renderer3 = new HighLowRenderer();
XYPlot subplot3 = new XYPlot(highLowDataset, null, new NumberAxis(ranges[2]), renderer3);
NumberAxis axis3 = (NumberAxis) subplot3.getRangeAxis();
axis3.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7));
axis3.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
axis3.setAutoRangeIncludesZero(false);
subplot3.setSecondaryDataset(0, highLowDatasetMA);
subplot3.setSecondaryRenderer(0, new StandardXYItemRenderer());
parent.add(subplot3, weight[2]);
// now create the master JFreeChart object
JFreeChart chart = new JFreeChart(
title,
new Font("SansSerif", Font.BOLD, 12),
parent,
true
);
// then customise it a little...
TextTitle subtitle = new TextTitle(subtitleStr, new Font("SansSerif", Font.BOLD, 10));
chart.addSubtitle(subtitle);
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
return chart;
}
/**
* Displays an XY chart that is periodically updated by a background thread. This is to
* demonstrate the event notification system that automatically updates charts as required.
*
* @return a chart.
*/
public JFreeChart createCombinedAndOverlaidDynamicXYChart() {
// chart title and axis labels...
String title = this.resources.getString("combined.dynamic.title");
String subtitleStr = this.resources.getString("combined.dynamic.subtitle");
String domainAxisLabel = this.resources.getString("combined.dynamic.domain");
String[] ranges = this.resources.getStringArray("combined.dynamic.ranges");
// setup sample base 2-series dataset
SampleXYDataset data = new SampleXYDataset();
// create some SubSeriesDatasets and CombinedDatasets to test events
XYDataset series0 = new SubSeriesDataset(data, 0);
XYDataset series1 = new SubSeriesDataset(data, 1);
CombinedDataset combinedData = new CombinedDataset();
combinedData.add(series0);
combinedData.add(series1);
// create common time axis
NumberAxis timeAxis = new NumberAxis(domainAxisLabel);
timeAxis.setTickMarksVisible(true);
timeAxis.setAutoRangeIncludesZero(false);
// make one vertical axis for each (vertical) chart
NumberAxis[] valueAxis = new NumberAxis[4];
for (int i = 0; i < valueAxis.length; i++) {
valueAxis[i] = new NumberAxis(ranges[i]);
valueAxis[i].setAutoRangeIncludesZero(false);
}
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(timeAxis);
// add subplot1...
XYItemRenderer renderer0 = new StandardXYItemRenderer();
XYPlot subplot0 = new XYPlot(series0, null, valueAxis[0], renderer0);
plot.add(subplot0, 1);
// add subplot2...
XYItemRenderer renderer1 = new StandardXYItemRenderer();
XYPlot subplot1 = new XYPlot(series1, null, valueAxis[1], renderer1);
plot.add(subplot1, 1);
// add subplot3...
XYPlot subplot2 = new XYPlot(series0, null, valueAxis[2], new StandardXYItemRenderer());
subplot2.setSecondaryDataset(0, series1);
subplot2.setSecondaryRenderer(0, new StandardXYItemRenderer());
plot.add(subplot2, 1);
// add subplot4...
XYItemRenderer renderer3 = new StandardXYItemRenderer();
XYPlot subplot3 = new XYPlot(data, null, valueAxis[3], renderer3);
plot.add(subplot3, 1);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
// then customise it a little...
TextTitle subtitle = new TextTitle(subtitleStr, new Font("SansSerif", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.cyan));
// setup thread to update base Dataset
SampleXYDatasetThread update = new SampleXYDatasetThread(data);
Thread thread = new Thread(update);
thread.start();
return chart;
}
/**
* Creates a gantt chart.
*
* @return a gantt chart.
*/
public JFreeChart createGanttChart() {
String title = resources.getString("gantt.task.title");
String domain = resources.getString("gantt.task.domain");
String range = resources.getString("gantt.task.range");
IntervalCategoryDataset data = DemoDatasetFactory.createGanttDataset1();
JFreeChart chart = ChartFactory.createGanttChart(title, domain, range, data,
true,
true,
false);
// then customise it a little...
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 1000, 0, Color.blue));
return chart;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -