国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot)

這篇具有很好參考價(jià)值的文章主要介紹了動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前言

因?yàn)樯衔闹刑岬降男枨缶褪窃?vue2 里面繪制echarts,所以,這里就搭建一個(gè) vue2 的腳手架了。

想要深入了解 echarts 屬性,請(qǐng)到此篇文章:如何用echarts畫(huà)一個(gè)好看的餅圖

至于如何在 vue2 中使用 echarts,請(qǐng)見(jiàn)這篇文章:https://blog.csdn.net/m0_54355172/article/details/131960527

1. 項(xiàng)目搭建

1.1. 前端

  1. 先搭建一個(gè) vue2.0 的腳手架

    • 安裝vue-cli

      1. 卸載老版本

        npm uninstall vue-cli -g
        
      2. 安裝腳手架

        npm install -g @vue/cli
        
    • 新建一個(gè) vue2 的項(xiàng)目

      vue create pie_front
      
  2. 引入 echarts 依賴:見(jiàn)博客:https://blog.csdn.net/m0_54355172/article/details/131960527

  3. MyPie.vue 初始代碼如下:

    <template>
        <div>
          <div class="charts">
            <div id="comPie" style="height: 400px; width: 44em" />
          </div>
        </div>
      </template>
      
      <script>
      export default {
        name: 'myPie',
        data () {
          return {
            pieOption : {
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    top: '5%',
                    left: 'center'
                },
                series: [
                    {
                    name: 'Access From',
                    type: 'pie',
                    radius: ['40%', '70%'],
                    avoidLabelOverlap: false,
                    itemStyle: {
                        borderRadius: 10,
                        borderColor: '#fff',
                        borderWidth: 2
                    },
                    label: {
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
                        label: {
                        show: true,
                        fontSize: 40,
                        fontWeight: 'bold'
                        }
                    },
                    labelLine: {
                        show: false
                    },
                    data: [
                        { value: 1048, name: 'Search Engine' },
                        { value: 735, name: 'Direct' },
                        { value: 580, name: 'Email' },
                        { value: 484, name: 'Union Ads' },
                        { value: 300, name: 'Video Ads' }
                    ]
                    }
                ]
                },
          }
        },
        mounted () {
          this.showPie()
        },
        methods: {
          showPie () {
            // 指定 echarts 圖表初始化的容器
            const pieCharts = this.$echarts.init(document.querySelector('#comPie'))
            // 渲染 echarts
            pieCharts.setOption(this.pieOption, true)
          },
        },
      }
      </script>
      
      <style scoped type="text/less">
        #channelPie {
          margin-top: 1em;
        }
        button {
          width: 80px;
          height: 30px;
          border: 1px solid #2a69ee;
          border-radius: 5px;
          font: normal normal 14px 微軟雅黑;
          color: #2a69ee;
          background-color: white;
        }
        .charts {
          display: flex;
          justify-content: center;
        }
      </style>
    
  4. App.vue 原始代碼

    <template>
      <div id="app">
        <myPie msg="Welcome to Your Vue.js App"/>
      </div>
    </template>
    
    <script>
    import myPie from './components/MyPie.vue'
    
    export default {
      name: 'App',
      components: {
        myPie
      }
    }
    </script>
    
    <style>
    
    </style>
    
  5. 初始頁(yè)面
    動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot

1.2. 后端

postgreSQL 表數(shù)據(jù):
動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot

后端接口:http://127.0.0.1:8099/pie/getPieData
動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot

依賴:
動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot

application.yml

spring:
 datasource:
   type: com.alibaba.druid.pool.DruidDataSource
   url: jdbc:postgresql://localhost:5432/study?useUnicode=true&characterEncoding=UTF-8&useSSL=false
   username: postgres
   password: admin
   driver-class-name: org.postgresql.Driver
server:
 port: 8099

PieReadMapper.java

@Repository
public interface PieReadMapper extends BaseMapper<Commodity> {

}

Commodity.java 實(shí)體類

@TableName("t_commodity")
@Data
public class Commodity {

    @TableId("cid")
    private String id;
    @TableField("cname")
    private String name;
    private Integer count;
    private BigDecimal income;
}

PieController.java

@Slf4j
@RestController
@RequestMapping("/pie")
public class PieController {

    @Resource
    private PieReadMapper pieReadMapper;


