Why doesn't JFreeCharts correctly connect the points in my xy-line graph?
Each letter A,T,G,C represents a direction for the plot to graph. Specifically, “A” means move right, “T” is move down, “C” is move up, and “G” is move left. When the applet reads A,T,C, it plots the graph correctly. However, when I plot G, the graph is messed up. When I input "ACACACA," the graph is like a rising staircase. When I input "gtgtgt," the graph should look like a staircase, but it looks like a lightning bolt instead.
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);
}
}
}
Note that XYSeries
allows "Both the sorting and duplicate defaults can be changed in the constructor." So try this:
XYSeries series = new XYSeries("DNA Walk", false, true);
Also, consider this variant form of the switch
statement:
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
...
精彩评论