首页
/ Vega-Lite 可视化入门教程:从数据到交互式图表

Vega-Lite 可视化入门教程:从数据到交互式图表

2025-07-08 04:54:19作者:尤辰城Agatha

前言

Vega-Lite 是一个高级可视化语法,它建立在 Vega 之上,能够帮助开发者通过简洁的 JSON 语法快速创建各种统计图表。本文将带你从零开始学习如何使用 Vega-Lite 创建可视化图表,并最终将其嵌入到网页中。

基础概念

1. 数据准备

Vega-Lite 支持多种数据格式,最常用的是 JSON 数组格式。每个数据点都是一个对象,属性对应数据字段:

[
  {"category": "A", "value": 28},
  {"category": "B", "value": 55},
  {"category": "C", "value": 43}
]

在 Vega-Lite 规范中,数据通过 data 属性指定:

{
  "data": {
    "values": [
      {"category": "A", "value": 28},
      {"category": "B", "value": 55},
      {"category": "C", "value": 43}
    ]
  }
}

2. 标记(Mark)与编码(Encoding)

标记定义了图表的基本图形元素,常见的有:

  • point:点图
  • bar:条形图
  • line:折线图
  • area:面积图

编码则定义了如何将数据映射到视觉属性:

{
  "mark": "bar",
  "encoding": {
    "x": {"field": "category", "type": "nominal"},
    "y": {"field": "value", "type": "quantitative"}
  }
}

这里我们创建了一个条形图,x轴显示分类数据,y轴显示数值数据。

完整示例解析

让我们通过一个完整示例来理解 Vega-Lite 的核心概念:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"product": "A", "sales": 28},
      {"product": "B", "sales": 55},
      {"product": "C", "sales": 43}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "product", 
      "type": "nominal",
      "axis": {"title": "产品类别"}
    },
    "y": {
      "field": "sales", 
      "type": "quantitative",
      "axis": {"title": "销售额"},
      "aggregate": "sum"
    },
    "color": {"value": "#4682b4"}
  },
  "width": 400,
  "height": 300
}

这个规范会生成一个蓝色的条形图,显示各产品的销售总额。

数据聚合

Vega-Lite 支持多种聚合操作,直接在编码中指定:

"y": {
  "field": "sales",
  "type": "quantitative",
  "aggregate": "average"
}

常用聚合函数包括:

  • sum:求和
  • average:平均值
  • median:中位数
  • min/max:最小值/最大值
  • count:计数

图表定制

Vega-Lite 提供了丰富的定制选项:

1. 坐标轴定制

"x": {
  "field": "date",
  "type": "temporal",
  "axis": {
    "title": "日期",
    "titleFontSize": 14,
    "labelAngle": 45,
    "format": "%Y-%m"
  }
}

2. 颜色定制

"color": {
  "field": "category",
  "type": "nominal",
  "scale": {
    "range": ["#1f77b4", "#ff7f0e", "#2ca02c"]
  },
  "legend": {
    "title": "产品类别",
    "orient": "right"
  }
}

3. 交互功能

Vega-Lite 支持通过 selection 添加交互:

"selection": {
  "brush": {
    "type": "interval",
    "encodings": ["x"]
  }
},
"mark": "bar",
"encoding": {
  "opacity": {
    "condition": {"selection": "brush", "value": 1},
    "value": 0.3
  }
}

网页集成

将 Vega-Lite 图表嵌入网页非常简单:

<!DOCTYPE html>
<html>
<head>
  <title>Vega-Lite 示例</title>
  <script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
  <script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
  <script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
</head>
<body>
  <div id="vis"></div>
  <script>
    const spec = {
      // 你的Vega-Lite规范
    };
    vegaEmbed('#vis', spec);
  </script>
</body>
</html>

最佳实践

  1. 数据预处理:尽量在Vega-Lite外完成复杂的数据处理
  2. 模块化设计:将复杂图表分解为多个层(layer)
  3. 响应式设计:使用 autosize 属性使图表适应容器
  4. 性能优化:对于大数据集,考虑使用 binsample 进行数据抽样

进阶学习

掌握基础后,可以探索Vega-Lite的更多高级功能:

  • 复合图表:层叠(layer)、拼接(concat)、分面(facet)
  • 自定义计算字段
  • 高级交互设计
  • 主题定制

Vega-Lite 的强大之处在于它能够通过简洁的语法表达复杂的数据可视化需求,是数据分析和可视化展示的理想工具。