首页 > 其他分享 >如何快速开始进行echart组件开发

如何快速开始进行echart组件开发

时间:2024-08-09 17:08:39浏览次数:12  
标签:const echart 快速 myChart type 组件 true options name

1、定义chart组件

<template>
  <div ref="chart"></div>
</template>
<script>
import elementResizeDetector from "element-resize-detector";
import * as echarts from "echarts/core";
import { LineChart, BarChart, PieChart, GraphChart, LinesChart } from "echarts/charts";
import {
  GridComponent,
  SingleAxisComponent,
  TooltipComponent,
  AxisPointerComponent,
  TitleComponent,
  LegendComponent,
  DataZoomComponent,
} from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";

echarts.use([
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  SingleAxisComponent,
  GridComponent,
  AxisPointerComponent,
  BarChart,
  LineChart,
  PieChart,
  GraphChart,
  LinesChart,
  DataZoomComponent,
  CanvasRenderer,
]);

export default {
  name: "viChart",
  props: {
    options: {
      type: Object,
      required: true,
    },
    isClear: {
      type: Boolean,
      default: true,
    },
  },
  // model: {
  //   prop: "myChart",
  //   event: "setModel",
  // },
  data() {
    return {
      myChart: null,
    };
  },
  beforeDestroy() {
    this.dispose();
  },
  methods: {
    watchOptions() {
      this.$watch("options", this.setOption, { deep: true });
    },
    init() {
      this.myChart = echarts.init(this.$refs.chart);
      this.setOption();
      // 监听echart的点击事件,点击元素
      this.myChart.on("click", params => {
        this.$emit("getChartObjOnClick", params, this.myChart);
      });
      // 监听echart的点击事件,getZr()方法可以监听到整个画布的点击事件
      this.myChart.getZr().on("click", params => {
        this.$emit("getChartPanelOnClick", params, this.myChart);
      });
    },
    resize() {
      if (!this.myChart) return;
      this.myChart.resize();
    },
    setOption() {
      if (!this.myChart || Object.keys(this.options).length === 0) return;
      if (this.isClear) {
        this.myChart.clear();
      }
      this.myChart.setOption(this.options);
    },
    dispose() {
      if (!this.myChart) return;
      this.myChart.dispose();
    },
  },
  mounted() {
    this.init();
    this.watchOptions();
    this.erd = elementResizeDetector();
    this.erd.listenTo(this.$refs.chart, this.resize);
  },
  beforeDestroy() {
    this.erd.removeListener(this.$refs.chart);
  },
};
</script>

 2、定义常见echart场景

// 饼状图颜色
const colors = ["#5B8FF9", "#5AD8A6", "#5D7092", "#F6BD16", "#E8684A", "#6DC8EC", "#9270CA", "#FF9D4D", "#269A99", "#FF99C3"];
// legend的大小
const itemHeight = 8;
const itemWidth = 11;

// 饼图(环形图,中心显示名称和数值,有标签名和标签连线)
const pieOptionFun = (options = {}) => {
  const pieOption = {
    title: {
      text: ["{name|名称}", "{value|0}"].join("\n"),
      top: options.titleTop || "center",
      left: options.titleLeft || "50%",
      textAlign: "center",
      textStyle: options.textStyle  || {
        rich: {
          value: {
            color: "#555",
            fontSize: 20,
            lineHeight: 24,
          },
          name: {
            color: "#555",
            fontSize: 14,
            lineHeight: 35,
          },
        },
      },
    },
    tooltip: {
      trigger: "item",
      confine: true,
      formatter: "{b}: {c} (占比{d}%)",
    },
    legend: {
      type: "scroll",            // 设置图例翻页
      orient: "vertical",
      right: "0%",
      icon: "circle",
      itemHeight,
      itemWidth,
      textStyle: {
        color: "#8C8C8C",
        rich: {
          nameCls: {
            fontWeight: 400,
            fontSize: 14,
            padding: [5, 0, 5, 0],
            width: options.richWidth || 80,
          },
          valueCls: {
            fontSize: 12,
            padding: [0, 0, 0, 0],
            width: options.valueWidth || 40,
            textAlign: "center",
          },
        },
      },
    },
    grid: {
      left: "0%",
      right: "0%",
      bottom: "0%",
      top: options.gridTop || "0%",
      containLabel: true,
    },
    color: colors,
    series: [
      {
        type: "pie",
        startAngle: options.seriesStartAngle || 90,
        radius: options.seriesRadius || ["45%", "65%"],
        center: options.seriesCenter || ["50%", "50%"],
        label: {
          show: false,
        },
        data: [
          // { name: "克莱因壶", value: 3.5 },
          // { name: "投资最重要的事", value: 2.8 },
          // { name: "简读中国史", value: 1.7 },
          // { name: "你当像鸟飞往你的山", value: 1.4 },
          // { name: "表象与本质", value: 0.5 },
          // { name: "其它", value: 3.8 }
        ],
      },
    ],
  };
  return pieOption;
};

