2012-08-29 74 views
4

我試圖在JFreeChart中生成一個由疊加的燭臺圖表和時間序列圖組成的單個圖表。 (像這樣的小)JFreeChart - 時間序列和CandleStick在同一張圖上

candlestick trend overlay http://www.prices-oil.org/wp-content/uploads/2009/04/oil1stapril.jpg

我曾嘗試創建燭臺圖表,然後添加額外的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; 
} 

任何意見將受到歡迎

回答

6

此我S可能使用JFreeChart的,關鍵是要創建一個額外的數據集和渲染

您需要創建一個新的TimeSeriesCollection來保存數據,另外三個系列

TimeSeriesCollection otherDataSet = new TimeSeriesCollection(); 
TimeSeries ts1 = new TimeSeries("Series 1"); 
otherDataSet.addSeries(ts1); 
TimeSeries ts2 = new TimeSeries("Series 2"); 
otherDataSet.addSeries(ts2); 
TimeSeries ts3 = new TimeSeries("Series 2"); 
otherDataSet.addSeries(ts3); 

然後將數據添加到TimeSeries像平常一樣。

然後,您將需要otherDataSetOhlcChart地圖它在原來的情節(mapDatasetToRangeAxis)添加到Plot到同一軸線上,並提供一個Renderer

//Add the otherDataSet to the plot and map it to the same axis at the original plot 
int index = 1; 
plot.setDataset(index, otherDataSet); 
plot.mapDatasetToRangeAxis(index, 0); 

XYItemRenderer renderer2 = new XYLineAndShapeRenderer(); 
plot.setRenderer(1, renderer2); 
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); 

下面是使用OHLCDataset而不是一個實例一個BoxAndWhiskerXYDataset

enter image description here

+0

非常感謝格雷厄姆。我不認爲我會想到我自己的 –

+0

@ user1417653樂於幫助,並歡迎來到Stack Overflow。如果此答案解決了您的問題,請將其標記爲已接受 – GrahamA

相關問題