springboot接口返回数据{stagesz: 0, stage: 0, suborder: 0, announce: 0, zs: 10, mytyper: 2, mytypez: 8, cld: 10, clb: 0}根据后端返回这样的数据用vue做出一个饼图
时间: 2023-07-16 20:15:57 浏览: 159
springboot+vue,前后端分离商城项目,有部署教程
5星 · 资源好评率100%
首先,我们需要使用一个图表库来绘制饼图,这里我推荐使用ECharts。在Vue中使用ECharts需要先安装echarts和vue-echarts两个依赖。
安装命令:
```
npm install echarts vue-echarts
```
然后在组件中引入ECharts和VueECharts:
```js
import echarts from 'echarts'
import VueECharts from 'vue-echarts'
export default {
components: {
'v-chart': VueECharts,
},
data() {
return {
chartData: {},
chartOption: {},
}
},
mounted() {
this.getChartData()
},
methods: {
getChartData() {
// 发送请求获取后端数据
// 省略代码...
// 假设数据存储在this.chartData中
// 处理数据
this.chartOption = {
title: {
text: '饼图示例',
left: 'center',
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} ({d}%)',
},
series: [
{
name: '数据',
type: 'pie',
radius: [0, '55%'],
label: {
show: false,
},
labelLine: {
show: false,
},
data: [
{ value: this.chartData.stagesz, name: 'stagesz' },
{ value: this.chartData.stage, name: 'stage' },
{ value: this.chartData.suborder, name: 'suborder' },
{ value: this.chartData.announce, name: 'announce' },
{ value: this.chartData.zs, name: 'zs' },
{ value: this.chartData.mytyper, name: 'mytyper' },
{ value: this.chartData.mytypez, name: 'mytypez' },
{ value: this.chartData.cld, name: 'cld' },
{ value: this.chartData.clb, name: 'clb' },
],
},
],
}
},
},
}
```
最后在模板中使用v-chart组件即可:
```html
<template>
<div>
<v-chart :option="chartOption" :style="{height: '400px'}"></v-chart>
</div>
</template>
```
阅读全文