echarts折线图tooltip自定义多条折现写法

本文介绍如何使用ECharts创建具有自定义样式的折线图,并详细解释了如何设置图表的各种属性,如数据轴、网格布局、提示框等,同时展示了如何通过监听窗口大小变化来调整图表尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
  <div class="ebox">
    <img class="borimg" src="~@/assets/img1/public/border.png" alt />
    <div class="outborder" :id= "id"></div>
  </div>
</template>
<script>
import echarts from 'echarts';
import { nowSize } from "@/util/util.js";
import resize from "element-resize-detector";
export default {
  name: "Bar",
  props: ["setData", "id"],
  data() {
    return {
      time:['1234','2345','5678'],
      sitedata:this.setData
    };
  },
  //实时监听父组件传过来的值,进而执行drawBar方法,重绘柱状图
  watch: {
    setData: {
      handler(val) {
        console.log("父组件传值",this.setData)
        this.drawBar(val);
      },
      deep: true
      // immediate: true
    }
  },
  mounted() {
    this.drawBar();

    // console.log(this.setData);
    // console.log(this.setData.color)
  },

  methods: {
    drawBar() {
 
      //折线图实现
      // console.log("setData",this.setData)
      // var scolor = this.color.color;
      let myChart = echarts.init(document.getElementById(this.id));
      let unit = this.setData.timeData;
      let name = this.setData.text;
      let eu = this.setData.name;
      let option = {
        title: {
          text: this.setData.text,
          textStyle: {
            color: "#fff",
            fontSize: 12,
            fontWeight:'normal'
          },
          left: "center",
          top:-2,
        },
        legend: {
          show: true,
          type: 'scroll',
          right:nowSize(30),
          top: nowSize(30),
          width: '70%',
          icon: "stack",
          itemWidth: nowSize(10),
          itemHeight: nowSize(10),
          pageTextStyle: {
              color: '#fff'
          },
          textStyle: {
            color: "#fff",
            fontSize: nowSize(10),
          },
          data: [this.setData.aname, this.setData.bname, this.setData.cname,],
        },

        xAxis: {
          data:this.setData.xAxisData,
          showMinLabel: true,
          showMaxLabel: true,
          axisLabel: {
            inside: false, //x轴文字显示位置,true则显示在柱子上
            // rotate: 30,
            textStyle: {
              color: "#fff"
            }
          },
          axisTick: {
            //x轴刻度样式
            show: false
          },
          axisLine: {
            //x轴轴线的样式
            show: true,
            lineStyle: {
              color: "#CCCCCC",
            }
          },
          z: 10
        },
        grid: {
          top: "30%",
          left: "8%",
          right: "8%",
          bottom: "6%",
          containLabel: true
        },

        tooltip: {
          trigger: "axis",
          formatter: function (params) {
            console.log(params,1234567)
            console.log(unit[params[0].dataIndex],1234567)
            // return [
            //       '时间: ' + unit[params[0].dataIndex],
            //        name + ':' + params[0].data + eu,
            //     ].join('<br/>')

            // console.log(this.time,1234567)
            // console.log(this.setData.timeData,1234567)
            // let newdata = this.setData.timeData;
            let str ='时间: ' + unit[params[0].dataIndex] + "<br />" ;
            params.forEach((item) => {
              str +=
                '<span style="display:inline-block;margin-right:5px;border-radius:50%;width:10px;height:10px;left:5px;background-color:'+item.color+'"></span>' + item.seriesName+ " : " + item.data + eu + "<br />";
            });
            return str;
          },
        },
        yAxis: {
          name:this.setData.name,
          axisLine: {
            show: true,
            lineStyle: {
              color: "#fff",
            }
          },
          axisTick: {
            show: false,
            interval: 'auto',
            lineStyle: {
              color: "#ccc",
              type: "dotted"
            }
          },
          axisLabel: {
            textStyle: {
              color: "#fff"
            },
            
          },
          splitLine: {
            // show: false,
            lineStyle: {
              color: "rgba(20, 251, 246, 0.1)",
              type: "solid"
            }
          }
        },
        dataZoom: [
          {
            type: "inside"
          }
        ],

        series: [
          {
            name:this.setData.aname,
            type: "line",
            symbol: "none",
            smooth: true,
            data:this.setData.seriesData,
            // data: [5, 10, 15, 12, 16, 20],
            barMaxWidth: 50,
            color: this.setData.linecolor ,//折线颜色
            areaStyle: {
              // color: "rgba(20, 251, 246, 0.2)"
              color: new echarts.graphic.LinearGradient(
                0,0,0,1,
                this.setData.color
              )
            },
          },{
            name:this.setData.bname,
            type: "line",
            symbol: "none",
            smooth: true,
            data:this.setData.seriesTwoData,
            // data: [5, 10, 55, 12, 16, 20],
            barMaxWidth: 50,
            color: this.setData.lineTwocolor,
            // lineStyle: {
            //   color: this.setData.lineTwocolor //折线颜色
            // },
            areaStyle: {
              // color: "rgba(20, 251, 246, 0.2)"
              color: new echarts.graphic.LinearGradient(
                0,0,0,1,
                this.setData.color1
              )
            },
          },{
            name:this.setData.cname,
            type: "line",
            symbol: "none",
            smooth: true,
            data:this.setData.seriesThreeData,
            // data: [5, 10, 55, 12, 16, 20],
            barMaxWidth: 50,
            color: this.setData.lineThreecolor, //折线颜色
        
            areaStyle: {
              // color: "rgba(20, 251, 246, 0.2)"
              color: new echarts.graphic.LinearGradient(
                0,0,0,1,
                this.setData.color2
              )
            },
          }
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
      var erd = resize();
      erd.listenTo(document.getElementById(this.id), element => {
        var width = element.offsetWidth;
        var height = element.offsetHeight;
        this.$nextTick(function() {
          //console.log("Size: " + width + "x" + height)
          //使echarts尺寸重置
          echarts.init(document.getElementById(this.id)).resize();
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.ebox {
  position: relative;
  width: 100%;
  height: 100%;
}

.borimg {
  position: absolute;
  top: 0;
  width: 100%;
  height:100%;
}
.outborder{
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
}
</style>

echarts折线图tooltip自定义多条折现写法

自定义echarts折线图tooltip的样式,可以使用tooltip的formatter属性和CSS样式来实现。以下是一个示例代码: ```javascript option = { tooltip: { trigger: 'axis', formatter: function(params) { var html = '<div class="tooltip">'; html += '<div class="tooltip-header">' + params[0].name + '</div>'; params.forEach(function(item) { html += '<div class="tooltip-item">'; html += '<div class="tooltip-point" style="background-color:' + item.color + '"></div>'; html += '<div class="tooltip-name">' + item.seriesName + ':</div>'; html += '<div class="tooltip-value">' + item.value + '</div>'; html += '</div>'; }); html += '</div>'; return html; }, axisPointer: { type: 'cross' } }, // 其他配置项 // ... } ``` 上面代码中,我们使用了tooltip的formatter属性来自定义tooltip的内容。在函数中,我们可以通过params参数获取到当前tooltip中包含的所有数据项,然后根据需要来组装html字符串。在这个例子中,我们使用了一个`<div>`元素来作为tooltip的容器,并且为它添加了一个自定义的CSS类名`tooltip`,以便后面可以通过CSS样式来对tooltip进行样式调整。 接下来,我们在formatter函数中遍历params参数,将每个数据项的名称、数值、颜色等信息组装成一个html字符串,并添加到tooltip容器中。在这个例子中,我们使用了一个`<div>`元素来作为每个数据项的容器,并为它添加了一个自定义的CSS类名`tooltip-item`,以便后面可以通过CSS样式来对每个数据项进行样式调整。 最后,我们在CSS样式中定义了`.tooltip`和`.tooltip-item`两个类的样式,来对tooltip进行样式调整。以下是CSS样式的示例代码: ```css .tooltip { background-color: #fff; border: 1px solid #ccc; padding: 10px; } .tooltip-header { font-weight: bold; margin-bottom: 5px; } .tooltip-item { margin-bottom: 5px; } .tooltip-point { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; } .tooltip-name { display: inline-block; font-weight: bold; margin-right: 5px; } .tooltip-value { display: inline-block; } ``` 上面代码中,我们定义了`.tooltip`类的背景色、边框、内边距等样式,以及`.tooltip-header`、`.tooltip-item`、`.tooltip-point`、`.tooltip-name`、`.tooltip-value`等类的其他样式,来对tooltip进行样式调整。其中,`.tooltip-point`类的样式用于设置每个数据项的颜色标识符,`.tooltip-name`类的样式用于设置每个数据项的名称,`.tooltip-value`类的样式用于设置每个数据项的数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值