jfreechart生成直方图,横坐标轴可旋转90°,带美化

简单使用jfreechart@TOC
网上搜罗一大堆我拼出来了一个暂时还算能用的写法。这个需求是要求后端生成直方图然后插入到word模版里。

  1. 生成图片部分
private void setPngFile(Map<String, Object> data, JFreeChart barChart) {
        TextTitle title = barChart.getTitle(); // 获取标题
        Font titleFont = new Font("SansSerif", Font.BOLD, 12); // 示例字体设置
        title.setFont(titleFont);
        CategoryPlot plot = (CategoryPlot) barChart.getPlot(); // 从图像中获取绘图区域
        plot.setOutlineVisible(false); // 设置绘图区域的轮廓线不可见
        // 隐藏背景网格线
        plot.setRangeGridlinesVisible(true); // 绘图区域网格线可见
        plot.setRangeGridlinePaint(Color.LIGHT_GRAY);  // 绘图区域网格线颜色(应与背景板颜色不一致)
        plot.getRangeAxis().setAxisLineVisible(false); // 设置Y轴轴线隐藏
        plot.setBackgroundPaint(Color.white); // 设置绘图区域背景板颜色
        CategoryAxis domainAxis = plot.getDomainAxis(); // x轴
        CategoryAxis categoryAxis = (CategoryAxis) barChart.getCategoryPlot().getDomainAxis();
        Font smallFont = new Font("SansSerif", Font.PLAIN, 6); 
        categoryAxis.setTickLabelFont(smallFont); // 设置轴刻度标签的字体
        // 横坐标标签旋转90°
        domainAxis.setMaximumCategoryLabelLines(10);
        domainAxis.setMaximumCategoryLabelWidthRatio(0.5f);
        domainAxis.setLowerMargin(0.01);
        domainAxis.setUpperMargin(0.01);
        plot.setDomainAxis(domainAxis);
        ValueAxis rangeAxis = plot.getRangeAxis(); 
        rangeAxis.setAutoTickUnitSelection(true);
        rangeAxis.setUpperMargin(0.1);// 设置最高的一个 Item 与图片顶端的距离
        rangeAxis.setLowerMargin(0.1);// 设置最低的一个 Item 与图片底端的距离
        NumberAxis vn = (NumberAxis) plot.getRangeAxis();
        DecimalFormat df = new DecimalFormat("#0");
        rangeAxis.setAutoTickUnitSelection(true);//步长自动设置
        rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());//纵轴单元为整型
        vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式
        BarRenderer renderer = new BarRenderer();
        renderer.setSeriesPaint(0, new Color(80, 120, 249));
        GradientPaint gradientPaint0 = new GradientPaint(
                new Point2D.Double(0, 0), Color.decode("#5DB5FF"),
                new Point2D.Double(0, 20), Color.decode("#0A5FEC"));
        renderer.setSeriesPaint(0, gradientPaint0);
        renderer.setMaximumBarWidth(0.05);// 设置柱子宽度//TODO 0.05
        renderer.setMinimumBarLength(0.1);// 设置柱子高度
        renderer.setDefaultOutlinePaint(Color.BLACK);// 设置柱子边框颜色
        renderer.setItemMargin(0.1);// 设置每个地区所包含的平行柱的之间距离
        renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setDefaultItemLabelsVisible(true);
        renderer.setBarPainter(new StandardBarPainter());  // 设置柱子为平面图不是立体的
        renderer.setShadowVisible(false); // 设置柱子的阴影,false代表没有阴影
        plot.setRenderer(renderer);
        try {
            File tempFile = FileUtil.createTempFile("loginTimes", "png", false);
            ChartUtils.saveChartAsPNG(tempFile, barChart, 600, 400);
            data.put("loginTimesStatisticBarPng", Pictures.ofStream(new FileInputStream(tempFile), PictureType.PNG).size(600, 400).create());
        } catch (IOException e) {
            System.err.println("Error saving loginTimes: " + e.getMessage());
        }
    }

2.解决中文乱码

private JFreeChart createJFreeChart(String title, String xLabel, String yLabel, CategoryDataset dataset, Boolean legend, Boolean tooltips, Boolean urls) {
        StandardChartTheme standardChartTheme = createStandardChartTheme();
        ChartFactory.setChartTheme(standardChartTheme);
        JFreeChart barChart = ChartFactory.createBarChart(
                title,                          // 图表标题
                xLabel,                         // X轴标签
                yLabel,                         // Y轴标签
                dataset,                        // 数据集
                PlotOrientation.VERTICAL,       // 图表方向
                false,                          // 是否包含图例
                false,                          // 是否包含工具提示
                false                           // 是否包含URL
        );
        return barChart;
    }
private StandardChartTheme createStandardChartTheme() {
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
        return standardChartTheme;
    }

3.数据集

CategoryDataset dataset = createDataset()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值