/** @format */ import _ from 'lodash'; import React, { useEffect, useState } from 'react'; import zrender from 'zrender'; import Echarts from '../Echarts'; interface BasicBarChartProps { size?: Record; [key: string]: any; showBackground?: boolean; backgroundStyle?: string; } // const colorList = ['#50E1', '#FDA568', '#DDD']; const BasicBarChart = (props: BasicBarChartProps) => { const { size = { height: 500 }, option = {}, data, data: { xData = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'], yData = [[10, 52, 200, 334, 390, 330, 220]], } = {}, theme = { /** * 文本颜色 */ textColor: 'red', /** * 组件主色 */ primaryColor: '#50E1', secondColor: '#FDAFFF', thirdColor: '#DDD', colorList: ['#3398DB', '#FDA568', '#DDD'], }, } = props; const themeList = theme; const optionBase = { color: [...themeList.colorList], legend: { show: true, padding: 2, itemGap: 5, left: 'right', itemWidth: 8, icon: 'rect', itemHeight: 4, orient: 'horizontal', textStyle: { fontFamily: 'Microsoft Yahei', fontWeight: 'normal', fontSize: 12, color: '#999', }, }, tooltip: { show: true, trigger: 'axis', triggerOn: 'mousemove', axisPointer: { type: 'shadow', }, hideDelay: 100, textStyle: { fontFamily: 'Microsoft Yahei', fontWeight: 'normal', fontSize: 14, color: '#fff', }, }, grid: { left: 30, right: 40, bottom: 30, top: 30, containLabel: true, }, xAxis: { type: 'category', show: true, name: '', data: xData, axisTick: { alignWithLabel: true, show: false, }, axisLabel: { show: true, color: '#999', fontWeight: 'normal', fontFamily: 'Microsoft YaHei', fontSize: 12, rotate: 0, interval: 0, }, axisLine: { show: false, lineStyle: { color: '#333' }, }, splitLine: { show: false, lineStyle: { color: '#999', type: 'dashed' }, }, }, yAxis: { type: 'value', min: null, max: null, axisTick: { show: false, }, nameTextStyle: {}, axisLabel: { show: true, color: '#999', fontWeight: 'normal', fontFamily: 'Microsoft YaHei', fontSize: 12, interval: 'auto', formatter: '{value}', }, axisLine: { show: true, lineStyle: { color: '#fff', width: 1 }, }, splitLine: { show: true, lineStyle: { color: '#aaa', type: 'dashed' }, }, name: '', }, series: [ { name: '直接访问', type: 'bar', data: [], itemStyle: { color: '#3398DB', }, showBackground: true, // 背景阴影 backgroundStyle: { // 背景阴影颜色 color: 'rgba(87, 84, 84, 0.5)', }, label: { show: true, color: '#fff', fontWeight: 'normal', align: 'center', fontFamily: 'Microsoft YaHei', fontSize: 12, position: 'insideTop', }, }, ], animation: true, animationEasing: 'cubicOut', animationEasingUpdate: 'cubicOut', animationDuration: 500, animationDurationUpdate: 300, }; const [latestOption, setLatestOption] = useState(optionBase); function customizer(objValue: any, srcValue: any) { if (_.isArray(objValue)) { return srcValue; } } function formatSeries(data: Record[], index: number) { const serie = (option.series && option.series[index]) || {}; const r = { name: serie.name || `系列${index + 1}`, type: 'bar', data: data, itemStyle: serie.itemStyle, barCategoryGap: serie.barCategoryGap || null, label: serie.label, showBackground: serie.showBackground, // 背景阴影 backgroundStyle: serie.backgroundStyle, // 阴影颜色 barWidth: serie.barWidth, }; // 处理柱图是渐变色的情况 if ( serie.itemStyle && serie.itemStyle.colorLinear && serie.itemStyle.color2 && serie.itemStyle.color1 ) { r.itemStyle.color = new zrender.LinearGradient(0, 0, 0, 1, [ { offset: 1, color: serie.itemStyle.color2, }, { offset: 0, color: serie.itemStyle.color1, }, ]); } return r; } function setData() { const dataConfig = { series: [], }; dataConfig.series = yData.map( (item: Record[], index: number) => formatSeries(item, index), ); const latestOption = _.mergeWith( optionBase, option, dataConfig, customizer, ); setLatestOption(latestOption); } useEffect(() => { setData(); }, [option, data]); return ; }; export default BasicBarChart;