6

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

How to implement it?

Baldrick
  • 23,882
  • 6
  • 74
  • 79
christo
  • 671
  • 1
  • 8
  • 26

2 Answers2

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.

Baldrick
  • 23,882
  • 6
  • 74
  • 79
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);
assylias
  • 321,522
  • 82
  • 660
  • 783