React之Echarts实现渐变和缩放

一、渐变

我这里是以折线图为例子
渐变还是很简单的
在series中,指定areaStyle中使用渐变函数就可以了

(1)封装渐变函数

以下是我封装的渐变函数

import echarts from 'echarts';
const getLinearColor = (color1: string, color2: string): any => {
    return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
      {
        offset: 0,
        color: color1,
      },
      {
        offset: 1,
        color: color2,
      },
    ]);
  };

(2)代码中直接调用渐变函数

代码中就可以直接调用

areaStyle: {
  opacity: 0.7,
  color: getLinearColor(colorMap['balck','white'),
},

或者直接在代码中使用该函数

areaStyle: {
    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
        offset: 0,
        color: 'rgb(255, 158, 68)'
    }, {
        offset: 1,
        color: 'rgb(255, 70, 131)'
    }])
},

二、横坐标缩放

(1)如何设置

与Y坐标即yAxis:平级,设置datazoom数组:

datazoom:[
  {
	type: 'inside',
	start: 0, // 开始位置的百分比,0 - 100
	end: 10 // 结束位置的百分比,0 - 100,为100时是整个横坐标
    startValue: number,// 开始位置的数值
    endValue: number// 结束位置的数值
  }
]

这时候就会显示如下结果:
在这里插入图片描述

(2)设置带样式的缩放条

上面的缩放条太单一了,可以试试下面带样式的,显示结果如下
在这里插入图片描述
有两个圆形的button,同时也可以修改进度条的默认颜色,代码如下:

dataZoom: [
  {
     type: 'inside',
     startValue: head(date),
     endValue: last(date),
     start: 0,
     end: 100,
   },
   {
     handleIcon:
       'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
     handleSize: '80%',
     handleStyle: {
       color: '#fef8f0', //button的背景颜色
       shadowBlur: 3, //阴影一类
       shadowColor: 'rgba(0, 0, 0, 0.6)',
       shadowOffsetX: 2,
       shadowOffsetY: 2,
     },
   },
 ],

(3)设置缩放条和横坐标的距离

通过grid参数bottom进行设置

 grid: {
      left: '3%',
      right: '4%',
      bottom: '15%',//设置缩放条和横坐标的距离,可以是px单位
      containLabel: true,
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在 React 项目中安装 echarts: ``` npm install echarts --save ``` 然后,在需要使用饼图的组件中引入 echarts,并在组件的生命周期函数 `componentDidMount` 中初始化 echarts 实例,并使用数据渲染饼图。 例如,以下是一个简单的饼图组件的代码: ``` import React, { Component } from 'react'; import echarts from 'echarts'; class PieChart extends Component { componentDidMount() { this.initChart(); } initChart = () => { const { data } = this.props; const chart = echarts.init(this.chartRef); chart.setOption({ tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)', }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center', }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold', }, }, labelLine: { show: false, }, data, }, ], }); }; render() { return ( <div ref={(ref) => { this.chartRef = ref; }} style={{ width: '100%', height: '300px' }} /> ); } } export default PieChart; ``` 在上面的代码中,我们使用 `componentDidMount` 函数初始化 echarts 实例,并使用传递进来的数据渲染饼图。注意,我们在组件的 `render` 函数中返回一个 `div` 元素,这个元素的 `ref` 属性绑定了一个回调函数,用来获取这个元素的引用,以便后续使用 echarts 来渲染图表。 在父组件中,我们可以使用以下代码来渲染这个饼图组件: ``` import React, { Component } from 'react'; import PieChart from './PieChart'; class App extends Component { state = { data: [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' }, ], }; render() { const { data } = this.state; return ( <div> <PieChart data={data} /> </div> ); } } export default App; ``` 在父组件中,我们传递一个 `data` 属性给饼图组件,这个属性包含了用于渲染饼图的数据。最终,我们可以在页面中看到一个简单的饼图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值