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

颜色图例 #4600

Closed
5 tasks
pearmini opened this issue Jan 28, 2023 · 5 comments · Fixed by #4607
Closed
5 tasks

颜色图例 #4600

pearmini opened this issue Jan 28, 2023 · 5 comments · Fixed by #4607
Assignees
Labels

Comments

@pearmini
Copy link
Member

pearmini commented Jan 28, 2023

颜色图例

目前 G2 5.0 中颜色通道只有当对应比例尺是 oridnal 比例尺的时候,才如下展现了图例。

image

期望当为如下比例尺的时候,也能展现对应的图例。

  • continuous: linear, log, pow, sqrt
  • threshold
  • quantize
  • quantile
  • sequential

实现思路

实现上参考 d3 的 color legend,如果没有更好的设计,可以完全或者尽量保持一致

image

接下来的案例只是一个说明,不用遵循。

案例

Linear

image

import { Chart } from '@antv/g2';

const chart = new Chart({
  container: 'container',
  width: 900,
  height: 300,
});

chart
  .cell()
  .data({
    type: 'fetch',
    value:
      'https://gw.alipayobjects.com/os/bmw-prod/89c20fe8-0c6f-46c8-b36b-4cb653dba8ed.json',
    transform: [{ type: 'map', callback: (d) => ({ salary: d }) }],
  })
  .scale('color', {
    type: 'linear',
    range: ['#eee', 'red'],
  })
  .encode('y', (_, i) => (i % 5) + 1)
  .encode('x', (_, i) => ((i / 5) | 0) + 1)
  .encode('color', 'salary')
  .style('stroke', '#000')
  .style('inset', 2);

chart.render();

threshold

image

参考 sparrow

import { Chart } from '@antv/g2';

const chart = new Chart({
  container: 'container',
  width: 900,
  height: 300,
});

chart
  .cell()
  .data({
    type: 'fetch',
    value:
      'https://gw.alipayobjects.com/os/bmw-prod/89c20fe8-0c6f-46c8-b36b-4cb653dba8ed.json',
    transform: [{ type: 'map', callback: (d) => ({ salary: d }) }],
  })
  .scale('color', {
    type: 'threshold',
    domain: [10000, 100000],
    range: ['#eee', 'pink', 'red'],
  })
  .encode('y', (_, i) => (i % 5) + 1)
  .encode('x', (_, i) => ((i / 5) | 0) + 1)
  .encode('color', 'salary')
  .style('stroke', '#000')
  .style('inset', 2);

chart.render();

quantize

image

参考 sparrow

import { Chart } from '@antv/g2';

const chart = new Chart({
  container: 'container',
  width: 900,
  height: 300,
});

chart
  .cell()
  .data({
    type: 'fetch',
    value:
      'https://gw.alipayobjects.com/os/bmw-prod/89c20fe8-0c6f-46c8-b36b-4cb653dba8ed.json',
    transform: [{ type: 'map', callback: (d) => ({ salary: d }) }],
  })
  .scale('color', { type: 'quantize', range: ['#eee', 'pink', 'red'] })
  .encode('y', (_, i) => (i % 5) + 1)
  .encode('x', (_, i) => ((i / 5) | 0) + 1)
  .encode('color', 'salary')
  .style('stroke', '#000')
  .style('inset', 2);

chart.render();

quantile

image

参考 sparrow

import { Chart } from '@antv/g2';

const chart = new Chart({
  container: 'container',
  width: 900,
  height: 300,
});

chart
  .cell()
  .data({
    type: 'fetch',
    value:
      'https://gw.alipayobjects.com/os/bmw-prod/89c20fe8-0c6f-46c8-b36b-4cb653dba8ed.json',
    transform: [{ type: 'map', callback: (d) => ({ salary: d }) }],
  })
  .scale('color', { type: 'quantile', range: ['#eee', 'pink', 'red'] })
  .encode('y', (_, i) => (i % 5) + 1)
  .encode('x', (_, i) => ((i / 5) | 0) + 1)
  .encode('color', 'salary')
  .style('stroke', '#000')
  .style('inset', 2);

chart.render();

sequential

image

export function settleWeatherCellGrouped(): G2Spec {
  return {
    type: 'cell',
    height: 300,
    data: {
      type: 'fetch',
      value: 'data/seattle-weather.csv',
    },
    transform: [{ type: 'group', color: 'max' }],
    encode: {
      x: (d) => new Date(d.date).getUTCDate(),
      y: (d) => new Date(d.date).getUTCMonth(),
      color: 'temp_max',
    },
    style: {
      inset: 0.5,
    },
    scale: {
      color: {
        palette: 'gnBu',
      },
    },
  };
}

TODO

对于上述的例子,分别创建一个集成测试去测试:如果是 API 改成 Spec 的形式,同时把远程的数据保存在本地;否者直接测试。

  • continuous: linear, log, pow, sqrt
  • threshold
  • quantize
  • quantile
  • sequential
@pearmini pearmini changed the title 颜色比例尺的图例展示 颜色图例 Jan 28, 2023
@Aarebecca
Copy link
Contributor

重合问题 GUI 这边基本上解决了,样式上的话,像这种刻度线必要性大吗,如果要做的话连续图例需要稍微调整一下
image

@pearmini
Copy link
Member Author

重合问题 GUI 这边基本上解决了,样式上的话,像这种刻度线必要性大吗,如果要做的话连续图例需要稍微调整一下 image

我感觉还是有必要的,就像坐标轴一下不仅仅展示首位的刻度。这样更容易感知不同颜色对应的数据。

@Aarebecca
Copy link
Contributor

重合问题 GUI 这边基本上解决了,样式上的话,像这种刻度线必要性大吗,如果要做的话连续图例需要稍微调整一下 image

我感觉还是有必要的,就像坐标轴一下不仅仅展示首位的刻度。这样更容易感知不同颜色对应的数据。

OK, 那 GUI 这边我调整下

@Aarebecca
Copy link
Contributor

Threshold 的图例稍微有点问题,domain: [10000, 100000],我理解是划分为 小于 10000, 10000~100000, 大于 100000 三个区间吧。所以绘制出来应该是这样:
image

@pearmini
Copy link
Member Author

Threshold 的图例稍微有点问题,domain: [10000, 100000],我理解是划分为 小于 10000, 10000~100000, 大于 100000 三个区间吧。所以绘制出来应该是这样: image

嗯嗯,是的,sparrow 里面的那个例子确实有问题

@pearmini pearmini linked a pull request Feb 1, 2023 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants