Plotting a line by using an euqation

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Scarlett
Posts: 11
Joined: Fri Sep 29, 2006 11:21 am

Plotting a line by using an euqation

Post by Scarlett » Mon Oct 16, 2006 7:51 am

Hi All,

Is there a way to plot a line using a line equation?

y = mx+c.

where 'm' is the slope
and 'c' is the Y intercept.

In the above case I know the values of 'm' and 'c'

Thanks,
Scarlett

andyt
Posts: 4
Joined: Fri Oct 27, 2006 5:58 pm
Contact:

Post by andyt » Fri Oct 27, 2006 6:18 pm

If you have a range of y or x values then generate a double[][] data. For a straight line just use the four coordinates at the extremes. Then use ChartFactory.createXYLineChart as follows:


DefaultXYDataset aDefaultXYDataset = new DefaultXYDataset();
aDefaultXYDataset.addSeries( "a", data );
JFreeChart aJFreeChart = ChartFactory.createXYLineChart(
null, null, null, aDefaultXYDataset,
PlotOrientation.HORIZONTAL, false, false, false );

Play around with the parameters until you get what your after.

I've only just started with JFreeChart, but that is how I did it.

Best wishes,

Andy

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Oct 31, 2006 3:31 pm

JFreeChart doesn't know how to plot equations, just data, so you need to get sample points from your equation. The way Andy suggests is probably the easiest.

Another alternative is to write a class that represents your function and implements the Function2D interface (this assumes that your function can be expressed as y = f(x))...having done that, you can use the DatasetUtilities.sampleFunction2D() method to create a dataset containing sample values from your function.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked