如今各类框架各类库都很强大,可是也由于功能强大,致使不少配置都过于繁重,真正有用的就那么几个,今天就基于echarts封装一个通用
柱形图
!
想要了解其余的组件能够看下我以前封装的其余组件有好的思路或者其余的也会分享出来的;
基于antd封装横向拖动时间轴react
基于以上几点咱们开始写代码:web
在componentDidMount
周期中接受父组件传来的属性,构建最基本的option
,在构建option以前咱们先作下关于maxShow
的处理;segmentfault
let newChartsData = [];
if (maxShow && maxShow >= 0 && chartsData.length > maxShow) { chartsData.sort((a, b) => { return b.value - a.value; }); newChartsData = chartsData.slice(0, maxShow); let total = 0; chartsData.map((item, index) => { if (index > 4) { total += item.value } }); newChartsData = [...newChartsData, {value: total, name: '其余'}]; } else { newChartsData = [...chartsData] }
*
这里注意下,咱们默认是不处理maxShow
,也就是说加入父级没传那么默认是所有展现的,在没有用zoom
配置的的前提下,建议maxShow=12
是最佳的;具体时间默认12仍是默认不处理要根据使用者处理的数据来定,我处理的数据多数状况下不会多于12,因此默认不处理;antd
let myCharts = echarts.init(document.getElementById(`${idPrefix}_pie`)); if (getCharts && typeof func === 'function') { getCharts(myCharts); } myCharts.clear(); _eventType.map(item => { myCharts.off(item); }); let option = { color: newChartsData.length ? chartColor : '#bfbfbf', title: { text: title, top: 20, x: 'center' }, toolbox: { feature: { saveAsImage: { type: 'png', title: '点击下载', } }, top: 13, right: 13 }, tooltip: { trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)" }, series: [ { name: title, type: 'pie', radius, center, avoidLabelOverlap: false, label: { show: label, }, labelLine: { normal: { show: label } }, itemStyle: { borderWidth: 2, //设置border的宽度有多大 borderColor: bgColor, }, hoverAnimation: false, hoverOffset: 0, data: newChartsData.length ? newChartsData : [{value: 0, name: '暂无数据'}], }, ], graphic: newChartsData.length ? null : [{ type: 'text', left: 'center', top: radius[0] === '0' ? 'auto' : center[1], bottom: 10, cursor: 'auto', style: { text: '暂无数据', textAlign: 'center', fill: '#bfbfbf', fontSize: 16, stroke: '#bfbfbf', lineWidth: 0 } }] };
这当中以及添加了关于无数据的展现代码,具体实现的方式请看我以前封装的基于echarts 灵活封装react饼图组件这里就不在重复了;echarts
if (deleteOption) { deleteOption.map(item => { delete option[item] }); } // 删除函数 if (options) { option = {...option, ...options} } // 补充的options myCharts.setOption(option);
if (eChartsEvent && typeof eChartsEvent === 'function') { myCharts.on(eventType, 'series', params => { eChartsEvent(params, chartsData, myCharts); }); } window.onresize = () => { let target = this; if (target.resizeFlag) { clearTimeout(target.resizeFlag); } target.resizeFlag = setTimeout(function () { myCharts.resize(); if (onResize && typeof onResize === 'function') { onResize(); } target.resizeFlag = null; }, 100); }
import React, {PureComponent} from 'react'; import echarts from 'echarts/lib/echarts'; import 'echarts/lib/chart/bar'; import 'echarts/lib/component/title'; import 'echarts/lib/component/tooltip'; import 'echarts/lib/component/toolbox'; import 'echarts/lib/component/graphic'; import PropTypes from 'prop-types'; /* * 柱形图组件 * 包含无数据展现,onresize重绘事件 * 能够自定义option, 支持多种鼠标事件 * 暂时未封装zoom,zoom等其余功能可经过 options本身定义添加 * deleteOption: ['title', 'toolbox', 'tooltip', 'graphic'] 包含echarts含有的属性 * * */ /* * chartsData 格式 * const barChartsData = { * xAxis: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], * data:[10, 52, 200, 334, 390, 330, 220] * }; * 备用颜色: * '#13c2c2', '#52c41a', '#faad14', '#f5222d', '#722ed1', '#eb2f96', '#faad14'] * */ const _eventType = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseup', 'mouseover', 'mouseout', 'globalout', 'contextmenu']; class BarChart extends PureComponent { static propTypes = { chartsData: PropTypes.object.isRequired, // 图形数据 idPrefix: PropTypes.oneOfType([ PropTypes.string.isRequired, // 惟一标识区分多个柱形图,, PropTypes.number.isRequired, // 惟一标识区分多个柱形图,, ]), getCharts: PropTypes.func, // 把echarts 对象传出去 onResize: PropTypes.func, // 全局onResize事件, eChartsEvent: PropTypes.func, // 图形点击事件, 返回这各图形的数据以及对应的param,echarts 对象 barWidth: PropTypes.string, // 柱子的宽度 bottom: PropTypes.string || PropTypes.number, // 底部距离 title: PropTypes.string, // 标题栏,名字 chartColor: PropTypes.string, // 柱形区域颜色 label: PropTypes.bool, // 是否展现x轴label tooltip: PropTypes.bool, // 是否展现tooltip options: PropTypes.object, // 修改 更新的想要的配置,直接那eCharts的就能够 deleteOption: PropTypes.array, // 删除不须要的配置 eventType: PropTypes.oneOf(_eventType), // 事件类型 }; static defaultProps = { title: '', barWidth: '25', chartColor: '#1890ff', label: false, eventType: 'click', bottom: '8%', tooltip: true }; componentDidMount() { const { chartsData, idPrefix, getCharts, onResize, title, chartColor, label, deleteOption, options, eChartsEvent, eventType, barWidth, bottom } = this.props; let myCharts = echarts.init(document.getElementById(`${idPrefix}_bar`)); if (getCharts && typeof func === 'function') { getCharts(myCharts); } _eventType.map(item => { myCharts.off(item); }); let series = JSON.parse(JSON.stringify(chartsData.data)); myCharts.clear(); if (!chartsData.data.length) { chartsData.data = [7, 8, 6, 9] } let option = { color: series.length ? chartColor : ['#bfbfbf'], title: { text: title, top: 20, x: 'center' }, toolbox: { feature: { saveAsImage: { type: 'png', title: '点击下载', } }, top: 13, right: 13 }, tooltip: { trigger: 'axis', axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: 'shadow' // 默认为直线,可选为:'line' | 'shadow' } }, grid: { left: '3%', right: '4%', bottom: series.length ? bottom : 40, containLabel: true }, xAxis: [ { type: 'category', data: series.length ? chartsData.xAxis : [], axisTick: { alignWithLabel: true }, axisLine: { lineStyle: { color: series.length ? '#333' : '#ccc', } }, axisLabel: { show: series.length && label, } } ], yAxis: [ { type: 'value', show: series.length, splitLine: { show: false, }, } ], series: [ { name: title, type: 'bar', barWidth, data: chartsData.data } ], graphic: series.length ? null : [{ type: 'text', left: 'center', bottom: 15, cursor: 'auto', style: { text: '暂无数据', textAlign: 'center', fill: '#bfbfbf', fontSize: 16, stroke: '#bfbfbf', lineWidth: 0 } }] }; if (deleteOption) { deleteOption.map(item => { delete option[item] }); } // 删除函数 if (options) { option = {...option, ...options} } // 补充的options if (eChartsEvent && typeof eChartsEvent === 'function') { myCharts.on(eventType, 'series', params => { eChartsEvent(params, chartsData, myCharts); }); } myCharts.setOption(option); window.onresize = () => { let target = this; if (target.resizeFlag) { clearTimeout(target.resizeFlag); } target.resizeFlag = setTimeout(function () { myCharts.resize(); if (onResize && typeof onResize === 'function') { onResize(); } target.resizeFlag = null; }, 100); } } render() { const {idPrefix} = this.props; return ( <div id={`${idPrefix}_bar`} style={{width: '100%', height: '100%'}}/> ) } } export default BarChart;
再上两张图
正常展现的
无数据的展现框架
柱形图默认颜色我选用的是ant图库的默认配色,不少小伙伴不知道怎么配色,那下边及分享几个配色的网站(基本都是咱们公司UE嘴里抠出来的);函数