資料集

dataset 是專門用於管理資料的元件。儘管你可以在每個系列(series)的 series.data 中設定資料,但自 ECharts 4 起,我們推薦使用 dataset 來管理資料,這樣資料可以被多個元件複用,也便於實現“資料與配置分離”。畢竟,資料是最常變化的部分,而其他配置在執行時大多不會改變。

series 中定義資料

如果資料定義在 series 下,例如:

option = {
  xAxis: {
    type: 'category',
    data: ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie']
  },
  yAxis: {},
  series: [
    {
      type: 'bar',
      name: '2015',
      data: [89.3, 92.1, 94.4, 85.4]
    },
    {
      type: 'bar',
      name: '2016',
      data: [95.8, 89.4, 91.2, 76.9]
    },
    {
      type: 'bar',
      name: '2017',
      data: [97.7, 83.1, 92.5, 78.1]
    }
  ]
};
線上示例

series 下定義 data 適用於一些特殊資料結構(如“樹圖”、“關係圖”)和大資料量的定製。但是,這不利於多個系列共享資料,也不利於根據原始資料進行圖表型別和系列的對映編排。另一個缺點是,程式設計師總是需要先將資料劃分到不同的系列(和類別)中。

dataset 中定義資料

如果你在 dataset 中定義 data,有以下優點:

  • 遵循資料視覺化的思路:(I) 提供資料,(II) 將資料對映到視覺化元素,形成圖表。
  • 將資料與其他配置分離。資料經常變動,而其他配置則不然。這樣易於分開管理。
  • 資料可以被多個系列或元件複用,你不需要為每個系列建立大量資料的副本。
  • 支援更通用的資料格式,如二維陣列、物件陣列等,在一定程度上避免了使用者進行資料格式轉換。

這是一個簡單的 dataset 示例:

option = {
  legend: {},
  tooltip: {},
  dataset: {
    // Provide a set of data.
    source: [
      ['product', '2015', '2016', '2017'],
      ['Matcha Latte', 43.3, 85.8, 93.7],
      ['Milk Tea', 83.1, 73.4, 55.1],
      ['Cheese Cocoa', 86.4, 65.2, 82.5],
      ['Walnut Brownie', 72.4, 53.9, 39.1]
    ]
  },
  // Declare an x-axis (category axis).
  // The category map the first column in the dataset by default.
  xAxis: { type: 'category' },
  // Declare a y-axis (value axis).
  yAxis: {},
  // Declare several 'bar' series,
  // every series will auto-map to each column by default.
  series: [{ type: 'bar' }, { type: 'bar' }, { type: 'bar' }]
};
線上示例

或者嘗試使用“物件陣列”格式:

option = {
  legend: {},
  tooltip: {},
  dataset: {
    // Define the dimension of array. In cartesian coordinate system,
    // if the type of x-axis is category, map the first dimension to
    // x-axis by default, the second dimension to y-axis.
    // You can also specify 'series.encode' to complete the map
    // without specify dimensions. Please see below.

    dimensions: ['product', '2015', '2016', '2017'],
    source: [
      { product: 'Matcha Latte', '2015': 43.3, '2016': 85.8, '2017': 93.7 },
      { product: 'Milk Tea', '2015': 83.1, '2016': 73.4, '2017': 55.1 },
      { product: 'Cheese Cocoa', '2015': 86.4, '2016': 65.2, '2017': 82.5 },
      { product: 'Walnut Brownie', '2015': 72.4, '2016': 53.9, '2017': 39.1 }
    ]
  },
  xAxis: { type: 'category' },
  yAxis: {},
  series: [{ type: 'bar' }, { type: 'bar' }, { type: 'bar' }]
};
線上示例

從資料到圖表的對映

資料視覺化的思路是:(I) 提供資料,(II) 將資料對映到視覺化元素,形成圖表。

簡而言之,你可以設定這些對映配置:

  • 指定 dataset 的“列”或“行”來對映到 series。你可以使用 series.seriesLayoutBy 來配置。預設是按列對映。
  • 指定維度對映的規則:如何將“dataset”的維度對映到 axis(座標軸)、tooltip(提示框)、label(標籤)和 visualMap(視覺對映)。要配置對映,請使用 series.encodevisualMap。前面的例子沒有給出對映配置,所以 ECharts 會遵循預設規則:如果 x 軸是類目軸,則對映到 dataset.source 的第一行;三個系列的圖表則將 dataset.source 中的每一行逐一對映。

