JFreeChart 生成双Y轴柱形图

下面代码注释详细,就不过多说明了
public static void main(String[] args) {
        ccc();
    }
    public static JFreeChart ccc() {
        // 创建数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(10, "数量", "PE");
        dataset.addValue(200, "长度和", "PE");
        dataset.addValue(30, "数量", "STEEL");
        dataset.addValue(400, "长度和", "STEEL");
        dataset.addValue(50, "数量", "CUTTER");
        dataset.addValue(600, "长度和", "CUTTER");
        dataset.addValue(70, "数量", "NYLON");
        dataset.addValue(800, "长度和", "NYLON");
        dataset.addValue(90, "数量", "COPPER");
        dataset.addValue(1000, "长度和", "COPPER");

        // 创建图表
        JFreeChart chart = ChartFactory.createBarChart(
                "低压燃气管道", // 图表标题
                "材质", // 横轴标签
                "", // 纵轴标签,在后面创建纵轴标签
                dataset, // 数据集
                PlotOrientation.VERTICAL, // 图表方向
                true, // 是否显示图例
                true, // 是否生成工具
                false // 是否生成 URL 链接
        );

        // 设置背景颜色
        chart.setBackgroundPaint(Color.white);

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

        // 创建左侧 Y 轴
        NumberAxis axis1 = new NumberAxis("数量");
        axis1.setAutoRangeIncludesZero(false); // 不包含零点
        axis1.setFixedDimension(55.0);//设置左右间距
        plot.setRangeAxis(0, axis1); // 将左侧 Y 轴添加到绘图区域

        axis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // 创建右侧 Y 轴
        NumberAxis axis2 = new NumberAxis("长度和");
        axis2.setAutoRangeIncludesZero(false); // 不包含零点
        axis2.setFixedDimension(55.0);
        plot.setRangeAxis(1, axis2); // 将右侧 Y 轴添加到绘图区域

        axis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // 渲染器
        BarRenderer renderer1 = new BarRenderer();
        BarRenderer renderer2 = new BarRenderer();

        // 将两个渲染器添加到绘图区域
        plot.setRenderer(0,  renderer1);
        plot.setRenderer(1, renderer2);


        /**
         * 在每个柱子上显示数值标签
         */
        renderer1.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer1.setDefaultItemLabelsVisible(true);
        renderer1.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
                TextAnchor.BOTTOM_CENTER));
        renderer2.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer2.setDefaultItemLabelsVisible(true);
        renderer2.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
                TextAnchor.BOTTOM_CENTER));
        /**
         * 创建辅助线(Marker)用于表示不同Y轴上的取值范围
         * 此方法仅是在距X周固定距离的地方产生一条线
         */

        /**
         *设置数据集与渲染器的对应关系,
         * ,这里使用第一个渲染器用于显示 Value 1 的数据,
         * ,第二个渲染器用于显示 Value 2 的数据
         */
        plot.setDataset(0,  dataset);
        plot.setRenderer(0,  renderer1);
        plot.mapDatasetToRangeAxis(0, 0);

        plot.setDataset(1,dataset);
        plot.setRenderer(1, renderer2);
        plot.mapDatasetToRangeAxis(1, 1);
        /** ---------------------- 中文乱码问题处理 Start ------------------------------- */
        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));//设置标题字体
        /** ---------------------- 中文乱码问题处理 End ------------------------------- */

        // 创建图表窗口并显示图表
        ChartPanel chartPanel = new ChartPanel(chart);
        //测试用,导入程序,可将此段删除
        JFrame frame = new JFrame("Dual Axis Bar Chart");
        frame.setContentPane(chartPanel);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        return chart;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值