我需要创建一个从数据库中列出访问记录的表中读取的条形图。
该表应显示访问它的供应商(X 轴)、代表谁访问它(每个条形图)以及访问次数(Y 轴)
每个供应商可以代表多个市政当局访问,但每个市政当局只有一个供应商。因此,每个供应商可能会显示多个条形图……但是,图表中的每个条形图都是唯一的,并且按供应商分组。
在 jfreechart 示例中,我看到 x 轴上的每个类别在哪里使用重复系列,但是在每个条形图都是唯一的地方,你如何做我需要的?
这是相关的servlet代码:
//Municipality, Vendor //for month of January //Vendor //Municipality
if (stmt.execute("Select COUNT(accessFor), accessBy, accessFor FROM bps_bam.access WHERE DATE_FORMAT(accessTime, '%Y %M') = '2012 January' GROUP BY accessBy, accessFor;")) {
rs = stmt.getResultSet();
} else {
System.err.println("select failed");
}
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); //Set of values for Bar Graph
while(rs.next())
{ //count //municipality //vendor
dataset.addValue(rs.getDouble(1), rs.getString(3), (String)rs.getString(2));
}
counter++;
JFreeChart chart = ChartFactory.createBarChart(
"January Access Times", // chart title
"Vendor", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
false, // tooltips
false // URLs?
);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryItemRenderer renderer = plot.getRenderer();
CategoryItemLabelGenerator generator
= new StandardCategoryItemLabelGenerator("{0}",
NumberFormat.getInstance());
renderer.setBaseItemLabelGenerator(generator);
renderer.setBaseItemLabelFont(new Font("SansSerif", Font.PLAIN, 12));
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
response.setContentType("image/png");
//response.addHeader("Refresh", "5");
//Write numbers on range axis just as integrals, not decimals
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
ChartUtilities.writeChartAsPNG(out, chart, 800, 500);