在react中插入Echarts

本文介绍如何在React项目中引入并使用Echarts库来创建动态图表,包括组件搭建、模块引入及图表配置等关键步骤。

在毕业设计中需要用到图表,首先想到的就是百度Echarts,简单的叙述一下怎么插入,其余的全靠个人扩展。



用官方的案例:

首先建立一个组件

import React,{Component} from 'react'

export class Waterall extends React.Component{
render(){
return(
<div ref="waterall" style={{width: "100%", height: "400px"}}></div>
)
}
}


然后根据官方事例引入Echarts模块


import React,{Component} from 'react';
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入柱状图
require('echarts/lib/chart/bar');
// 引入提示框和标题组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');

export class Waterall extends React.Component{
render(){
return(
<div ref="waterall" style={{width: "100%", height: "400px"}}></div>
)
}
}


因为Echarts是通过操控DOM来建立图表的 所以我们需要获取到div,而在react中,渲染时产生的是虚拟DOM,所以我才用ref来取得div


import React,{Component} from 'react';
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入柱状图
require('echarts/lib/chart/bar');
// 引入提示框和标题组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');

export class Waterall extends React.Component{
componentDidMount(){
const {data} = this.props;
let myChart = echarts.init(this.refs.waterall);
myChart.setOption({
title: { text: '' },
tooltip: {},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
render(){
return(
<div ref="waterall" style={{width: "100%", height: "400px"}}></div>
)
}
}


然后再插入到一个上级组件里,因为我是把这个组件和上级组件写在一个js里的,所以就不用再引入这个生成Echarts的组件

const WaterAll = () => (
<div>
<Waterall />
</div>
)


export default WaterAll;

效果图


React 中使用 ECharts 时,如果需要在图例(legend)名称前添加图标(icon),可以通过自定义 `legend.formatter` 和 `legend.icon` 属性来实现。ECharts 提供了灵活的图例配置选项,允许开发者对图例的显示样式进行定制。 ### 使用 `legend.formatter` 自定义图例名称 `legend.formatter` 是一个函数,用于控制图例名称的显示格式。可以在名称前插入 Unicode 字符或 HTML 实体来表示图标。例如: ```javascript option = { legend: { data: ['Category 1', 'Category 2'], formatter: function(name) { return '● ' + name; // 使用 Unicode 字符作为图标 } }, xAxis: { type: 'category', data: ['A', 'B', 'C'] }, yAxis: {}, series: [ { name: 'Category 1', type: 'bar', data: [10, 20, 30] }, { name: 'Category 2', type: 'bar', data: [15, 25, 35] } ] }; ``` ### 使用 `legend.icon` 自定义图例图标 除了使用 `formatter`,还可以通过 `legend.icon` 设置图例的图标形状。ECharts 支持多种内置图标类型,如 `'circle'`, `'rect'`, `'roundRect'`, `'arrow'` 等。你也可以通过自定义 SVG 图标来实现更复杂的图例图标。 ```javascript option = { legend: { data: ['Category 1', 'Category 2'], icon: 'circle' // 设置图例图标为圆形 }, xAxis: { type: 'category', data: ['A', 'B', 'C'] }, yAxis: {}, series: [ { name: 'Category 1', type: 'bar', data: [10, 20, 30] }, { name: 'Category 2', type: 'bar', data: [15, 25, 35] } ] }; ``` ### 结合 `formatter` 和 `icon` 实现图标 + 文字效果 如果你想在图例名称前添加一个自定义图标(如 FontAwesome 图标),可以结合 `formatter` 和 CSS 样式来实现。例如,在 `formatter` 中使用 HTML 标签并结合外部样式表: ```javascript option = { legend: { data: ['Category 1', 'Category 2'], formatter: function(name) { return '<i class="fas fa-chart-bar"></i> ' + name; // 使用 FontAwesome 图标 } }, xAxis: { type: 'category', data: ['A', 'B', 'C'] }, yAxis: {}, series: [ { name: 'Category 1', type: 'bar', data: [10, 20, 30] }, { name: 'Category 2', type: 'bar', data: [15, 25, 35] } ] }; ``` ### 在 React 中集成 ECharts 如果你使用的是 `echarts` 和 `react-echarts-wrapper` 或 `echarts-for-react` 等库,可以在组件中直接传递 `option` 配置对象: ```jsx import React from 'react'; import ReactECharts from 'echarts-for-react'; const ChartComponent = () => { const option = { legend: { data: ['Category 1', 'Category 2'], formatter: function(name) { return '● ' + name; } }, xAxis: { type: 'category', data: ['A', 'B', 'C'] }, yAxis: {}, series: [ { name: 'Category 1', type: 'bar', data: [10, 20, 30] }, { name: 'Category 2', type: 'bar', data: [15, 25, 35] } ] }; return <ReactECharts option={option} />; }; export default ChartComponent; ``` ### 总结 通过 `legend.formatter` 和 `legend.icon`,可以灵活地在 ECharts 的图例名称前添加图标。结合 React 的组件化开发模式,可以轻松地将这些配置集成到项目中,实现更丰富的可视化效果。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值