Springboot项目中运用vue+ElementUI+echarts前后端交互实现动态圆环图(推荐)

yizhihongxing

以下是 “Springboot项目中运用vue+ElementUI+echarts前后端交互实现动态圆环图(推荐)”的完整攻略。

1. 环境搭建

首先需要安装好Node.jsnpm, 安装好之后,通过npm安装vue-cli, 并用命令vue init webpack projectname创建项目。

npm install -g vue-cli
vue init webpack projectname

完成之后,安装所需要的ElementUIecharts模块。

npm install element-ui --save
npm install echarts --save

2. 后端部分

后端使用Springboot,同时需要配置CORS跨域支持,这样才能把数据传递给前端。

首先在pom.xml中添加相关依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--防止CORS攻击-->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
</dependency>

然后在application.properties中配置跨域请求相关的参数。

# 允许所有源访问
cors.allowed.origins = *
cors.allowed.methods = GET, POST, PUT, DELETE
cors.allowed.headers = Authorization,Content-Type

Controller中添加接口,返回需要展示的数据。

@RequestMapping(value = "/ring", method = RequestMethod.GET)
public List<Integer> getRingData() {
    List<Integer> data = new ArrayList<Integer>(
            Arrays.asList(70, 30));
    return data;
}

3. 前端部分

在前端项目中,需要用到ElementUIecharts库。

Vue中使用ElementUI库,加入代码

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

然后需要安装vue-axios库,用来进行AJAX请求。

npm i vue-axios -S

main.js中引入。

import axios from 'axios';
import VueAxios from 'vue-axios';
Vue.use(VueAxios, axios);

接下来就可以通过vue-axios进行请求,把后端的数据获取到,并传递给echarts进行展示。

<template>
  <el-row>
    <el-col :span="12">
      <div ref="ring" :style="{ width: '300px', height: '300px', margin: '0 auto' }"></div>
    </el-col>
  </el-row>
</template>

<script>
import echarts from 'echarts';

export default {
  name: 'HelloWorld',
  data() {
    return {
      option: null
    }
  },
  mounted() {
    this.$axios.get('/ring').then(response => {
      this.renderChart(response.data[0], response.data[1]);
    }).catch(error => {
      console.log(error);
    });
  },
  methods: {
    renderChart(data1, data2) {
      var ring = echarts.init(this.$refs.ring);
      var option = {
        series: [
          {
            type: 'pie',
            radius: ['80%', '90%'],
            startAngle: 225,
            color: ['#1ccc6e', '#f0f2f5'],
            hoverAnimation: false,
            legendHoverLink: false,
            labelLine: {
              normal: {
                show: false
              }
            },
            data: [
              {
                value: data1,
                itemStyle: {
                  normal: {
                    borderColor: {
                      type: 'linear',
                      x: 0,
                      y: 0,
                      x2: 0,
                      y2: 1,
                      colorStops: [
                        {
                          offset: 0,
                          color: '#1ccc6e' // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: '#1ccc6e' // 100% 处的颜色
                        }
                      ],
                      globalCoord: false // 缺省为 false
                    }
                  }
                }
              },
              {
                value: data2,
              }
            ]
          }
        ]
      };
      ring.setOption(option);
    }
  }
}
</script>

4. 示例说明

示例1: 后端部分

在后端中,我们需要注意以下几点:

  1. 保证所需要的依赖已添加

  2. 正确配置CORS跨域参数

  3. 编写需要返回数据的接口

比如,在Controller中的代码可以改为:

@RestController
@RequestMapping(value = "/api")
public class ApiController {

    @GetMapping("/ring")
    public List<Integer> getRingData() {
        return Arrays.asList(70, 30);
    }
}

示例2: 前端部分

在前端中,我们需要注意以下几点:

  1. 使用ElementUI库和echarts库

  2. 使用vue-axios库进行AJAX请求

  3. 注意渲染图表的方式

比如,可以在Vue中添加下面代码:

<template>
  <div class="ring-chart" ref="chart"></div>
</template>

<script>
import axios from 'axios'
import echarts from 'echarts'

