I need to create a XYLineChart with values from database (Java(Mysql)

杀马特。学长 韩版系。学妹 提交于 2019-12-02 19:57:37

问题


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.


回答1:


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:

  • Lesson: JDBC Introduction
  • Lesson: JDBC Basics

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:

  • MySQL and Java JDBC - Tutorial


来源:https://stackoverflow.com/questions/22300828/i-need-to-create-a-xylinechart-with-values-from-database-javamysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!