import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class ResponseChart extends ApplicationFrame {
public static ArrayList mlist;
public static ArrayList jlist;
public static ArrayList lrlist;
public static ArrayList bwlist;
public static ArrayList dlist;
public static ArrayList<String> datelist;
public ResponseChart(String s) {
super(s);
JPanel content = new JPanel(new FlowLayout());
JFreeChart jfreechart=createCombinedChart();
JPanel toppanel = new JPanel(new BorderLayout()) ;
ChartPanel chartpanel = new ChartPanel(jfreechart, true, true, true,
true, true);
JButton btn =new JButton("change chart");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jdbcmysql db = new jdbcmysql();
db.SelectTable();
JFreeChart jfreechart = createCombinedChart();
ChartPanel chartpanel = new ChartPanel(jfreechart, true, true, true,
true, true);
System.out.println("You clicked the button");
}
});
toppanel.setSize(400,400);
toppanel.add(btn);
content.add(toppanel,BorderLayout.NORTH);
content.add(chartpanel, BorderLayout.SOUTH);
chartpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(content);
}
public static JFreeChart createCombinedChart() {
XYDataset xydataset1 = createdelayDataset();
StandardXYItemRenderer standardxyitemrenderer1 = new StandardXYItemRenderer();
NumberAxis numberaxis1 = new NumberAxis("price");
numberaxis1.setAutoRangeIncludesZero(false);
XYPlot xyplot1 = new XYPlot(xydataset1, null, numberaxis1,
standardxyitemrenderer1);
xyplot1.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
XYDataset xydataset2 = createdelayDataset();
StandardXYItemRenderer standardxyitemrenderer2 = new StandardXYItemRenderer();
NumberAxis numberaxis2 = new NumberAxis("averagy");
numberaxis2.setAutoRangeIncludesZero(false);
XYPlot xyplot2 = new XYPlot(xydataset2, null, numberaxis2,
standardxyitemrenderer2);
xyplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(
new DateAxis("Year"));
combineddomainxyplot.setGap(10D);
combineddomainxyplot.add(xyplot1, 1);
combineddomainxyplot.add(xyplot2, 1);
combineddomainxyplot.setOrientation(PlotOrientation.VERTICAL);
return new JFreeChart(null,
JFreeChart.DEFAULT_TITLE_FONT, combineddomainxyplot, true);
}
private static IntervalXYDataset createDataset1() {
mlist = jdbcmysql.getmlist();
datelist = jdbcmysql.getdatelist(); //2009-10-26
//TimeSeries series1 = new TimeSeries("Series 1", Day.class);
TimeSeries series1 = new TimeSeries("Series 1",Hour.class);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i =0;i<mlist.size();i++)
try {
java.util.Date d = new Date();
String datestr = datelist.get(i).toString();
d = formatter.parse(datestr);
System.out.println("***************"+d );
double value = Double.parseDouble(mlist.get(i).toString());
series1.add(new Hour(formatter.parse(datestr)), value);
} catch (java.text.ParseException e) {
// TODO 自動產生 catch 區塊
e.printStackTrace();
}
return new TimeSeriesCollection(series1);
}
private static IntervalXYDataset createdelayDataset() {
dlist = jdbcmysql.getdlist();
datelist = jdbcmysql.getdatelist(); //2009-10-26
//TimeSeries series1 = new TimeSeries("Series 1", Day.class);
TimeSeries series1 = new TimeSeries("Series 1",Hour.class);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i =0;i<dlist.size();i++)
try {
java.util.Date d = new Date();
String datestr = datelist.get(i).toString();
d = formatter.parse(datestr);
System.out.println("***************"+d );
double value = Double.parseDouble(dlist.get(i).toString());
series1.add(new Hour(formatter.parse(datestr)), value);
} catch (java.text.ParseException e) {
// TODO 自動產生 catch 區塊
e.printStackTrace();
}
return new TimeSeriesCollection(series1);
}
public static void main(String args[]) {
ResponseChart combinedxyplotdemo1 = new ResponseChart(
"CombinedDomainXYPlot Demo");
combinedxyplotdemo1.pack();
RefineryUtilities.centerFrameOnScreen(combinedxyplotdemo1);
combinedxyplotdemo1.setVisible(true);
}
}