g2plot 多折线图相关示例

二氧化碳排放量来源

在这里插入图片描述

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

fetch('https://gw.alipayobjects.com/os/bmw-prod/55424a73-7cb8-4f79-b60d-3ab627ac5698.json')
  .then((res) => res.json())
  .then((data) => {
    const line = new Line('container', {
      data,
      xField: 'year',
      yField: 'value',
      seriesField: 'category',
      xAxis: {
        type: 'time',
      },
      yAxis: {
        label: {
          // 数值格式化为千分位
          formatter: (v) => `${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g, (s) => `${s},`),
        },
      },
    });

    line.render();
  });

网格填充

在这里插入图片描述

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

fetch('https://gw.alipayobjects.com/os/bmw-prod/55424a73-7cb8-4f79-b60d-3ab627ac5698.json')
  .then((res) => res.json())
  .then((data) => {
    const line = new Line('container', {
      data: data
        .slice(data.length - 90, data.length)
        .filter((item) => item.category === 'Gas fuel' || item.category === 'Cement production'),
      xField: 'year',
      yField: 'value',
      seriesField: 'category',
      // X 轴相关配置
      xAxis: {
        nice: true,
        // tickCount: 8,
        // 文本标签
        label: {
          // autoRotate: false,
          rotate: Math.PI / 6,
          offset: 10,
          style: {
            fill: '#aaa',
            fontSize: 12,
          },
          formatter: (name) => name,
        },
        title: {
          text: '年份',
          style: {
            fontSize: 16,
          },
        },
        // 坐标轴线的配置项 null 表示不展示
        line: {
          style: {
            stroke: '#aaa',
          },
        },
        tickLine: {
          style: {
            lineWidth: 2,
            stroke: '#aaa',
          },
          length: 5,
        },
        grid: {
          line: {
            style: {
              stroke: '#ddd',
              lineDash: [4, 2],
            },
          },
          alternateColor: 'rgba(0,0,0,0.05)',
        },
      },
      // Y 轴相关配置
      yAxis: {
        // max: 3000,
        // 文本标签
        label: {
          autoRotate: false,
          style: {
            fill: '#aaa',
            fontSize: 12,
          },
          // 数值格式化为千分位
          formatter: (v) => `${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g, (s) => `${s},`),
        },
        title: {
          text: '排放量(顿)',
          style: {
            fontSize: 16,
          },
        },
        // 坐标轴线的配置项 null 表示不展示
        line: {
          style: {
            stroke: '#aaa',
          },
        },
        tickLine: {
          style: {
            lineWidth: 2,
            stroke: '#aaa',
          },
          length: 5,
        },
        grid: {
          line: {
            style: {
              stroke: '#ddd',
              lineDash: [4, 2],
            },
          },
          alternateColor: 'rgba(0,0,0,0.05)',
        },
      },
      // label
      label: {
        layout: [{ type: 'hide-overlap' }], // 隐藏重叠label
        style: {
          textAlign: 'right',
        },
        formatter: (item) => item.value,
      },
      // point
      point: {
        size: 5,
        style: {
          lineWidth: 1,
          fillOpacity: 1,
        },
        shape: (item) => {
          if (item.category === 'Cement production') {
            return 'circle';
          }
          return 'diamond';
        },
      },
      annotations: [
        // 辅助线
        {
          type: 'line',
          start: ['0%', '10%'],
          end: ['100%', '10%'],
          top: true,
          style: {
            stroke: 'l(0) 0:#ffffff 0.5:#7ec2f3 1:#1890ff',
            lineWidth: 2,
          },
        },
        // 辅助区域
        {
          type: 'region',
          start: ['0%', '0%'],
          end: ['20%', '10%'],
          top: true,
          style: {
            fill: '#1890ff',
            fillOpacity: 1,
            opacity: 1,
          },
        },
        // 辅助文本
        {
          type: 'text',
          position: ['10%', '5%'],
          content: '二氧化碳排放量来源',
          style: {
            fill: '#fff',
            fontSize: 12,
            textAlign: 'center',
            textBaseline: 'middle',
            shadowColor: '#fff',
            shadowOffsetX: 12,
            shadowOffsetY: 12,
            shadowBlur: 2,
          },
        }, // 辅助线
        {
          type: 'line',
          start: ['min', 'median'],
          end: ['max', 'median'],
          style: {
            stroke: 'Turquoise',
            lineDash: [4, 2],
          },
        },
      ],
      legend: {
        position: 'top-right',
        itemName: {
          style: {
            fill: '#000',
          },
          formatter: (name) => name,
        },
      },
      // 度量相关配置
      meta: {
        // year 对应 xField || yField
        year: {
          range: [0, 1],
        },
      },
    });

    line.render();
  });

