列矩行分面

源码
/**
 * A recreation of one of these demos: https://observablehq.com/@observablehq/plot-facets?collection=@observablehq/plot
 */
import { Chart } from '@antv/g2';
import { groupSort, median } from 'd3-array';

fetch('https://assets.antv.antgroup.com/g2/barley.json')
  .then((res) => res.json())
  .then((data) => {
    const chart = new Chart({
      container: 'container',
      height: 800,
      paddingLeft: 140,
      paddingRight: 130,
      paddingBottom: 60,
    });

    chart.options({
      type: 'facetRect',
      data: data,
      encode: { y: 'site' },
      scale: {
        y: {
          domain: groupSort(
            data,
            (g) => -median(g, (d) => d.yield),
            (d) => d.site,
          ),
        },
      },
      children: [
        {
          type: 'point',
          insetLeft: 5,
          insetRight: 5,
          scale: {
            color: { type: 'ordinal' },
            y: {
              domain: groupSort(
                data,
                (g) => -median(g, (d) => d.yield),
                (d) => d.variety,
              ),
            },
          },
          encode: { x: 'yield', y: 'variety', color: 'year', shape: 'hollow' },
          axis: { y: { labelAutoRotate: false } },
        },
      ],
    });

    chart.render();
  });
全站搜索
文档与示例输入关键词开始搜索

输入关键词,查找文档、API 与示例

选择打开Esc关闭