vue3+g2plot之旭日图

概述

旭日图(Sunburst Chart)是一种现代饼图,它超越传统的饼图和环图,能表达清晰的层级和归属关系,以父子层次结构来显示数据构成情况。旭日图中,离远点越近表示级别越高,相邻两层中,是内层包含外层的关系。

在实际项目中使用旭日图,可以更细分溯源分析数据,真正了解数据的具体构成。而且,旭日图不仅数据直观,而且图表用起来特别炫酷,分分钟拉高数据汇报的颜值!很多数据场景都适合用旭日图,

基础旭日图

效果预览:
在这里插入图片描述

核心代码:

import {
    Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antfincdn/ryp44nvUYZ/coffee.json')
  .then((data) => data.json())
  .then((data) => {
   
    const plot = new Sunburst('container', {
   
      data,
      innerRadius: 0.3,
      interactions: [{
    type: 'element-active' }],
    });
    plot.render();
  });

自定义 hierarchy field

效果预览:
在这里插入图片描述

核心代码:

import {
    Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/sunburst.json')
  .then((res) => res.json())
  .then((data) => {
   
    const plot = new Sunburst('container', {
   
      data,
      innerRadius: 0.3,
      interactions: [{
    type: 'element-active' }],
      hierarchyConfig: {
   
        field: 'sum',
      },
      drilldown: {
   
        breadCrumb: {
   
          rootText: '起始',
        },
      },
    });
    plot.render();
  });

设置标签布局

效果预览:
在这里插入图片描述

核心代码:

import {
    Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/sunburst.json')
  .then((res) => res.json())
  .then((data) => {
   
    const plot = new Sunburst('container', {
   
      data,
      innerRadius: 0.2,
      radius: 1,
      interactions: [{
    type: 'element-active' }],
      hierarchyConfig: {
   
        field: 'sum',
      },
      label: {
   
        // label layout: limit label in shape, which means the labels out of shape will be hide
        layout: [{
    type: 'limit-in-shape' }],
      },
    });
    plot.render();
  });

自定义颜色通道

效果预览:
在这里插入图片描述

核心代码:

import {
    Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/sunburst.json')
  .then((res) => res.json())
  .then((data) => {
   
    const plot = new Sunburst('container', {
   
      data,
      innerRadius: 0.3,
      colorField: 'label',
      interactions: [{
    type: 'element-active' }],
      hierarchyConfig: {
   
        field: 'sum',
      },
    });
    plot.render();
  });

自定义旭日图颜色

效果预览:
在这里插入图片描述

核心代码:

import {
    Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/sunburst.json')
  .then((res) => res.json())
  .then((data) => {
   
    const plot = new Sunburst('container', {
   
      data,
      innerRadius: 0.3,
      hierarchyConfig: {
   
        field: 'sum',
      },
      // 取色来自于:http://zhongguose.com/
      color: ['#f26b1f', '#fc8c23', '#f97d1c'],
      interactions: [{
    type: 'element-active' }],
      state: {
   
        active: {
   
          style: {
   
            stroke: '#fff',
            lineWidth: 2,
          },
        },
      },
    });
    plot.render();
  });

自定义旭日图样式

效果预览:
在这里插入图片描述

核心代码:

import {
    Sunburst } from '@antv/g2plot';
import {
    last } from '@antv/util';
import chromaJs from 'chroma-js';

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/sunburst.json')
  .then((res) => res.json())
  .then((data) => {
   
    const colors = [
      '#5B8FF9',
      '#61DDAA',
      '#65789B',
      '#F6BD16',
      '#7262fd',
      '#78D3F8',
      '#9661BC',
      '#F6903D',
      '#008685',
      '#F08BB4',
    ];
    function getPaletteByColor(color, count) {
   
      const origin = chromaJs(color);
      const range = [origin.brighten(0.5), origin, origin.darken(0.5)];
      return (
        chromaJs
          // @ts-ignore
          .scale(range)
          .mode('lab')
          .cache(false)
          .colors(count)
      );
    }

    const plot = new Sunburst('container', {
   
      data,
      innerRadius: 0.3,
      interactions: [{
    type: 'element-active' }],
      hierarchyConfig: {
   
        field: 'sum',
      },
      sunburstStyle: (datum) => {
   
        const depth = datum.depth;
        const nodeIndex = datum.nodeIndex;

        const ancestorIndex = last(datum[Sunburst.NODE_ANCESTORS_FIELD])?.nodeIndex || 0;

        const colorIndex = depth === 1 ? nodeIndex : ancestorIndex;
        let color = colors[colorIndex % colors.length];

        if (depth > 1) {
   
          const newColors = getPaletteByColor(color, last(datum[Sunburst.NODE_ANCESTORS_FIELD])?.childNodeCount);
          color = newColors[nodeIndex % colors.length];
        }

        return {
   
          fill: color,
          stroke: '#fff',
          lineWidth: 0.5,
        };
      },
    });
    plot.render();
  });

带贴图的旭日图

效果预览:
在这里插入图片描述

核心代码:

import {
    Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/sunburst.json')
  .then((res) => res.json())
  .then((data) => {
   
    const plot = new Sunburst('container', {
   
      data,
      innerRadius: 0.3,
      interactions: [{
    type: 'element-active' }],
      hierarchyConfig: {
   
        field: 'sum',
      },
      drilldown: {
   
        breadCrumb: {
   
          rootText: '起始',
        },
      },
      pattern: {
   
        type: 'line',
      },
    });
    plot.render();
  });

指定父节点权重

效果预览:
在这里插入图片描述

核心代码:

import {
    Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antfincdn/%24ixDFx9%248M/coffee-data.json')
  .then((data) => data.json())
  .then((data) => {
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源滚滚编程

创业不易,请打赏支持我一点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值