christo
christo

Reputation: 701

How to draw line over a JFreeChart chart?

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

How to implement it?

Upvotes: 8

Views: 17443

Answers (2)

assylias
assylias

Reputation: 328873

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

Upvotes: 4

Baldrick
Baldrick

Reputation: 24350

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.

Upvotes: 32

Related Questions