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
|
public class BarChart extends ApplicationFrame {
HashMap<String, ArrayList> lstPoints;
private int hauteur = 350;
int largeur = 25;
/**
* Creates a new demo instance.
*
* @param title the frame title.
* @throws Exception
*/
public BarChart( HashMap<String, ArrayList> lstPoints, String title, HttpServletRequest p_request, HttpServletResponse p_response, int size) throws Exception {
super(title);
this.lstPoints = lstPoints;
int i = lstPoints.size();
final CategoryDataset dataset = createDatasetIndex(lstPoints);
final JFreeChart chart = createChart(dataset, p_request, p_response);
ChartRenderingInfo info = new ChartRenderingInfo();
try {
String filename = ServletUtilities.saveChartAsPNG(chart,
largeur*size*6, hauteur, info,p_request.getSession());
Graphe graphe = this.initImage(p_request, info, filename);
p_request.setAttribute("grapheIndex", graphe);
} catch (IOException e) {
throw new Exception(e.getMessage());
}
}
private Graphe initImage(HttpServletRequest request,
ChartRenderingInfo info, String filename) {
String sMap = ChartUtilities.getImageMap(filename, info);
String src = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename;
String useMap = "#" + filename;
Graphe graphe = new Graphe();
graphe.setMap(sMap);
graphe.setSrc(src);
graphe.setUseMap(useMap);
return graphe;
}
/**
* Returns a sample dataset.
*
* @return The dataset.
*/
private CategoryDataset createDatasetIndex(HashMap<String, ArrayList> lstPoints) {
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(String key: lstPoints.keySet()){
ArrayList<IndexJournaliers> list = new ArrayList<IndexJournaliers>();
list = lstPoints.get(key);
for(int i=0; i<list.size(); i++){
IndexJournaliers lpe = list.get(i);
dataset.addValue(lpe.getActiveEnergyImportTarif1(),"Tarif 1",lpe.getDateAndTime());
dataset.addValue(lpe.getActiveEnergyImportTarif2(),"Tarif 2",lpe.getDateAndTime());
dataset.addValue(lpe.getActiveEnergyImportTarif3(),"Tarif 3",lpe.getDateAndTime());
dataset.addValue(lpe.getActiveEnergyImportTarif4(),"Tarif 4",lpe.getDateAndTime());
dataset.addValue(lpe.getActiveEnergyImportTarif5(),"Tarif 5",lpe.getDateAndTime());
dataset.addValue(lpe.getActiveEnergyImportTarif6(),"Tarif 6",lpe.getDateAndTime());
}
}
return dataset;
}
/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return The chart.
*/
private JFreeChart createChart(final CategoryDataset dataset , HttpServletRequest p_request, HttpServletResponse p_response) {
// create the chart...
final JFreeChart chart = ChartFactory.createBarChart(
"", // chart title
"Date", // domain axis label
"kWh", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
final BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
final GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, Color.lightGray
);
final GradientPaint gp1 = new GradientPaint(
0.0f, 0.0f, Color.green,
0.0f, 0.0f, Color.lightGray
);
final GradientPaint gp2 = new GradientPaint(
0.0f, 0.0f, Color.red,
0.0f, 0.0f, Color.lightGray
);
final GradientPaint gp3 = new GradientPaint(
0.0f, 0.0f, Color.CYAN,
0.0f, 0.0f, Color.lightGray
);
final GradientPaint gp4 = new GradientPaint(
0.0f, 0.0f, Color.ORANGE,
0.0f, 0.0f, Color.lightGray
);
final GradientPaint gp5 = new GradientPaint(
0.0f, 0.0f, Color.PINK,
0.0f, 0.0f, Color.lightGray
);
final GradientPaint gp6 = new GradientPaint(
0.0f, 0.0f, Color.MAGENTA,
0.0f, 0.0f, Color.lightGray
);
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setSeriesPaint(2, gp2);
renderer.setSeriesPaint(3, gp3);
renderer.setSeriesPaint(4, gp4);
renderer.setSeriesPaint(5, gp5);
renderer.setSeriesPaint(6, gp6);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
} |