java画折线图和柱状图结合的综合图(用jfreechart)

发布时间:2023-03-20 15:00

最近项目增加了需求,想要下载折线图和柱状图结合的综合图。我查了一会儿,要实现这个功能,我使用了这两个第三方包了:

  1. jfreechart-1.0.19.jar
  2. jcommon-1.0.23.jar

效果:

java画折线图和柱状图结合的综合图(用jfreechart)_第1张图片

步骤:

①、导入其相应的 jcommon-1.0.23.jar和 jfreechart-1.0.19.jar文件
②、下面就可以写实现功能的代码了

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class test {

    public static void main(String[] args) {

        //折线图数据
        DefaultCategoryDataset lineDataset = new DefaultCategoryDataset();
            //添加数据
            lineDataset.addValue(9, "", "语文");
            lineDataset.addValue(7, "", "数学");
            lineDataset.addValue(6, "", "英语");
            lineDataset.addValue(4, "", "物理");
            lineDataset.addValue(3, "", "化学");
            lineDataset.addValue(2, "", "生物");

        //柱状图数据
         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            //添加数据
            dataset.addValue(4, "", "语文");
            dataset.addValue(7, "", "数学");
            dataset.addValue(6, "", "英语");
            dataset.addValue(5, "", "物理");
            dataset.addValue(3, "", "化学");
            dataset.addValue(9, "", "生物");

          //生成的柱状图
          JFreeChart chart = ChartFactory.createBarChart3D(
                   "科目成绩",
                   "科目",//X轴的标签 
                   "分数",//Y轴的标签 
                   dataset, //图标显示的数据集合
                   PlotOrientation.VERTICAL, //图像的显示形式(水平或者垂直)
                   false,//是否显示子标题 
                   false,//是否生成提示的标签 
                   false); //是否生成URL链接

            /*
             * 处理图形上的乱码
             */

            //处理主标题的乱码
            chart.getTitle().setFont(new Font("黑体",Font.BOLD,18));

            //获取图表区域对象
            CategoryPlot categoryPlot = (CategoryPlot)chart.getPlot();

            //获取X轴的对象
            CategoryAxis3D categoryAxis3D = (CategoryAxis3D)categoryPlot.getDomainAxis();

            //获取Y轴的对象
            NumberAxis3D numberAxis3D = (NumberAxis3D)categoryPlot.getRangeAxis();

            //处理X轴上的乱码
            categoryAxis3D.setTickLabelFont(new Font("黑体",Font.BOLD,10));

            //处理X轴外的乱码
            categoryAxis3D.setLabelFont(new Font("黑体",Font.BOLD,10));

            //处理Y轴上的乱码
            numberAxis3D.setTickLabelFont(new Font("黑体",Font.BOLD,10));

            //处理Y轴外的乱码
            numberAxis3D.setLabelFont(new Font("黑体",Font.BOLD,10));

            //自定义Y轴上显示的刻度,以10作为1格
            numberAxis3D.setAutoTickUnitSelection(false);
            NumberTickUnit unit = new NumberTickUnit(1); 
            numberAxis3D.setTickUnit(unit);

            //获取绘图区域对象
            BarRenderer3D barRenderer3D = (BarRenderer3D)categoryPlot.getRenderer();

            //设置柱形图的宽度
            barRenderer3D.setMaximumBarWidth(0.07);

            //在图形上显示数字
            barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
            barRenderer3D.setBaseItemLabelsVisible(true);
            barRenderer3D.setBaseItemLabelFont(new Font("宋体",Font.BOLD,10));

            /*
             * 放折线图数据
             */
            categoryPlot.setDataset(1, lineDataset);
            //设置折线
            LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
            lineandshaperenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
            categoryPlot.setRenderer(1, lineandshaperenderer);
            // 柱状图和纵轴紧靠
            categoryAxis3D.setLowerMargin(0.0); 

            categoryAxis3D.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
            //折线在柱面前面显示
            categoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

             /*
              * 在E盘目录下生成图片
              */
            File file = new File("e:\\a.png");
            try {
                 ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
                } catch (IOException e) {
                   e.printStackTrace();
              }

    }

}

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号