Reputation: 11648
How can I draw such a graph using Swing? I have used a JFreeChart
library, but I don't know how can I draw such a line graph using that library?
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.*;
public class DrawGraph{
public void drawGraph(int[][] drawPoints) {
XYSeries series = new XYSeries("Average Weight");
for(int i=0;i<drawPoints.length;i++){
for(int j=0;j<=1;j+=2){
if(drawPoints[i][j]!=0){
series.add(bla...bla...bla...);
}
}
}
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart
("XYLine Chart using JFreeChart", "Age", "Weight",
xyDataset, PlotOrientation.VERTICAL, true, true, false);
ChartFrame frame1=new ChartFrame("XYLine Chart",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
I have drawn graph using this but isn't working...
Upvotes: 0
Views: 2015
Reputation: 205775
It looks like you're having trouble constructing a dataset. You can use a method like that shown below with either ChartFactory.createXYAreaChart()
or ChartFactory.createXYLineChart()
.
private static XYDataset createDataset() {
XYSeriesCollection result = new XYSeriesCollection();
XYSeries series = new XYSeries("Test");
series.add(0, 2);
// more points here
series.add(10, 10);
result.addSeries(series);
return result;
}
See also these examples.
As an aside, it's not clear what's important in you picture, and I can't make sense of the unordered axis at the top. In my opinion, the better question is not How do I make this graph? but rather How can I best display this data?
Upvotes: 1
Reputation: 338
http://sourceforge.net/apps/trac/jung/wiki/JUNGManual
Use JUNG instead. Its easy and written in java.
Upvotes: 0