JFreeChart统计表(柱状图)代码块

所需依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.offcn</groupId>
    <artifactId>jfreechart</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>itext</groupId>
            <artifactId>itext</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <dependency>
            <groupId>jfreechart</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.13</version>
        </dependency>
        <dependency>
            <groupId>jfree</groupId>
            <artifactId>jfreechart-experimental</artifactId>
            <version>1.0.9</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
</project>
import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.junit.Test;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class DemoTest {
    /**
     * 普通柱状图,垂直方向(VERTICAL)
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        //1.创建数据集合
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(100,"长沙","香蕉");
        dataset.addValue(200,"上海","苹果");
        dataset.addValue(320,"北京","大蒜");
        dataset.addValue(500,"兰州","牛肉面");
        dataset.addValue(450,"福建","菠萝");
        dataset.addValue(250,"武汉","橘子");
        dataset.addValue(350,"石家庄","橙子");
        dataset.addValue(100,"保定","梨子");
        //2.调用工厂方法来生成柱状图
        JFreeChart chart = ChartFactory.createBarChart3D("水果销售图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, false);

        //乱码解决
        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
        CategoryAxis domainAxis=plot.getDomainAxis();
        domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
        ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
        //3.调用工具类生成图片
        FileOutputStream outputStream = new FileOutputStream("D:\\javaphoto\\zhuzhuangtu\\1.jpg");
        ChartUtilities.writeChartAsJPEG(outputStream,chart,700,500);
        System.out.println("创建成功!!!!!!!");
    }

在这里插入图片描述

/**
     * 普通柱状图,水平方向(HORIZONTAL)
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {

        //1.创建数据集合
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(20,"长沙","香蕉");
        dataset.addValue(30,"上海","苹果");
        dataset.addValue(50,"北京","大蒜");
        dataset.addValue(85,"兰州","牛肉面");
        dataset.addValue(45,"福建","菠萝");
        dataset.addValue(96,"武汉","橘子");
        dataset.addValue(58,"石家庄","橙子");
        dataset.addValue(53,"保定","梨子");
        //2.创建工厂生成柱状图
        JFreeChart chart = ChartFactory.createBarChart3D("水果销售图", "水果", "销售", dataset, PlotOrientation.HORIZONTAL, true, true, false);
        //乱码解决
        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
        CategoryAxis domainAxis=plot.getDomainAxis();
        domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
        ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体

        //3.调用工具类生成图片
        FileOutputStream outputStream = new FileOutputStream("D:\\javaphoto\\zhuzhuangtu\\2.jpg");
        ChartUtilities.writeChartAsJPEG(outputStream,chart,700,500);
        System.out.println("创建成功!!!!!!!");
    }

在这里插入图片描述

/**
     * 绘制柱状图,颜色问题
     * 单独绘制一个地区的水果销售情况
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        //1.创建数据集合
        double data[][]={{200},{300},{250},{562},{562},{120},{203},{308}};
        String rowKeys[]={"香蕉","苹果","大蒜","牛肉面","菠萝","橘子","橙子","梨子"};
        String coulumsKeys[]={"长沙"};

        CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, coulumsKeys, data);

        //2.调用工厂方法来生成柱状图
        JFreeChart chart = ChartFactory.createBarChart3D("水果销售图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, false);

        //乱码解决
        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
        CategoryAxis domainAxis=plot.getDomainAxis();
        domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
        ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体

        //3.调用工具类生成图片
        FileOutputStream outputStream = new FileOutputStream("D:\\javaphoto\\zhuzhuangtu\\3.jpg");
        ChartUtilities.writeChartAsJPEG(outputStream,chart,700,500);
        System.out.println("创建成功!!!!!!!");
    }

在这里插入图片描述

  /**
     * 多数据类型柱状图
     * @throws IOException
     */
    @Test
    public void test4() throws IOException {

        //1.创建数据集
        double data[][]={{20,30,15,50},{50,20,88,99},{20,30,50,50},{50,55,56,65}};
        String rowKeys[]={"香蕉","苹果","梨子","橘子"};
        String columsKeys[]={"长沙","上海","保定","武汉"};
        CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columsKeys, data);
        //2.调用工厂方法来生成柱状图
        JFreeChart chart = ChartFactory.createBarChart3D("水果销售图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, false);

        //乱码解决
        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
        CategoryAxis domainAxis=plot.getDomainAxis();
        domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
        ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体

        //3.调用工具类生成图片
        FileOutputStream stream = new FileOutputStream("D:\\javaphoto\\zhuzhuangtu\\4.jpg");
        ChartUtilities.writeChartAsJPEG(stream,chart,700,500);
        System.out.println("创建成功!!!!!!!!");

    }
}

在这里插入图片描述

/**
     * 设置柱状图相关属性
     * @throws IOException
     */
    @Test
    public void test5() throws IOException {
        //1.创建数据集
        double[][]data=new double[][]{{20000,2300,5222,6999},{6000,20000,32000,5220},{10001,3020,5000,6000},{9999,10000,22002,9299}};
        String[] rowKeys={"苹果","香蕉","橘子","梨子"};
        String []columnKeys={"北京","长沙","福建","四川"};
        CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
        //2.调用工厂生成柱状图
        JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, false);

        //乱码解决
        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
        CategoryAxis domainAxis=plot.getDomainAxis();
        domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
        ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
        //设置网络背景颜色
        plot.setBackgroundPaint(Color.white);
        //设置网络竖线颜色
        plot.setDomainGridlinePaint(Color.pink);
        //设置网络横线颜色
        plot.setRangeGridlinePaint(Color.pink);
        //显示每个柱子上的数值
        BarRenderer3D renderer3D = new BarRenderer3D();
        renderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer3D.setBaseItemLabelsVisible(true);
        renderer3D.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE2, TextAnchor.BASELINE_LEFT));
        renderer3D.setItemLabelAnchorOffset(10D);
        //设置平行柱的之间距离
        renderer3D.setItemMargin(0.5);
        plot.setRenderer(renderer3D);
        //3.调用工具类生成图片
        FileOutputStream stream = new FileOutputStream("D:\\javaphoto\\zhuzhuangtu\\5.jpg");
        ChartUtilities.writeChartAsJPEG(stream,chart,700,500);
        System.out.println("创建成功!!!!!!");
    }
}

在这里插入图片描述
重点:柱状图乱码问题以及样式属性问题

//乱码解决
        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
        CategoryAxis domainAxis=plot.getDomainAxis();
        domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
        ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
        //设置网络背景颜色
        plot.setBackgroundPaint(Color.white);
        //设置网络竖线颜色
        plot.setDomainGridlinePaint(Color.pink);
        //设置网络横线颜色
        plot.setRangeGridlinePaint(Color.pink);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值