    @PostMapping("getPieData")
    public JSONArray getPieData(String param) {
        log.info("前端參數(shù)===>{}", param);
//        QueryWrapper<Commodity> wrapper = new QueryWrapper<>();
//        wrapper.setEntity(new Commodity());
        List<Commodity> commodities = pieReadMapper.selectList(null);
        String s = JSONObject.toJSONString(commodities);
        return JSONArray.parseArray(s);
    }
}

PieBackApplication.java 啟動(dòng)類

@MapperScan("com.chenjy.pie_back.mapper.**")
@SpringBootApplication
public class PieBackApplication {
    public static void main(String[] args) {
        SpringApplication.run(PieBackApplication.class, args);
    }

}

2. 后端數(shù)據(jù)渲染前端

2.1 補(bǔ)充1:在 vue 中使用 axios

  1. 引入依賴

    npm install axios
    
  2. main.js 全局引入 axios

    import axios from 'axios'
    
    Vue.prototype.$axios = axios
    
  3. 使用 axios 發(fā)送 post 請(qǐng)求

          getPieData() {
            const url = 'http://127.0.0.1:8099/pie/getPieData'
            this.$axios({
                method: 'post',
                url: url,
                data: null
            }).then(res => {
                console.log(res.data)
            }, err => {
                console.log('錯(cuò)誤信息:', err.message)
            })
          }
    

    那如何用 axios 發(fā)送 GET 請(qǐng)求呢?如下:

          testGet() {
            const url = 'http://127.0.0.1:8099/pie/testGet'
            this.$axios({
                // method: 'get', 默認(rèn) get,可不寫(xiě)
                url: url,
                params: {
                    str: '前端發(fā)起一次 get 請(qǐng)求'
                }
            }).then(res => {
                console.log(res.data)
            }, err => {
                console.log('錯(cuò)誤信息:', err.message)
            })
          }
    

2.2. 補(bǔ)充2:Springboot 處理跨域問(wèn)題

  1. 解決跨域問(wèn)題
    動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot
    在后臺(tái)新加一個(gè)配置類

    @Configuration
    public class config implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOriginPatterns("*")
                    .allowCredentials(true)
                    .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
                    .maxAge(3600);
        }
    
    }
    
    動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot

2.3. 修改前端代碼

2.3.1 修改餅圖樣式

動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot

假數(shù)據(jù)先不去掉,后續(xù)把方法加上了再去掉。

        pieOption : {
            title: {
                show: true,
                text: '商品收益',
                x: 'left',
                y: 'top'
            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                x: 'right',
                y: 'center',
                align: 'left',
                icon: 'circle',
            },
            series: [
                {
                type: 'pie',
                radius: ['60%', '70%'],
                roseType: 'area',
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data: [
                    { value: 1048, name: 'Search Engine' },
                    { value: 735, name: 'Direct' },
                    { value: 580, name: 'Email' },
                    { value: 484, name: 'Union Ads' },
                    { value: 300, name: 'Video Ads' }
                ]
                }
            ]
            },
      }

2.3.2 調(diào)用后臺(tái)數(shù)據(jù)渲染餅圖