多折线动画

在这里插入图片描述

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

fetch('https://gw.alipayobjects.com/os/bmw-prod/e00d52f4-2fa6-47ee-a0d7-105dd95bde20.json')
  .then((res) => res.json())
  .then((data) => {
    const linePlot = new Line('container', {
      data,
      xField: 'year',
      yField: 'gdp',
      seriesField: 'name',
      yAxis: {
        label: {
          formatter: (v) => `${(v / 10e8).toFixed(1)} B`,
        },
      },
      legend: {
        position: 'top',
      },
      smooth: true,
      // @TODO 后续会换一种动画方式
      animation: {
        appear: {
          animation: 'path-in',
          duration: 5000,
        },
      },
    });

    linePlot.render();
  });

折线趋势填充

在这里插入图片描述

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

fetch('https://gw.alipayobjects.com/os/bmw-prod/e00d52f4-2fa6-47ee-a0d7-105dd95bde20.json')
  .then((res) => res.json())
  .then((data) => {
    const linePlot = new Line('container', {
      data,
      xField: 'year',
      yField: 'gdp',
      seriesField: 'name',
      yAxis: {
        label: {
          formatter: (v) => `${(v / 10e8).toFixed(1)} B`,
        },
      },
      legend: {
        position: 'top',
      },
      smooth: true,
      // 配置折线趋势填充
      area: {
        style: {
          fillOpacity: 0.15,
        },
      },
      animation: {
        appear: {
          animation: 'wave-in',
          duration: 3000,
        },
      },
    });

    linePlot.render();
  });

指定折线颜色

在这里插入图片描述

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

fetch('https://gw.alipayobjects.com/os/bmw-prod/55424a73-7cb8-4f79-b60d-3ab627ac5698.json')
  .then((res) => res.json())
  .then((data) => {
    const line = new Line('container', {
      data,
      xField: 'year',
      yField: 'value',
      seriesField: 'category',
      yAxis: {
        label: {
          // 数值格式化为千分位
          formatter: (v) => `${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g, (s) => `${s},`),
        },
      },
      color: ['#1979C9', '#D62A0D', '#FAA219'],
    });

    line.render();
  });

通过回调函数指定折线样式

在这里插入图片描述

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

fetch('https://gw.alipayobjects.com/os/bmw-prod/c48dbbb1-fccf-4a46-b68f-a3ddb4908b68.json')
  .then((res) => res.json())
  .then((data) => {
    const line = new Line('container', {
      data,
      xField: 'date',
      yField: 'value',
      yAxis: {
        label: {
          // 数值格式化为千分位
          formatter: (v) => `${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g, (s) => `${s},`),
        },
      },
      seriesField: 'type',
      color: ({ type }) => {
        return type === 'register' ? '#F4664A' : type === 'download' ? '#30BF78' : '#FAAD14';
      },
      lineStyle: ({ type }) => {
        if (type === 'register') {
          return {
            lineDash: [4, 4],
            opacity: 1,
          };
        }
        return {
          opacity: 0.5,
        };
      },
    });

    line.render();
  });

通过回调函数指定数据点样式

在这里插入图片描述

