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
|
package Controlleur;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import Modele.Produit;
import Modele.ProduitFabriquer;
import Outil.ADO;
import com.db4o.foundation.NotSupportedException;
public class Graph {
private ADO ado = new ADO();
static final int WIDTH = 450;
static final int HEIGHT = 320;
public String generateBarChart(HttpSession session, PrintWriter pw) {
String filename = null;
try {
String series1 = "";
String series2 = "";
String series3= "";
// column keys...
String category1 = "Produit Fabriqué";
// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
ado.connexion();
for(Produit instance : ado.listefab()){
for(int i=1; i<ado.listefab().size(); i++){
// row keys...
if(i==1){
series1 = instance.getCode();
dataset.addValue(instance.getStock(), series1, category1);
}else if(i==2){
series2 = instance.getCode();
dataset.addValue(instance.getStock(), series2, category1);
}else if(i==3){
series3 = instance.getCode();
dataset.addValue(instance.getStock(), series3, category1);
}
}
}
ado.deconnexion();
// Create the chart object
CategoryAxis categoryAxis = new CategoryAxis("");
ValueAxis valueAxis = new NumberAxis("");
BarRenderer renderer = new BarRenderer();
//renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","series","section"));
renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
Plot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
JFreeChart chart = new JFreeChart("Quantité de produit en stock", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint(java.awt.Color.blue);
// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 800, 500, info, session);
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info, false);
pw.flush();
} catch (NotSupportedException e) {
System.out.println(e.toString());
filename = "public_nodata_500x300.png";
} catch (Exception ex) {
System.out.println("Exception - " + ex.toString());
ex.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
public static void main(String[] args) {
try {
Graph graf = new Graph();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
PrintWriter pw = new PrintWriter(System.out);
String filename = graf.generateBarChart( null, pw);
// String filename = WebHitChart.generatePieChart(sdf.parse("01-Aug-2002"), null, pw);
// String filename = WebHitChart.generateXYChart("service", null, pw);
System.out.println("filename - " + filename);
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace();
}
return;
}
} |
Partager