当前位置: 首页 > 工具软件 > JFreeChart > 使用案例 >

【JFreeChart】JFreeChart—输出组合图表

何兴学
2023-12-01

组合图表(Combined Chart)可以组合不同的图形,例如柱状图和折线图等,通常显示股票的图,比如上方式股票价格,下方是成交量。

实现代码:

CombinedChartServlet.java

package com.lmb.jfreechart;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class CombinedChartServlet extends HttpServlet {

    private static final long serialVersionUID = -566713680648708515L;

    public CombinedChartServlet() {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 设置返回类型为图片
        response.setContentType("image/png");
        // 创建第一个数据集
        CategoryDataset categorydataset = createDataset();
        NumberAxis numberaxis = new NumberAxis("销量");
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
        CategoryPlot categoryplot = new CategoryPlot(categorydataset, null,
                numberaxis, lineandshaperenderer);
        categoryplot.setDomainGridlinesVisible(true);
        // 创建第二个数据集
        CategoryDataset categorydataset1 = createDataset();
        NumberAxis numberaxis1 = new NumberAxis("销量");
        BarRenderer barrenderer = new BarRenderer();
        barrenderer
                .setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
        CategoryPlot categoryplot1 = new CategoryPlot(categorydataset1, null,
                numberaxis1, barrenderer);
        categoryplot1.setDomainGridlinesVisible(true);
        // 创建组合数据集
        CategoryAxis categoryaxis = new CategoryAxis();
        CombinedDomainCategoryPlot combineddomaincategoryplot = new CombinedDomainCategoryPlot(
                categoryaxis);
        combineddomaincategoryplot.add(categoryplot, 2);
        combineddomaincategoryplot.add(categoryplot1, 1);
        // 创建图表对象
        JFreeChart jfreechart = new JFreeChart("组合图", null,
                combineddomaincategoryplot, true);

        // 将图表已数据流的方式返回给客户端
        ChartUtilities.writeChartAsPNG(response.getOutputStream(), jfreechart,
                500, 270);
    }

    /**
     * 返回数据集
     * 
     * @return
     */
    private static CategoryDataset createDataset() {
        // 时间维度
        String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" };
        // 分类维度
        String[] category2 = { "JAVA", "C/C++", "PHP" };
        DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
        for (int i = 0; i < category1.length; i++) {
            String category = category1[i];
            for (int j = 0; j < category2.length; j++) {
                String cat = category2[j];
                // 模拟添加数据
                defaultdataset.addValue(DataUtils.getRandomData(), cat,
                        category);
            }
        }
        return defaultdataset;
    }

}

web.xml略。。。

总结:

1、组合图的数据集主要有两个以上的数据集组成,通过CombinedDomainCategoryPlot对象的add()方法,将不同的数据集装载;

2、创建组合图使用JFreeChart对象的构造方法创建。

 类似资料: