Vue ECharts markline设置为legend - 附完整示例

13 篇文章 0 订阅

ECharts:一个基于 JavaScript 的开源可视化图表库。

目录

 效果

一、介绍

1、官方文档:Apache ECharts

 二、准备工作

1、安装依赖包

2、示例版本 

三、完整示例


 效果

一、介绍

1、官方文档:Apache ECharts

Apache EChartsApache ECharts,一款基于JavaScript的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。icon-default.png?t=N7T8https://echarts.apache.org/zh/index.html


 

 二、准备工作

1、安装依赖包

npm install echarts --save

2、示例版本 

"echarts": "^5.4.2",

三、完整示例

<template>
  <div id="main"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
  name: "forExample",
  data() {
    return {
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  methods: {
    initChart() {
      var chartDom = document.getElementById("main");
      var myChart = echarts.init(chartDom);
      var option;

      const data = [50, 64, 96, 80, 70, 82, 73];
      const meanValue = data.reduce((pre, cur) => pre + cur, 0) / data.length;

      option = {
        tooltip: {},
        legend: {
          data: [
            { name: "Sale" },
            {
              /* TIP: 设置虚线 */
              name: "平均值",
              lineStyle: {
                type: "dotted",
                width: 3,
                color: "#fd0100",
              },
            },
          ],
          selectedMode: false, //图例选择模式关闭
        },
        xAxis: {
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {},
        series: [
          {
            name: "Sale",
            type: "bar",
            data,
            markLine: {
              lineStyle: {
                color: "#fd0100",
                width: 3,
                height: 14,
              },
              data: [
                {
                  yAxis: meanValue,
                },
              ],
              label: {
                position: "insideEndTop",
                formatter: (params) => "平均值:" + params.value,
                padding: [0, 20, 0, 0],
                fontSize: 18,
                distance: 0,
              },
            },
          },
          {
            /* TIP: 增加的series */
            type: "line",
            symbol: "none",
            name: "平均值",
            color: "transparent",
          },
        ],
      };

      option && myChart.setOption(option);
    },
  },
};
</script>

<style lang="less" scoped>
#main {
  width: 1000px;
  height: 500px;
}
</style>

 欢迎扫码下方二维码关注VX公众号

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue项目中使用EchartsVue-Awesome-Swiper,你需要先安装它们。你可以使用npm或者yarn安装它们: ``` npm install echarts vue-awesome-swiper ``` 或者 ``` yarn add echarts vue-awesome-swiper ``` 接下来,你需要在Vue组件中导入并使用它们。下面是一个例子: ```html <template> <div> <swiper :options="swiperOptions"> <swiper-slide> <div ref="chart1" class="chart"></div> </swiper-slide> <swiper-slide> <div ref="chart2" class="chart"></div> </swiper-slide> <swiper-slide> <div ref="chart3" class="chart"></div> </swiper-slide> </swiper> </div> </template> <script> import Swiper from 'vue-awesome-swiper'; import 'swiper/dist/css/swiper.css'; import echarts from 'echarts'; export default { components: { Swiper, }, data() { return { swiperOptions: { loop: true, }, }; }, mounted() { this.renderChart(this.$refs.chart1, 'chart1'); this.renderChart(this.$refs.chart2, 'chart2'); this.renderChart(this.$refs.chart3, 'chart3'); }, methods: { renderChart(container, chartId) { const chart = echarts.init(container); chart.setOption(this.getChartOption(chartId)); }, getChartOption(chartId) { // 这里根据不同的chartId生成不同的Echarts配置 // 省略具体实现代码 }, }, }; </script> ``` 这里我们使用了Vue-Awesome-Swiper来实现轮播图,每个swiper-slide里面都包含了一个Echarts图表。在mounted函数中,我们通过ref获取每个chart的DOM元素,并使用Echarts渲染图表。你可以根据自己的需求修改getChartOption函数,生成不同的Echarts配置。 注意:你需要在组件中引入EchartsVue-Awesome-Swiper,并且在样式中引入Swiper的CSS文件。如果你使用了Vue CLI创建项目,可以在main.js中全局引入echarts和swiper.css: ```javascript import Vue from 'vue'; import App from './App.vue'; import echarts from 'echarts'; import 'swiper/dist/css/swiper.css'; Vue.prototype.$echarts = echarts; new Vue({ render: (h) => h(App), }).$mount('#app'); ``` 这样,在整个应用中都可以使用echarts和swiper了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值