Java 如何在jfreechart中结合数据库执行多个查询以绘制折线图

Java 如何在jfreechart中结合数据库执行多个查询以绘制折线图,java,jfreechart,Java,Jfreechart,我必须画一个多折线图,在一个图表中显示三种类型的值。它必须从数据库中获取数据并绘制线图。我的代码是 package barr4; import java.sql.*; import java.io.*; import org.jfree.ui.*; import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.j

我必须画一个多折线图,在一个图表中显示三种类型的值。它必须从数据库中获取数据并绘制线图。我的代码是

package barr4;


import java.sql.*;
import java.io.*;
import org.jfree.ui.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.*;
import org.jfree.data.jdbc.JDBCCategoryDataset;

public class Barr4 {
public static void main(String[] args) throws Exception 
{
String query = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Five by Eight' group by tmp.s_id, u.u_keyboard ";
JDBCCategoryDataset dataset = new JDBCCategoryDataset( "jdbc:mysql://localhost:3306/kumararaja", 
    "com.mysql.jdbc.Driver","root", "test123");
dataset.executeQuery(query);
JFreeChart chart = ChartFactory.createLineChart("Tamil Five by Eight", "s_id", "AVG(cpm)", dataset, PlotOrientation.VERTICAL, true, true, false);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
ApplicationFrame f = new ApplicationFrame("Chart");
f.setContentPane(chartPanel);
f.pack();
f.setVisible(true);
}
}
当我执行上述get时,我只得到一个查询的折线图。如果我得到三个值的折线图,这些值应该用不同的颜色表示,会怎么样。我应该提出另外两个问题,例如

String query = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Logical' group by tmp.s_id, u.u_keyboard ";
还有一个

String query = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Inscript' group by tmp.s_id, u.u_keyboard ";
请给我一个解决方案

编辑:这是我在XY图表上的一次尝试

package xychart;

import java.sql.*;
import java.io.*;
import org.jfree.ui.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYSeries;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.data.*;
import org.jfree.data.jdbc.JDBCCategoryDataset;


public class XYchart
{
public static void main( String[ ] args )throws Exception
{

   final XYSeries TamilFivebyEight = new XYSeries( "TamilFivebyEight" );

   String query = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Five by Eight' group by tmp.s_id, u.u_keyboard ";
JDBCCategoryDataset dataset = new JDBCCategoryDataset( "jdbc:mysql://localhost:3306/kumararaja", 
    "com.mysql.jdbc.Driver","root", "test123");
   dataset.executeQuery(query);


  final XYSeries TamilLogical = new XYSeries( "TamilLogical" );
  String query1 = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Logical' group by tmp.s_id, u.u_keyboard ";
  dataset.executeQuery(query1);


  final XYSeries TamilInscript = new XYSeries( "TamilInscript" );
  String query2 = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Inscript' group by tmp.s_id, u.u_keyboard ";
  dataset.executeQuery(query2);


 final XYSeriesCollection dataset1 = new XYSeriesCollection( );
 dataset1.addSeries( TamilFivebyEight );
 dataset1.addSeries( TamilLogical );
 dataset1.addSeries( TamilInscript );

  JFreeChart xylineChart = ChartFactory.createXYLineChart(
     "Keyboard performance", 
          "s_id", 
          "AVG(cpm)", 
          dataset1, PlotOrientation.VERTICAL,
          true, true, false);


  ChartPanel chartPanel = new ChartPanel(xylineChart);
  chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
  ApplicationFrame f = new ApplicationFrame("Chart");
  f.setContentPane(chartPanel);
  f.pack();
  f.setVisible(true);

   }
}
这是为xy图表编辑的图表

package xychart;

import java.sql.*;
import java.io.*;
import org.jfree.ui.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYSeries;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.data.*;
import org.jfree.data.jdbc.JDBCCategoryDataset;


public class XYchart
{
public static void main( String[ ] args )throws Exception
{

final XYSeries TamilFivebyEight = new XYSeries( "TamilFivebyEight" );

String query = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Five by Eight' group by tmp.s_id, u.u_keyboard ";
 JDBCCategoryDataset dataset = new JDBCCategoryDataset( "jdbc:mysql://localhost:3306/kumararaja", 
"com.mysql.jdbc.Driver","root", "test123");
dataset.executeQuery(query);


 final XYSeries TamilLogical = new XYSeries( "TamilLogical" );
 String query1 = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Logical' group by tmp.s_id, u.u_keyboard ";
 dataset.executeQuery(query1);


 final XYSeries TamilInscript = new XYSeries( "TamilInscript" );
 String query2 = "select tmp.s_id, avg(tmp.cpm), u.u_keyboard from (SELECT u_id, s_id, avg((char_length(phrase_typed)-1)/((l_timestamp - f_timestamp)/60000)) as cpm FROM `session_details_table` WHERE s_id<32 group by U_ID, S_ID having cpm is not null) tmp, userdetails u where tmp.u_id = u._id and u.u_keyboard = 'Tamil Inscript' group by tmp.s_id, u.u_keyboard ";
 dataset.executeQuery(query2);


 final XYSeriesCollection dataset1 = new XYSeriesCollection( );
 dataset1.addSeries( TamilFivebyEight );
 dataset1.addSeries( TamilLogical );
 dataset1.addSeries( TamilInscript );

 JFreeChart xylineChart = ChartFactory.createXYLineChart(
 "Keyboard performance", 
      "s_id", 
      "AVG(cpm)", 
      dataset1, PlotOrientation.VERTICAL,
      true, true, false);


   ChartPanel chartPanel = new ChartPanel(xylineChart);
   chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
   ApplicationFrame f = new ApplicationFrame("Chart");
   f.setContentPane(chartPanel);
   f.pack();
   f.setVisible(true);

    }
    }

您是否尝试使用其他图表类型作为解决方案?您似乎可以使用createxylinechart。查看一个示例

我尝试使用xyline图表,但我不知道如何查询它,即与数据库的集成。我将发布xy图表的代码。只有当我给它打分时,它才会被标出来。我不知道如何使用query绘制它。请注意。可能使用JDBCCategoryDataset;有些人使用JDBCXYDataset.Cross-posted。