codice:
public void creaGraficoDistributori(WebServer wb, CreaGraficoForm cgForm, HttpServletRequest request, String tipoServizio){
// PREPARO IL GRAFICO
String pathGrafico = System.currentTimeMillis() + ".png";
cgForm.addPathGrafico(pathGrafico);
DefaultPieDataset dataset = new DefaultPieDataset();
String titoloGrafico = "";
if(tipoServizio.equals("DIST_ORD")){
dataset = addDettaglioDistributori(wb, cgForm, request, "FCKOrdine");
titoloGrafico = CHART_TITLE_DIST_ORD;
}
if(tipoServizio.equals("DIST_DISP")){
dataset = addDettaglioDistributori(wb, cgForm, request, "FCKDisponibilita");
titoloGrafico = CHART_TITLE_DIST_DISP;
}
if(tipoServizio.equals("DIST_RDOC")){
dataset = addDettaglioDistributori(wb, cgForm, request, "FCKRichiestaDocumento");
titoloGrafico = CHART_TITLE_DIST_RDOC;
}
if(tipoServizio.equals("DIST_INFO")){
dataset = addDettaglioDistributori(wb, cgForm, request, "FCKInfoCom");
titoloGrafico = CHART_TITLE_DIST_INFO;
}
if(tipoServizio.equals("DIST_TOTAL")){
dataset = addDettaglioDistributori(wb, cgForm, request, "FCK");
titoloGrafico = CHART_TITLE_DIST_TOTAL;
}
// calcolo del totale
int totale = 0;
for(int i=0;i<dataset.getItemCount();i++){
totale = totale + dataset.getValue(i).intValue();
}
String titolo = titoloGrafico + " (Somma Totale : " + totale + ")";
// creo il grafico
JFreeChart chart = ChartFactory.createPieChart3D(
titolo, // chart title
dataset, // dataset
true, // include legend
true,
false
);
PiePlot plot = (PiePlot) chart.getPlot();
// effetto trasparenza
plot.setForegroundAlpha(0.8f);
// colori personalizzati
Color[] colors = {new Color(255,92,255), new Color(92,255,255), Color.orange, new Color(255,178,178), new Color(255,92,92), new Color(92,92,255), new Color(92,255,92), new Color(255,255,92), Color.lightGray};
PieRenderer renderer = new PieRenderer(colors);
renderer.setColor(plot, dataset);
plot.setNoDataMessage("Nessun dato presente");
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));
plot.setLegendLabelGenerator(new CustomLegendGenerator());
plot.setStartAngle(290);
// personalizza il titolo del grafico
chart.getTitle().setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 15));
// scrive il file
try{
ChartUtilities.saveChartAsPNG(new File(GRAFICO_PATH_FILE + pathGrafico), chart, 1000, 470);
} catch (IOException e) {
ActionErrors errori = new ActionErrors();
ActionError err = new ActionError("errors.statistiche.IOException");
errori.add("exception", err);
saveErrors(request,errori);
}
}
public static class PieRenderer
{
private Color[] color;
public PieRenderer(Color[] color)
{
this.color = color;
}
public void setColor(PiePlot plot, DefaultPieDataset dataset)
{
List <Comparable> keys = dataset.getKeys();
int aInt;
for (int i = 0; i < keys.size(); i++)
{
aInt = i % this.color.length;
plot.setSectionPaint(keys.get(i), this.color[aInt]);
}
}
public void setColor(PiePlot plot, CategoryDataset dataset)
{
List <Comparable> keys = dataset.getRowKeys();
int aInt;
for (int i = 0; i < keys.size(); i++)
{
aInt = i % this.color.length;
plot.setSectionPaint(keys.get(i), this.color[aInt]);
}
}
}
static class CustomLegendGenerator implements PieSectionLabelGenerator {
/**
* Set customized legend in the pie chart
*
* @param dataset PieChart DataSet
* @param key Comparable Key
* @return Result String to be displayed
*/
public String generateSectionLabel(final PieDataset dataset, final Comparable key) {
/* Creating a temporary string to hold value defined by us */
String temp = null;
String value = "";
if (dataset != null) {
value = String.valueOf(dataset.getValue(key.toString()).intValue());
/* Assigning the Value from dataset as a legend item for display */
temp = key.toString() + " = " + value + " ";
}
/* Returning the formatted string back to set the label */
return temp;
}
@Override
public AttributedString generateAttributedSectionLabel(PieDataset arg0,Comparable arg1) {
// TODO Auto-generated method stub
return null;
}
}
// FUNZIONE PER IL CARICAMENTO DEI DATI DISTRIBUTORI
public DefaultPieDataset addDettaglioDistributori(WebServer wb, CreaGraficoForm cgForm, HttpServletRequest request, String tipoServizio){
DefaultPieDataset dataset = new DefaultPieDataset();
DBInterface db = new DBInterface();
DateHelper dh = new DateHelper();
String dataDa = "";
String dataA = "";
try{
// RECUPERO LE DATE DI RICERCA
if(cgForm.getPeriodo().equals("0")){
dataDa = dh.dateToDbf(cgForm.getDataDa());
dataA = dh.dateToDbf(cgForm.getDataA());
}else{
dataDa = dh.addDay(dh.dateNowToDbf(), - Integer.parseInt(cgForm.getPeriodo())+1);
dataA = dh.dateNowToDbf();
}
db.selectDistributore(wb, dataset, tipoServizio, dataDa, dataA);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dataset;
}
public void selectDistributore(WebServer wb, DefaultPieDataset dataset, String tipoServizio, String dataDa, String dataA) throws Exception{
ParseXML parseXML = new ParseXML();
// preparo connessione
prepareConnection(wb);
// preparo query sql
String strSql = querySelectDistributore(tipoServizio, dataDa, dataA);
String codiceGrossista = "";
String numeroRichieste = "";
TelnetAS as400;
String descGrossista = "";
// esecuzione query
try{
Vector v = sqlHelper.eseguiQuery(strSql);
int i=0;
// ciclo per ogni riga trovata
while ( i<v.size() ) {
String [] record = (String[]) v.elementAt(i++);
// recupero i campi
codiceGrossista = record[0].trim();
descGrossista = record[1].trim();
numeroRichieste = record[2].trim();
if(!descGrossista.equals("")){
descGrossista = descGrossista + " ( " + codiceGrossista + " )";
}else{
descGrossista = codiceGrossista;
}
// AGGIORNO IL DATASET
dataset.setValue(descGrossista, Integer.parseInt(numeroRichieste));
}
}catch(Exception e){
throw new Exception(e);
}finally{
sqlHelper.disconnetti();
}
}