// 柱状图,类型过多需要分页
const barOptionFun = (options = {}) => {
  const barOption = {
    title: {
      text: "",
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
      },
      formatter: "{b}<br />数量: {c}%",
    },
    legend: {
      right: "0%",
      itemHeight,
      itemWidth,
    },
    xAxis: {
      type: "category",
      data: [], // ["Hannah Krause", "Zhao Qian", "Jasmin Krause", "Li Lei", "Karle Neumann", "Adrian Groß", "Mia Neumann"],
      axisTick: {
        alignWithLabel: true,
      },
      axisLabel: {
        interval: 0, // 坐标轴刻度标签的显示间隔,在类目轴中有效。默认会采用标签不重叠的策略间隔显示标签。可以设置成 0 强制显示所有标签。
        formatter: (params) => {
          let newParamsName = params;
          if (options.xAxisLabelLength) { // 表示是否截取数据
            if (params.length > options.xAxisLabelLength) {
              newParamsName = `${params.slice(0, options.xAxisLabelLength - 1)}...`;
            }
          }
          return newParamsName; // 将最终的字符串返回
        },
      },
    },
    yAxis: {
      type: "value",
      alignTicks: true,
      axisLine: {
        show: true,
      },
      axisLabel: {
        formatter: "{value}",
      },
    },
    grid: {
      left: "0%",
      right: "0%",
      bottom: "10%",
      top: "10%",
      containLabel: true,
    },
    color: ["#eecb5f"],
    series: [
      {
        type: "bar",
        barMaxWidth: "30",
        data: [], // [41, 20, 52, 37, 25, 19, 71]
      },
    ],
  };
  return barOption;
};

// 折线图
const lineOptionFun = (options = {}) => {
  const lineOption = {
    title: {
      text: "",
    },
    tooltip: {
      trigger: "axis",
      formatter: "",
      confine: true,
    },
    legend: {
      top: 0,
      right: 0,
      icon: "rect",
      itemHeight,
      itemWidth,
    },
    color: options.colors || colors,
    xAxis: {
      type: "category",
      data: [],
      // data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
      axisTick: {
        alignWithLabel: true,
      },
      axisLabel: {
        interval: 0, // 坐标轴刻度标签的显示间隔,在类目轴中有效。默认会采用标签不重叠的策略间隔显示标签。可以设置成 0 强制显示所有标签。
        color: "#000000",
      },
    },
    yAxis: {
      name: options.yAxisName || "",
      nameTextStyle: options.nameTextStyle || undefined,
      type: "value",
      alignTicks: true,
      axisLine: {
        show: options.yAxisAxisLine,  // 是否显示y轴坐标轴轴线
      },
      axisLabel: {
        color: "#000000"
      }
    },
    grid: {
      left: options.gridLeft || "2%",
      right: "2%",
      bottom: options.gridBottom || "5px",
      top: options.gridTop || "10%",
      containLabel: true,
    },
    series: [
      // {
      //   name: "Email",
      //   type: "line",
      //   smooth: true,
      //   data: [20, 132, 101, 134, 90, 230, 110]
      // },
      // {
      //   name: "Union Ads",
      //   type: "line",
      //   smooth: true,
      //   data: [20, 182, 191, 234, 290, 230, 110]
      // },
    ],
  };
  return lineOption;
};

 

3、vue页面使用

// vue页面定义
<vi-chart class="echart" :options="weekAlarmOptions" @getChartObjOnClick="getChartObjOnClickWeek"></vi-chart>


