当前位置:编程学习 > JAVA >>

JFreeChart开发web报表

一般的柱状图

\


 
三维的柱状图

 \
 
竖的柱状图

\
 \

 
 
多行的柱状图

 \

 前期准备工作,以及页面调用可以参考JFreeChart开发--饼状 图(1)
这里只有参考图和后台代码


对应代码
一般的柱状图
package chart;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
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.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

 

public class Barchart2D {

    private static CategoryDataset createDataset() {
        String s = "生源省份";//这个就是用于说明图形颜色表的的是什么意思

        String s1 = "北京";
        String s2 = "天津";
        String s3 = "河北";
        String s4 = "安徽";
        String s5 = "辽宁";
        String s6 = "河南";
        String s7 = "山东";

        DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();

        defaultCategoryDataset.addValue(2000, s, s1);
        defaultCategoryDataset.addValue(600, s, s2);
        defaultCategoryDataset.addValue(1200, s, s3);
        defaultCategoryDataset.addValue(579, s, s4);
        defaultCategoryDataset.addValue(347, s, s5);
        defaultCategoryDataset.addValue(468, s, s6);
        defaultCategoryDataset.addValue(2000, s, s7);

        return defaultCategoryDataset;
    }

    private static JFreeChart createChart(CategoryDataset categorydataset) {
        String name = "学校生源统计柱状图";
        String x_name = "省份";
        String y_name = "数量";
        System.out.println("name=" + name);
        System.out.println("x_name=" + x_name);
        JFreeChart jfreechart = ChartFactory.createBarChart(name, x_name,
                y_name, categorydataset, PlotOrientation.VERTICAL, true, true,
                false);

        jfreechart.setBackgroundPaint(Color.white);//Color 是paint类型的对象
        CategoryPlot categoryplot = jfreechart.getCategoryPlot();

      
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        CategoryAxis domainAxis = categoryplot.getDomainAxis();

        //定义字体格式
        Font font = new Font("微软雅黑", Font.CENTER_BASELINE, 12);
        TextTitle title = new TextTitle("Liguokun 李国坤2D图");
        //设置标题的格式
        title.setFont(font);
        //把标题设置到图片里面
        jfreechart.setTitle(title);

      
        domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));

      
        domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));

      
        numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));

      
        numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

      
        jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

      

        categoryplot.setDomainGridlinePaint(Color.white);
        categoryplot.setDomainGridlinesVisible(true);
        categoryplot.setDomainGridlinePaint(Color.blue);

        //下面用来让柱图更加好看
        BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
        barrenderer.setDrawBarOutline(true);
        barrenderer.setOutlinePaint(Color.black);//加了个黑边,
        barrenderer.setMaximumBarWidth(0.05);
        barrenderer.setSeriesFillPaint(0, Color.blue);

        return jfreechart;

    }

    public static String generateBarChart(HttpSession session, PrintWriter pw) {
        String filename = "";

        CategoryDataset categorydataset = createDataset();//在类里可以调用私有的成员啊
        JFreeChart jfreechart = createChart(categorydataset);

        ChartRenderingInfo info = new ChartRenderingInfo(
                new StandardEntityCollection());

        try {
            filename = ServletUtilities.saveChartAsPNG(jfreechart, 500, 300,
                    info, session);
        } catch (IOException e) {
            e.printStackTrace();
        }<

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,