網站首頁 編程語言 正文
自定義圖例: 這個可能實現的思路不太對, 目前是在數據列表里新建了個無用字段,在坐標軸隱藏該字段,通過color觸發圖例,再通過legend自定義內容
找這些東西太難了,找半天找不到,不用color的情況下我用legend無效
import React, { useState, useEffect, useRef} from "react";
import "./chart.less"
import { Chart } from "@antv/g2";
const Charts = () => {
const chartRef = useRef()
const data = [
{ name: "杭州銀行", score: 2, num: 5, city: "bj" },
{ name: "南京銀行", score: 4, num: 11, city: "bj" },
{ name: "江西郵儲", score: 8, num: 13, city: "bj" },
{ name: "農業銀行", score: 5, num: 9, city: "bj" },
{ name: "工商銀行", score: 9, num: 6, city: "bj" },
{ name: "人民銀行", score: 3, num: 29, city: "bj" },
]
const [chart, setChart] = useState(null)
useEffect(() => {
if (data && !chart) {
const c = new Chart({
container: 'c7',
autoFit: true,
width: 500,
height: 300,
});
setChart(c)
}
}, [data])
useEffect(() => {
chart && renderChart()
}, [chart, data])
const renderChart = () => {
const nums = data.map(i => i.num)
const maxValue = Math.max(...nums)
const max = Math.ceil(maxValue / 10) * 10
const margin = 1 / 5
chart.clear()
chart.data(data);
chart.scale('score', { // 右側坐標軸
min: 0,
max: 10,
tickCount: 6, // 左右坐標軸刻度數量保持一致 好看點
range: [0, 1 - margin / 2],
})
const tick = max / 5
let ticks = new Array(6).fill(null)
ticks = ticks.map((i, t) => t * tick)
chart.scale('num', { // 左側坐標軸
min: 0,
max: max,
ticks: ticks,
tickCount: 6,
range: [0, 1 - margin / 2],
})
chart.scale('city', { // 無用坐標軸
min: 0,
max: max,
ticks: ticks,
tickCount: 6,
range: [0, 1 - margin / 2],
})
chart.scale('name', {
nice: true,
})
chart.axis("city", { // 隱藏無用坐標軸內容
label: {
formatter: () => ""
}
})
chart.axis("num", {
title: {
text: "單位: 個",
autoRotate: false,
position: "end",
offset: -10,
textStyle: {
textAlign: 'start', // 文本對齊方向,可取值為: start middle end
fontSize: '12', // 文本大小
fontWeight: 'bold', // 文本粗細
textBaseline: 'top' // 文本基準線,可取 top middle bottom,默認為middle
},
},
})
chart.axis("score", {
title: {
text: "單位: 分",
autoRotate: false,
position: "end",
offset: -10,
textStyle: {
textAlign: 'start', // 文本對齊方向,可取值為: start middle end
fontSize: '12', // 文本大小
fontWeight: 'bold', // 文本粗細
textBaseline: 'top' // 文本基準線,可取 top middle bottom,默認為middle
},
},
})
chart.axis("name", {
label: {
autoRotate: true,
autoHide: false
},
})
chart.interaction('active-region');
// 柱狀圖設置
chart
.interval()
.label("num", {
offset: -10
})
.size(36)
.position('name*num')
.tooltip('num*score', (num, score) => {
return {
num,
score
}
})
.color("city")
// 折線圖設置 隱藏無用的字段
chart.
line()
.position('city')
.style({
stroke: "transparent"
})
// 關閉圖例點擊事件 沒找到api 沒實現 點擊圖例可以觸發這個方法
// chart.on('legend:click', ev => {
// return false
// });
// 修改 自定義圖例 增加 custom: true
chart.legend({
custom: true,
position: "bottom",
flipPage: false,
items: [
{
name: "項目",
marker: {
symbol: "square",
style:{
fill: "#6395f9"
},
clickable: false
},
},
{
name: "評分",
marker: {
symbol: "circle",
style:{
fill: "#f59a23"
},
clickable: false
},
}
]
})
// tooltip的自定義內容
const itemTpl = `
<div class='chart7Tpl'>
<div class='tpl'>
<span class="tpl1">·</span>
評分: {score} 分
</div>
<div class='tpl'>
<span class="tpl2">·</span>
項目: {num} 個
</div>
</div>
`
chart.tooltip({
showTitle: true,
showMarkers: false,
itemTpl: itemTpl
});
// 設置折線內容 顏色
chart
.line()
.position('name*score')
.size(4)
.color("", () => "#f59a23")
.tooltip('num*score', (num, score) => {
return {
num,
score
}
})
chart.removeInteraction('legend-filter'); // 自定義圖例,移除默認的分類圖例篩選交互
chart.render();
}
return (
<div className={"chart7"}>
<div id="c7" ref={chartRef} />
</div>
)
}
export default Charts;
原文鏈接:https://blog.csdn.net/aminwangaa/article/details/107567359
相關推薦
- 2022-03-22 C++實現簡易選課系統代碼分享_C 語言
- 2023-02-05 不同的編程語言輸出?“Hello?World”?代碼_其它綜合
- 2022-07-01 ASP.NET中的Web控件介紹_基礎應用
- 2022-06-01 C++的matlab接口轉換方法詳解_C 語言
- 2022-04-28 shell命令執行hive腳本(hive交互)_linux shell
- 2023-03-03 C++?高精度乘法運算的實現_C 語言
- 2022-10-23 Android事件分發機制示例分析_Android
- 2022-06-01 React經典面試題之倒計時組件詳解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支