Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(static-mark): add chart.legends #5209

Merged
merged 1 commit into from
Jun 20, 2023
Merged

feat(static-mark): add chart.legends #5209

merged 1 commit into from
Jun 20, 2023

Conversation

pearmini
Copy link
Member

Legends

可以通过 chart.legends(options) 添加一个或者多个图例到画布中。

开始使用

export function mockLegendColor(): G2Spec {
  return {
    type: 'legends',
    padding: 'auto',
    height: 100,
    scale: {
      color: {
        type: 'ordinal',
        domain: ['a', 'b'],
        range: ['steelblue', 'orange'],
      },
    },
  };
}

多个图例

export function mockLegendColorSize(): G2Spec {
  return {
    type: 'legends',
    padding: 'auto',
    height: 100,
    scale: {
      color: {
        type: 'ordinal',
        domain: ['a', 'b'],
        range: ['steelblue', 'orange'],
      },
      size: {
        type: 'linear',
        domain: [0, 10],
        range: [0, 100],
      },
    },
  };
}

多轴图的图例

export function weatherLineMultiAxesLegend(): G2Spec {
  const labelByValue = {
    '#EE6666': 'Temperature',
    '#5470C6': 'Evaporation',
    '#91CC75': 'Precipitation',
  };
  const markerByValue = {
    '#EE6666': 'line',
    '#5470C6': 'rect',
    '#91CC75': 'line',
  };
  return {
    type: 'view',
    data: weather,
    children: [
      // ...
      // 添加新的图例
      {
        type: 'legends',
        scale: {
          color: {
            type: 'ordinal',
            independent: true,
            domain: ['#EE6666', '#91CC75', '#5470C6'],
            range: ['#EE6666', '#91CC75', '#5470C6'],
          },
        },
        itemMarker: (d) => markerByValue[d],
        labelFormatter: (d) => labelByValue[d],
      },
    ],
  };
}

@pearmini pearmini force-pushed the feat/static-mark-legend branch 2 times, most recently from f6f98cd to 222129a Compare June 20, 2023 06:33
Copy link
Contributor

@Aarebecca Aarebecca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以有简单一些的使用方式吗,比如不依赖比例尺进行创建

@pearmini
Copy link
Member Author

可以有简单一些的使用方式吗,比如不依赖比例尺进行创建

主要有以下考虑:

  • chart.legends 本生就是 color,size 等通道的可视化。
  • 通过比例尺创建的图例才能响应交互。
  • chart.axisX 以及 chart.axisY 使用保持一致。

@pearmini pearmini merged commit e6fcfe3 into v5 Jun 20, 2023
@pearmini pearmini deleted the feat/static-mark-legend branch June 20, 2023 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants