uniapp echarts x轴 支持html标签

在 UniApp 中使用 ECharts,也可以通过自定义渲染函数来支持在 x 轴标签中使用 HTML 标签。以下是一个示例代码,展示如何在 UniApp 中使用 ECharts 并支持 x 轴标签中的 HTML 标签:

  1. template 中引入 ECharts 组件,并为其设置一个标识符(例如 chart):
<template>
  <view class="echarts-container">
    <ec-canvas id="chart" canvas-id="chart" :ec="ec"></ec-canvas>
  </view>
</template>
  1. script 中引入 ECharts 库,并设置 x 轴标签数据和自定义渲染函数:
<script>
import * as echarts from '@/components/ec-canvas/echarts';

export default {
   
  data() {
   
    return {
   
      ec: {
   
        lazyLoad: true
      },
      xAxisData: ['<span style="color: red;">A</span>', '<span style="color: blue;">B</span>', '<span style="color: green;">C</span>']
    };
  },
  onReady() {
   
    this.initChart();
  },
  methods: {
   
    initChart() {
   
      this.$nextTick(() => {
   
        // 获取图表组件实例
        const chart = this.selectComponent('#chart');

        // 调用 setOption 方法配置图表
        chart.init((canvas, width, height, dpr) => {
   
          const myChart = echarts.init(canvas, null, {
   
            width: width,
            height: height,
            devicePixelRatio: dpr
          });

          // 自定义渲染函数
          const renderXAxisLabel = (params) => {
   
            const xAxisData = this.xAxisData;
            if (xAxisData.length) {
   
              const item = xAxisData[params.dataIndex];
              return item;
            }
            return '';
          };

          // 配置选项
          const option = {
   
            xAxis: {
   
              type: 'category',
              data: this.xAxisData,
              axisLabel: {
   
                interval: 0,
                formatter: renderXAxisLabel
              }
            },
            yAxis: {
   
              type: 'value'
            },
            series: [{
   
              data: [120, 200, 150],
              type: 'bar'
            }]
          };

          // 使用配置项显示图表
          myChart.setOption(option);

          // 将图表实例保存在 data 中,方便后续操作
          this.chartInstance = myChart;

          // 返回 chart 实例,供外部使用
          return myChart;
        });
      });
    }
  }
};
</script>

相关推荐

  1. uniapp echarts x 支持html标签

    2024-01-20 06:26:04       29 阅读
  2. HTML|02HTML标签

    2024-01-20 06:26:04       9 阅读
  3. HTML标签与介绍

    2024-01-20 06:26:04       37 阅读
  4. HTML常用标签

    2024-01-20 06:26:04       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-20 06:26:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-20 06:26:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-20 06:26:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-20 06:26:04       20 阅读

热门阅读

  1. Webpack5入门到原理10:处理其他资源

    2024-01-20 06:26:04       33 阅读
  2. 【mfc/VS2022】绘图工具设计-绘制基本图元2

    2024-01-20 06:26:04       35 阅读
  3. JVM实战(26)——SystemGC

    2024-01-20 06:26:04       37 阅读
  4. PHP 把秒转换为多少天/小时/分钟

    2024-01-20 06:26:04       34 阅读
  5. Tomcat Notes: Common Issues Of Tomcat

    2024-01-20 06:26:04       20 阅读
  6. React底层原理分析(简单大白话版本)

    2024-01-20 06:26:04       32 阅读