How to draw line over a JFreeChart chart?

15,459

Solution 1

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.

Solution 2

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);
Share:
15,459

Related videos on Youtube

christo
Author by

christo

Updated on June 24, 2022

Comments

  • christo
    christo almost 2 years

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

    How to implement it?

  • Line
    Line over 5 years
    You can use getXYPlot() instead of getPlot() and casting.