我必须绘制一个多线图,表示单个图表中的三种类型的值。它必须从数据库中获取数据并绘制线图。我的代码是
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);
}
}
当我执行上面的操作时,我只得到一个查询的折线图。如果我应该得到三个值的线图,应该用不同的颜色表示怎么办?我应该再增加两个额外的查询,例如
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);
}
}
答案 0 :(得分:0)
您是否尝试过使用其他图表类型作为解决方案?您似乎可以使用createxylinechart。查看示例http://www.codejava.net/java-se/graphics/using-jfreechart-to-draw-xy-line-chart-with-xydataset