配置詳情如下所示:

dataset 的行或列對映到 series

有了 dataset,你就可以靈活地配置資料如何對映到座標軸和系列。

你可以使用 seriesLayoutBy 來改變圖表對行和列的理解。seriesLayoutBy 可以是:

  • 'column': 預設值。系列對應於 dataset 的列。
  • 'row': 系列對應於 dataset 的行。

檢視這個例子

option = {
  legend: {},
  tooltip: {},
  dataset: {
    source: [
      ['product', '2012', '2013', '2014', '2015'],
      ['Matcha Latte', 41.1, 30.4, 65.1, 53.3],
      ['Milk Tea', 86.5, 92.1, 85.7, 83.1],
      ['Cheese Cocoa', 24.1, 67.2, 79.5, 86.4]
    ]
  },
  xAxis: [
    { type: 'category', gridIndex: 0 },
    { type: 'category', gridIndex: 1 }
  ],
  yAxis: [{ gridIndex: 0 }, { gridIndex: 1 }],
  grid: [{ bottom: '55%' }, { top: '55%' }],
  series: [
    // These series will show in the first coordinate, each series map a row in dataset.
    { type: 'bar', seriesLayoutBy: 'row', xAxisIndex: 0, yAxisIndex: 0 },
    { type: 'bar', seriesLayoutBy: 'row', xAxisIndex: 0, yAxisIndex: 0 },
    { type: 'bar', seriesLayoutBy: 'row', xAxisIndex: 0, yAxisIndex: 0 },
    // These series will show in the second coordinate, each series map a column in dataset.
    { type: 'bar', seriesLayoutBy: 'column', xAxisIndex: 1, yAxisIndex: 1 },
    { type: 'bar', seriesLayoutBy: 'column', xAxisIndex: 1, yAxisIndex: 1 },
    { type: 'bar', seriesLayoutBy: 'column', xAxisIndex: 1, yAxisIndex: 1 },
    { type: 'bar', seriesLayoutBy: 'column', xAxisIndex: 1, yAxisIndex: 1 }
  ]
};
線上示例

配置效果如此示例所示。

維度(Dimension)

常用圖表中描述的資料大多是“二維表”結構,在前面的例子中,我們使用二維陣列來表示二維表。現在,當我們把一個系列對映到一個列時,該列被稱為一個“維度”,而每一行被稱為一個“資料項”,反之亦然。

維度可以有自己的名稱以便在圖表中顯示。維度名稱可以在第一列(行)中定義。在下一個示例中,'score''amount''product' 是維度的名稱。實際資料從第二行開始。ECharts 會自動檢查 dataset.source 的第一列(行)是否包含維度名稱。你也可以使用 dataset.sourceHeader: true 來宣告第一列(行)代表維度名稱。

嘗試使用單個 dataset.dimensions 或某些 series.dimensions 來定義維度,這樣你可以同時指定名稱和型別。

var option1 = {
  dataset: {
    dimensions: [
      { name: 'score' },
      // can be abbreviated as 'string', to indicate dimension name
      'amount',
      // Specify dimensions in 'type'.
      { name: 'product', type: 'ordinal' }
    ],
    source: []
  }
  // ...
};

var option2 = {
  dataset: {
    source: []
  },
  series: {
    type: 'line',
    // series.dimensions will cover the config in dataset.dimension
    dimensions: [
      null, // use null if you do not want dimension name.
      'amount',
      { name: 'product', type: 'ordinal' }
    ]
  }
  // ...
};

在大多數情況下,你不需要定義維度型別,因為 ECharts 會自動判斷。如果判斷不準確,你可以手動定義。

維度型別可以是以下值:

  • 'number': 預設值,普通資料。
  • 'ordinal': 字串型別的資料,如類別、文字,只有維度型別為 'ordinal' 時才能用於座標軸。ECharts 會嘗試自動判斷此型別,但可能不準確,所以你可以手動指定。
  • 'time': 用於表示時間資料,如果維度型別定義為 'time',ECharts 可以自動將資料解析為時間戳。例如,如果這個維度的資料是 '2017-05-10',ECharts 會自動解析。如果維度用作時間軸 (axis.type = 'time'),維度型別也會是 'time'。更多支援的時間型別請參見 data
  • 'float': 在 'float' 維度中使用 TypedArray 最佳化效能。
  • 'int': 在 'int' 維度中使用 TypedArray 最佳化效能。

從資料到圖表的對映 (series.encode)

