JFreeChart getSeriesStroke / Paint / Shape返回null

时间:2017-03-18 09:29:22

标签: java swing jfreechart

通过向{JFreeChart构造函数传递XYSeriesCollection数据集创建折线图后,我试图获得系列Stroke / Paint / Shape

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
for (int i = 0; i < dataset.getSeriesCount(); i++) {
     renderer.getSeriesStroke(i);
     renderer.getSeriesPaint(i);
     renderer.getSeriesShape(i);
}

但都返回null。

为什么?如何获取非null对象?

1 个答案:

答案 0 :(得分:2)

XYLineAndShapeRendererXYItemRenderer,支持“在XYPlot上呈现单个(x,y)项目的直观表示”。虽然相应的系列属性为null,但可以访问属性。从此example开始,以下更改将生成显示的输出:

代码:

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
for (int i = 0; i < xyPlot.getDataset().getSeriesCount(); i++) {
     System.out.println(renderer.getItemStroke(i, 0));
     System.out.println(renderer.getItemPaint(i, 0));
     System.out.println(renderer.getItemShape(i, 0));
     System.out.println(renderer.getItemShape(i, N));
}

控制台:

$ java -cp .:$JFREE_LIB/* ScatterShape
java.awt.BasicStroke@d1a007c0
java.awt.Color[r=255,g=85,b=85]
java.awt.geom.Rectangle2D$Double[x=-3.0,y=-3.0,w=6.0,h=6.0]
java.awt.geom.GeneralPath@7ef51f0f

enter image description here