動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot
<template>
    <div>
      <div class="charts">
        <div id="comPie" style="height: 400px; width: 44em" />
      </div>
    </div>
  </template>
  
  <script>


  export default {
    name: 'myPie',
    data () {
      return {
        pieOption : {
            title: {
                show: true,
                text: '商品收益',
                left: 100,
            },
            tooltip: {
                trigger: 'item',
                formatter: '&emsp;&emsp;n5n3t3z% <br> 商品收益}&emsp;&emsp;{c}',
            },
            legend: {
                orient: 'vertical',
                right: 80,
                top: 100,
                align: 'left',
                icon: 'circle',
                data:[],
            },
            series: [
                {
                type: 'pie',
                radius: ['60%', '70%'],
                roseType: 'area',
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data: []
                }
            ]
            },
      }
    },
    mounted () {
      this.getPieData()
    },
    methods: {
      // 每次給餅圖傳入新的數(shù)據(jù)之后都要調(diào)用這個(gè)函數(shù)來(lái)重新渲染餅圖
      showPie () {
        // 指定 echarts 圖表初始化的容器
        const pieCharts = this.$echarts.init(document.querySelector('#comPie'))
        // 渲染 echarts
        pieCharts.setOption(this.pieOption, true)
      },
      // 調(diào)用后臺(tái)獲取餅圖數(shù)據(jù),并重新渲染餅圖
      getPieData() {
        const url = 'http://127.0.0.1:8099/pie/getPieData'
        this.$axios({
            method: 'post',
            url: url,
            data: null
        }).then(res => {
            const datas = res.data
            this.setPieData(datas)
            this.showPie()
        }, err => {
            console.log('錯(cuò)誤信息:', err.message)
        })
      },
      // 根據(jù)傳入數(shù)據(jù)給餅圖參數(shù)賦值
      setPieData(datas) {
        // 根據(jù) arrays 配置 option 的 legend 和 series.data 的數(shù)據(jù)
        const data = Array.from(datas)
        const legendArr = []
        const seriesArr = []
        for (let i = 0; i < data.length; i++) {
           const seriesObj = {}
           legendArr.push(data[i].name)
           seriesObj.value = data[i].income
           seriesObj.name = data[i].name
           seriesArr.push(seriesObj)
        }
        this.pieOption.legend.data = legendArr
        this.pieOption.series[0].data = seriesArr
      }
    },
  }
  </script>
  
  <style scoped type="text/less">
    #channelPie {
      margin-top: 1em;
    }
    button {
      width: 80px;
      height: 30px;
      border: 1px solid #2a69ee;
      border-radius: 5px;
      font: normal normal 14px 微軟雅黑;
      color: #2a69ee;
      background-color: white;
    }
    .charts {
      display: flex;
      justify-content: center;
    }
  </style>

2.3.3 改造成內(nèi)外兩個(gè)圈

如果要弄成內(nèi)外兩個(gè)圈的餅圖,可以在 series 中再加一個(gè)數(shù)組:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-707539.html

