TS使用echarts柱状图鼠标放上去并弹出

效果

在这里插入图片描述

代码

<template>
  <div>
   <Chart style="width: 100%; height: 344px" :option="chartOption" />
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';

  import { ToolTipFormatterParams } from '@/types/echarts';

  const tooltipItemsHtmlString = (items: ToolTipFormatterParams[]) => {
    return items
      .map(
        (el) => `<div class="content-panel">
    <p>
      <span style="background-color: ${
        el.color
      }" class="tooltip-item-icon"></span>
      <span>
      ${el.seriesName}
      </span>
    </p>
    <span class="tooltip-value">
      ${Number(el.value).toLocaleString()}
    </span>
  </div>`
      )
      .join('');
  };

  const xAxis = ref<string[]>([
    '1月',
    '2月',
    '3月',
    '4月',
    '5月',
    '6月',
    '7月',
    '8月',
    '9月',
    '10月',
    '11月',
    '12月',
  ]);

  const aseries = ref<number[]>([
    32, 43, 43, 53, 23, 143, 25, 23, 76, 98, 62, 34,
  ]);
  const bseries = ref<number[]>([
    32, 13, 33, 53, 123, 43, 35, 54, 34, 47, 76, 54,
  ]);
  const cseries = ref<number[]>([
    32, 143, 33, 73, 23, 13, 95, 54, 24, 22, 12, 23,
  ]);
  const chartOption = {
    grid: {
      left: '3%', // 调整左边距
      right: '3%', // 调整右边距
      top: '20',
      bottom: '60',
    },
    color: ['#246EFF', '#81E2FF', '#00B2FF'],
    tooltip: {
      show: true, // 启用提示框
      trigger: 'axis', // 触发类型为坐标轴
      axisPointer: {
        type: 'cross', // 坐标轴指示器类型
      },
      formatter: (params: ToolTipFormatterParams[]) => {
        // 定义提示框的内容格式
        const [firstElement] = params as ToolTipFormatterParams[];
        return `<div>
          <p class="tooltip-title">${firstElement.axisValueLabel}</p>
          ${tooltipItemsHtmlString(params as ToolTipFormatterParams[])}
        </div>`;
      },
      className: 'echarts-tooltip-diy',
    },
    xAxis: {
      type: 'category',
      data: xAxis.value,
    },
    yAxis: {
      type: 'value',
    },
    series: [
      {
        name: '系列 A',
        data: aseries.value,
        type: 'bar',
      },
      {
        name: '系列 B',
        data: bseries.value,
        type: 'bar',
      },
      {
        name: '系列 C',
        data: cseries.value,
        type: 'bar',
      },
    ],
  };
</script>

<style scoped lang="less">
  
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡忘_cx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值