我有多个数据集让arrTimeSeriesCollection [10],我正在使用第一个数据集创建时间序列图表,即; arrTimeSeriesCollection [0]其中第一个数据集的值为'null'即; series.add(time,null);但是,当我向图表添加第二个数据集时,它没有显示(第二个数据集有一些值)。如何将范围轴调整为有效数据集(具有非空值)。
public class MultipleDatasetDemo extends ApplicationFrame implements ActionListener
{
private XYPlot plot;
private int datasetIndex = 0;
public MultipleDatasetDemo(final String title)
{
super(title);
final TimeSeriesCollection dataset1 = createEmptyRandomDataset("Series 1");
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Multiple Dataset Demo 1", "Time", "Value", dataset1, true, true, false
);
chart.setBackgroundPaint(Color.white);
this.plot = chart.getXYPlot();
this.plot.setBackgroundPaint(Color.lightGray);
this.plot.setDomainGridlinePaint(Color.white);
this.plot.setRangeGridlinePaint(Color.white);
final ValueAxis axis = this.plot.getDomainAxis();
axis.setAutoRange(false);
final NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2");
rangeAxis2.setAutoRangeIncludesZero(false);
final JPanel content = new JPanel(new BorderLayout());
final ChartPanel chartPanel = new ChartPanel(chart);
content.add(chartPanel);
final JButton button1 = new JButton("Add Dataset");
button1.setActionCommand("ADD_DATASET");
button1.addActionListener(this);
final JButton button2 = new JButton("Remove Dataset");
button2.setActionCommand("REMOVE_DATASET");
button2.addActionListener(this);
final JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(button1);
buttonPanel.add(button2);
content.add(buttonPanel, BorderLayout.SOUTH);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(content);
}
private TimeSeriesCollection createRandomDataset(final String name) {
final TimeSeries series = new TimeSeries(name);
double value = 100.0;
RegularTimePeriod t = new Day();
for (int i = 0; i < 50; i++) {
series.add(t, value);
t = t.next();
value = value * (1.0 + Math.random() / 100);
}
return new TimeSeriesCollection(series);
}
private TimeSeriesCollection createEmptyRandomDataset(final String name) {
final TimeSeries series = new TimeSeries(name);
double value = 100.0;
RegularTimePeriod t = new Day();
for (int i = 0; i < 50; i++) {
series.add(t, null);
t = t.next();
value = value * (1.0 + Math.random() / 100);
}
return new TimeSeriesCollection(series);
}
public void actionPerformed(final ActionEvent e) {
if (e.getActionCommand().equals("ADD_DATASET")) {
if (this.datasetIndex < 20) {
this.datasetIndex++;
this.plot.setDataset(
this.datasetIndex, createRandomDataset("S" + this.datasetIndex)
);
this.plot.setRenderer(this.datasetIndex, new StandardXYItemRenderer());
}
this.plot.getRangeAxis().setAutoRange(false);
}
else if (e.getActionCommand().equals("REMOVE_DATASET")) {
if (this.datasetIndex >= 1) {
this.plot.setDataset(this.datasetIndex, null);
this.plot.setRenderer(this.datasetIndex, null);
this.datasetIndex--;
}
}
}
public static void main(final String[] args) {
final MultipleDatasetDemo demo = new MultipleDatasetDemo("Multiple Dataset Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}