123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <template>
- <div ref="chartDom" style="width: 100%; height: 100%"></div>
- </template>
- <script>
- import * as echarts from "echarts";
- export default {
- name: "EChartsComponent",
- props: {
- result: {
- type: Object, // 使用 Object 作为类型
- default: () => ({}),
- },
- },
- data() {
- return {
- myChart: null,
- };
- },
- mounted() {
- console.log(this.result, "this.result");
- this.initChart(); // 初始化图表
- },
- watch: {
- // 监听 result 的变化
- result(newValue) {
- console.log("result changed", newValue);
- this.updateChart(newValue); // 更新图表
- },
- },
- methods: {
- initChart() {
- const chartDom = this.$refs.chartDom;
- this.myChart = echarts.init(chartDom);
- const option = {
- title: {
- text: "状态统计图",
- left: "center",
- textStyle: {
- fontSize: 14, // 设置字体大小
- fontWeight: "600", // 可选,设置字体粗细
- color: "#333", // 可选,设置字体颜色
- },
- },
- tooltip: {
- trigger: "item",
- formatter: "{b}: {c} 次 ({d}%)", // 显示次数和百分比
- },
- series: [
- {
- name: "状态次数",
- type: "pie",
- radius: ["40%", "60%"], // 设置内外半径来形成圆环
- data: [
- { value: 0, name: "正常", itemStyle: { color: "#8AE359" } },
- { value: 0, name: "报警", itemStyle: { color: "#EECB5F" } },
- { value: 0, name: "危险", itemStyle: { color: "#F7715F" } },
- ],
- emphasis: {
- itemStyle: {
- shadowBlur: 10,
- shadowOffsetX: 0,
- shadowColor: "rgba(0, 0, 0, 0.5)",
- },
- },
- label: {
- // formatter: "{b}: {c} 次", // 在图表中显示次数
- },
- },
- ],
- };
- this.myChart.setOption(option);
- },
- // 更新图表方法
- updateChart(newResult) {
- console.log(newResult, "111111111111111111");
- const option = {
- title: {
- text: "状态统计图",
- left: "center",
- textStyle: {
- fontSize: 14, // 设置字体大小
- fontWeight: "600", // 可选,设置字体粗细
- color: "#333", // 可选,设置字体颜色
- },
- },
- tooltip: {
- trigger: "item",
- // formatter: "{b}: {c} 次 ({d}%)", // 显示次数和百分比
- },
- series: [
- {
- name: "状态次数",
- type: "pie",
- radius: ["40%", "60%"], // 设置内外半径来形成圆环
- data: [
- {
- value: newResult[1] || 0,
- name: "正常",
- itemStyle: { color: "#8AE359" },
- },
- {
- value: newResult[2] || 0,
- name: "报警",
- itemStyle: { color: "#EECB5F" },
- },
- {
- value: newResult[3] || 0,
- name: "危险",
- itemStyle: { color: "#F7715F" },
- },
- ],
- emphasis: {
- itemStyle: {
- shadowBlur: 10,
- shadowOffsetX: 0,
- shadowColor: "rgba(0, 0, 0, 0.5)",
- },
- },
- label: {
- // formatter: "{b}: {c} 次", // 在图表中显示次数
- },
- },
- ],
- };
- // 更新图表
- this.myChart.setOption(option);
- },
- },
- };
- </script>
- <style scoped>
- /* 可根据需求添加样式 */
- </style>
|