我需要创建一个条形图,从DB中列出访问记录的表中读取。
该表应显示哪个供应商访问过它(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);
答案 0 :(得分:2)
这是你想要做的吗?
private static CategoryDataset createDataset() {
// row keys...
String series1 = "Municipality 1";
String series2 = "Municipality 2";
String series3 = "Municipality 3";
// column keys...
String category1 = "Vendor 1";
String category2 = "Vendor 2";
String category3 = "Vendor 3";
String category4 = "Vendor 4";
String category5 = "Vendor 5";
// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, category1);
dataset.addValue(4.0, series1, category2);
dataset.addValue(3.0, series1, category3);
dataset.addValue(5.0, series1, category4);
dataset.addValue(5.0, series1, category5);
dataset.addValue(5.0, series2, category1);
dataset.addValue(6.0, series2, category3);
dataset.addValue(8.0, series2, category4);
dataset.addValue(4.0, series2, category5);
dataset.addValue(4.0, series3, category1);
dataset.addValue(3.0, series3, category2);
dataset.addValue(3.0, series3, category4);
dataset.addValue(6.0, series3, category5);
return dataset;
}