G2自定義Tooltip

G2自定義Tooltip

<div id="interfaceChart" class="interface-chart-main"></div>
import G2 from '@antv/g2'
import DataSet from '@antv/data-set'

let chart = new G2.Chart({
  container: 'interfaceChart',
  forceFit: true,
  height: 300
})
	
let data = [{
     methodName: 'getUserInfo',
     callCount: 38,
     failRate: 0,
     overTimeCount: 0
   }, {
     methodName: 'getSystemInfo',
     callCount: 52,
     failRate: 0,
     overTimeCount: 0
   }, {
     methodName: 'getTime',
     callCount: 61,
     failRate: 0,
     overTimeCount: 0
   }]
   
 const dataSet = new DataSet()
 const dv = dataSet.createView().source(data)
 dv.transform({
   type: 'fold',
   fields: ['callCount'], // 展開字段集
   key: 'key', // key字段
   value: 'value' // value字段
 })
 
 chart.source(dv, {
   methodName: {
     type: 'cat'
   }
 })

chart.tooltip({
  crosshairs: 'y',
  htmlContent: function htmlContent(title, items) {
    let selfItem = items[0]
    let html = '<div class="g2-tooltip">'
    let titleDom = `<div class="g2-tooltip-title" style="margin-bottom: 4px;">${title}</div>`
    let sumDom = `<div>
                調用量:${selfItem.value}
              </div>
              <div>
                失敗率:${selfItem.failRate}%
              </div>
              <div>
                超時次數:${selfItem.overTimeCount}
              </div>
              `
    return html + titleDom + sumDom + '</div>'
  }
})

chart.scale('value', {
  alias: '調用量'
})
chart.axis('value', {
  label: {
    offset: [-10, 10]
  },
  title: {
    textStyle: {
      fontSize: 12, // 文本大小
    }
  },
})

chart.interval().tooltip('methodName*value*failRate*overTimeCount', (methodName, value, failRate, overTimeCount) => {
  return {
    methodName,
    value,
    failRate,
    overTimeCount
  }
}).position('methodName*value').size(20)

chart.render()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章