【图形基础篇】03 # 声明式图形系统:如何用SVG图形元素绘制可视化图表?

简介: 【图形基础篇】03 # 声明式图形系统:如何用SVG图形元素绘制可视化图表?

说明

【跟月影学可视化】学习笔记。




什么是SVG


SVG 的全称是 Scalable Vector Graphics,可缩放矢量图,它是浏览器支持的一种基于 XML 语法的图像格式。

SVG 可以作为 HTML 内嵌元素使用,也可以作为图像通过 img 元素加载,或者绘制到 Canvas 内。




利用 SVG 绘制几何图形


svg 元素是 SVG 的根元素,属性 xmlns 是 xml 的名字空间。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>如何用SVG图形元素绘制可视化图表</title>
    </head>
    <body>
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <circle
                cx="100"
                cy="50"
                r="40"
                stroke="black"
                stroke-width="2"
                fill="orange"
            />
        </svg>
    </body>
</html>

0e52957bf52e491885fa237e5e013da1.png


SVG 坐标系和 Canvas 坐标系完全一样,都是以图像左上角为原点,x 轴向右,y 轴向下的左手坐标系。可以通过给 svg 元素设置 viewBox 属性,来改变 SVG 的坐标系。



利用 SVG 绘制层次关系图


SVG 元素要使用 document.createElementNS 方法来创建。该方法用于创建一个具有指定的命名空间 URI 和限定名称的元素。


有效的命名空间 URI:


   HTML - 参阅 http://www.w3.org/1999/xhtml

   SVG - 参阅 http://www.w3.org/2000/svg

   XBL - 参阅 http://www.mozilla.org/xbl

   XUL - 参阅 http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用SVG绘制层次关系图和给层次关系图增加鼠标控制</title>
    <style>
        h1#title {
            position: absolute;
            width: 800px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1 id="title"></h1>
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="800" height="800" viewBox="0 0 1600 1600"></svg>
    <script src="https://d3js.org/d3-hierarchy.v1.min.js"></script>
    <script type="module">
        import dataSource from './data/map.js';
        // 用 d3.hierarchy(data).sum(…).sort(…) 将省份数据按照包含城市的数量,从多到少排序
        const regions = d3.hierarchy(dataSource)
            .sum(d => 1)
            .sort((a, b) => b.value - a.value);
        // 通过 d3.pack() 将数据映射为一组 1600 宽高范围内的圆形,留3px的padding
        const pack = d3.pack()
            .size([1600, 1600])
            .padding(3);
        const root = pack(regions);
        console.log(root)
        const svgroot = document.querySelector('svg');
        function draw(parent, node, { fillStyle = 'rgba(0, 0, 0, 0.2)', textColor = 'white' } = {}) {
            const children = node.children;
            const { x, y, r } = node;
            // 创建 circle 元素绘制圆型
            const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
            circle.setAttribute('cx', x);
            circle.setAttribute('cy', y);
            circle.setAttribute('r', r);
            circle.setAttribute('fill', fillStyle);
            circle.setAttribute('data-name', node.data.name);
            parent.appendChild(circle);
            if (children) {
                // SVG 的 g 元素表示一个分组,给 g 元素设置属性,那么它的子元素会继承这些属性。
                const group = document.createElementNS('http://www.w3.org/2000/svg', 'g');
                group.setAttribute('data-name', node.data.name);
                // 遍历子节点绘制
                for (let i = 0; i < children.length; i++) {
                    draw(group, children[i], {fillStyle, textColor});
                }
                parent.appendChild(group);
            } else {
                // 绘制文本
                const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
                text.setAttribute('fill', textColor);
                text.setAttribute('font-family', 'Arial');
                text.setAttribute('font-size', '1.5rem');
                text.setAttribute('text-anchor', 'middle');
                text.setAttribute('x', x);
                text.setAttribute('y', y);
                text.textContent = node.data.name;
                parent.appendChild(text);
            }
        }
        draw(svgroot, root);
        const titleEl = document.getElementById('title');
        // 获取文本
        function getTitle(target) {
            const name = target.getAttribute('data-name');
            if(target.parentNode && target.parentNode.nodeName === 'g') {
                const parentName = target.parentNode.getAttribute('data-name');
                return `${parentName}-${name}`;
            }
            return name;
        }
        // 给svg添加mousemove事件
        let activeTarget = null;
        svgroot.addEventListener('mousemove', (evt) => {
            let target = evt.target;
            // 如果是文本节点
            if(target.nodeName === 'text') {
                // previousSibling 返回当前节点的前一个兄弟节点,没有则返回null.
                // 因为这里的text前一个节点就是circle
                target = target.previousSibling;
            };
            // 判断是否是已经激活的
            if(activeTarget !== target) {
                if(activeTarget) {
                    activeTarget.setAttribute('fill', 'rgba(0, 0, 0, 0.2)')
                };
            }
            target.setAttribute('fill', 'rgba(0, 128, 0, 0.1)');
            activeTarget = target;
            titleEl.textContent = getTitle(target);
        });
    </script>