// options渲染
// deepCopy()方法,表示深度拷贝
    this.weekAlarmOptions = deepCopy(
      barOption4Fun({
        xAxisLabelLength: 3,
        colors: ["#2277CC", "#9270CA", "#269A99", "#3300f9", "#6DC8EC"],
        gridBottom: "1%",
      })
    );
    // echart的点击事件获取
    getChartObjOnClickWeek(params) {
      const xxx= params.xxx;
      const query = {
      xxx
      };
      this.$router.push({ path: "/xxx", query });
    },

 

标签:const,echart,快速,myChart,type,组件,true,options,name
From: https://www.cnblogs.com/luoxuemei/p/18351065

相关文章

  • 自定义周选择组件、年选择组件
    //周组件weekSelect<!--周选择组件--><template><divref="viYearSelect"class="vi-year-select"><ui-inputv-model="selectVal":placeholder="placeholder":d......
  • ant disign vue pro 使用日期组件,无法动态赋值 解决
    原文地址:https://blog.csdn.net/weixin_43865196/article/details/121849591组件使用渲染<a-date-picker v-model="date" format="YYYY-MM-DD" valueFormat="YYYYMMDD" :allowClear="false" @change="(date,dateStr)=>{ this.da......
  • Vue3实现印章徽章组件
    1、组件结构2、组件封装src/components/StampBadge/src/StampBadge.vue文件代码<template><divclass="first-ring"v-bind="getBindValue":class="getStampBadgeClass"><divclass="second-ri......
  • Vue3水印组件
    1、组件封装<template><divref="watermarkContainerRef"class="watermark-container"><!--插槽--><slot></slot></div></template><scriptsetup>import{ref,onMounted,watc......
  • 用echarts绘制图表时,横坐标数据太多显示不全/堆叠的解决方法
    一、横坐标数据过多可能出现的问题1、横坐标文字堆叠2、横坐标显示不全(数据2,数据4,数据6,数据8,数据10,数据12,数据14均没有显示出来)二、解决方法1、使用rotate属性给echarts的横坐标标签设置旋转角度xAxis:{type:'category',data:xAxis,......
  • LDAR(泄漏检测与修复)如何快速准确建档
    ​ LDAR建档前肯定需要对现场分析,先了解现场的装置信息,区域、单元以及物料分析,PID图分析等等工作,现在有理论派和现实派:1、理论派认为建档前必须进行物料分析、PID图分析等规范要求2、现实派,现实环境不是理论派能解决的,比如PID图企业涉及保密不给提供、或者企业经过多年的变更P......
  • Endnote如何快速导入期刊格式
    首先进入Endnote官网,官网上提供了期刊文献格式的下载地址。 链接:https://endnote.com/downloads/styles/https://endnote.com/downloads/styles/以作者君领域的期刊“ActaMaterialia”为例注意一定要输入期刊全称“ActaMaterialia”,然后点击“research”,会弹出“downlo......
  • OpenTiny HUICharts开源发布,带你了解一个简单、易上手的图表组件库
    摘要:目前OpenTinyHUICharts已经成功落地在华为内部100多个产品中,持续提升了用户的可视化体验。本文分享自华为云社区《OpenTinyHUICharts正式开源发布,一个简单、易上手的图表组件库》,作者:OpenTiny。引言大家好!我们非常高兴地跟大家宣布,今天正式发布我们的新产品——Open......
  • 使用 defineNuxtComponent`定义 Vue 组件
    title:使用defineNuxtComponent`定义Vue组件date:2024/8/9updated:2024/8/9author:cmdragonexcerpt:摘要:本文介绍了在Nuxt3中使用defineNuxtComponent辅助函数定义类型安全的Vue组件的方法,适用于习惯OptionsAPI的开发者。defineNuxtComponent支持asyncData获取异......
  • 快速基于 ClickHouse + Grafana 搭建可观测性解决方案 - 分布式链路追踪篇(ClickHouse
    引言在ClickHouse,我们认为可观测性仅仅是另一个实时分析问题。作为一款高性能的实时分析数据库,ClickHouse被用于多种场景,包括时间序列数据的实时分析。其应用场景的多样性推动了大量分析函数的发展,这些函数有助于查询大多数数据类型。这些查询特性和高压缩率使得越来越多的用户......