How to draw line over a JFreeChart chart?
Asked Answered
T

2

6

I have updatable OHLCChart. I need to draw a line over chart.

How to implement it?

T answered 9/2, 2012 at 16:23 Comment(0)
E
29

If you want to draw a vertical or horizontal line at a given position on an axis, you can use a ValueMarker :

ValueMarker marker = new ValueMarker(position);  // position is the value on the axis
marker.setPaint(Color.black);
//marker.setLabel("here"); // see JavaDoc for labels, colors, strokes

XYPlot plot = (XYPlot) chart.getPlot();
plot.addDomainMarker(marker);

Use plot.addRangeMarker() if you want to draw an horizontal line.

Excise answered 9/2, 2012 at 16:42 Comment(1)
You can use getXYPlot() instead of getPlot() and casting.Kletter
M
3

Something like this should work if you want to plot a line indicator (like a moving average for example):

    XYDataset dataSet = // your line dataset

    CombinedDomainXYPlot plot = (CombinedDomainXYPlot) chart.getPlot();
    XYPlot plot = (XYPlot) plot.getSubplots().get(0);
    int dataSetIndx = plot.getDatasetCount();
    plot.setDataset(dataSetIndx, dataSet);

    XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(true, false);
    plot.setRenderer(dataSetIndx, lineRenderer);
Muscat answered 9/2, 2012 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.