</body>
</html>


b4ffedd949c946899b175a581c794eb2.png



SVG 和 Canvas 的不同点

  1. 写法上的不同
  2. 用户交互实现上的不同



绘制大量几何图形时 SVG 的性能问题

可以使用虚拟 DOM 方案来尽可能地减少重绘,这样就可以优化 SVG 的渲染。但是这些方案只能解决一部分问题,当节点数太多时,这些方案也无能为力。这个时候,我们还是得依靠 Canvas 和 WebGL 来绘图,才能彻底解决问题。





目录
相关文章
echarts修改tooltip默认样式(使用formatter函数拼接加工)
echarts修改tooltip默认样式(使用formatter函数拼接加工)
1335 0
|
4月前
|
机器学习/深度学习 数据可视化 算法
基于YOLOv8的无人机道路损伤检测[四类核心裂缝/坑洼识别]的识别项目|完整源码数据集+PyQt5界面+完整训练流程+开箱即用!
基于YOLOv8的无人机道路损伤检测系统,精准识别鳄鱼纹、纵向/横向裂缝及坑洼四类病害。集成PyQt5可视化界面,支持图片、视频、实时摄像头多模式检测,提供完整源码、数据集、预训练权重与部署教程,开箱即用,助力智慧交通巡检与养护决策。
基于YOLOv8的无人机道路损伤检测[四类核心裂缝/坑洼识别]的识别项目|完整源码数据集+PyQt5界面+完整训练流程+开箱即用!
|
Java API Maven
SLF4J
【7月更文挑战第3天】
398 6
|
并行计算 API 异构计算
JAX 中文文档(十六)(2)
JAX 中文文档(十六)
856 1
|
Android开发 Kotlin
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.10 and higher
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.10 and higher
466 3
|
机器学习/深度学习 人工智能 自然语言处理
claude3.5官网入口 - Claude3.5 Sonnet国内使用教程
在这个信息爆炸的时代 💥,我们每天都被海量的数据和信息包围 🌊。如何高效地处理信息、获取知识、提升效率,成为了现代人面临的巨大挑战 💪
SpringMVC拦截器的介绍,拦截器的基本实现,拦截器链配置
SpringMVC拦截器的介绍,拦截器的基本实现,拦截器链配置
507 2
|
安全 网络协议 Shell
永恒之蓝(MS17-010)漏洞利用
永恒之蓝(MS17-010)漏洞利用
1485 1
|
人工智能 监控 Java
IDEA启动tomcat 端口1099被占用(1099 is already in use)
IDEA启动tomcat 端口1099被占用(1099 is already in use)
IDEA启动tomcat 端口1099被占用(1099 is already in use)
06Echarts - 折线图(Stacked area chart)
06Echarts - 折线图(Stacked area chart)
444 0

热门文章

最新文章