博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用JFreeChart-创建组合图(之CombinedCategoryPlot)
阅读量:6961 次
发布时间:2019-06-27

本文共 4010 字,大约阅读时间需要 13 分钟。

hot3.png

之前基本图,例如饼图,柱状图,曲线图已经单个做了总结了,现在要将这些图形组合起来,一般业务也是需要这些组合起来的图形的。

显示介绍如何创建柱状图与曲线图结合起来的图形。

  • 首先,还是需要获取dataset数据源,这么是两个数据源,都是DefaultCategoryDataset类型的
public static CategoryDataset createDataset1() {        DefaultCategoryDataset result = new DefaultCategoryDataset();        String series1 = "First";        String series2 = "Second";        String type1 = "Type 1";        String type2 = "Type 2";        String type3 = "Type 3";        String type4 = "Type 4";        String type5 = "Type 5";        String type6 = "Type 6";        String type7 = "Type 7";        String type8 = "Type 8";        result.addValue(1.0, series1, type1);        result.addValue(4.0, series1, type2);        result.addValue(3.0, series1, type3);        result.addValue(5.0, series1, type4);        result.addValue(5.0, series1, type5);        result.addValue(7.0, series1, type6);        result.addValue(7.0, series1, type7);        result.addValue(8.0, series1, type8);        result.addValue(5.0, series2, type1);        result.addValue(7.0, series2, type2);        result.addValue(6.0, series2, type3);        result.addValue(8.0, series2, type4);        result.addValue(4.0, series2, type5);        result.addValue(4.0, series2, type6);        result.addValue(2.0, series2, type7);        result.addValue(1.0, series2, type8);                return result;    }
public static CategoryDataset createDataset2() {        DefaultCategoryDataset result = new DefaultCategoryDataset();        String series1 = "Third";        String series2 = "Fourth";        String type1 = "Type 1";        String type2 = "Type 2";        String type3 = "Type 3";        String type4 = "Type 4";        String type5 = "Type 5";        String type6 = "Type 6";        String type7 = "Type 7";        String type8 = "Type 8";        result.addValue(11.0, series1, type1);        result.addValue(14.0, series1, type2);        result.addValue(13.0, series1, type3);        result.addValue(15.0, series1, type4);        result.addValue(15.0, series1, type5);        result.addValue(17.0, series1, type6);        result.addValue(17.0, series1, type7);        result.addValue(18.0, series1, type8);        result.addValue(15.0, series2, type1);        result.addValue(17.0, series2, type2);        result.addValue(16.0, series2, type3);        result.addValue(18.0, series2, type4);        result.addValue(14.0, series2, type5);        result.addValue(14.0, series2, type6);        result.addValue(12.0, series2, type7);        result.addValue(11.0, series2, type8);        return result;    }
  • 然后,就需要我们去画组合图形了,对于组合图形所使用到的JfreeChart比较特殊,并不是通过chaetFactory创建的,而是就是JFreeChart本身。
JFreeChart result = new JFreeChart(            String title, Font titleFont, Plot plot, boolean createLegend        );

我们只需要传递Plot对象就可以了。

现在需要我们将两个dataset组合成一个plot对象。要怎么做呢???

我们先看看需要生成的图片的样式:

在图中我们可以发现,两个图表是共享一个横坐标的,因此,在创建数据源的时候,要主要将横坐标一项写成相同才行的。

接下来我们的任何就是创建两个纵坐标,然后将两个纵坐标合并成一个。

在这里我们使用的是CombinedDomainCategoryPlot类来合并两个纵坐标。

//创建数据源一的纵坐标对象。NumberAxis rangeAxis1 = new NumberAxis("Value");LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();//我们在这里将横坐标设置为NULL,表示不使用自己的横坐标对象,只使用自己的纵坐标和图表渲染对象(折线图)CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1,                 renderer1);//纵坐标对象。NumberAxis rangeAxis2 = new NumberAxis("Value");//柱状图的图表渲染对象。BarRenderer renderer2 = new BarRenderer();//同样的不是用横坐标对象。CategoryPlot subplot2 = new CategoryPlot(dataset2, null, rangeAxis2,                 renderer2);
  • 接着。我们创建横坐标对象

CategoryAxis domainAxis = new CategoryAxis("Category");
  • 然后,我们使用合并对象CombinedDomainCategoryPlot合并这些对象。

//先合并横坐标。CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(                domainAxis);//添加纵坐标。        plot.add(subplot1, 2);//添加纵坐标。        plot.add(subplot2, 1);
  • 最后,我们使用JFreeChart类创建一个对象就可以了;

JFreeChart result = new JFreeChart(            "Combined Domain Category Plot Demo",            new Font("SansSerif", Font.BOLD, 12),            plot,            true        );
这样就可以生成一个组合图形了。。。

转载于:https://my.oschina.net/bosscheng/blog/124992

你可能感兴趣的文章
Hibernate 分组查询 子查询 原生SQL
查看>>
基于.NET平台常用的框架整理
查看>>
springmvc(3)拦截器HandlerInterceptor源码的简单解析
查看>>
初学Python(六)——输入输出
查看>>
用布局方式作出模仿微信选项卡 1
查看>>
Java虚拟机的启动与程序的运行
查看>>
几个有用的java 7特性
查看>>
jS数组
查看>>
php函数xml转化数组
查看>>
五分钟读懂UML类图
查看>>
sql case 函数与详细说明
查看>>
旋转矩阵(模拟)
查看>>
点头(1163)
查看>>
js传输图片路径
查看>>
HDU 6029 Graph Theory【水题】
查看>>
HDU 4053 or ZOJ 3541 The Last Puzzle【区间dp】【经典题】
查看>>
1163: [Baltic2008]Mafia
查看>>
物联网系统框架介绍
查看>>
centos6 安装 ansible_ui
查看>>
搭建区块链浏览器——基于hyperledger fabric 1.0,MySQL容器
查看>>