網(wǎng)站首頁(yè) 編程語(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
相關(guān)推薦
- 2022-11-07 ASP.NET?MVC通過勾選checkbox更改select的內(nèi)容_實(shí)用技巧
- 2022-07-13 Python字符串中如何去除數(shù)字之間的逗號(hào)_python
- 2022-06-19 C++深入探究引用的本質(zhì)與意義_C 語(yǔ)言
- 2022-06-14 C#通過cmd調(diào)用7z軟件實(shí)現(xiàn)壓縮和解壓文件_C#教程
- 2022-08-12 k8s?series初級(jí)calico使用介紹_云其它
- 2023-03-29 Pytorch損失函數(shù)torch.nn.NLLLoss()的使用_python
- 2022-10-14 分布式系統(tǒng)Redis解決Session共享問題
- 2022-09-25 python mac版本解釋器安裝
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支