Code difference between jfreechart XYLineAndShaperanderer, XYDotRenderer and XYSplineRenderer?
Asked Answered
S

1

2

I'm trying to create simple XYSplineRenderer, but code only work if I write XYDotRenderer a = new XYDotRenderer(); or XYLineAndShapeRenderer a = new XYLineAndShapeRenderer(); Can sameone tell what i do wrong? I'm just beginner in programming.

Here is my code:

package kubas;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.ApplicationFrame;

public class spausdink {

    public static void main(String[] args) {

XYSeries series = new XYSeries("XY grafikas");
        series.add(1, 2);
        series.add(2, 4);
        series.add(3, 8);
        series.add(4, 16);
        series.add(5, 32);
        series.add(6, 64);
        series.add(7, 128);
        series.add(8, 256);
        series.add(9, 512);
        series.add(10, 1024);

XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);



ApplicationFrame frame = new ApplicationFrame("Mano grafikas");


        NumberAxis xax = new NumberAxis("x");
        NumberAxis yax = new NumberAxis("y");
        XYSplineRenderer a = new XYSplineRenderer();
        a.setPrecision(10);
        XYPlot xyplot = new XYPlot(dataset, xax, yax, a);

        JFreeChart chart = new JFreeChart(xyplot);

        ChartPanel chartPanel = new ChartPanel(chart);
        frame.setContentPane(chartPanel);
        frame.pack();
        frame.setVisible(true);


    }

}

Edited message:

Both codes work properly, then I created new project.

Stupefacient answered 9/2, 2013 at 10:44 Comment(1)
+1 for sscce.Seawright
S
5

I'm not sure what's wrong, but there's a working example below. I tend to choose small odd values for the precision; the default is 5. The same code works with XYLineAndShapeRenderer or

XYDotRenderer r = new XYDotRenderer();
r.setDotWidth(5);
r.setDotHeight(5);

image

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.*;

public class Test {

    public static final String Title = "f(x) = 2^x";

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }

    private static void display() {
        XYSeries series = new XYSeries(Title);
        for (int i = 0; i <= 10; i++) {
            series.add(i, Math.pow(2, i));
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        NumberAxis domain = new NumberAxis("x");
        NumberAxis range = new NumberAxis("f(x)");
        XYSplineRenderer r = new XYSplineRenderer(3);
        XYPlot xyplot = new XYPlot(dataset, domain, range, r);
        JFreeChart chart = new JFreeChart(xyplot);
        ChartPanel chartPanel = new ChartPanel(chart){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        JFrame frame = new JFrame(Title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(chartPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
Seawright answered 9/2, 2013 at 16:33 Comment(5)
See also Initial Threads and Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?Seawright
Thanks for code, but when I copy/paste your code to Netbeans it show same error like before (. And also with XYLineAndShape and XYDotRanderer works poperly. Maybe problem is in my software?Stupefacient
@VaidasTomkus: Did you add a library in NetBeans? Edit your question and paste the error message.Seawright
I had already solved the problem, I created again new project and now both codes work properly.Stupefacient
@VaidasTomkus: Glad you got it sorted. See also Run > Clean and Build Project (name).Seawright

© 2022 - 2024 — McMap. All rights reserved.