import { ReactEchartsPropsTypes } from "echarts-for-react"; import { GlobalItemProp } from "../App"; import { ConfigListProp,ConfigItemProp, ConfigItemColorProp } from "./chart.d"; import echarts from "echarts"; import { ListProp } from "./table/biaoge/biaoge"; /** * 获取url参数 * @param originData * @param key */ export function getUrlParam():Object{ let url = window.location.search; url = decodeURI(url); url = url.substr(1,url.length); let obj:any = {}; let paramArr = url.split("&"); paramArr.forEach(item=>{ const kv = item.split("="); obj[kv[0]]=kv[1]; }) return obj; } export function getPropertyValue(originData?:ConfigListProp,key?:string):any { if(!key){ return undefined ; } if(originData && originData[key]){ if(originData[key].value.value!=null || originData[key].value.value!=undefined){ return originData[key].value.value; }else{ return originData[key].value } }else{ return undefined; } } /** * 根据主键获取数据 */ export function getValue(data?:ConfigListProp,key?:string):any{ if(!key){ return undefined; } const keys:string[] = key.split("."); let v : any = data; for(let i=0;i{ let tmpItem : any = {...item}; tmpItem.label = { ...tmpItem?.label, show : valShow, position : "top", textStyle:{ ...tmpItem?.itemStyle?.label?.textStyle, color : valColor , fontSize : valFontSize, fontWeight : valFontWeight , } }; // // 饼图需要特殊处理 // if(dealForItemStyle){ // // 饼图需要特殊处理 // const PieItemStyle : any = { // ...tmpItem?.itemStyle, // label : {...tmpItem.label} // } ; // tmpItem.itemStyle = PieItemStyle ; // } return tmpItem; }) } // 坐标轴边距设置 if(setting['坐标轴边距设置']){ // 左边距 const insetLeft : number = getValue(setting,"坐标轴边距设置.左边距") // 右边距 const insetRight : number = getValue(setting,"坐标轴边距设置.右边距") // 顶边距 const insetTop : number = getValue(setting,"坐标轴边距设置.顶边距") // 底边距 const insetBottom : number = getValue(setting,"坐标轴边距设置.底边距") insetSetting = { left : `${insetLeft}%` , right : `${insetRight}%` , top : `${insetTop}%`, bottom : `${insetBottom}%`, containLabel : true, } } // 图例操作 if(setting['图例操作']){ // 图例 const legendShow : boolean = getValue(setting,"图例操作.图例") // 字体颜色 const legendLabelColor : string = getValue(setting,"图例操作.字体颜色") // 图例宽度 const legendWidth : string = getValue(setting,"图例操作.图例宽度") // 横向位置 const legendXAlign : string = getValue(setting,"图例操作.横向位置") // 纵向位置 const legendYAlign : string = getValue(setting,"图例操作.纵向位置") // 字体大小 const legendFontSize : number = getValue(setting,"图例操作.字体大小") let d : any = []; // legend数据从dataset获取 let fromDataset : boolean = (setting['饼图设置']||setting['反转']!=undefined) ? true : false; if(fromDataset){ const s:any = opt.option.series; d = [...s][0].data.map((item:any)=>item.name) }else{ d = opt.option.series?.map(item=>item.name) } legendSetting = { data : d, textStyle : { ...opt.option.legend?.textStyle , color : legendLabelColor , fontSize : legendFontSize, }, orient : "horizontal", width : legendWidth , ...alignMap(legendXAlign,true), ...alignMap(legendYAlign,false), show:legendShow, } opt = { ...opt, option:{ ...opt.option, legend : { ...legendSetting, } } } } // 标题设置 if(setting['标题设置']){ // 显示 const titleShow : boolean = getValue(setting,"标题设置.显示"); // 标题 const titleText : boolean = getValue(setting,"标题设置.标题"); // 字体颜色 const titleColor : boolean = getValue(setting,"标题设置.字体颜色"); // 字体粗细 const titleWeight : boolean = getValue(setting,"标题设置.字体粗细"); // 字体大小 const titleFontSize : boolean = getValue(setting,"标题设置.字体大小"); // 字体位置 const titleTextAlign : boolean = getValue(setting,"标题设置.字体位置"); // 副标题 const subTitleText : boolean = getValue(setting,"标题设置.副标题"); // 副标题字体颜色 const subTitleColor : boolean = getValue(setting,"标题设置.副标题字体颜色"); // 副标题字体粗细 const subTitleWeight : boolean = getValue(setting,"标题设置.副标题字体粗细"); // 副标题字体大小 const subTitleFontSize : boolean = getValue(setting,"标题设置.副标题字体大小"); titleSetting = titleShow ? { ...opt.option.title, text : titleText , textStyle : { color : titleColor , fontWeight: titleWeight , fontSize : titleFontSize, }, textAlign : titleTextAlign , // subtext : { // text : subTitleText , // textStyle : { // color : subTitleColor , // fontWeight : subTitleWeight, // fontSize : subTitleFontSize // } // }, subtext : subTitleText, subtextStyle : { color : subTitleColor , fontWeight : subTitleWeight, fontSize : subTitleFontSize } }:{} } if(setting['配色']){ const colorArr: ConfigItemColorProp[] = getValue(setting,"配色.自定义配色") // 饼图不支持渐变,所以饼图需要使用纯颜色 const PureColor : boolean = setting['饼图设置']?true:false; if(PureColor){ color = colorArr.map(item=>item.startColor) }else{ color = colorArr.map(item=>{ return new echarts.graphic.LinearGradient( 0,0,0,1,[ {offset:0,color:item.startColor||""}, {offset:(item.position||0)/100.0,color:item.endColor||''} ] ) }) } opt = { option : { ...opt.option, color : color } } } // ----------------个性属性---------------- // // 竖展示 if(setting['竖展示']){ // 竖展示 const isVertical :boolean = getValue(setting,"竖展示"); if(isVertical){ const tmp : any = yAxis ; yAxis = xAxis; xAxis = tmp ; }else{ yAxis = yAxis ; xAxis = xAxis; } seriesProp = seriesProp.map((item:any)=>{ const p :string = item.label.position ; item.label = { ...item.label, position : isVertical ? (p=='top'?"right":p) : p } return item; }) } // 反转 if(setting['反转']){ // 反转 const revert :boolean = getValue(setting,"反转"); seriesProp = seriesProp.map((item:any)=>{ item.sort = revert ? "descending":"ascending"; return item ; }) } // 文字设置 if(setting['文字设置']){ // 显示 const show : boolean = getValue(setting,"文字设置.显示") // 字体大小 const fontSize : number = getValue(setting,"文字设置.字体大小") // 字体颜色 const fontColor : string = getValue(setting,"文字设置.字体颜色") // 字体粗细 const weight : number = getValue(setting,"文字设置.字体粗细") seriesProp = seriesProp.map((item:any)=>{ item.label = { ...item.label, show : show , textStyle : { color : fontColor, fontSize : fontSize , fontWeight:weight } } return item ; }) } // 饼图设置 if(setting['饼图设置']){ // 设置为环形 const asACircle : boolean = getValue(setting,"饼图设置.设置为环形") // 半径大小 const radius : string = getValue(setting,"饼图设置.半径大小") // 自动排序 const autoSort : boolean = getValue(setting,"饼图设置.自动排序") const c : ConfigItemColorProp[] = getValue(setting,"配色.自定义配色") const shuzhi : boolean = getValue(setting,"数值显示.数值"); const baifenbi : boolean = getValue(setting,"数值显示.百分比"); const name : boolean = getValue(setting,"数值显示.数据项名称"); let formatter : string = ""; //{b}\n{c} ({d}%) if(name){ formatter = "{b}\n" } if(shuzhi){ formatter = formatter+"{c}" } if(baifenbi){ formatter = formatter + "({d}%)" } seriesProp = seriesProp.map((item:any,index:number)=>{ const currentColor : ConfigItemColorProp = c.length>index ? c[index] : {startColor:"#fff",endColor:"#000",id:"123","position":50} ; item.radius = radius.split(",") item.itemStyle = { ...item.itemStyle, color : currentColor.startColor, normal : { label : { ...item.label, formatter:formatter } } } return item; }) } // 柱体设置 if(setting['柱体设置']){ // 宽度 const width : number= getValue(setting,"柱体设置.宽度"); // 圆角 const radius : number = getValue(setting,"柱体设置.圆角"); seriesProp = seriesProp.map((item:any)=>{ item.barWidth = width ; item.itemStyle = { ...item.itemStyle, barBorderRadius:radius, } return item; }) } // 折线设置 if(setting['折线设置']){ // 标记点 const point : boolean = getValue(setting,"折线设置.标记点") // 点的大小 const pointSize : number = getValue(setting,"折线设置.点的大小") // 平滑曲线 const smooth : boolean = getValue(setting,"折线设置.平滑曲线") // 面积堆积 const area : boolean = getValue(setting,"折线设置.面积堆积") // 面积透明度 const areaOpacity : number = getValue(setting,"折线设置.面积透明度") // 线条宽度 const width : number = getValue(setting,"折线设置.线条宽度") // 配色 const c : ConfigItemColorProp[] = getValue(setting,"配色.自定义配色") seriesProp = seriesProp.map((item:any,index:number)=>{ const currentColor : ConfigItemColorProp = c.length>index ? c[index] : {startColor:"#fff",endColor:"#000",id:"123","position":50} ; item.type = item.type || 'line'; item.smooth = smooth ; item.lineStyle = { ...item.lineStyle, width : width , color : currentColor.startColor, } item.areaStyle = { ...item.areaStyle, opacity : areaOpacity, color : area ? { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [{ offset: 0, color: currentColor.startColor // 0% 处的颜色 }, { offset: 1, color: 'rgba(0,0,0,0)' // 100% 处的颜色 }], }:"none" }; item.itemStyle = { ...item.itemStyle, color : currentColor.startColor } item.symbol = point?"emptyCircle":"none"; item.symbolSize = pointSize ; return item; }) opt.option ={ ...opt.option, color:c.map(item=>`${item.startColor}`) } } // 仪表盘设置 if(setting['刻度线粗度']){ // 刻度线粗度 const lineWidth : number = getValue(setting,"刻度线粗度"); // 显示刻度值 const lineShowValue : boolean = getValue(setting,"显示刻度值") // 显示刻度线 const lineShow : boolean = getValue(setting,"显示刻度线") // 刻度字号 const valueFontSize : number = getValue(setting,"刻度字号") // 指标字号 const fontSize : number = getValue(setting,"指标字号") seriesProp = seriesProp.map((item:any)=>{ item.axisLine = { lineStyle:{ ...item.axisLine?.lineStyle, width : lineWidth, } } item.axisLabel={ show : lineShowValue, fontSize:valueFontSize, } item.axisTick = { show : lineShow, lineStyle : { width : lineWidth, } } item.detail = { fontSize : fontSize } return item }) } // 地图设置 if(setting['区域线粗细']){ const lineWidth : number = getValue(setting , "区域线粗细"); const color : string = getValue(setting,"区域线颜色"); const colorArr: ConfigItemColorProp[] = getValue(setting,"配色.自定义配色") const textColor : string = getValue(setting,"文字设置.字体颜色") let min : number = 0; let max : number = 0; seriesProp = seriesProp.map((item:any)=>{ item.itemStyle = { ...item.itemStyle, borderWidth : lineWidth , borderColor : color, } min = item.data[0].value; max = item.data[0].value; item.data.forEach((element:any) => { const v : number = element.value; if(v>max){ max = v } if(v${max};min=${min}`) const vm : any = { max:max, min:min, type : "continuous", color : colorArr.map(item=>{ return item.startColor||""; }), textStyle:{ color : textColor } } opt = { ...opt, option :{ ...opt.option, visualMap : [vm] } } } if(xAxis){ opt.option = { ...opt.option, xAxis : xAxis , } } if(yAxis){ opt.option = { ...opt.option, yAxis : yAxis , } } // ChartKeyMap的key if(_type == "zhibiao_yibiaopan"){ seriesProp = seriesProp.map((item:any)=>{ const colors = getValue(setting,"表盘配置.自定义配色"); const len = colors.length ; const isDouble = len % 2 == 0 ; let step = parseFloat((1/len).toFixed(2)); let middleStep = 1 - (step * len) ; // 校验用户输入比例是否合法 let dataValid : boolean = true ; let preValue : number = 0 ; for (const v of colors) { if(preValue > v.position){ dataValid = false; console.log('配色比例有问题:前项的数值不能大于后项') break ; }else{ preValue = v.position; } if(preValue>1){ console.log('比例值必须从0开始,1结束') break; } } let useColor = []; if(dataValid){ useColor = colors.map((item:any,index:number)=>{ return [item.position,item.startColor] }) }else{ useColor = colors.map((item:any,index:number)=>{ return [step*(index+1)+(len/2==index?middleStep:0),item.startColor]; }) } const axisLine = { ...item.axisLine||{}, lineStyle : { ...item.axisLine.lineStyle || {}, width : getValue(setting,"表盘配置.表盘粗细"), color : useColor } } item['axisLine'] = axisLine; const pointer = { ...item.itemStyle || {}, ...item.itemStyle&&item.itemStyle||{}, color : getValue(setting,"指针配置.颜色") } item['itemStyle'] = pointer; const detail = { ...item.detail, formatter : getValue(setting,"数据格式"), color : getValue(setting,"数值颜色") } item['detail']=detail; const axisTick = { ...item.axisTick , lineStyle : { ...item.axisTick && item.axisTick.lineStyle, color : getValue(setting,"表盘配置.刻度线颜色") } } item['axisTick'] = axisTick; return item; }) } if(_type == "tubiao_leida"){ const tmpRadar:any = {...opt.option.radar} const radar = { ...tmpRadar , shape : getValue(setting,"形状"), name : { ...tmpRadar.name, fontSize : getValue(setting,"字体大小"), color : getValue(setting,"字体颜色"), } } const series = opt.option.series?.map(v=>{ const item:any = {...v} const list:any = item.data; item.data = list.map((s:any,index:number)=>{ s.areaStyle = { opacity : getValue(setting,"区域透明度") } const color:any = opt.option.color ; s.label = { show : getValue(setting,"数值显示.显示"), fontSize : getValue(setting,"数值显示.字体大小"), fontWeight : getValue(setting,"数值显示.字体粗细"), formatter : getValue(setting,"数值显示.数据格式"), color : color[index >= color.length ? (index % color.length) : index].colorStops[0].color } return s }); return item; }) // const title = { // ...titleSetting, // subtext : "helo", // }; const legendData:any = {...remoteData.option.legend} const legend = { ...legendData, show : getValue(setting,"图例操作.图例"), textStyle : { // 字体颜色 color : getValue(setting,"图例操作.字体颜色"), // 字体大小 fontSize : getValue(setting,"图例操作.字体大小") }, // 图例宽度 width : getValue(setting,"图例操作.图例宽度"), // 横向 left : getValue(setting,"图例操作.横向位置"), top: getValue(setting,"图例操作.纵向位置") } opt.option = { ...opt.option, radar : radar, series : series, tooltip : { formatter : "", textStyle : { color : getValue(setting,"提示语设置.字体颜色"), fontSize : getValue(setting,"提示语设置.字体大小") } }, legend : legend, // title : title, } } opt.option = { ...opt.option, series : seriesProp, title : titleSetting, grid : insetSetting , } return opt; } catch (error) { console.warn(error) return {option:{}} } } /** * 日期格式化 */ export function dateFormat(date:Date,formatter:string):string{ var o:any = { "M+" : date.getMonth()+1, //月份 "d+" : date.getDate(), //日 "h+" : date.getHours(), //小时 "m+" : date.getMinutes(), //分 "s+" : date.getSeconds(), //秒 "q+" : Math.floor((date.getMonth()+3)/3), //季度 "S" : date.getMilliseconds() //毫秒 }; if(/(y+)/.test(formatter)) { formatter=formatter.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(formatter)){ formatter = formatter.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); } } return formatter; } export function onClickNewTag (setting:ConfigListProp,param:object,paramKey?:string){ const enableLink : boolean = getValue(setting,"超链设置.启用"); if(!enableLink){ return ; } const type = getValue(setting,"超链设置.打开方式"); const link = getValue(setting,"超链设置.链接地址"); let hasParam = link.indexOf("?")>=0?"&":"?"; if(!paramKey){ hasParam = ""; } const urlParam = {...getUrlParam(),...param} ; let origin = `${link}${hasParam}${paramKey||''}` let url = '' eval(`url=\`${origin}\``) if(type == "新窗口"){ window.open(url) }else{ window.location.href=url; } }