我正在尝试在 JFreeChart 中生成一个图表,其中包含一个叠加的烛台图和时间序列图。(有点像这样)
(来源:prices-oil.org)
我尝试创建烛台图表,然后添加一个额外的 XY 系列和它的渲染器,但这会导致运行时错误
org.jfree.data.xy.XYSeriesCollection cannot be cast to org.jfree.data.xy.OHLCDataset
我的代码片段如下
private XYPlot plot;
private XYSeriesCollection dataTrend;
private XYItemRenderer renderer;
public OhlcChart(BarCollection bars)
{
JFreeChart jfreechart = ChartFactory.createCandlestickChart("FX Trader Prototype", "Time", "Value", getDataset(bars), true);
plot = (XYPlot)jfreechart.getPlot();
plot.setDomainPannable(true);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setAutoRangeIncludesZero(false);
numberAxis.setAutoRangeStickyZero(false);
numberAxis.setUpperMargin(0.0D);
numberAxis.setLowerMargin(0.0D);
DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM HH:mm.ss");
dateAxis.setDateFormatOverride(formatter);
this.renderer = plot.getRenderer();
Stroke myStroke = new BasicStroke((float) 1.0);
this.renderer = new XYLineAndShapeRenderer();
this.renderer.setSeriesPaint(0, Color.blue);
this.renderer.setSeriesStroke(0, myStroke);
}
public OhlcChart update(Timeseries<Double> ts)
{
Stroke myStroke = new BasicStroke((float) 1.0);
XYLineAndShapeRenderer timeSeriesRenderer = new XYLineAndShapeRenderer();
timeSeriesRenderer.setBaseShapesVisible(false);
timeSeriesRenderer.setSeriesPaint(0, Color.blue);
timeSeriesRenderer.setSeriesStroke(0, myStroke);
UiTimeseries series = new UiTimeseries(ts);
dataTrend.addSeries(series);
plot.setDataset(plot.getDatasetCount()+1, dataTrend);
plot.setRenderer(plot.getDatasetCount()+1, timeSeriesRenderer);
return this;
}
任何建议将不胜感激