碳排放占比饼图代码前端vue
时间: 2024-12-28 21:35:41 浏览: 17
在Vue.js中创建碳排放占比饼图,你可以使用第三方库如`echarts`或更现代的`v-charts`来实现。以下是使用`vue-echarts`库的一个基本示例:
首先,你需要安装依赖:
```bash
npm install vue-echarts echarts @types/vue-echarts
```
然后在组件里引入并配置图表:
```html
<template>
<div id="carbon-emissions-chart">
<v-chart :options="chartOptions" />
</div>
</template>
<script setup>
import { ECharts } from 'vue-echarts';
import * as echarts from 'echarts';
const chartOptions = {
// 饼图的基本配置
type: 'pie',
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b}: {c}%"
},
series: [
{
name: '碳排放比例',
data: [
// 这里替换为实际的碳排放数据,如对象数组 [{name: '能源A', value: 30}, ...]
{ name: '能源A', value: 30 },
{ name: '能源B', value: 40 },
{ name: '能源C', value: 30 }
],
label: {
normal: {
show: true,
position: 'center'
}
},
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
// 初始化ECharts实例
const myChart = ref(new ECharts());
setup() {
onMounted(() => {
if (myChart.value) {
myChart.value.setOption(chartOptions);
}
});
}
</script>
```
记得替换`data`部分的实际碳排放数据,并根据需求调整样式和配置选项。
阅读全文
相关推荐

















