echarts树状图图表,可点击查询(vue3+vben admin框架)

点击树状图获取树状图key来查询tbale表格功能:

效果如下:

showChart方法里面 

// 监听柱状图点击事件给点击到的加颜色,最后通过emit传给父组件来获取数据

  myChart.on('click', (params: any) => {
      if (params && params.dataIndex !== undefined) {
        clickedIndex = params.dataIndex;
        // console.log(`点击了第 ${params.dataIndex} 个柱子`);
        // console.log(params.name);
        key.value = params.name;
        // console.log(key.value);

        // 更新柱状图颜色和样式
        myChart.setOption({
          series: [
            {
              itemStyle: {
                color: (params: any) => {
                  // 根据数据索引决定颜色
                  return params.dataIndex === clickedIndex
                    ? 'rgba(92, 123, 217, 1)'
                    : 'rgba(92, 123, 217, 0.3)';
                },
              },
            },
          ],
        });
      }
      emit('key-clicked', key);
    });

完整的树状图组件代码如下:

<template>
  <PageWrapper dense fixedHeight contentFullHeight>
    <!-- <h6 class="h6">{{ infoecharts.title }}</h6>// -->
    <h6 class="h6">库龄报表</h6>
    <div class="top-chart">
      <div id="main" ref="chartContainer" class="chart-container"></div>
    </div>
  </PageWrapper>
</template>
<script lang="ts" setup name="Reporttree">
  import {
    defineComponent,
    inject,
    watch,
    onMounted,
    PropType,
    onUnmounted,
    defineProps,
    ref,
    shallowRef,
    nextTick,
    toRefs,
    defineExpose,
    defineEmits,
  } from 'vue';
  import echarts from '/@/utils/lib/echarts';
  // 父组件传过来数据
  const emit = defineEmits(['key-clicked']);
  let clickedIndex = null;
  const props = defineProps({
    infoecharts: Array,
  });
  //使用父组件传递过来的值
  // console.log('props', props.infoecharts);

  const chartContainer = ref<HTMLElement | null>(null);
  const key = ref();
  const showChart = () => {
    if (!chartContainer.value) return;
    // console.log(props.infoecharts);
    const myChart = echarts.init(chartContainer.value);
    const xAxisData = props.infoecharts.map((val) => {
      return val.key;
    });
    const data2 = props.infoecharts.map((i) => {
      return i.value;
    });
    const data1 = data2.map((value) => Math.abs(value));

    const option = {
      xAxis: {
        type: 'category',
        data: xAxisData,
      },
      yAxis: {
        type: 'value',
        show: false,
      },
      series: [
        {
          barMinHeight: 100, // 设置高度最小值为200
          data: data1,
          type: 'bar',
          label: {
            show: true,
            position: 'top',
            formatter: (params: any) => {
              const value = data2[params.dataIndex];
              return value;
            },
          },

          barWidth: 120,
          barGap: '50%',
          itemStyle: {
            color: (params: any) => {
              // 根据数据索引决定颜色
              return params.dataIndex === 0 ? 'rgba(92, 123, 217, 1)' : 'rgba(92, 123, 217, 0.3)';
            },
          },
        },
      ],
    };
    myChart.setOption(option);

    // 监听柱状图点击事件
    myChart.on('click', (params: any) => {
      if (params && params.dataIndex !== undefined) {
        clickedIndex = params.dataIndex;
        // console.log(`点击了第 ${params.dataIndex} 个柱子`);
        // console.log(params.name);
        key.value = params.name;
        // console.log(key.value);

        // 更新柱状图颜色和样式
        myChart.setOption({
          series: [
            {
              itemStyle: {
                color: (params: any) => {
                  // 根据数据索引决定颜色
                  return params.dataIndex === clickedIndex
                    ? 'rgba(92, 123, 217, 1)'
                    : 'rgba(92, 123, 217, 0.3)';
                },
              },
            },
          ],
        });
      }
      emit('key-clicked', key);
    });

    // 监听窗口变化,自适应图表大小
    window.addEventListener('resize', () => {
      myChart.resize();
    });

    // 在组件销毁时销毁图表
    onUnmounted(() => {
      myChart.dispose();
    });
  };

  watch(
    () => props.infoecharts,
    () => {
      showChart();
    },
    { immediate: true },
  );

  onMounted(() => {
    // showChart();
  });
  defineExpose({
    showChart,
  });
</script>

<style lang="less" scoped>
  .h6 {
    position: absolute;
    left: 5px;
  }

  .top-chart {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;

    .chart-container {
      height: 320px;
      width: 100vw;
    }
  }
</style>

父组件绑定emit事件:

 <Reporttree :infoecharts="infodatas" ref="ReporttreeRef" @key-clicked="getkey" />


<script lang="ts" setup name="InventoryAgeReport">
const getkey = (clickedKey) => {
    key.value = clickedKey;
  };
</script>

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值