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

4
  • Yes, I believe that you can do this. What does the JFreeChart documentation tell you about this? Commented May 7, 2012 at 20:45
  • 1
    Yes you can - not sure if it is built in but you just need to create an XY series (with y = cos(x) for example) and draw it. Where are you stuck? Commented May 7, 2012 at 20:48
  • Well, not much. This is my problem in fact: I did not find any helpful stuff in the reference. Commented May 7, 2012 at 20:48
  • I couldn't figure out how to create a graph of some mathematical functions. But this XY series seems to help me. Thanks. Commented May 7, 2012 at 20:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.