Question

I need to create a XYLineChart with values from database.

I see this:

public class Test {

  public static void main(String[] args) {

    XYSeries                series1   = new XYSeries("Lions");
                            series1.add(20, 10);
                            series1.add(40, 20);
                            series1.add(70, 50);

    XYSeries                series2   = new XYSeries("Rabbits");
                            series2.add(20, 30);
                            series2.add(40, 40);
                            series2.add(70, 10);

    XYSeriesCollection      xyDataset = new XYSeriesCollection();
                            xyDataset.addSeries(series1);
                            xyDataset.addSeries(series2);

    JFreeChart              chart     = ChartFactory.createXYLineChart("Weight","kg","Numbers",xyDataset,PlotOrientation.VERTICAL,true,false,false);
                            chart.setBackgroundPaint(Color.yellow); 

    XYPlot                  plot      = (XYPlot) chart.getPlot();
                            plot.setBackgroundPaint       (Color.white);
                            plot.setDomainGridlinePaint   (Color.GREEN);
                            plot.setRangeGridlinePaint    (Color.orange);
                            plot.setAxisOffset            (new RectangleInsets(50, 0, 20, 5));
                            plot.setDomainCrosshairVisible(true);
                            plot.setRangeCrosshairVisible (true);

    XYLineAndShapeRenderer  renderer  = (XYLineAndShapeRenderer) plot.getRenderer();      
                            renderer.setBaseShapesVisible(true);
                            renderer.setBaseShapesFilled (true);

    ChartFrame              frame     = new ChartFrame("ChartFrame", chart);
                            frame.setSize   (450, 250);
                            frame.setVisible(true);                   
  }  

}

But don't know how to fetch data from the database and show them on the graph.

With a single line graph I can connect to the database, but several don't know.

Can anyone help me? I really need help, please.

Was it helpful?

Solution

You might want to take a look to this topic: Multiple graphs in multiple figures using jFreeChart. In the answer is described how to work with JFreeChart and SwingWorker to make time consuming tasks (like database calls) in a background thread and update Swing components (in this case the chart) in the Event Dispatch Thread where Swing components creation and update should take place.

Having said this you should do database calls within doInBackground() method publishing interim results through publish() method and adding those to the chart within process() method.

About database call you should have a look to JDBC technology. There are official trails here:

Depending on what database engine are you using there are some good tutorials too, just google for "jdbc + used DBMS" (e.g.: jdbc + mysql, jdbc + postgresql, jdbc + sqlite, etc.). Here is a really good one:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top