0

我需要创建一个从数据库中列出访问记录的表中读取的条形图。

该表应显示访问它的供应商(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);
4

1 回答 1

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;

}
于 2013-04-02T13:58:47.950 回答