为什么JFreeCharts没有正确连接我的xy-line图中的点?

时间:2010-05-12 20:39:45

标签: java plot coordinates line jfreechart

每个字母A,T,G,C表示绘图的方向。具体而言,“A”表示向右移动,“T”向下移动,“C”向上移动,“G”向左移动。当applet读取A,T,C时,它会正确绘制图形。但是,当我绘制G时,图形会混乱。当我输入“ACACACA”时,图形就像一个上升的楼梯。当我输入“gtgtgt”时,图形应该看起来像一个楼梯,但它看起来像闪电。

import java.applet.Applet;
import java.awt.Button;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
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;

public class If_Graph extends Applet implements ActionListener {

    Panel panel;
    TextArea textarea, outputArea;
    Button move;
    String thetext;
    Scanner reader = new Scanner(System.in);
    String thetext2;
    int size, p, q;
    int x, y;

    public void init() {

        setSize(500, 500); //set size of applet

        panel = new Panel();
        add(panel);
        setVisible(true);
        textarea = new TextArea(10, 20);
        add(textarea);

        move = new Button("Graph");
        move.addActionListener(this);
        add(move);
    }

    public void actionPerformed(ActionEvent e) {
        XYSeries series = new XYSeries("DNA Walk");

        x = 0;
        y = 0;
        series.add(x, y);

        if (e.getSource() == move) {
            thetext = textarea.getText(); //the text is the DNA bases pasted
            thetext = thetext.replaceAll(" ", ""); //removes spaces
            thetext2 = "";

            for (int i = 0; i < thetext.length(); i++) {
                char a = thetext.charAt(i);

                switch (a) {
                    case 'A': //moves right
                        x += 1;
                        y += 0;
                        series.add(x, y);
                        break;
                    case 'a':
                        x += 1;
                        y += 0;
                        series.add(x, y);
                        break;
                    case 'C': //moves up
                        x += 0;
                        y += 1;
                        series.add(x, y);
                        break;
                    case 'c':
                        x += 0;
                        y += 1;
                        System.out.println(x + "," + y);
                        series.add(x, y);
                        break;
                    case 'G': //move left
                        x -= 1;
                        y += 0;
                        series.add(x, y);
                        System.out.println("G is: " + x + "," + y);
                        break;
                    case 'g':
                        x -= 1;
                        y += 0;
                        System.out.println("g is: " + x + "," + y);
                        series.add(x, y);
                        break;
                    case 'T': //move down
                        x += 0;
                        y -= 1;
                        series.add(x, y);
                        System.out.println("T is: " + x + "," + y);
                        break;
                    case 't':
                        x += 0;
                        y -= 1;
                        series.add(x, y);
                        System.out.println("t is: " + x + "," + y);
                        break;
                    default: // series.add(0,0);
                        break;
                }
            }
            XYDataset xyDataset = new XYSeriesCollection(series);
            JFreeChart chart = ChartFactory.createXYLineChart(
                "DNA Random Walk", "", "", xyDataset,
                PlotOrientation.VERTICAL, true, true, false);
            ChartFrame frame1 = new ChartFrame("DNA Random Walk", chart);
            frame1.setVisible(true);
            frame1.setSize(300, 300);
            outputArea.setText(thetext2);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

请注意XYSeries允许“在构造函数中可以更改排序和重复的默认值。”所以试试这个:

XYSeries series = new XYSeries("DNA Walk", false, true);

alt text http://i43.tinypic.com/16ab03p.png

另外,请考虑switch语句的这种变体形式:

switch (a) {
    case 'A': case 'a': //moves right
        x += 1;
        y += 0;
        series.add(x, y);
        System.out.println("A is: " + x + "," + y);
        break;
    case 'C': case 'c': //moves up
    ...