惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Hacker News: Ask HN
Hacker News: Ask HN
The Hacker News
The Hacker News
S
Security Affairs
T
Tor Project blog
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
P
Palo Alto Networks Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
W
WeLiveSecurity
Y
Y Combinator Blog
TaoSecurity Blog
TaoSecurity Blog
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
大猫的无限游戏
大猫的无限游戏
Blog — PlanetScale
Blog — PlanetScale
G
GRAHAM CLULEY
博客园 - 聂微东
月光博客
月光博客
T
The Exploit Database - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
Latest news
Latest news
H
Help Net Security
U
Unit 42
P
Privacy & Cybersecurity Law Blog
The GitHub Blog
The GitHub Blog
S
Securelist
The Last Watchdog
The Last Watchdog
Stack Overflow Blog
Stack Overflow Blog
H
Hacker News: Front Page
C
Check Point Blog
Schneier on Security
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
Arctic Wolf
P
Privacy International News Feed
WordPress大学
WordPress大学
T
Threatpost
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Secure Thoughts
Simon Willison's Weblog
Simon Willison's Weblog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI

寒士杰克

想不到未来的一点光明 - 寒士杰克 2-模组类型和modinfo - 寒士杰克 1-初识模组 - 寒士杰克 开发日志:Discourse的开发网络 - 寒士杰克 欢迎关注,博主开了一个公众号 - 寒士杰克 当一个游戏没有社区:我如何从0搭建Vintage Story中文论坛 - 寒士杰克 Typecho插件:增强渲染MarkdownPlus - 寒士杰克 phpBB扩展:数据库文档 - 寒士杰克 为什么考研? - 寒士杰克 本站入驻爱发电! - 寒士杰克 复古物语(Vintage Story)中国玩家社区已经开放! - 寒士杰克 AIP:我的世界AI机器人 - 寒士杰克 我的Python学习记录-陆续更新 - 寒士杰克 考研压力是有的,但也不是很大 - 寒士杰克 人成熟的过程,是承认孤独的过程 - 寒士杰克 AI声音克隆助力思政作业 - 寒士杰克 用Typecho备份文件构建博客专属字库 - 寒士杰克 在Typecho中添加Excalidraw Markdown - 寒士杰克 如何按需加载 Markdown 渲染资源 - 寒士杰克 假睡的代价:三年期的体检 - 寒士杰克 如何给 Typecho 支持额外 Markdown 渲染 - 寒士杰克 我的博客又又又更新了! - 寒士杰克 Vue3Admin: 一款必备的Typecho后台面板插件 - 寒士杰克 我并联了家中的双路由网络 - 寒士杰克
Markdown解析vega-lite图形 - 寒士杰克
HansJack · 2026-03-24 · via 寒士杰克

1. 示范渲染

Vega-lite 是一个基于 VEGA 的高级可视化库,旨在简化数据可视化的过程。它通过简单的 JSON 规范来描述数据与视觉元素之间的映射关系,支持多种图表类型,如条形图、散点图和折线图等。Vega-lite 的设计使得用户无需编写复杂的 JavaScript 代码即可创建交互式和美观的可视化效果,特别适合数据科学家、记者和研究人员使用。它还支持多种编程语言,方便集成到各种环境中,如 Web 页面和 Jupyter Notebook。

