JFreeChart - Timeseries和CandleStick在同一张图表上

时间:2012-08-29 08:00:53

标签: java overlay jfreechart candlestick-chart timeserieschart

我正在尝试在JFreeChart中生成一个包含重叠烛台图和时间序列图的单个图表。 (有点像这样)

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;
}

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; }

将非常感激地收到任何建议

1 个答案:

答案 0 :(得分:6)

这可以使用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

然后,您需要将otherDataSet添加到Plot OhlcChart中,将其映射到原始图表(mapDatasetToRangeAxis)的同一轴,并提供{Renderer 1}}

//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