Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

java - Code difference between jfreechart XYLineAndShaperanderer, XYDotRenderer and XYSplineRenderer?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...