1

I want to know how to draw lines, dots(or tiny circles), rectangles and trapezoids in jfreechart on a XY Plot. Most of them from certain coordinate to the Range Zero Baseline. I have to represent root-finding methods like this:

http://www2.lv.psu.edu/ojj/courses/cmpsc-201/201-images/bisect.jpg

or Simpson's rule like this:

http://upload.wikimedia.org/wikipedia/commons/0/08/Simpson_rule.png

I already have the functions for to find the solution and to plot, and I only need draw the shapes in certain coordinates. I'm new with jfreechart and plotting and I was been looking for ways to do this.

My plot's code:

import javax.swing.JPanel;
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.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.plot.XYPlot;

public class Grafica1 {
JFreeChart grafica;
XYSeriesCollection datos=new XYSeriesCollection();
String titulo;
String etiquetax;
String etiquetay;
int cont=1;

public Grafica1(String t, String x, String y){
    titulo=t;
    etiquetax=x;
    etiquetay=y;
    grafica=ChartFactory.createXYLineChart(titulo, x, y, datos, PlotOrientation.VERTICAL, true, true, true);
    XYPlot plot= (XYPlot) grafica.getPlot();
    //plot.setDomainZeroBaselineVisible(true);
    plot.setRangeZeroBaselineVisible(true);
}
public Grafica1(){
    this("Sin título", "x", "y");
}

public void agregarGrafica(String id, double[] x, double[] y){
    XYSeries s=new XYSeries("["+(cont++)+"] "+id);
    int n=x.length;
    for(int i=0;i<n;i++){
        s.add(x[i], y[i]);
    }
    datos.addSeries(s);

}

public void crearGrafica(String id, double[] x, double[] y){
    cont=1;
    datos.removeAllSeries();
    agregarGrafica(id, x, y);

}

public JPanel obtieneGrafica(){
    return new ChartPanel(grafica);
}

}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Janyo
  • 11
  • 1
  • 2

1 Answers1

1

You can combine XYLineAndShapeRenderer with SwingWorker, as shown here. Use annotations and markers as required.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045