理解了維度的概念後,你可以使用 series.encode 來進行對映:

var option = {
  dataset: {
    source: [
      ['score', 'amount', 'product'],
      [89.3, 58212, 'Matcha Latte'],
      [57.1, 78254, 'Milk Tea'],
      [74.4, 41032, 'Cheese Cocoa'],
      [50.1, 12755, 'Cheese Brownie'],
      [89.7, 20145, 'Matcha Cocoa'],
      [68.1, 79146, 'Tea'],
      [19.6, 91852, 'Orange Juice'],
      [10.6, 101852, 'Lemon Juice'],
      [32.7, 20112, 'Walnut Brownie']
    ]
  },
  xAxis: {},
  yAxis: { type: 'category' },
  series: [
    {
      type: 'bar',
      encode: {
        // Map "amount" column to x-axis.
        x: 'amount',
        // Map "product" row to y-axis.
        y: 'product'
      }
    }
  ]
};
線上示例

series.encode 宣告的基本結構:

  • 冒號左邊:座標軸或標籤的特定名稱。
  • 冒號右邊:維度名稱(字串)或數字(整數,從0開始計數),用於指定一個或多個維度(使用陣列)。

通常,以下資訊不是必須定義的。按需填寫。

series.encode 建議的屬性:

// Supported in every coordinate and series:
encode: {
  // Display the value of dimension named "product" and "score" in tooltip.
  tooltip: ['product', 'score']
  // Connect dimension name of "Dimension 1" and "Dimension 3" as the series name. (Avoid to repeat longer names in series.name)
  seriesName: [1, 3],
  // Means to use the value in "Dimension 2" as the id. It makes the new and old data correspond by id
	// when using setOption to update data, so that it can show animation properly.
  itemId: 2,
  // The itemName will show in the legend of Pie Charts.
  itemName: 3
}

// Grid/cartesian coordinate unique configs:
encode: {
  // Map "Dimension 1", "Dimension 5" and "dimension named 'score'" to x-axis:
  x: [1, 5, 'score'],
  // Map "Dimension 0" to y-axis:
  y: 0
}

// singleAxis unique configs:
encode: {
  single: 3
}

// Polar coordinate unique configs:
encode: {
  radius: 3,
  angle: 2
}

// Geo-coordinate unique configs:
encode: {
  lng: 3,
  lat: 2
}

// For some charts without coordinate like pie chart, funnel chart:
encode: {
  value: 3
}

這是一個更豐富的 series.encode 示例

預設的 series.encode

值得一提的是,如果沒有指定 series.encode,ECharts 會對一些常規圖表(如折線圖、柱狀圖、散點圖、K線圖等)使用一些預設的對映規則。預設規則是:

  • 在座標系中(如直角座標系、極座標系):
    • 如果存在類目軸 (axis.type = 'category'),則將第一列(行)對映到該軸,後續的每一列(行)依次對映到一個系列。
    • 如果兩個軸都不是類目軸,則將每兩列對映到一個系列,分別對應兩個座標軸。
  • 無座標系的圖表(如餅圖):
    • 使用第一列(行)作為名稱,第二列(行)作為值。如果只有一列(行),ECharts 不會設定名稱。

當預設規則無法滿足需求時,你可以自己配置 encode,這並不複雜。這是一個示例

一些常見的 series.encode 設定

問:如何將第3列設定為x軸,第5列設定為y軸?

答:

option = {
  series: {
    // dimensionIndex count from 0, so the 3rd line is dimensions[2].
    encode: { x: 2, y: 4 }
    // ...
  }
};

問:如何將第3行設定為x軸,第5行設定為y軸?

答:

option = {
  series: {
    encode: { x: 2, y: 4 },
    seriesLayoutBy: 'row'
    // ...
  }
};

問:如何將第2列設定為標籤?

答:我們現在支援透過 label.formatter 從特定維度取值。

series: {
  label: {
    // `'{@score}'` means the value in the dimension named "score".
    // `'{@[4]}'` means the value in dimension 4.
    formatter: 'aaa{@product}bbb{@score}ccc{@[4]}ddd';
  }
}

問:如何在提示框(tooltip)中顯示第2列和第3列?

答:

option = {
  series: {
    encode: {
      tooltip: [1, 2]
      // ...
    }
    // ...
  }
};

問:如果維度名稱沒有包含在資料集中,如何定義?

答:

