我想在图表中添加一个滚动条,以便查看不适合屏幕的大数据集,因为要保留所有的xtick。然而,我收到了以下错误消息:
Exception in thread "main" java.lang.NullPointerException
at MySlidingCategoryDatasetDemo2$DemoPanel.createChart(MySlidingCategoryDatasetDemo2.java:96)
at MySlidingCategoryDatasetDemo2$DemoPanel.<init>(MySlidingCategoryDatasetDemo2.java:148)
at MySlidingCategoryDatasetDemo2.createDemoPanel(MySlidingCategoryDatasetDemo2.java:170)
at MySlidingCategoryDatasetDemo2.<init>(MySlidingCategoryDatasetDemo2.java:166)
at MySlidingCategoryDatasetDemo2.main(MySlidingCategoryDatasetDemo2.java:174)
带有以下代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.event.ChartChangeListener;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class MySlidingCategoryDatasetDemo2 extends ApplicationFrame {
static class DemoPanel extends JPanel implements ChangeListener {
private static XYDataset createDataset() {
XYSeries xyseries = new XYSeries("First");
xyseries.add(1.0D, 1.0D);
xyseries.add(2D, 4D);
xyseries.add(3D, 3D);
xyseries.add(4D, 5D);
xyseries.add(5D, 5D);
xyseries.add(6D, 7D);
xyseries.add(7D, 7D);
xyseries.add(8D, 8D);
xyseries.add(9D, 8D);
xyseries.add(10D, 8D);
xyseries.add(11D, 8D);
xyseries.add(12D, 8D);
xyseries.add(13D, 8D);
xyseries.add(14D, 8D);
xyseries.add(15D, 8D);
xyseries.add(16D, 25D);
xyseries.add(17D, 8D);
xyseries.add(18D, 5D);
xyseries.add(19D, 7D);
xyseries.add(20D, 6D);
xyseries.add(21D, 8D);
xyseries.add(22D, 4D);
xyseries.add(23D, 4D);
xyseries.add(24D, 2D);
xyseries.add(25D, 1.0D);
xyseries.add(26D, 8D);
xyseries.add(27D, 8D);
xyseries.add(28D, 8D);
xyseries.add(29D, 8D);
xyseries.add(30D, 8D);
xyseries.add(31D, 8D);
xyseries.add(32D, 25D);
xyseries.add(33D, 8D);
xyseries.add(34D, 5D);
xyseries.add(35D, 7D);
xyseries.add(36D, 6D);
xyseries.add(37D, 8D);
xyseries.add(38D, 4D);
xyseries.add(39D, 4D);
xyseries.add(40D, 2D);
xyseries.add(41D, 1.0D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
return xyseriescollection;
}
private static JFreeChart createChart(XYDataset xydataset) {
final CombinedDomainXYPlot xyplot = new CombinedDomainXYPlot();
NumberAxis rangeAxis1 = new NumberAxis();
rangeAxis1.setLowerBound(0);
rangeAxis1.setUpperBound(30);
final XYPlot subplot1 = new XYPlot(xydataset, null, rangeAxis1,
new StandardXYItemRenderer());
xyplot.add(subplot1);
final JFreeChart jfreechart = new JFreeChart("Hello",
JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
// JFreeChart jfreechart =
// ChartFactory.createXYLineChart("Line Chart Demo 2", "X", "Y",
// xydataset, PlotOrientation.VERTICAL, true, true, false);
// XYPlot xyplot = (XYPlot)jfreechart.getPlot();
//
xyplot.setDomainPannable(true);
xyplot.setRangePannable(true);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
.getRenderer();
xylineandshaperenderer.setBaseShapesVisible(true);
xylineandshaperenderer.setBaseShapesFilled(true);
// NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
// numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// numberaxis.setRange(0.0D, 100D);
ValueAxis valueaxis = xyplot.getDomainAxis();
// valueaxis.setLowerMargin(0.34999999999999998D);
valueaxis.setLowerBound(0);
valueaxis.setUpperBound(60);
return jfreechart;
}
public void stateChanged(ChangeEvent changeevent) {
// dataset.setFirstCategoryIndex(scroller.getValue());
BoundedRangeModel scrollBarModel = scroller.getModel();
try {
Object src = changeevent.getSource();
if (src == scrollBarModel) {
int val = scrollBarModel.getValue();
int ext = scrollBarModel.getExtent();
System.out.println(val);
System.out.println(ext);
Plot plot = this.chartpanel.getChart().getPlot();
if (plot instanceof XYPlot) {
XYPlot hvp = (XYPlot) plot;
ValueAxis axis = hvp.getDomainAxis();
this.chartpanel.getChart().removeChangeListener(
(ChartChangeListener) this);
axis.setRange(val / this.xscrollFactor, (val + ext)
/ this.xscrollFactor);
this.chartpanel.getChart().addChangeListener(
(ChartChangeListener) this);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
JScrollBar scroller;
SlidingCategoryDataset dataset;
ChartPanel chartpanel;
double xscrollFactor = 50;
public DemoPanel() {
super(new BorderLayout());
JFreeChart jfreechart = createChart(createDataset());
chartpanel = new ChartPanel(jfreechart);
// chartpanel.setPreferredSize(new Dimension(400, 400));
scroller = new JScrollBar(JScrollBar.HORIZONTAL);// (0, 0, 10, 0, 50);
add(chartpanel);
scroller.getModel().addChangeListener(this);
JPanel jpanel = new JPanel(new BorderLayout());
jpanel.add(scroller);
jpanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
jpanel.setBackground(Color.white);
add(jpanel, "South");
}
}
public MySlidingCategoryDatasetDemo2(String s) {
super(s);
setDefaultCloseOperation(3);
setContentPane(createDemoPanel());
}
public static JPanel createDemoPanel() {
return new DemoPanel();
}
public static void main(String args[]) {
MySlidingCategoryDatasetDemo2 slidingcategorydatasetdemo2 = new MySlidingCategoryDatasetDemo2(
"JFreeChart: SlidingCategoryDatasetDemo2.java");
slidingcategorydatasetdemo2.pack();
RefineryUtilities.centerFrameOnScreen(slidingcategorydatasetdemo2);
slidingcategorydatasetdemo2.setVisible(true);
}
}
我做错了什么?
提前谢谢。
NullPointerException
的出现是因为名为xyplot
的CombinedDomainXYPlot
没有渲染器;只有它的子画面有渲染器。xyplot
中唯一的子图形是名为subplot1
的XYPlot
,它具有类型为StandardXYItemRenderer
的标准渲染器。你可以在createChart()
中这样调节它:
StandardXYItemRenderer renderer =
(StandardXYItemRenderer) subplot1.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);