日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

在react中使用antv g2繪制帶有sider滑塊的chart圖表

作者:小五Ivy 更新時(shí)間: 2022-02-17 編程語(yǔ)言

在slider中會(huì)遇到的問題

1.slider兩邊要求展示的為文字,但過濾的時(shí)候過濾條件為下標(biāo),實(shí)現(xiàn)思路如下:
  • 過濾的時(shí)候判斷條件是數(shù)值類型即下標(biāo),但為了在滑塊兩端展示文字,需要將’start’,'end’那邊改成對(duì)應(yīng)的起始終止文字,但又會(huì)出現(xiàn)新的問題,就是在onChange里面打印的startText和endText也為相應(yīng)的文字而不是下標(biāo),考慮著在變化的時(shí)候需要將變化的下標(biāo)賦值給ds里面的from和to,不然chart圖表不再展示,就需要進(jìn)行遍歷判斷一下,找到文字下對(duì)應(yīng)的下標(biāo),然后賦值給from和to。
2. 視野中默認(rèn)展示5條數(shù)據(jù),鼠標(biāo)移入柱圖的時(shí)候,label變顏色,但當(dāng)移動(dòng)slider后,再移入柱圖時(shí)會(huì)報(bào)錯(cuò)
  • 原因:移動(dòng)完slider之后,重新獲取的視野中的五個(gè)label又是新的0-4的label,labelAll = document.querySelectorAll('.g2-label-customize'),但移入的當(dāng)前的柱圖的下標(biāo)是所有數(shù)據(jù)中的某一個(gè),這樣就會(huì)有沖突(比如:移入的當(dāng)前的柱圖,下標(biāo)是3,但在當(dāng)前l(fā)abel下可能是0)
  • 解決:截取當(dāng)前視野下的數(shù)據(jù),放在一個(gè)數(shù)組里面,其順序就會(huì)與所有的label的順序一致

在這里插入圖片描述

import React from 'react';
import G2 from '@antv/g2';
import Slider from '@antv/g2-plugin-slider';
import DataSet from '@antv/data-set';

class Silder extends React.Component {
  constructor(props) {
    super(props)
    this.state = {}
  }
  componentDidMount() {
    this.fun()
  }