export default {
  name: 'RingChart',
  data () {
    return {
      data: []
    }
  },
  mounted () {
    this.chart = echarts.init(this.$refs.chart)
    axios.get('/api/ring').then((res) => {
      this.data = res.data
      this.renderChart()
    })
  },
  methods: {
    renderChart () {
      this.chart.setOption({
        series: [
          {
            name: '动态圆环图',
            type: 'pie',
            radius: ['70%', '90%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            data: [
              {
                value: this.data[0],
                itemStyle: {
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: 'rgba(32,163,212,1)'
                  }, {
                    offset: 1,
                    color: 'rgba(9,136,213,1)'
                  }]),
                  label: {
                    normal: {
                      formatter: '{d} %',
                      textStyle: {
                        fontSize: 24
                      }
                    }
                  }
                }
              },
              {
                value: this.data[1],
                itemStyle: {
                  color: '#dcdfe2'
                }
              }]
          }
        ]
      })
    }
  }
}
</script>

<style scoped>
.ring-chart {
  width: 300px;
  height: 300px;
  margin: 0 auto;
}
</style>

这些示例可以帮助开发人员更好地理解和实现该功能。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Springboot项目中运用vue+ElementUI+echarts前后端交互实现动态圆环图(推荐) - Python技术站

(0)
上一篇 2023年5月21日
下一篇 2023年5月21日

相关文章

  • 傻瓜式用Eclipse连接MySQL数据库

    下面是傻瓜式用Eclipse连接MySQL数据库的攻略。 前置条件 已安装Eclipse,建议使用最新版本。 已安装MySQL数据库,建议使用最新版本。 已安装MySQL JDBC驱动。 步骤 1. 导入MySQL JDBC驱动 首先需要将MySQL JDBC驱动导入Eclipse。 在Eclipse中,选择“Window” -> “Preferenc…

    database 2023年5月18日
    00
  • mysql 动态执行存储过程语句

    MySQL 支持使用 PREPARE STATEMENT 和 EXECUTE STATEMENT 命令动态执行存储过程语句,具体操作步骤如下: 创建存储过程 首先,我们需要先创建一个包含动态 SQL 的存储过程,如下所示: CREATE PROCEDURE dynamic_query(IN col_name VARCHAR(64)) BEGIN DECLAR…

    database 2023年5月22日
    00
  • Consider defining a bean of type ‘redis.clients.jedis.JedisPool’ in your configuration.

    报错信息   原因是没有Jedispool没有注入 import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.Ob…

    Redis 2023年4月12日
    00
  • Python中执行存储过程及获取存储过程返回值的方法

    在Python中执行存储过程并获取返回值通常可以通过Python的数据库连接库来完成。下面我们将通过以下步骤详细讲解Python中执行存储过程及获取存储过程返回值的方法: 创建数据库连接对象并连接数据库 首先需要使用Python中的数据库连接库连接到数据库。以MySQL为例,我们可以使用pymysql库来连接MySQL数据库: import pymysql …

    database 2023年5月21日
    00
  • 数据库开发总结(ADO.NET小结)

    数据库开发总结 (ADO.NET小结) 是一篇介绍 ADO.NET 的文章,本文将为你提供详细的攻略。 ADO.NET 简介 ADO.NET 是一种用于访问数据源的技术,它能够让我们轻松地访问多种数据源,如数据库、XML 文件等。ADO.NET 提供了一组数据访问技术,包括连接、命令、读取和写入数据等操作,具有高性能和可扩展性。 ADO.NET 中的核心对象…

    database 2023年5月21日
    00
  • redis 启动配置文件加载报错 service redis does not support chkconfig

    # chkconfig:2345 90 10 # description:Redis is a persistent key-value database   网上资料 上面的注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。   linux 运行级别 运行级别就是操作系统当前正在运行的功能级别。这…

    Redis 2023年4月12日
    00
  • 在Ubuntu系统中安装MariaDB数据库的教程

    下面是在Ubuntu系统中安装MariaDB数据库的教程: 准备工作 在终端中输入以下命令,更新Ubuntu软件包列表: sudo apt update 安装MariaDB所需的软件包: sudo apt install software-properties-common 安装MariaDB 使用以下命令添加MariaDB官方仓库: sudo apt-ke…

    database 2023年5月22日
    00
  • 数据库常用的sql语句汇总

    数据库是存储数据的大型软件系统,而SQL是可用于访问和管理数据库的语言。因此,掌握SQL语言是数据库开发中非常重要的一环。在本文中,我们将分享一个“数据库常用的SQL语句汇总”攻略,帮助数据库开发者更好地理解SQL语句以及它们在实际工作中的应用。 SQL语句的类型 SQL语句可以分为以下几种类型: DDL(Data Definition Language):…

    database 2023年5月21日
    00
合作推广
合作推广
分享本页
返回顶部