npm install echarts -S
一,引用组件
<template>
<div class="analysis">
<PieChart :chartData="chartList"
@chartClick="chartClick" />
</div>
</template>
<script>
import PieChart from './PieChart.vue'
export default {
name: 'IndexAnalysis',
components: {
PieChart,
},
data() {
return {
chartList: [
{
name: 'recall_error',
value: 3,
itemStyle: {
color: '#9a60b4',
},
},
{
name: 'rank_error',
value: 5,
itemStyle: {
color: '#fc8452',
},
},
{
name: 'qp_error-QP',
value: 2,
itemStyle: {
color: '#5470c6',
},
},
],
}
},
methods: {
//接受echarts扇形信息传递
chartClick(param) {
console.log(param)
},
},
}
</script>
<style scoped>
</style>
二,复用 Echarts 组件
<template>
<div id="chart"
ref="chart">
</div>
</template>
<style scoped>
#chart {
width: 500px;
height:300px;
display: inline-block;
}
</style>
<script>
// 引入 ECharts 主模块
import * as ECharts from 'echarts'
export default {
props: {
chartData: {
type: Array,
default: [],
},
},
methods: {
echartTree() {
this.myChart = ECharts.init(this.$refs.chart)
this.myChart.setOption(
(this.optionChart = {
tooltip: {
triggerOn: 'click',
trigger: 'item',
},
series: [
{
type: 'pie',
radius: '50%', //图形大小
data: this.seriesData,
center: ['50%', '50%'], // 图形位置 默认全局居中
emphasis: {
//设置鼠标放到哪一块扇形上面的时候,扇形样式、阴影
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'blue',
normal: {
color: ['#FE8463', 'red', 'green'],
},
},
},
itemStyle: {
normal: {
label: {
show: true,
//百分比展示
formatter: `{b} : {d}% - {c}`,
textStyle: {
fontSize: 14,
fontWeight: 'bolder',
},
},
},
},
},
],
})
)
this.myChart.off('click')
// 饼状图点击扇形 数据传递
this.myChart.on('click', (params) => {
this.$emit('chartClick', params)
})
},
},
mounted() {
console.log(this.seriesData)
this.echartTree()
},
data() {
return {
myChart: null,
seriesData: this.$props.chartData,
optionChart: '',
}
},
watch: {
chartData(val) {
if (val) {
this.seriesData = val
} else {
this.seriesData = null
}
this.echartTree()
},
},
}
</script>
效果图展示(对应上方数据)
name: 'recall_error',
value: 4