ECharts 为visualMap视觉映射添加自适应外边框

问题

visualMap如何添加一个渐变的外边框

实现

  1. 创建初始模板
jsx 复制代码
import { useEffect } from "react";
import * as echarts from "echarts";
import styles from "./page.module.css";

export default function Home() {
  useEffect(() => {
    // 初始化图表实例
    const chart = echarts.init(document.getElementById("main"));
    // 配置图表选项
    const options = {
      title: {
        text: "ECharts Entry Example",
      },
      tooltip: {},
      xAxis: {
        data: ["shirt", "cardigan", "chiffon", "pants", "heels", "socks"],
      },
      yAxis: {},
      visualMap: {
        type: "piecewise", //  piecewise: 分段型; continuous: 连续型
        min: 0,
        max: 40,
        itemGap: 4, // 设置段间距
        inRange: {
          color: ["#6eaeefff", "#1c55abff"],
        },
      },
      series: [
        {
          name: "Sales",
          type: "bar",
          data: [5, 20, 36, 10, 10, 20],
        },
      ],
    };
    // 设置图表选项
    chart.setOption(options);
  }, []);
  return (
    <div className={styles.page}>
      <div
        id="main"
        className={styles.main}
        style={{ width: "100%", height: "600px"}}
      ></div>
    </div>
  );
}
  1. 创建一个自定义框使用graphic元素
arduino 复制代码
const options = {
      // 其他配置项
      // ......
      // 自定义图形元素
      graphic: [
        {
          type: "rect", // 图形类型矩形
          // 图形位置以及大小, 默认左上角
          shape: {
            x: 10,
            y: 490,
            width: 30,
            height: 100,
          },
          style: {
            fill: "transparent", // 内容填充为透明, 默认黑色的
            lineWidth: 2, // 边框宽度
            stroke: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 1, color: "#4589DC66" },
              { offset: 0, color: "#4589DCFF" },
            ]), // 线性渐变
          },
        },
      ]
    };

这样就可以了。但是还是有一个问题,我这里写的定位是固定的,一旦有要响应式的展示,那么固定的就无法满足我们的要求了,所以我们要添加一个响应式的计算,保证位置一致是正确的。

响应式的计算

  1. 采用 rem 的方式要计算出初始的字体大小
  2. 根据字体的大小来计算每一块的位置以及宽高

处理

jsx 复制代码
"use client";
import { useEffect, useState } from "react";
import * as echarts from "echarts";
import styles from "./page.module.css";

export default function Home() {
  // TODO: 改动-添加
  // =========================================
  const [fontSize, setFontSize] = useState(16);
  // =========================================

  useEffect(() => {
    // 初始化图表实例
    const chart = echarts.init(document.getElementById("main"));

    // 自定义框,位置计算处理 TODO: 改动-添加
    // ===================================================
    const chartHeightValue = chart.getHeight(); // DOM 高度
    // 视觉映射位置计算
    const visualMapLeftValue = 4 * fontSize;
    const visualMapBottomValue = 0.75 * fontSize;
    const visualMapHeightValue = 1.25 * fontSize;
    const visualMapGapValue = 0.13 * fontSize;
    const visualMapTop =
      chartHeightValue -
      visualMapBottomValue -
      (visualMapHeightValue * 5 + visualMapGapValue * 4);
    // ===================================================
    // 配置图表选项
    const options = {
      title: {
        text: "ECharts Entry Example",
      },
      tooltip: {},
      xAxis: {
        data: ["shirt", "cardigan", "chiffon", "pants", "heels", "socks"],
      },
      yAxis: {},
      visualMap: {
        type: "piecewise", //  piecewise: 分段型; continuous: 连续型
        min: 0,
        max: 40,
        // TODO: 改动-修改
        // =============================================
        left: visualMapLeftValue,
        bottom: visualMapBottomValue,
        itemWidth: 0.88 * fontSize,
        itemHeight: visualMapHeightValue,
        itemGap: visualMapGapValue, // 图例每项之间的间隔
        // =============================================
        inRange: {
          color: ["#6eaeefff", "#1c55abff"],
        },
      },
      // 自定义图形元素
      graphic: [
        {
          type: "rect", // 图形类型矩形
          // 图形位置以及大小, 默认左上角
          shape: {
            // TODO: 改动-修改
            // =========================
            x: visualMapLeftValue + 11,
            y: visualMapTop - 19,
            width: fontSize * 1.3,
            height: fontSize * 7.2,
            // ========================
          },
          style: {
            fill: "transparent", // 内容填充为透明, 默认黑色的
            lineWidth: 2, // 边框宽度
            stroke: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 1, color: "#4589DC66" },
              { offset: 0, color: "#4589DCFF" },
            ]), // 线性渐变
          },
        },
      ],
      series: [
        {
          name: "Sales",
          type: "bar",
          data: [5, 20, 36, 10, 10, 20],
        },
      ],
    };
    // 设置图表选项
    chart.setOption(options);
  }, [fontSize]);
  return (
    <div className={styles.page}>
      {/* TODO: 改动-添加 */}
      {/* =================================================== */}
      <div>
        字体大小:
        <input
          value={fontSize}
          onChange={(e) => setFontSize(+e.target.value)}
        />
      </div>
      {/* ================================================= */}
      <div
        id="main"
        className={styles.main}
        style={{ width: "100%", height: "600px" }}
      ></div>
    </div>
  );
}
相关推荐
web小白成长日记16 小时前
企业级 Vue3 + Element Plus 主题定制架构:从“能用”到“好用”的进阶之路
前端·架构
APIshop17 小时前
Python 爬虫获取 item_get_web —— 淘宝商品 SKU、详情图、券后价全流程解析
前端·爬虫·python
风送雨17 小时前
FastMCP 2.0 服务端开发教学文档(下)
服务器·前端·网络·人工智能·python·ai
XTTX11017 小时前
Vue3+Cesium教程(36)--动态设置降雨效果
前端·javascript·vue.js
LYFlied18 小时前
WebGPU与浏览器边缘智能:开启去中心化AI新纪元
前端·人工智能·大模型·去中心化·区块链
Setsuna_F_Seiei18 小时前
2025 年度总结:人生重要阶段的一年
前端·程序员·年终总结
model200518 小时前
alibaba linux3 系统盘网站迁移数据盘
java·服务器·前端
han_19 小时前
从一道前端面试题,谈 JS 对象存储特点和运算符执行顺序
前端·javascript·面试
aPurpleBerry19 小时前
React 01 目录结构、tsx 语法
前端·react.js
jayaccc19 小时前
微前端架构实战全解析
前端·架构