蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

折线和饼图共享并联动:独立实例

JAVASCRIPTECharts6.1.0 / Edge156.0.4314.58 / 自有同源数据与本地HTTP服务;实际程序化指针和动作echarts-dataset.js
echarts-dataset.js759 行
import * as echarts from 'echarts';
import {mount} from './lab.js';

// 本章独立配置和实际运行共用;数值均为自有虚构数据。
const specification = {
  "key": "echarts-dataset",
  "title": "ECharts dataset:行列、encode及图表联动",
  "cases": [
    {
      "id": "dataset-matrix",
      "title": "二维数组三系列",
      "option": {
        "animation": false,
        "legend": {
          "top": 10
        },
        "tooltip": {},
        "dataset": {
          "source": [
            [
              "课程",
              "第1期",
              "第2期",
              "第3期"
            ],
            [
              "HTML",
              12,
              24,
              18
            ],
            [
              "CSS",
              16,
              22,
              29
            ],
            [
              "JavaScript",
              21,
              17,
              34
            ],
            [
              "Web项目",
              15,
              26,
              32
            ]
          ]
        },
        "grid": {
          "top": 65,
          "bottom": 70,
          "containLabel": true
        },
        "xAxis": {
          "type": "category"
        },
        "yAxis": {
          "type": "value"
        },
        "series": [
          {
            "id": "s0",
            "type": "bar"
          },
          {
            "id": "s1",
            "type": "bar"
          },
          {
            "id": "s2",
            "type": "bar"
          }
        ]
      }
    },
    {
      "id": "dataset-objects",
      "title": "同数据对象数组",
      "option": {
        "animation": false,
        "legend": {
          "top": 10
        },
        "tooltip": {},
        "dataset": {
          "source": [
            {
              "课程": "HTML",
              "第1期": 12,
              "第2期": 24,
              "第3期": 18
            },
            {
              "课程": "CSS",
              "第1期": 16,
              "第2期": 22,
              "第3期": 29
            },
            {
              "课程": "JavaScript",
              "第1期": 21,
              "第2期": 17,
              "第3期": 34
            },
            {
              "课程": "Web项目",
              "第1期": 15,
              "第2期": 26,
              "第3期": 32
            }
          ],
          "dimensions": [
            "课程",
            "第1期",
            "第2期",
            "第3期"
          ]
        },
        "grid": {
          "top": 65,
          "bottom": 70,
          "containLabel": true
        },
        "xAxis": {
          "type": "category"
        },
        "yAxis": {
          "type": "value"
        },
        "series": [
          {
            "id": "s0",
            "type": "bar"
          },
          {
            "id": "s1",
            "type": "bar"
          },
          {
            "id": "s2",
            "type": "bar"
          }
        ]
      }
    },
    {
      "id": "dataset-row-column",
      "title": "双网格行列对照",
      "option": {
        "animation": false,
        "legend": {
          "top": 10
        },
        "tooltip": {},
        "dataset": {
          "source": [
            [
              "课程",
              "第1期",
              "第2期",
              "第3期",
              "第4期"
            ],
            [
              "HTML",
              12,
              24,
              18,
              31
            ],
            [
              "CSS",
              16,
              22,
              29,
              27
            ],
            [
              "JavaScript",
              21,
              17,
              34,
              30
            ]
          ]
        },
        "grid": [
          {
            "top": 60,
            "bottom": "55%",
            "containLabel": true
          },
          {
            "top": "60%",
            "bottom": 40,
            "containLabel": true
          }
        ],
        "xAxis": [
          {
            "type": "category",
            "gridIndex": 0
          },
          {
            "type": "category",
            "gridIndex": 1
          }
        ],
        "yAxis": [
          {
            "gridIndex": 0
          },
          {
            "gridIndex": 1
          }
        ],
        "series": [
          {
            "type": "bar",
            "seriesLayoutBy": "row",
            "xAxisIndex": 0,
            "yAxisIndex": 0
          },
          {
            "type": "bar",
            "seriesLayoutBy": "row",
            "xAxisIndex": 0,
            "yAxisIndex": 0
          },
          {
            "type": "bar",
            "seriesLayoutBy": "row",
            "xAxisIndex": 0,
            "yAxisIndex": 0
          },
          {
            "type": "bar",
            "xAxisIndex": 1,
            "yAxisIndex": 1
          },
          {
            "type": "bar",
            "xAxisIndex": 1,
            "yAxisIndex": 1
          },
          {
            "type": "bar",
            "xAxisIndex": 1,
            "yAxisIndex": 1
          },
          {
            "type": "bar",
            "xAxisIndex": 1,
            "yAxisIndex": 1
          }
        ]
      }
    },
    {
      "id": "dataset-encode",
      "title": "显式水平编码",
      "option": {
        "animation": false,
        "dataset": {
          "source": [
            [
              "score",
              "amount",
              "product"
            ],
            [
              20,
              120,
              "阅读"
            ],
            [
              50,
              240,
              "实作"
            ],
            [
              80,
              180,
              "复习"
            ],
            [
              95,
              310,
              "调试"
            ],
            [
              10,
              220,
              "整理"
            ],
            [
              65,
              270,
              "回顾"
            ],
            [
              35,
              160,
              "表达"
            ],
            [
              72,
              200,
              "设计"
            ],
            [
              88,
              260,
              "验证"
            ]
          ]
        },
        "grid": {
          "left": 90,
          "right": 40,
          "top": 55,
          "bottom": 85
        },
        "xAxis": {
          "type": "value",
          "name": "amount"
        },
        "yAxis": {
          "type": "category"
        },
        "series": [
          {
            "type": "bar",
            "encode": {
              "x": "amount",
              "y": "product",
              "tooltip": [
                "product",
                "score",
                "amount"
              ]
            }
          }
        ]
      }
    },
    {
      "id": "dataset-four",
      "title": "同一五维数据的四视图",
      "option": {
        "animation": false,
        "dataset": {
          "dimensions": [
            "Income",
            "Life Expectancy",
            "Population",
            "Country",
            {
              "name": "Year",
              "type": "ordinal"
            }
          ],
          "source": [
            [
              "Income",
              "Life Expectancy",
              "Population",
              "Country",
              "Year"
            ],
            [
              10,
              60,
              20,
              "地区甲",
              2018
            ],
            [
              20,
              65,
              35,
              "地区乙",
              2019
            ],
            [
              30,
              70,
              15,
              "地区丙",
              2020
            ],
            [
              40,
              75,
              40,
              "地区丁",
              2021
            ]
          ]
        },
        "tooltip": {},
        "grid": [
          {
            "left": 55,
            "right": "55%",
            "top": 45,
            "bottom": "55%"
          },
          {
            "left": "60%",
            "right": 35,
            "top": 45,
            "bottom": "55%"
          },
          {
            "left": 55,
            "right": "55%",
            "top": "60%",
            "bottom": 45
          },
          {
            "left": "60%",
            "right": 35,
            "top": "60%",
            "bottom": 45
          }
        ],
        "xAxis": [
          {
            "type": "value",
            "gridIndex": 0
          },
          {
            "type": "category",
            "gridIndex": 1
          },
          {
            "type": "value",
            "gridIndex": 2
          },
          {
            "type": "value",
            "gridIndex": 3
          }
        ],
        "yAxis": [
          {
            "type": "value",
            "gridIndex": 0
          },
          {
            "type": "value",
            "gridIndex": 1
          },
          {
            "type": "value",
            "gridIndex": 2
          },
          {
            "type": "value",
            "gridIndex": 3
          }
        ],
        "series": [
          {
            "type": "scatter",
            "symbolSize": 9,
            "xAxisIndex": 0,
            "yAxisIndex": 0,
            "encode": {
              "x": "Income",
              "y": "Life Expectancy",
              "tooltip": [
                0,
                1,
                2,
                3,
                4
              ]
            }
          },
          {
            "type": "scatter",
            "symbolSize": 9,
            "xAxisIndex": 1,
            "yAxisIndex": 1,
            "encode": {
              "x": "Country",
              "y": "Income",
              "tooltip": [
                0,
                1,
                2,
                3,
                4
              ]
            }
          },
          {
            "type": "scatter",
            "symbolSize": 9,
            "xAxisIndex": 2,
            "yAxisIndex": 2,
            "encode": {
              "x": "Income",
              "y": "Population",
              "tooltip": [
                0,
                1,
                2,
                3,
                4
              ]
            }
          },
          {
            "type": "scatter",
            "symbolSize": 9,
            "xAxisIndex": 3,
            "yAxisIndex": 3,
            "encode": {
              "x": "Life Expectancy",
              "y": "Population",
              "tooltip": [
                0,
                1,
                2,
                3,
                4
              ]
            }
          }
        ]
      }
    },
    {
      "id": "dataset-color",
      "title": "score颜色与amount坐标分开",
      "option": {
        "animation": false,
        "dataset": {
          "source": [
            [
              "score",
              "amount",
              "product"
            ],
            [
              20,
              120,
              "阅读"
            ],
            [
              50,
              240,
              "实作"
            ],
            [
              80,
              180,
              "复习"
            ],
            [
              95,
              310,
              "调试"
            ],
            [
              10,
              220,
              "整理"
            ],
            [
              65,
              270,
              "回顾"
            ],
            [
              35,
              160,
              "表达"
            ],
            [
              72,
              200,
              "设计"
            ],
            [
              88,
              260,
              "验证"
            ]
          ]
        },
        "grid": {
          "left": 90,
          "right": 40,
          "top": 55,
          "bottom": 85
        },
        "xAxis": {
          "type": "value",
          "name": "amount"
        },
        "yAxis": {
          "type": "category"
        },
        "series": [
          {
            "type": "bar",
            "encode": {
              "x": "amount",
              "y": "product",
              "tooltip": [
                "product",
                "score",
                "amount"
              ]
            }
          }
        ],
        "visualMap": {
          "type": "continuous",
          "min": 0,
          "max": 100,
          "dimension": 0,
          "orient": "horizontal",
          "bottom": 5,
          "inRange": {
            "color": [
              "#d7da8b",
              "#e15457"
            ]
          }
        }
      }
    },
    {
      "id": "dataset-linked",
      "title": "折线和饼图共享并联动",
      "option": {
        "animation": false,
        "legend": {
          "bottom": 0
        },
        "tooltip": {
          "trigger": "axis",
          "showContent": false
        },
        "dataset": {
          "source": [
            [
              "课程",
              "第1期",
              "第2期",
              "第3期",
              "第4期",
              "第5期",
              "第6期"
            ],
            [
              "HTML",
              12,
              24,
              18,
              31,
              33,
              35
            ],
            [
              "CSS",
              16,
              22,
              29,
              27,
              29,
              31
            ],
            [
              "JavaScript",
              21,
              17,
              34,
              30,
              32,
              34
            ],
            [
              "Web项目",
              15,
              26,
              32,
              38,
              40,
              42
            ]
          ]
        },
        "xAxis": {
          "type": "category"
        },
        "yAxis": {},
        "grid": {
          "top": "55%",
          "bottom": 70
        },
        "series": [
          {
            "id": "course-0",
            "type": "line",
            "smooth": true,
            "seriesLayoutBy": "row"
          },
          {
            "id": "course-1",
            "type": "line",
            "smooth": true,
            "seriesLayoutBy": "row"
          },
          {
            "id": "course-2",
            "type": "line",
            "smooth": true,
            "seriesLayoutBy": "row"
          },
          {
            "id": "course-3",
            "type": "line",
            "smooth": true,
            "seriesLayoutBy": "row"
          },
          {
            "id": "linked-pie",
            "type": "pie",
            "radius": "25%",
            "center": [
              "50%",
              "25%"
            ],
            "label": {
              "formatter": "{b}: {@第1期} ({d}%)"
            },
            "encode": {
              "itemName": "课程",
              "value": "第1期",
              "tooltip": "第1期"
            }
          }
        ]
      },
      "behavior": "linked"
    }
  ]
};
export default async function run(){await mount(echarts,specification);}
本例需在 ECharts6.1.0 / Edge156.0.4314.58 / 自有同源数据与本地HTTP服务;实际程序化指针和动作 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:ECharts6.1.0 / Edge156.0.4314.58 / 自有同源数据与本地HTTP服务;实际程序化指针和动作

入口文件:projects/echarts-lab/src/echarts-dataset.js

预期结果与验证条件观察提示
  • 对应变体真实运行;其数据、图元、事件或状态独立记录

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:ECharts

课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202611a498.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习