Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw line over a JFreeChart chart?

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

How to implement it?

like image 680
christo Avatar asked Feb 09 '12 16:02

christo


People also ask

How do you make a line graph in Java?

To create a line chart, at a minimum, you must define two axes, create the LineChart object by instantiating the LineChart class, create one or more series of data by using the XYChart. Series class, and assign the data to the chart.

How do you create a bar chart in Java?

Creating a Bar Chart To build a bar chart in your JavaFX application, create two axes, instantiate the BarChar class, define the series of data, and assign the data to the chart. Example 7-1 creates a bar chart with three series of data to present financial information about five countries.


2 Answers

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.

like image 135
Baldrick Avatar answered Oct 06 '22 01:10

Baldrick


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);
like image 37
assylias Avatar answered Oct 06 '22 00:10

assylias