var option = {
  dataset: {
    dimensions: ['score', 'amount'],
    source: [
      [89.3, 3371],
      [92.1, 8123],
      [94.4, 1954],
      [85.4, 829]
    ]
  }
};

問:如何將第3列對映到散點圖的大小?

答:

var option = {
  dataset: {
    source: [
      [12, 323, 11.2],
      [23, 167, 8.3],
      [81, 284, 12],
      [91, 413, 4.1],
      [13, 287, 13.5]
    ]
  },
  visualMap: {
    show: false,
    dimension: 2, // means the 3rd column
    min: 2, // lower bound
    max: 15, // higher bound
    inRange: {
      // Size of the bubble.
      symbolSize: [5, 60]
    }
  },
  xAxis: {},
  yAxis: {},
  series: {
    type: 'scatter'
  }
};
線上示例

問:我在 encode 中指定了對映,為什麼沒有生效?

答:檢查你的拼寫,例如,在 encode 中是否將維度名稱 'Life Expectancy' 錯拼成了 'Life Expectency'

視覺通道對映

我們可以使用 visualMap 來對映視覺通道。詳情請檢視 visualMap 文件。這是一個示例

圖表的資料格式

在大多數常規圖表中,資料適合用二維表的形式來描述。像“MS Excel”和“Numbers”這類著名軟體都使用二維表。它們的資料可以匯出為 JSON 格式並輸入到 dataset.source 中,從而避免一些資料處理步驟。

你可以使用像 dsvPapaParse 這樣的工具將 .csv 檔案轉換為 JSON。

如下面的例子所示,在 JavaScript 的資料傳輸中,二維資料可以直接用二維陣列儲存。

除了二維陣列,dataset 還支援使用鍵值對(key-value)的方式,這也是一種常見的方式。但是,我們目前在這種格式下不支援 seriesLayoutBy

dataset: [
  {
    // column by column key-value array is a normal format
    source: [
      { product: 'Matcha Latte', count: 823, score: 95.8 },
      { product: 'Milk Tea', count: 235, score: 81.4 },
      { product: 'Cheese Cocoa', count: 1042, score: 91.2 },
      { product: 'Walnut Brownie', count: 988, score: 76.9 }
    ]
  },
  {
    // row by row key-value
    source: {
      product: ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie'],
      count: [823, 235, 1042, 988],
      score: [95.8, 81.4, 91.2, 76.9]
    }
  }
];

如何引用多個數據集

ECharts 支援同時定義多個數據集。系列可以透過 series.datasetIndex 指定引用哪一個。例如:

var option = {
  dataset: [
    {
      // 1st Dataset
      source: []
    },
    {
      // 2nd Dataset
      source: []
    },
    {
      // 3rd Dataset
      source: []
    }
  ],
  series: [
    {
      // Use 2nd dataset
      datasetIndex: 1
    },
    {
      // Use 1st dataset
      datasetIndex: 0
    }
  ]
};

ECharts 3 中的 series.data

ECharts 4 仍然支援 ECharts 3 中的資料宣告方式。如果系列已經聲明瞭 series.data,那麼會使用 series.data 而不是 dataset

option = {
  xAxis: {
    type: 'category',
    data: ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie']
  },
  yAxis: {},
  series: [
    {
      type: 'bar',
      name: '2015',
      data: [89.3, 92.1, 94.4, 85.4]
    },
    {
      type: 'bar',
      name: '2016',
      data: [95.8, 89.4, 91.2, 76.9]
    },
    {
      type: 'bar',
      name: '2017',
      data: [97.7, 83.1, 92.5, 78.1]
    }
  ]
};

事實上,series.data 是一種重要的設定方法,它將一直存在。一些特殊的非表格格式圖表,如矩形樹圖關係圖路徑圖,仍然不能在 dataset 中編輯,你仍然需要使用 series.data。另一方面,對於渲染海量資料(超過一百萬),你需要使用 dataset 不支援的 appendData

其他

目前支援 dataset 的圖表有:line(折線圖)、bar(柱狀圖)、pie(餅圖)、scatter(散點圖)、effectScatter(漣漪散點圖)、parallel(平行座標系)、candlestick(K線圖)、map(地圖)、funnel(漏斗圖)、custom(自定義系列)。ECharts 將來會支援更多圖表。

最後,這裡是一個多個圖表共享一個 dataset 並進行聯動互動的示例

貢獻者 在 GitHub 上編輯此頁

plainheartpissangOvilia100pahHertz-HuBruce20190410YuanyeChisimonmcconnell