echarts 使用示例

本文展示了如何使用Echarts图表库与SpringBoot应用程序整合,通过一个简单的示例解释了如何从后台获取数据并展示在前端的Echarts图表上。在控制器中创建了一个返回Fruit对象列表的方法,前端页面通过jQuery的Ajax获取数据,并用Echarts绘制条形图进行数据展示。示例包括两种不同的数据绑定方式,分别是在dataset中定义数据源和直接在series中指定数据。
摘要由CSDN通过智能技术生成

echarts 使用示例

                          

                                       

*****************

示例

                  

***********

pojo 层

               

Fruit

@Data
public class Fruit {

    private String name;
    private Float price;
}

                     

***********

config 层

               

WebConfig

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/index2").setViewName("index2");
    }
}

                        

***********

controller 层

           

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public List<Fruit> hello(){
        List<Fruit> list = new ArrayList<>();

        Fruit apple = new Fruit();
        apple.setName("苹果");
        apple.setPrice(6.0f);
        list.add(apple);

        Fruit banana = new Fruit();
        banana.setName("香蕉");
        banana.setPrice(4.0f);
        list.add(banana);

        Fruit orange = new Fruit();
        orange.setName("橘子");
        orange.setPrice(3.6f);
        list.add(orange);

        Fruit pipeApple = new Fruit();
        pipeApple.setName("菠萝");
        pipeApple.setPrice(4.8f);
        list.add(pipeApple);

        return list;
    }
}

                    

*****************

前端页面

            

index.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>echarts 示例</title>
    <script src="/echarts/echarts.min.js"></script>
    <script src="/jquery/jquery-3.6.0.min.js"></script>
    <script>
        $(function (){
            $("#btn").click(function (){
                $.get({
                    url: "/hello",
                    success: function (data){
                        const myChart = echarts.init($("#con")[0]);
                        let source= [];

                        $.each(data,function (index,object){
                            let item = {fruit:object.name, price:object.price }
                            source.push(item);
                        });
                        console.log(source);

                        const option = {
                            title: {
                                text: 'ECharts 入门示例'
                            },
                            tooltip: {},
                            legend: {
                                data: ['价格']
                            },
                            dataset: {       //数据集提供数据源
                                source: source
                            },
                            xAxis: {
                                type: 'category'
                            },
                            yAxis: {},
                            series: [
                                {
                                    type: 'bar',
                                    seriesLayoutBy: 'row'
                                }
                            ]
                        };

                        myChart.setOption(option);
                    }
                });
            })
        })
    </script>
</head>
<body>
<div th:align="center" id="con" style="width: 600px;height:400px;">
</div>
<div th:align="center">
    <button id="btn">加载数据</button>
</div>
</body>
</html>

                   

index2.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
  <meta charset="UTF-8">
  <title>echarts 示例</title>
  <script src="/echarts/echarts.min.js"></script>
  <script src="/jquery/jquery-3.6.0.min.js"></script>
  <script>
    $(function (){
      $("#btn").click(function (){
        $.get({
          url: "/hello",
          success: function (data){
            const myChart = echarts.init($("#con")[0]);
            let fruits= [];
            let prices=[];

            $.each(data,function (index,object){
              fruits.push(object.name);
              prices.push(object.price);
            });

            const option = {
              title: {
                text: 'ECharts 入门示例'
              },
              tooltip: {},
              legend: {
                data: ['价格']
              },
              xAxis: {
                data: fruits     //在xAxis中提供x轴数据
              },
              yAxis: {},
              series: [
                {
                  type: 'bar',
                  data: prices   //在seris中提供纵坐标数据
                }
              ]
            };

            myChart.setOption(option);
          }
        });
      })
    })
  </script>
</head>
<body>
<div th:align="center" id="con" style="width: 600px;height:400px;">
</div>
<div th:align="center">
  <button id="btn">加载数据</button>
</div>
</body>
</html>

                               

                                

*****************

使用测试

                    

localhost:8080/index,点击加载数据

                 

                         

localhost:8080/index2,点击加载数据

                 

                                 

                        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值