下面就是我最近实验课DSC结果,通过csv文件生成的图形。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "description": "Temperature vs DTG/DSC/TG (curves)",
  "width": 820,
  "height": 380,
  "data": {
    "url": "/usr/uploads/data/ExpDat_1_clean.csv",
    "format": {
      "type": "csv",
      "parse": {
        "Temp_C": "number",
        "DTG": "number",
        "DSC": "number",
        "TG": "number"
      }
    }
  },
  "encoding": {
    "x": {
      "field": "Temp_C",
      "type": "quantitative",
      "title": "Temperature (°C)"
    }
  },
  "layer": [
    {
      "mark": {
        "type": "line",
        "interpolate": "monotone",
        "color": "#e11d48",
        "strokeWidth": 2
      },
      "encoding": {
        "y": {
          "field": "DTG",
          "type": "quantitative",
          "title": "DTG (%/min)",
          "axis": {
            "orient": "left",
            "titleColor": "#e11d48",
            "labelColor": "#e11d48"
          }
        }
      }
    },
    {
      "mark": {
        "type": "line",
        "interpolate": "monotone",
        "color": "#2563eb",
        "strokeWidth": 2
      },
      "encoding": {
        "y": {
          "field": "DSC",
          "type": "quantitative",
          "title": "DSC (mW/mg)",
          "axis": {
            "orient": "right",
            "offset": 0,
            "titleColor": "#2563eb",
            "labelColor": "#2563eb"
          }
        }
      }
    },
    {
      "mark": {
        "type": "line",
        "interpolate": "monotone",
        "color": "#16a34a",
        "strokeWidth": 2
      },
      "encoding": {
        "y": {
          "field": "TG",
          "type": "quantitative",
          "title": "TG (%)",
          "axis": {
            "orient": "right",
            "offset": 58,
            "titleColor": "#16a34a",
            "labelColor": "#16a34a"
          }
        }
      }
    }
  ],
  "resolve": {
    "scale": {
      "y": "independent"
    }
  }
}

可以对比机器结果:
热分析机器结果

下面是实际的Markdown代码:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "description": "Temperature vs DTG/DSC/TG (curves)",
  "width": 820,
  "height": 380,
  "data": {
    "url": "/usr/uploads/data/ExpDat_1_clean.csv",
    "format": {
      "type": "csv",
      "parse": {
        "Temp_C": "number",
        "DTG": "number",
        "DSC": "number",
        "TG": "number"
      }
    }
  },
  "encoding": {
    "x": {
      "field": "Temp_C",
      "type": "quantitative",
      "title": "Temperature (°C)"
    }
  },
  "layer": [
    {
      "mark": {
        "type": "line",
        "interpolate": "monotone",
        "color": "#e11d48",
        "strokeWidth": 2
      },
      "encoding": {
        "y": {
          "field": "DTG",
          "type": "quantitative",
          "title": "DTG (%/min)",
          "axis": {
            "orient": "left",
            "titleColor": "#e11d48",
            "labelColor": "#e11d48"
          }
        }
      }
    },
    {
      "mark": {
        "type": "line",
        "interpolate": "monotone",
        "color": "#2563eb",
        "strokeWidth": 2
      },
      "encoding": {
        "y": {
          "field": "DSC",
          "type": "quantitative",
          "title": "DSC (mW/mg)",
          "axis": {
            "orient": "right",
            "offset": 0,
            "titleColor": "#2563eb",
            "labelColor": "#2563eb"
          }
        }
      }
    },
    {
      "mark": {
        "type": "line",
        "interpolate": "monotone",
        "color": "#16a34a",
        "strokeWidth": 2
      },
      "encoding": {
        "y": {
          "field": "TG",
          "type": "quantitative",
          "title": "TG (%)",
          "axis": {
            "orient": "right",
            "offset": 58,
            "titleColor": "#16a34a",
            "labelColor": "#16a34a"
          }
        }
      }
    }
  ],
  "resolve": {
    "scale": {
      "y": "independent"
    }
  }
}

2. 如何实现

  1. 短代码写法 embed
{% vega src="/usr/uploads/vega/sales.json" height=360 renderer=svg actions=off %}
  1. 代码块写法
{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "data": {"values":[{"x":"A","y":28},{"x":"B","y":55}]},
  "mark": "bar",
  "encoding": {
    "x": {"field":"x","type":"nominal"},
    "y": {"field":"y","type":"quantitative"}
  }
}
  1. 服务端实现
  • 识别 embed 语法:{% ... %}
  • 解析参数:支持 key=value、单双引号值。
  • vega 短代码渲染成占位节点:
<section class="vega-embed-shortcode"
  data-embed-type="vega"
  data-vega-src="/usr/uploads/vega/sales.json"
  data-vega-renderer="svg"
  data-vega-actions="false">
  <div class="vega-embed-stage" data-vega-stage data-vega-height="360"></div>
</section>
  1. 安全
  • src 必须是站内绝对路径
  • 禁止 //、..、反斜杠、控制字符、空白等

比如:

{% vega src="/uploads/vega/sales.json" height=360 renderer=svg actions=off %}

{
  "data": {
    "url": "https://evil.com/data.json"
  },
  "mark": "bar"
}