import { Line } from '@antv/g2plot';
import { uniq, findIndex } from '@antv/util';

fetch('https://gw.alipayobjects.com/os/bmw-prod/55424a73-7cb8-4f79-b60d-3ab627ac5698.json')
  .then((res) => res.json())
  .then((data) => {
    const COLOR_PLATE_10 = [
      '#5B8FF9',
      '#5AD8A6',
      '#5D7092',
      '#F6BD16',
      '#E8684A',
      '#6DC8EC',
      '#9270CA',
      '#FF9D4D',
      '#269A99',
      '#FF99C3',
    ];

    const container = document.getElementById('container');

    const line = new Line(container, {
      data,
      xField: 'year',
      yField: 'value',
      seriesField: 'category',
      yAxis: {
        label: {
          // 数值格式化为千分位
          formatter: (v) => `${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g, (s) => `${s},`),
        },
      },
      color: COLOR_PLATE_10,
      point: {
        shape: ({ category }) => {
          return category === 'Gas fuel' ? 'square' : 'circle';
        },
        style: ({ year }) => {
          return {
            r: Number(year) % 4 ? 0 : 3, // 4 个数据示一个点标记
          };
        },
      },
    });

    line.render();
  });

  • 14
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要显示多个折线图并避免它们脱离画布,您可以使用 `theme` 属性来调整 G2Plot 的主题样式。下面是一个示例代码,演示如何创建一个堆叠柱线图表,并显示多个折线图。 ```jsx import React, { Component } from 'react'; import { StackedColumnLine } from '@antv/g2plot'; class StackedColumnLineChart extends Component { constructor(props) { super(props); this.chartRef = React.createRef(); } componentDidMount() { const data = [ { year: '1991', value: 3, type: 'sales' }, { year: '1992', value: 4, type: 'sales' }, { year: '1993', value: 3.5, type: 'sales' }, { year: '1994', value: 5, type: 'sales' }, { year: '1995', value: 4.9, type: 'sales' }, { year: '1996', value: 6, type: 'sales' }, { year: '1997', value: 7, type: 'sales' }, { year: '1998', value: 9, type: 'sales' }, { year: '1999', value: 13, type: 'sales' }, { year: '1991', value: 1, type: 'profits' }, { year: '1992', value: 3, type: 'profits' }, { year: '1993', value: 1.5, type: 'profits' }, { year: '1994', value: 2, type: 'profits' }, { year: '1995', value: 1.9, type: 'profits' }, { year: '1996', value: 4, type: 'profits' }, { year: '1997', value: 5, type: 'profits' }, { year: '1998', value: 8, type: 'profits' }, { year: '1999', value: 10, type: 'profits' }, ]; const lineTheme = { geometries: { line: { style: { lineWidth: 2, }, }, }, }; const chart = new StackedColumnLine(this.chartRef.current, { data, xField: 'year', yField: 'value', xAxis: { label: { autoRotate: false, }, }, yAxis: { label: { formatter: (value) => `${value}k`, }, }, seriesField: 'type', legend: { position: 'top', }, theme: { styleSheet: { ...lineTheme, }, }, }); chart.render(); } componentWillUnmount() { if (this.chart) { this.chart.destroy(); } } render() { return <div ref={this.chartRef}></div>; } } export default StackedColumnLineChart; ``` 在上面的代码中,我们定义了一个 `lineTheme` 对象,用于调整折线图的样式。然后,我们在创建图表实例时,将 `lineTheme` 对象添加到主题中。这样,我们就可以根据需要调整折线图的线宽和样式,从而避免它们脱离画布。 另外,我们还可以通过设置 `xAxis.label.autoRotate` 属性来禁用 X 轴标签的自动旋转,以确保标签不重叠。同时,我们通过设置 `yAxis.label.formatter` 属性来自定义 Y 轴标签的格式,以便更好地显示数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Python私教

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

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

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

打赏作者

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

抵扣说明:

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

余额充值