如何使用java在html中嵌入2条形图?

时间:2015-07-02 15:30:04

标签: java html charts

我使用gagawa

生成带有java的html页面

我现在尝试添加这样的图表:

enter image description here

我尝试了org.jfree框架

使用此代码:

private Img createChart(LatencyHistogram current, LatencyHistogram baseLine) {

    Img image = null;
    final CategoryDataset dataset = createDataset(current, baseLine);

    final JFreeChart chart = ChartFactory.createLineChart(
            "Line Chart Demo 1",       // chart title
            "Type",                    // domain axis label
            "Value",                   // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            true,                      // include legend
            true,                      // tooltips
            false                      // urls
    );
    chart.setBackgroundPaint(java.awt.Color.white);
    // save it to an image
    try {
        final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        final String pathname = Constants.HTML_PAGES_PATH + "images/latency_delta.png";
        final File file1 = new File(pathname);
        ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
        image = new Img("latency", "../images/latency_delta.png").setWidth("400");
        return image;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;
}

得到了这个结果: enter image description here

有没有办法创建2条形图?

是否有更干净整洁的免费java图表抽屉框架?

1 个答案:

答案 0 :(得分:0)

创建一个你应该使用的条形图

ChartFactory.createBarChart() 

而不是

ChartFactory.createLineChart()

方法。但由于我不知道代码中的LatencyHistogram是什么,因此这不是确切的代码。您必须使用这些数据类型。

例如以下代码将生成此

enter image description here

示例代码

import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BarChartx extends ApplicationFrame {

    public BarChartx(String applicationTitle, String chartTitle) {
        super(applicationTitle);
        JFreeChart barChart = ChartFactory.createBarChart(chartTitle, "year", "value", createDataset(), PlotOrientation.VERTICAL, true, true, false);
        CategoryPlot p = barChart.getCategoryPlot();
        BarRenderer renderer = (BarRenderer) p.getRenderer();
        renderer.setSeriesPaint(0, new Color(255, 0, 102));
        renderer.setSeriesPaint(1, new Color(0, 153, 255));
        renderer.setItemMargin(0.03);

        ChartPanel chartPanel = new ChartPanel(barChart);
        chartPanel.setPreferredSize(new java.awt.Dimension(560, 367));
        setContentPane(chartPanel);
    }

    private CategoryDataset createDataset() {
        final String apple = "apple";
        final String mango = "mango";

        final String y2000 = "2000";
        final String y2001 = "2001";
        final String y2002 = "2002";
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(700, apple, y2000);
        dataset.addValue(550, mango, y2000);

        dataset.addValue(700, apple, y2001);
        dataset.addValue(550, mango, y2001);

        dataset.addValue(720, apple, y2002);
        dataset.addValue(570, mango, y2002);

        return dataset;
    }

    public static void main(String[] args) {
        BarChartx chart = new BarChartx("example", "Bar Chart Demo 1");
        chart.pack();
        RefineryUtilities.centerFrameOnScreen(chart);
        chart.setVisible(true);
    }
}