  fun() {
    let mIndex;//當(dāng)前移入的下標(biāo)
    let startIndex = 0;//起始下標(biāo)
    let endIndex = 4;//終止下標(biāo)
    const data = [
      { index: 0, type: 'a', type1: 'b', type2: 'c', country: '巴西', value: 18203, value1: 1820 },
      { index: 1, type: 'a', type1: 'b', type2: 'c', country: '印尼', value: 23489, value1: 2349 },
      { index: 2, type: 'a', type1: 'b', type2: 'c', country: '美國(guó)', value: 29034, value1: 2934 },
      { index: 3, type: 'a', type1: 'b', type2: 'c', country: '印度', value: 104970, value1: 14970 },
      { index: 4, type: 'a', type1: 'b', type2: 'c', country: '中國(guó)', value: 131744, value1: 31744 },
      { index: 5, type: 'a', type1: 'b', type2: 'c', country: '加拿大', value: 11203, value1: 1320 },
      { index: 6, type: 'a', type1: 'b', type2: 'c', country: '塞爾維亞', value: 143489, value1: 12349 },
      { index: 7, type: 'a', type1: 'b', type2: 'c', country: '倫敦', value: 20034, value1: 21934 },
      { index: 8, type: 'a', type1: 'b', type2: 'c', country: '泰國(guó)', value: 14970, value1: 1470 },
      { index: 9, type: 'a', type1: 'b', type2: 'c', country: '巴基斯坦', value: 0, value1: 21744 },
    ];

    let nums = []//value的所有值
    data.forEach(item => {
      nums.push(item.value)
    })

    let maxNum = 0;//最大值
    maxNum = Math.max(...nums) + 1

    data.forEach(item => {
      item.value2 = maxNum
    })

    const ds = new DataSet({
      state: {
        from: data[0].index,
        to: data[4].index
      }
    })

    const dv = ds.createView();
    dv.source(data)
      .transform({
        type: 'filter',//過濾條件,與滑塊聯(lián)動(dòng)進(jìn)行chart展示
        callback: obj => {
          // console.log(obj);
          return obj.index >= ds.state.from && obj.index <= ds.state.to
        }
      });

    const chart = new G2.Chart({
      container: 'container',
      forceFit: true,
      height: 320,
      padding: [40, 60, 70, 90]
    })

    const view1 = chart.view()
    view1.source(dv)
    view1.scale('value', {
      min: Math.min(...nums),
      max: Math.max(...nums) + 1
    })
    view1.interval().position('country*value').color('type').tooltip('country*value*value1*value2', function (country, value, value1, value2) {
      // console.log(country, value, value1, 'ccc');
      return {
        value: !value ? '--' : value,
        value1: !value1 ? '--' : value1,
        value2: !value2 ? '--' : value2
      }
    }).label('country*value', function (country, value) {
      return {
        useHtml: true,
        htmlTemplate: function htmlTemplate(text, item) {
          return (
            `<span class="g2-label g2-label-customize" style="color:#b3f;font-size:20px" >` +
            value +
            "</span > "
          );
        },
        // offset: 10,
      }
    })

    const view2 = chart.view()
    view2.source(dv)
    //triangle 三角形
    view2.point().position('country*value1').color('type1', 'orange').shape('triangle').size(7).style({
      lineWidth: 0
    })
    view2.axis('value1', {
      position: 'right',
      grid: null
    })
    view2.tooltip(false)

    const view3 = chart.view()
    view3.source(dv)
    view3.scale('value2', {
      min: Math.min(...nums),
      max: Math.max(...nums) + 1
    })
    view3.interval().position('country*value2').color('transparent')
    view3.axis('value2', false)
    view3.tooltip(false)

    chart.on('interval:mouseenter', ev => {
      mIndex = dv.origin.slice(startIndex, endIndex + 1).findIndex(item => item.country === ev.data._origin.country
      )
      let labelAll = document.querySelectorAll('.g2-label-customize');
      labelAll[mIndex].style.color = 'red';
    })

    chart.on('interval:mouseleave', ev => {
      let labelAll = document.querySelectorAll('.g2-label-customize');
      labelAll[mIndex].style.color = '#b3f';
    })

    //使用輔助文本來(lái)設(shè)置坐標(biāo)上方的單位
    chart.guide().text({
      top: true,
      position: ['start', 'end'],
      content: '(萬(wàn)m2)',
      style: {
        fill: '#000',
        fontSize: '12'
      },
      offsetX: -40,
      offsetY: -20
    })

    chart.guide().text({
      top: true,
      position: ['end', 'end'],
      content: '(月)',
      style: {
        fill: '#000',
        fontSize: '12'
      },
      offsetX: 15,
      offsetY: -20
    })

    //設(shè)置tooltip
    chart.tooltip({
      // title: 'year',
      containerTpl: '<div class="g2-tooltip">' + '<p class="g2-tooltip-title" ></p>' + '<ul class="g2-tooltip-list"></ul>' + '</div>', // tooltip的外層模板
      itemTpl:
        `<li class="g2-tooltip-list-item" data-index={index} >
  <span style="width:5px;height:5px;background:#2762d3;float:left;margin-right:5px;margin-top:8px; border-radius: 50%;"></span><p>a(萬(wàn)m2)<span class="g2-tooltip-value">{value}</span></p>
  <span style="width:5px;height:5px;background:#ffa900;float:left;margin-right:5px;margin-top:8px; border-radius: 50%;"></span><p>b(月)<span class="g2-tooltip-value" style="display: inline-block; float: right; margin-left: 30px;"
  >{value1}</span></p>
  </li> `,
      offset: 50,
      'g2-tooltip': {
        position: 'absolute',
        width: '160px',
        backgroundColor: '#fff',
        color: '#000',
        padding: '5px 15px',
        fontSize: '12px',
        'transition': 'top 200ms,left 200ms'
      }
    });

    chart.render()

    //滑塊
    const slider = new Slider({
      container: 'silder',
      padding: [60],
      height: 15,
      start: data[0].country,//聲明滑動(dòng)條起始滑塊的位置對(duì)應(yīng)的數(shù)據(jù)值
      end: data[4].country,//聲明滑動(dòng)條結(jié)束滑塊的位置對(duì)應(yīng)的數(shù)據(jù)值
      data,//原始數(shù)據(jù)data不是轉(zhuǎn)換后的數(shù)據(jù)dv
      xAxis: 'country',//x軸對(duì)應(yīng)的字段
      yAxis: 'value1',//y軸對(duì)應(yīng)的字段
      fillerStyle: {//選中區(qū)域的樣式配置,默認(rèn)配置如下:
        fill: 'red',
        fillOpacity: 0.4
      },
      backgroundChart: {//slider 整體背景樣式。
        type: 'line',
        color: 'rgba(0, 0, 0, 0.3)',
        fill: 'yellow'
      },
      textStyle: {//slider 輔助文本字體樣式配置。
        fill: 'orange'
      },
      backgroundStyle: {
        fill: 'blue'
      },
      onChange: ({ startText, endText }) => {//當(dāng)滑動(dòng)條滑塊發(fā)生變化時(shí),觸發(fā)該回調(diào)函數(shù),主要用于更新 ds 的狀態(tài)量。該回調(diào)函數(shù)會(huì)提供一個(gè)參數(shù),該參數(shù)是一個(gè)對(duì)象,包含如下屬性:const { startValue, endValue, startText, endText } = obj;
        console.log(startText, endText);

        //過濾的時(shí)候判斷條件是數(shù)值類型即下標(biāo),但為了在滑塊兩端展示文字,需要將'start','end'那邊改成對(duì)應(yīng)的起始終止文字,但又會(huì)出現(xiàn)新的問題,就是在onChange里面打印的startText和endText也為相應(yīng)的文字而不是下標(biāo),考慮著在變化的時(shí)候需要將變化的下標(biāo)賦值給ds里面的from和to,不然chart圖表不再展示,就需要進(jìn)行遍歷判斷一下,找到文字下對(duì)應(yīng)的下標(biāo),然后賦值給from和to


        //dv.rows的數(shù)據(jù)是實(shí)時(shí)變化的,所以不能拿dv.rows數(shù)據(jù)做判斷條件,拿原始的數(shù)據(jù)dv.origin或者data做判斷

        let startRows = dv.origin.filter(item => item.country === startText)
        let endRows = dv.origin.filter(item => item.country === endText)

        startIndex = startRows[0].index
        endIndex = endRows[0].index

        // !!! 更新狀態(tài)量
        ds.setState('from', startIndex);
        ds.setState('to', endIndex);
      }
    });

    slider.render();
  }

  render() {
    return (
      <div>
        <div id='container'></div>
        <div id='silder'></div>
      </div >
    )
  }
}
export default Silder

原文鏈接:https://blog.csdn.net/weixin_44471622/article/details/106331638

欄目分類
最近更新