我们试图不绘制时间序列jFreeChart中的某些段。
我尝试过这种解决方案,但对我而言,数据集始终为null,因此不确定我在做什么错。 JFreeChart - change SeriesStroke of chart lines from solid to dashed in one line
这是一个带有间隙的图形示例,我们希望不绘制连接线,或者至少要画一条虚线。 这是我尝试过的代码:
XYLineAndShapeRenderer minuteRenderer = new XYLineAndShapeRenderer() {
Stroke solid = new BasicStroke(2.0f);
@Override
public Stroke getItemStroke(int row, int column) {
if (dataset == null) {
System.out.println("why am I null?");
return solid;
}
}
};
有人可以建议吗?
更新:添加周围的代码以进行说明。注意我确实有一个有效的数据集,因为它正在绘图,但是我拥有的渲染器似乎没有引用它。发生这种类型的覆盖时,也许需要做一些事情来将渲染器链接到数据集?
// ~~ Plot the Minute Average Data (avg, min, max)
plot.setDataset(MINUTE_DATA, getMinuteDataset());
plot.mapDatasetToRangeAxis(MINUTE_DATA, FILL_RANGE_AXIS);
//XYLineAndShapeRenderer minuteRenderer = new XYLineAndShapeRenderer();
XYLineAndShapeRenderer minuteRenderer = new XYLineAndShapeRenderer() {
@Override
public Paint getItemPaint(int row, int col) {
System.out.println("get item paint");
Paint cpaint = getItemColor(row, col);
if (cpaint == null) {
cpaint = super.getItemPaint(row, col);
}
return cpaint;
}
public Color getItemColor(int row, int col) {
System.out.println(col + "," + dataset.getY(row, col));
double y = dataset.getYValue(row, col);
if(y<=3) return Color.black;
if(y<=4) return Color.green;;
if(y<=5) return Color.red;;
if(y<=6) return Color.yellow;;
if(y<=10) return Color.orange;;
return null;
}
@Override
protected void drawFirstPassShape(Graphics2D g2, int pass, int series,
int item, Shape shape) {
System.out.println("drawing first pass shape");
g2.setStroke(getItemStroke(series, item));
Color c1 = getItemColor(series, item);
Color c2 = getItemColor(series, item - 1);
GradientPaint linePaint = new GradientPaint(0, 0, c1, 0, 300, c2);
g2.setPaint(linePaint);
g2.draw(shape);
}
};
minuteRenderer.setSeriesShapesVisible(0, false);
minuteRenderer.setSeriesShapesVisible(1, false);
minuteRenderer.setSeriesShapesVisible(2, false);
minuteRenderer.setSeriesPaint(0, XBLUE);
minuteRenderer.setSeriesPaint(1, DODGER_BLUE);
minuteRenderer.setSeriesPaint(2, DODGER_BLUE);
plot.setRenderer(MINUTE_DATA, minuteRenderer);
执行上述操作会给我这个空指针错误:
SEVERE: Uncaught exception thrown in java.awt.EventDispatchThread, thread: 14-AWT-EventQueue-0
java.lang.NullPointerException
at filtec.gui.widgets.fmsqa.FmsLtaChartPanel$1.getItemColor(FmsLtaChartPanel.java:234)
其中数据集为空...