Bellkross
Bellkross

Reputation: 11

JFreeChart - can't draw graphics like in excel

I have a problem with draw graphics in JFreeChart, I need to draw a graphic like as in this pic.

excel graphic

But in java am getting a graphic like this

graphic in java

is there any possible way to solve this.

My java code:

    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    
    import javax.swing.*;
    
    public class MyGraph {
        private static double A=20, R0=-40;
    
        public static void main(String[] args) {
            XYSeries series = new XYSeries("my graphic");
    
            for (double fi = 0,step = 0.05; fi < 2*Math.PI; fi+=step) addCoordinate(series,fi);
    
            XYDataset xyDataset = new XYSeriesCollection(series);
            JFreeChart chart = ChartFactory
                    .createXYLineChart("my graphic 14", "x", "y",
                            xyDataset,
                            PlotOrientation.VERTICAL,
                            true, true, true);
            JFrame frame =
                    new JFrame("MinimalStaticChart");
            frame.getContentPane()
                    .add(new ChartPanel(chart));
            frame.setSize(400,300);
            frame.setVisible(true);
        }
        private static void addCoordinate(XYSeries series,double fi){
            double ro = Math.cos(fi)-0.5;
            series.add(ro*Math.cos(fi),ro*Math.sin(fi));
            System.out.printf("fi = %f   ro = %f   x = %f   y = %f\n", fi , ro, ro*Math.cos(fi), ro*Math.sin(fi));
        }
    }

How you can see in images, form and coordinates the same, but JFreeChart draw graphics not like in excel, how can I draw my Graphic like in excel with java, which methods should I use? (If you can - give examples, please)

Upvotes: 0

Views: 80

Answers (1)

Stefan Warminski
Stefan Warminski

Reputation: 1835

You're using the XYSeries(String) constructor wich sorts the values by x. You will see it if you would add this after filling your series:

for (Object i : series.getItems()) {
    System.out.println(i);
}

So the only thing you need to change is the initialization of series:

XYSeries series = new XYSeries("my graphic", false);

Upvotes: 2

Related Questions