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
|
public class LineChartGenerator {
public static CategoryDataset createDataset() throws Exception{
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
final String serieFierro = "Fierro";
final String serieArrastre = "Arrastre";
final String serieMadera = "Madera";
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat formatinv = new SimpleDateFormat("dd-mm");
try {
DBConnexionAction dbConnexion = new DBConnexionAction();
dbConnexion.ouvrirConnexion();
// TRAITEMENT DES LANCES BARCOS DE FIERRO
ResultSet rsFierro = dbConnexion.getStmt().executeQuery(
"select DIA, NUMERO_LANCES from BIT_LANCES_FIERRO_DIA group by DIA,NUMERO_LANCES order by DIA");
// Boucle de traitement des enregistrements
while (rsFierro.next()) {
String jour = rsFierro.getString("DIA");
Date day = format.parse(jour);
DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.FRANCE).format(day);
String nombre = rsFierro.getString("NUMERO_LANCES");
dataset.addValue(new Double(nombre), serieFierro, day.toString());
}
// TRAITEMENT DES LANCES BARCOS DE ARRASTRE
ResultSet rsArrastre = dbConnexion.getStmt().executeQuery(
"select DIA, NUMERO_LANCES from BIT_LANCES_ARRASTRE_DIA group by DIA,NUMERO_LANCES order by DIA");
// Boucle de traitement des enregistrements
while (rsArrastre.next()) {
String jour = rsArrastre.getString("DIA");
Date day = format.parse(jour);
String nombre = rsArrastre.getString("NUMERO_LANCES");
dataset.addValue(new Double(nombre), serieArrastre, day.toString());
}
// TRAITEMENT DES LANCES BARCOS DE MADERA
ResultSet rsMadera = dbConnexion.getStmt().executeQuery(
"select DIA, NUMERO_LANCES from BIT_LANCES_MADERA_DIA group by DIA,NUMERO_LANCES order by DIA");
// Boucle de traitement des enregistrements
while (rsMadera.next()) {
String jour = rsMadera.getString("DIA");
Date day = format.parse(jour);
String nombre = rsMadera.getString("NUMERO_LANCES");
dataset.addValue(new Double(nombre), serieMadera, day.toString());
}
dbConnexion.fermerConnexion();
} catch (Exception e) {
throw e;}
return dataset;
}
public static String generateLineChart(HttpSession session, PrintWriter pw) {
String filename = null;
final CategoryDataset dataset;
try {
dataset = createDataset();
// create the chart...
final JFreeChart chart = ChartFactory.createLineChart(
"Numero de lances por dia", // chart title
"Dia", // domain axis label
"Numero de lance", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// final StandardLegend legend = (StandardLegend) chart.getLegend();
// legend.setDisplaySeriesShapes(true);
// legend.setShapeScaleX(1.5);
// legend.setShapeScaleY(1.5);
// legend.setDisplaySeriesLines(true);
chart.setBackgroundPaint(Color.white);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
// customise the range axis...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
// ****************************************************************************
// * JFREECHART DEVELOPER GUIDE *
// * The JFreeChart Developer Guide, written by David Gilbert, is
// available *
// * to purchase from Object Refinery Limited: *
// * *
// * http://www.object-refinery.com/jfreechart/guide.html *
// * *
// * Sales are used to provide funding for the JFreeChart project -
// please *
// * support us so that we can continue developing free software. *
// ****************************************************************************
// customise the renderer...
final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
.getRenderer();
// renderer.setDrawShapes(true);
renderer.setSeriesStroke(0, new BasicStroke(2.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 10.0f, 6.0f }, 0.0f));
renderer.setSeriesStroke(1, new BasicStroke(2.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 6.0f, 6.0f }, 0.0f));
renderer.setSeriesStroke(2, new BasicStroke(2.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 2.0f, 6.0f }, 0.0f));
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 1000, 600, info,
session);
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (Exception e) {
}
return filename;
}
} |
Partager