vue3使用 echarts - 饼图、折线图

下载安装

npm install -S echarts

使用

饼图 - 带中心图形 - graphic - elements

js 复制代码
import { onMounted, ref, reactive, unref, nextTick } from "vue";
import * as echarts from "echarts";
import circle from "@/assets/panel/circle.png"
import star from '@/assets/panel/star.png'


/** 页面初始化 */
onMounted(() => {
  drawPieEchart("echart1", rightData.echartData1);
});

/** 饼图-echart */
const drawPieEchart = function (domId: string, data: any[]) {
  const chartDom: any = document.getElementById(domId);
  const myChart: any = echarts.init(chartDom);
  const option: any = {
    color: ['#43A2E7', '#EF6648'],
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
      },
    },
    graphic: {
      elements: [
        {
          type: 'image',
          style: {
            image: circle,
            width: 90,
            height: 90
          },
          left: 'center',
          top: '106'
        }]
    },
    series: [
      {
        type: "pie",
        top: "6%",
        radius: [70, 60],
        silent: true, // 不响应和触发鼠标事件,默认为 false
        itemStyle: {
          borderRadius: 2,
          borderColor: "#060F1E",
          borderWidth: 2,
        },
        label: {
          show: "true",
          alignTo: "edge",
          position: "outside",
          formatter: function (data: any) {
            return data.name + "\n" + data.percent + "%";
          },
          minMargin: 5,
          edgeDistance: 0,
          lineHeight: 20,
          color: "#fff",
          fontSize: 14,
          rich: {
            value: {
              fontSize: 18,
              color: "#30eeff",
            },
          },
        },
        labeLine: {
          show: "true",
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: "bold",
          },
        },
        data: data,
      },
    ],
  };
  option && myChart.setOption(option, true);
  // 浏览器窗口大小变化,图表大小自适应
  window.addEventListener("resize", () => {
    myChart.resize();
  });
};

折线图 - 图表标记 markPoint

js 复制代码
/** 折线图-echart */
const drawFoldLine = function (domId: string, data: any) {
  const chartDom: any = document.getElementById(domId);
  const myChart = echarts.init(chartDom);
  const option: any = {
    color: ['#43A2E7', '#EF6648'],
    tooltip: {
      trigger: "axis",
      backgroundColor:"#000910",
      borderWidth:0,
      borderRadius:0,
      textStyle:{
        color:'#fff'
      },
      axisPointer: {
        type: "shadow",
      },
    },
    legend: {
      type: "plain",
      textStyle: {
        color: "#fff",
        fontSize: 12,
      },
      left: 26,
      top: 16,
      data: [
        {
          name: '计划产值',
          icon: 'circle',
        },
        {
          name: '累计完成产值',
          icon: 'circle',
        }
      ],
    },
    grid: {
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true,
    },
    xAxis: {
      data: data.xAxisData,
      type: "category",
      boundaryGap: false,
      textStyle: {
        fontSize: 12,
        color: "#A5B2BB",
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: "#657A8A",
        },
      },
      axisLabel: {
        showMaxLabel: true,
        showMinLabel: true,
      },
    },
    yAxis: {
      type: "value",
      textStyle: {
        fontSize: 12,
        color: "#A5B2BB",
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: "#657A8A",
        },
      },
      splitLine: { // 修改背景线条样式
        lineStyle: {
          color: "#657A8A", // 线条颜色
          // type: "dashed" // 线条样式,默认是实现,dashed是虚线
        }
      },
    },
    series: [
      {
        data: data.seriesData1,
        type: 'line',
        name: '计划产值',
        smooth: true,
        markPoint: {
          symbol: "image://" + star,
          symbolSize: 16,
          symbolOffset: [0, '-50%'],
          animation: true,
          label: {
            show: true,
            color: '#fff',
            fontSize: 14,
            position: 'top',
            backgroundColor: '#0B2E47',
            padding: [4, 12]
          },
          data: [
            {
              coord: [1, 820],
              value: '特殊标记1',

            },
            {
              coord: [3, 901],
              value: '特殊标记2',
            },
            {
              coord: [6, 1330],
              value: '特殊标记3',
            }
          ]
        }
      },
      {
        data: data.seriesData2,
        type: 'line',
        name: '累计完成产值',
        smooth: true
      }
    ]
  };

  option && myChart.setOption(option, true);
  // 浏览器窗口大小变化,图表大小自适应
  window.addEventListener("resize", () => {
    myChart.resize();
  });
};
相关推荐
也无晴也无风雨1 小时前
深入剖析输入URL按下回车,浏览器做了什么
前端·后端·计算机网络
Martin -Tang1 小时前
Vue 3 中,ref 和 reactive的区别
前端·javascript·vue.js
FakeOccupational3 小时前
nodejs 020: React语法规则 props和state
前端·javascript·react.js
放逐者-保持本心,方可放逐3 小时前
react 组件应用
开发语言·前端·javascript·react.js·前端框架
曹天骄4 小时前
next中服务端组件共享接口数据
前端·javascript·react.js
阮少年、4 小时前
java后台生成模拟聊天截图并返回给前端
java·开发语言·前端
郝晨妤6 小时前
鸿蒙ArkTS和TS有什么区别?
前端·javascript·typescript·鸿蒙
AvatarGiser6 小时前
《ElementPlus 与 ElementUI 差异集合》Icon 图标 More 差异说明
前端·vue.js·elementui
喝旺仔la6 小时前
vue的样式知识点
前端·javascript·vue.js
别忘了微笑_cuicui6 小时前
elementUI中2个日期组件实现开始时间、结束时间(禁用日期面板、控制开始时间不能超过结束时间的时分秒)实现方案
前端·javascript·elementui