1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
public class LineChartTests extends ApplicationFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private String TG,Dt2,Dt1;
public LineChartTests(String title,String tgrp,String dt2) {
super(title);
TG=tgrp;
Dt2=dt2;
CategoryDataset dataset = createDataset(tgrp);
CategoryDataset dataset1 = createDataset1(tgrp);
CategoryDataset dataset2 = createDataset2(tgrp);
JFreeChart chart = createChart(dataset,dataset1,dataset2);
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
// ---> chart.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);
}
private String url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/pfe.mdb";
private String user = "user";
private String pass = "pass";
private String []temps = new String [100];
private float[] Traf= new float [100];
private float[] SRVLINES= new float [100];
private float[] CULC= new float [100];
private int y=0;
private CategoryDataset createDataset(String tgrp) {
// row keys...
String series1 = "TGRP:CULC";
// String series2 = "TGRP:SRVLINES";
// String series3 = "TGRP:CULC";
// --connection à la base donnée
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
}
catch(ClassNotFoundException e) {
System.err.println("Erreur de chargement du driver : + e") ;
}
try {
Connection connect = DriverManager.getConnection(url,user,pass) ;
Statement statement = connect.createStatement() ;
String query = "select \"TGRP:CULC\",Time from TGRPDetailed where (Date=\'"+Dt2+"\' and Object=\'"+TG+"\') ";
//String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') ";
ResultSet resultset = statement.executeQuery(query);
while (resultset.next()!= false){
String Str1 = resultset.getString(1);
String Str2 = resultset.getString(2);
if (Str1.indexOf("-")==-1){
Traf[y]= Float.parseFloat(Str1);
}
temps[y]=Str2;
y++;
}
connect.close();// fermuture de la connection à la BD
}
catch(SQLException sqle) {
System.err.println("Erreur lors de la connexion : " + sqle) ;
}
String Str;
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i=0;i<y;i++){
Str=Dt2+","+ temps[i];
dataset.addValue(Traf[i], series1, Str);
}
return dataset;
}
private CategoryDataset createDataset1(String tgrp) {
// row keys...
String series2 = "TGRP:SRVLINES";
// --connection à la base donnée
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
}
catch(ClassNotFoundException e) {
System.err.println("Erreur de chargement du driver : + e") ;
}
try {
Connection connect = DriverManager.getConnection(url,user,pass) ;
Statement statement = connect.createStatement() ;
y=0;
String query = "select \"TGRP:SRVLINES\",Time from TGRPDetailed where (Date=\'"+Dt2+"\' and Object=\'"+TG+"\') ";
//String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') ";
ResultSet resultset = statement.executeQuery(query);
while (resultset.next()!= false){
String Str1 = resultset.getString(1);
if (Str1.indexOf("-")==-1){
SRVLINES[y]= Float.parseFloat(Str1);
}
// temps[y]=Str2;
y++;
}
connect.close();// fermuture de la connection à la BD
}
catch(SQLException sqle) {
System.err.println("Erreur lors de la connexion : " + sqle) ;
}
String Str;
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i=0;i<y;i++){
Str=Dt2+","+ temps[i];
// dataset.addValue(Traf[i], series1, temps[i]);
dataset.addValue(SRVLINES[i], series2, Str);
// dataset.addValue(CULC[i], series3, temps[i]);
}
return dataset;
}
private CategoryDataset createDataset2(String tgrp) {
// row keys...
//String series1 = "TGRP:TRAFCARR";
//String series2 = "TGRP:SRVLINES";
// String series3 = "TGRP:CULC";
String series3 ="TGRP:TRAFCARR";
// --connection à la base donnée
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
}
catch(ClassNotFoundException e) {
System.err.println("Erreur de chargement du driver : + e") ;
}
try {
Connection connect = DriverManager.getConnection(url,user,pass) ;
Statement statement = connect.createStatement() ;
y=0;
String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Dt2+"\' and Object=\'"+TG+"\') ";
//String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') ";
ResultSet resultset = statement.executeQuery(query);
while (resultset.next()!= false){
String Str1 = resultset.getString(1);
if (Str1.indexOf("-")==-1){
CULC[y]= Float.parseFloat(Str1);
}
// temps[y]=Str2;
y++;
}
connect.close();// fermuture de la connection à la BD
}
catch(SQLException sqle) {
System.err.println("Erreur lors de la connexion : " + sqle) ;
}
String Str;
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i=0;i<y;i++){
Str=Dt2+","+ temps[i];
// dataset.addValue(Traf[i], series1, temps[i]);
// dataset.addValue(SRVLINES[i], series2, temps[i]);
dataset.addValue(CULC[i], series3, Str);
}
return dataset;
}
private JFreeChart createChart(CategoryDataset dataset, CategoryDataset dataset12, CategoryDataset dataset22) {
// create the chart...
JFreeChart jfreechart = ChartFactory.createLineChart(
"Line Chart Demo 1", // chart title
"Time", // domain axis label
"TGRP:CULC", // range axis label
dataset, // data
//dataset12, // data
//dataset22, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setOrientation(PlotOrientation.VERTICAL);
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setRangeGridlinePaint(Color.white);
categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
categoryplot.getRangeAxis().setFixedDimension(15D);
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer() ;
lineandshaperenderer.setSeriesPaint(0, Color.black);
categoryplot.setRenderer(0, lineandshaperenderer);
CategoryDataset dataset1 = createDataset1(TG);
categoryplot.setDataset(1,dataset1) ;
categoryplot.mapDatasetToRangeAxis(1, 1);
//LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer();
LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer(true,false);
lineandshaperenderer1.setSeriesPaint(1, Color.red);
categoryplot.setRenderer(1, lineandshaperenderer1);
NumberAxis numberaxis = new NumberAxis("TGRP:SRVLINES");
numberaxis.setFixedDimension(10D);
numberaxis.setAutoRangeIncludesZero(false);
numberaxis.setLabelPaint(Color.red);
numberaxis.setTickLabelPaint(Color.red);
categoryplot.setRangeAxis(1, numberaxis);
categoryplot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
//StandardXYItemRenderer standardxyitemrenderer1 = new StandardXYItemRenderer();
CategoryDataset dataset2 = createDataset2(TG);
categoryplot.setDataset(2,dataset2) ;
categoryplot.mapDatasetToRangeAxis(2, 2);
LineAndShapeRenderer lineandshaperenderer2 = new LineAndShapeRenderer(true,false);
lineandshaperenderer2.setSeriesPaint(2, Color.blue);// couleur de la courbe
categoryplot.setRenderer(2, lineandshaperenderer2);
NumberAxis numberaxis1 = new NumberAxis("TGRP:TRAFCARR");
numberaxis1.setLabelPaint(Color.blue);// couleur indexe de l'axe
numberaxis1.setTickLabelPaint(Color.blue);// couleur des value
categoryplot.setRangeAxis(2, numberaxis1);
categoryplot.setRangeAxisLocation(2, AxisLocation.BOTTOM_OR_RIGHT);
return jfreechart;
} |
Partager