動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot),前端,Spring Boot,echarts,vue.js,spring boot
series: [
                {
                name: '商品收益',
                type: 'pie',
                radius: ['60%', '70%'],
                roseType: 'area',
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data: []
                },
                {
                name: '商品收益',
                type: 'pie',
                radius: '35%',
                // roseType: 'area',
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data: []
                }
            ]
            },
      setPieData(datas) {
        // 根據(jù) arrays 配置 option 的 legend 和 series.data 的數(shù)據(jù)
        const data = Array.from(datas)
        const legendArr = []
        const seriesArr = []
        for (let i = 0; i < data.length; i++) {
           const seriesObj = {}
           legendArr.push(data[i].name)
           seriesObj.value = data[i].income
           seriesObj.name = data[i].name
           seriesArr.push(seriesObj)
        }

到了這里,關(guān)于動(dòng)態(tài)渲染 echarts 餅圖(vue 2 + axios + Springboot)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • vue3使用 echarts - 餅圖、折線圖

    vue3使用 echarts - 餅圖、折線圖

    餅圖 - 帶中心圖形 - graphic - elements 折線圖 - 圖表標(biāo)記 markPoint

    2024年02月08日
    瀏覽(19)
  • vue實(shí)現(xiàn)echarts3D餅圖

    vue實(shí)現(xiàn)echarts3D餅圖

    效果圖: 1.首先安裝依賴 2.mainjs中導(dǎo)入以及掛載 3.傳入數(shù)據(jù)生成3D的配置項(xiàng)以及option的配置 4.指示線的配置

    2024年02月06日
    瀏覽(22)
  • vue-echarts餅圖/柱狀圖點(diǎn)擊事件

    vue-echarts餅圖/柱狀圖點(diǎn)擊事件

    在實(shí)際的項(xiàng)目開(kāi)發(fā)中,我們通常會(huì)用到Echarts來(lái)對(duì)數(shù)據(jù)進(jìn)行展示,有時(shí)候需要用到Echarts的點(diǎn)擊事件,增加系統(tǒng)的交互性,一般是點(diǎn)擊Echarts圖像的具體項(xiàng)來(lái)跳轉(zhuǎn)路由并攜帶參數(shù),當(dāng)然也可以根據(jù)具體需求來(lái)做其他的業(yè)務(wù)邏輯。下面就Echarts圖表的點(diǎn)擊事件進(jìn)行實(shí)現(xiàn),文章省略了

    2024年02月06日
    瀏覽(19)
  • vue3+heightchart實(shí)現(xiàn)3D餅圖,echarts3D餅圖,3D餅圖引導(dǎo)線實(shí)現(xiàn)

    vue3+heightchart實(shí)現(xiàn)3D餅圖,echarts3D餅圖,3D餅圖引導(dǎo)線實(shí)現(xiàn)

    ?附上 heightcharts 官網(wǎng)地址? Highcharts 演示 | Highcharts https://www.hcharts.cn/demo/highcharts 首先需要下載一下 heightcharts執(zhí)行命令 ?然后初始化: 如此你就得到了一個(gè)3D餅圖?

    2024年02月13日
    瀏覽(26)
  • vue 使用echarts實(shí)現(xiàn)3D餅圖和環(huán)形圖

    vue 使用echarts實(shí)現(xiàn)3D餅圖和環(huán)形圖

    記錄一下echarts實(shí)現(xiàn)3d餅圖和環(huán)形圖功能## 標(biāo)題 實(shí)現(xiàn)效果 首先第一步安裝echarts和echarts-gl echarts-gl安裝最新版本可能會(huì)有異常,建議安裝\\\"echarts-gl\\\": \\\"^1.1.2\\\"版本 第二步在vue文件中引入 第三步我這里把實(shí)現(xiàn)3d餅圖的代碼給封裝一下,如下: 第四步 vue文件內(nèi)使用 餅圖的實(shí)現(xiàn) 如果對(duì)

    2024年02月12日
    瀏覽(25)
  • 用echarts在vue2中實(shí)現(xiàn)3d餅圖

    用echarts在vue2中實(shí)現(xiàn)3d餅圖

    先看效果,再看文章: 3d的圖不僅用到echarts,還用到了echarts-gl,因此都需要安裝一下哦~ 直接復(fù)制粘貼吧,省事 1、修改3d餅圖大小,在大概244行的位置,grid3D的對(duì)象里面,修改distance屬性,即可調(diào)整 值越小,圖越大? ? 2、修改3d餅圖視角高度,在大概161行的位置,修改函數(shù)

    2024年02月07日
    瀏覽(18)
  • vue2之echarts的封裝 折線圖,餅圖,大圖

    vue2之echarts的封裝 折線圖,餅圖,大圖

    chartPan.vue 使用 chartPan.vue 之餅圖 效果 使用 chartPan.vue 之折線圖 效果 展開(kāi)大圖 handlePreViewChart 事件 大圖組件 maxChart.vue 大圖效果

    2024年02月01日
    瀏覽(21)
  • vue中用echarts實(shí)現(xiàn)復(fù)合餅圖,帶關(guān)系連接線

    vue中用echarts實(shí)現(xiàn)復(fù)合餅圖,帶關(guān)系連接線

    1.拿到產(chǎn)品原型圖,需求中有這樣一個(gè)圖表 2.翻看echart的餅圖示例,沒(méi)有這種復(fù)合餅圖,只有一個(gè)嵌套餅圖 3. 于是網(wǎng)上查網(wǎng)友的文章,查到兩篇類似的貼子,(52條消息) echarts模仿excel復(fù)合餅圖(餅-餅)_相忘于江湖426543的博客-CSDN博客_echarts復(fù)核餅圖 和 (52條消息) echarts實(shí)現(xiàn)復(fù)合

    2024年02月10日
    瀏覽(25)
  • vue3之echarts3D環(huán)柱餅圖

    vue3之echarts3D環(huán)柱餅圖

    vue3之echarts3D環(huán)柱餅圖 效果: 版本 \\\"echarts\\\": \\\"^5.4.1\\\", \\\"echarts-gl\\\": \\\"^2.0.9\\\" 核心代碼:

    2024年03月25日
    瀏覽(25)
  • vue3.0 使用echarts與echarts-gl 實(shí)現(xiàn)可旋轉(zhuǎn),可放大3D餅圖

    vue3.0 使用echarts與echarts-gl 實(shí)現(xiàn)可旋轉(zhuǎn),可放大3D餅圖

    echarts與echarts-gl 實(shí)現(xiàn)3D餅圖 實(shí)現(xiàn)效果: 旋轉(zhuǎn)效果 縮放效果 實(shí)現(xiàn)步驟 1、安裝echarts npm install echarts npm install echarts-gl 2、頁(yè)面定義容器 3、js中引入echarts VUE 組件完整源碼:

    2024年04月26日
    瀏覽(27)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包