網(wǎng)站首頁 編程語言 正文
React使用Echarts
1.React項(xiàng)目安裝導(dǎo)入Echarts
$ npm install echarts --save
2.組件頁面中使用Echarts
// 導(dǎo)入echarts 并將全部導(dǎo)入的命名為echarts
import * as echarts from 'echarts'
import { useEffect, useRef } from 'react'
const Home = () => {
const domRef = useRef()
useEffect(() => {
chartTnit()
}, [])
const chartTnit = () => {
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
const myChart = echarts.init(domRef.current)
// 繪制圖表
myChart.setOption({
title: {
text: 'ECharts 入門示例'
},
tooltip: {},
xAxis: {
data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子']
},
yAxis: {},
series: [
{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
})
}
return (<div>
{/* 掛載節(jié)點(diǎn) */}
<div ref={domRef} style={{ width: '500px', height: '500px' }}></div>
</div>)
}
export default Home
React使用Ant-design-charts
1.React項(xiàng)目安裝導(dǎo)入Ant-design-charts
$ npm install @ant-design/charts --save
2.組件頁面中使用Ant-design-charts
import React from 'react'
// 引入Column柱狀圖表
import { Column } from '@ant-design/charts'
const Home = () => {
const data = [
{ type: '家具家電', sales: 38 },
{ type: '糧油副食', sales: 52 },
{ type: '生鮮水果', sales: 61 },
{ type: '美容洗護(hù)', sales: 145 },
{ type: '母嬰用品', sales: 48 },
{ type: '進(jìn)口食品', sales: 38 },
{ type: '食品飲料', sales: 38 },
{ type: '家庭清潔', sales: 38 },
]
const config = {
data,
xField: 'type',
yField: 'sales',
label: {
// 可手動(dòng)配置 label 數(shù)據(jù)標(biāo)簽位置
position: 'middle',
// 'top', 'bottom', 'middle',
// 配置樣式
style: {
fill: '#FFFFFF',
opacity: 0.6,
},
},
xAxis: {
label: {
autoHide: true,
autoRotate: false,
},
},
meta: {
type: {
alias: '類別',
},
sales: {
alias: '銷售額',
},
},
}
return <div>
<Column {...config} />
</div>
}
export default Home
組件封裝(封裝Echarts組件示例)
1.在components下新建組件
這里名字為Bar,目錄結(jié)構(gòu)如下:
2. components/Bar/index.js
// Bar組件 子組件
import * as echarts from 'echarts'
import { useEffect, useRef } from 'react'
// 將用來自定義的提取出來
const Bar = ({ title, xData, yData, style }) => {
const domRef = useRef()
useEffect(() => {
chartTnit()
}, [])
const chartTnit = () => {
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
const myChart = echarts.init(domRef.current)
// 繪制圖表
myChart.setOption({
title: {
text: title
},
tooltip: {},
xAxis: {
data: xData
},
yAxis: {},
series: [
{
name: '銷量',
type: 'bar',
data: yData
}
]
})
}
return (<div>
{/* 掛載節(jié)點(diǎn) */}
<div ref={domRef} style={style}></div>
</div>)
}
export default Bar
3.Home/index.js
//Home組件 父組件
import Bar from '@/components/Bar'
const Home = () => {
return (<div>
{/* 使用Bar組件 */}
<Bar
title='ECharts 入門示例111'
xData={['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子']}
yData={[5, 20, 36, 10, 10, 20]}
style={{ width: '500px', height: '500px' }} />
<Bar
title='ECharts 入門示例222'
xData={['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子']}
yData={[5, 20, 36, 10, 10, 20]}
style={{ width: '500px', height: '500px' }} />
</div>)
}
export default Home
4.效果展示
原文鏈接:https://blog.csdn.net/weixin_57844432/article/details/127994250
相關(guān)推薦
- 2022-05-19 C++實(shí)現(xiàn)教師管理系統(tǒng)_C 語言
- 2022-03-19 關(guān)于docker中?WSL?配置與修改問題_docker
- 2022-11-18 詳解C語言內(nèi)核字符串拷貝與比較_C 語言
- 2022-11-11 Android利用Canvas類繪制圖形_Android
- 2022-10-06 Python交互Redis的實(shí)現(xiàn)_Redis
- 2022-06-16 C語言學(xué)習(xí)筆記之字符串間的那些事_C 語言
- 2022-12-25 Flutter桌面開發(fā)windows插件開發(fā)_Android
- 2022-11-23 一文教會(huì)你用正則表達(dá)式校驗(yàn)日期時(shí)間格式_正則表達(dá)式
- 最近更新
-
- 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)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支