springboot+jfreechart实现折线图只需一个类

需要源码小伙伴可以到微信小程序:”MINIIT资源库“获取,作者承诺免费

springboot那些内容我就不说了哈,自己搭建好一个springboot加入我的代码就可以了用啦

先上个图

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ZOGbGU=,size_20,color_FFFFFF,t_70,g_se,x_16

先导入1个依赖

<dependency>
    <groupId>org.jfree</groupId>
    <artifactId>jfreechart</artifactId>
    <version>1.0.19</version>
</dependency>

在你的启动类里放入如下代码

  @Bean
  public ServletRegistrationBean Servlet() {
      return new ServletRegistrationBean<>(new DisplayChart(),"/chart");
  }

这是html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Insert title here</title>
</head>
<body>
<div style="text-align: center">
  折线图 <br> <br> <img th:src="${makeLineAndShapeChart}"
                                width=1200 height=400 border=0 color=gray>
</div>
</body>
</html>
</body>
</html>

这是LineChartController类,

为了你们方便你们操作成功,里面数据写死,若要操作数据库,把数组作为参数传进来就好了

package com.doll.springbootregisterlogin.controller;


import java.awt.*;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.DefaultCategoryDataset;

@Controller
public class LineChartController {
    @GetMapping("makeLineAndShapeChart")
    public String makeLineAndShapeChart(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        System.out.println("已进");
        // 定义图表对象数据,数据
        DefaultCategoryDataset  dataset  =createDataset();
        JFreeChart  chart = ChartFactory.createLineChart(
                "折线图", // chart title
                "时间", // domain axis label
                "销售额(百万)", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips
                false // urls
        );

        CategoryPlot plot = chart.getCategoryPlot();
        // 设置图示字体
        chart.getTitle().setFont(new Font("宋体", Font.BOLD, 22));
        //设置横轴的字体
        CategoryAxis categoryAxis = plot.getDomainAxis();
        categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));//x轴标题字体
        categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 16));//x轴刻度字体

        //以下两行 设置图例的字体
        LegendTitle legend = chart.getLegend(0);
        legend.setItemFont(new Font("宋体", Font.BOLD, 14));
        //设置竖轴的字体
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setLabelFont(new Font("宋体" , Font.BOLD , 19)); //设置数轴的字体
        rangeAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 16));
        LineAndShapeRenderer lasp = (LineAndShapeRenderer) plot.getRenderer();
        lasp.setBaseShapesVisible(true);
        lasp.setDrawOutlines(true);
        // 设置线条是否被显示填充颜色
        lasp.setUseFillPaint(false);
        // 设置拐点颜色
        lasp.setBaseFillPaint(Color.red);//蓝色
        rangeAxis.setAxisLinePaint(Color.black);
        // 设置背景颜色
        plot.setBackgroundPaint(Color.white);
        // 设置网格竖线颜色
        plot.setDomainGridlinePaint(Color.pink);
        // 设置网格横线颜色
        plot.setRangeGridlinePaint(Color.pink);

        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//去掉竖轴字体显示不全
        rangeAxis.setAutoRangeIncludesZero(true);
        rangeAxis.setUpperMargin(0.20);
        rangeAxis.setLabelAngle(Math.PI / 2.0);

        LineAndShapeRenderer  renderer =  (LineAndShapeRenderer) plot
                .getRenderer();
        renderer.setSeriesPaint(0, Color.BLACK);//折线颜色,下标0始

        // 6. 将图形转换为图片,传到前台
        String fileName = ServletUtilities.saveChartAsJPEG(chart, 1000, 400, null, request.getSession());
        String chartURL = request.getContextPath() + "/chart?filename=" + fileName;
        model.addAttribute("makeLineAndShapeChart", chartURL);

        return "LineChart";
    }

    // 生成数据
    public static DefaultCategoryDataset createDataset() {
        DefaultCategoryDataset linedataset = new DefaultCategoryDataset();

        // 各曲线名称
        String [] series= {"冰箱","彩电","菠萝"};
        // 横轴名称(列名称)
        String [] month = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        //具体数据
//      为了方便你们操作成功,这里数据写死
        double [] num1 = {4,5,6,10,1,2,3,7,4,2,8,3};
        double [] num2 = {10,15,20,16,7,4,7,11,21,42,54,32};
        double [] num3 = {10,18,25,19,43,12,54,22,24,65,11,32};
        //将多个数组合并,且不打乱顺序
        double[] num= new double[num1.length + num2.length + num3.length];
        System.arraycopy(num1, 0, num, 0, num1.length);
        System.arraycopy(num2, 0, num, num1.length, num2.length);
        System.arraycopy(num3, 0, num, num1.length+num2.length, num3.length);
        int l1 = num.length/series.length; //4
        int l2 = month.length;
        int j=0;
        for(int i=0;i<num.length;i++) {
            linedataset.addValue(num[i], series[i/l1], month[j]);
            j++;
            if(j==month.length)
                j=0;
        }
        return linedataset;
    }
}

OK啦,浏览器访问:localhost:8080/makeLineAndShapeChart,就这么简单

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哆le

一分两分也是爱,不求多哈哈哈

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值