Echarts图表自定义切换明亮暗黑风格

先引入useDark来获取当前的风格状态

根据isDark的值来改变主题chartTheme的值,watch和computed都可以

import { useDark } from '@vueuse/core'


const isDark = useDark()
// const chartTheme = computed(() => {
//   return isDark.value ? 'dark' : 'light'
// })
// console.log(chartTheme.value, 'chartTheme')
const chartTheme = ref('light')
watch(
  () => isDark.value,
  (newVal: any) => {
    chartTheme.value = newVal ? 'dark' : 'light'
    leftChartguawang()
  }
)

这里以饼图为例

记得给canvas宽度和高度这里用的class样式,自己补全

<div id="leftChartguawang" class="chartvh"></div>

 在主题chartTheme值改变时发现图表并没有重新绘制,所以先销毁之前的实例再重新绘制

const chartBoxleftChartguawang = ref()
const leftChartguawang = () => {
  if (chartBoxleftChartguawang.value) {
    chartBoxleftChartguawang.value.dispose() // 销毁当前实例
  }
  // document.getElementById('leftChartguawang')?.removeAttribute('_echarts_instance_')
  chartBoxleftChartguawang.value = echarts.init(
    document.getElementById('leftChartguawang'),
    isDark.value ? 'dark' : 'light'
  )
  let datas: any = []
  datas.push(left_echart_obj.netAreaList)
  const option = {
    title: {
      text: `挂网总面积\n \n${left_echart_obj.netArea}(万㎡)`,
      left: 'center',
      top: '40%',
      textStyle: {
        color: '#999',
        fontWeight: 'normal',
        fontSize: 14
      }
    },
    series: datas.map(function (data: any, idx: any) {
      var top = idx * 50
      return {
        type: 'pie',
        radius: [50, 60],
        top: top + '%',
        height: '100%',
        left: 'center',
        width: 400,
        itemStyle: {
          borderColor: '#fff',
          borderWidth: 1
        },
        label: {
          alignTo: 'edge',
          formatter: '{name|{b}}\n{time|{c} %}',
          minMargin: 5,
          edgeDistance: 10,
          lineHeight: 15,
          rich: {
            time: {
              fontSize: 10,
              color: '#999'
            }
          }
        },
        labelLine: {
          length: 15,
          length2: 0,
          maxSurfaceAngle: 80
        },
        labelLayout: function (params: any) {
          const isLeft = params.labelRect.x < chartBoxleftChartguawang.value.getWidth() / 2
          const points = params.labelLinePoints
          points[2][0] = isLeft ? params.labelRect.x : params.labelRect.x + params.labelRect.width
          return {
            labelLinePoints: points
          }
        },
        data: data
      }
    })
  }
  option && chartBoxleftChartguawang.value.setOption(option)
  window.addEventListener('resize', function () {
    chartBoxleftChartguawang.value.resize()
  })
}

下面是一些,如何让Echarts重绘的方法

1. 使用setOption()方法

// 假设已经有一个ECharts实例chart  
var newOption = {  
    // 新的配置项  
    // ...  
};  
chart.setOption(newOption, true); // 第二个参数为true时,表示不合并之前的option,而是完全使用新的option

2. 使用clear()方法

// 假设已经有一个ECharts实例chart  
chart.clear(); // 清空图表  
// 随后可以调用setOption()方法重新绘制图表  
chart.setOption(option);

3. 使用dispose()方法

// 假设已经有一个ECharts实例chart  
chart.dispose(); // 销毁图表实例  
// 重新初始化ECharts实例  
var newChart = echarts.init(document.getElementById('chart'));  
// 使用新的配置项重新绘制图表  
newChart.setOption(option);

4. 移除实例属性(特定场景)

// 移除容器上的ECharts实例属性  
document.getElementById('myChart').removeAttribute('_echarts_instance_');  
// 重新初始化ECharts实例  
var chart = echarts.init(document.getElementById('myChart'));  
// 使用新的配置项重新绘制图表  
chart.setOption(option);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值