Reputation: 884
I need to create a bar chart that reads from a table in a DB that lists access records.
The table should show which vendor accessed it (X-Axis), who it was accessed on behalf of (each bar), and how many times (Y-Axis)
Each vendor can access on behalf of multiple municipalities, but each municipality only has one vendor. So each vendor will probably show multiple bars.... however, every bar in the chart is unique and is grouped by vendor.
In the jfreechart examples, I'm seeing where each category on the x-axis uses a repeating series, but how do you do what I need where each bar is unique?
Here's the relevant servlet code:
//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);
Upvotes: 0
Views: 7070
Reputation: 5923
Is this what you are trying to do?
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;
}
Upvotes: 2