2

I've been googling my for a while now, and did not find any useful stuff, so that is why I ask you guys.

Can I draw mathematical functions (e.g. sine, cosine, etc.) with JFreeChart?

Thanks

overbet13
  • 1,654
  • 1
  • 20
  • 36

3 Answers3

4

JFreeChart is for plotting data, not functions. But you should be able to create a table of (x,y) values and then plot them. If this is for a desktop app, look at the JavaFX api. The API includes charts and functions to draw lines.

Thorn
  • 4,015
  • 4
  • 23
  • 42
1

Im assuming that you can plot the points yourself in which case you would simply evaluate the mathematical function for each x along the graph.

getY(float x) {
    return /*your function*/ Math.sin(x);
}
Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
1

There may not be a built in way to plot sinx but there doesn't need to be. Remember that what your saying is y=sin(x)! What you need to plot is the x and y value. Create a loop of x values then plug them into sin(x) using Java and Math. That answer IS your y value! So now you have your x and y values to plot sin(x).

Example

final XYSeries series1 = new XYSeries("First");


    for(double i = 0; i < 10; i += 0.2){
        double sinx = Math.sin(i);
        series1.add(i, sinx);
    }


    final XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series1);
Michael Scott
  • 429
  • 2
  • 8
  • 18