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

推荐订阅源

I
Intezer
宝玉的分享
宝玉的分享
V
Visual Studio Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
博客园 - Franky
J
Java Code Geeks
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
D
Docker
IT之家
IT之家
小众软件
小众软件
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
N
News | PayPal Newsroom
量子位
The Last Watchdog
The Last Watchdog
The Register - Security
The Register - Security
PCI Perspectives
PCI Perspectives
罗磊的独立博客
S
Secure Thoughts
WordPress大学
WordPress大学
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
B
Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
博客园 - 叶小钗
U
Unit 42
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Help Net Security
Cloudbric
Cloudbric
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
人人都是产品经理
人人都是产品经理
H
Heimdal Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
Jina AI
Jina AI
O
OpenAI News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
美团技术团队

寒士杰克

想不到未来的一点光明 - 寒士杰克 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"
}