index.jsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import React, { Component } from "react";
  2. import style from "../index.less";
  3. import { Radio} from '@commonComp';
  4. import echarts from 'echarts';
  5. import config from "@config";
  6. import 'zrender/lib/svg/svg';
  7. /**
  8. * 来源于后台数据
  9. * 图表类型
  10. *
  11. *
  12. * **/
  13. class ChartItem extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.getContainers = this.getContainers.bind(this);
  17. this.rangChange = this.rangChange.bind(this);
  18. this.getXAxisArr = this.getXAxisArr.bind(this);
  19. }
  20. getXAxisArr(type){
  21. const endDate = this.props.endDate;
  22. let now = endDate?new Date(endDate).getTime():new Date().getTime();
  23. let arr = [],temp=0;
  24. //近一周
  25. switch(type){
  26. case 'week':
  27. arr.push(...this.getDayHours(now,true,1));
  28. for(let i=1;i<6;i++){
  29. temp=i*1000*60*60*24;
  30. arr.unshift(...this.getDayHours(now-temp,true));
  31. };
  32. arr.unshift(...this.getDayHours(now-6*1000*60*60*24,true,2));
  33. break;
  34. case 'month':
  35. for(let i=0;i<30;i++){
  36. temp=i*1000*60*60*24;
  37. arr.unshift(this.getDayHours(now-temp));
  38. }
  39. break;
  40. case 'sixMonth':
  41. for(let i=0;i<180;i++){
  42. temp=i*1000*60*60*24;
  43. arr.unshift(this.getDayHours(now-temp));
  44. }
  45. break;
  46. case 'year':
  47. for(let i=0;i<365;i++){
  48. temp=i*1000*60*60*24;
  49. arr.unshift(this.getDayHours(now-temp));
  50. }
  51. break;
  52. default:
  53. }
  54. return arr;
  55. }
  56. getDayHours(time,isHour,isLimit){
  57. const year = new Date(time).getFullYear();
  58. const month = new Date(time).getMonth()+1;
  59. const date = new Date(time).getDate();
  60. const hour = new Date(time).getHours();
  61. let arr=[],temp='';
  62. const str= year+'-'+(month<10?'0'+month:month)+'-'+(date<10?'0'+date:date);
  63. if(isHour){
  64. const num = isLimit===1?hour+1:24;
  65. for(let i=(isLimit===2?hour:0);i<num;i++){
  66. //temp = hour-i>-1?hour-i:hour-i+24;
  67. arr.push(str+" "+(i<10?'0'+i:i)+":00:00");
  68. }
  69. return arr;
  70. }else{
  71. return str;
  72. }
  73. }
  74. rangChange(type,i){
  75. const {initFn,disName,handleChange,data,pindex} = this.props;
  76. const times = this.getXAxisArr(type);
  77. const startTime=times[0];
  78. const endTime=times[times.length-1];
  79. const range = [startTime,endTime];
  80. const temp=this.props.timeTypes;
  81. if(!data[startTime+endTime]){
  82. initFn&&initFn({range,disName,rangeType:type,index:i,pindex,getNew:false});
  83. }
  84. handleChange&&handleChange(Object.assign(temp,{[i]:type}));
  85. }
  86. getContainers(obj){
  87. const timeTypes = this.props.timeTypes;
  88. const {endDate} = this.props;
  89. let len = 1,idx=0; //单个图表第一条线的在所有线中的index,用于显示不同的形状,打印使用
  90. let arr = [],keys = Object.keys(obj||{});
  91. for(let i in obj){
  92. idx = keys.findIndex((t)=>{return t==i});
  93. len = idx==0?0:len+obj[keys[idx-1]].length;
  94. arr.push(<Chart data={obj[i]} len={len} endDate={endDate} type={timeTypes&&timeTypes[i]} index={i} getXAxisArr={this.getXAxisArr} handleRangeChange={this.rangChange}/>)
  95. }
  96. return arr.length>0?arr:<p style={{color:'#acacac'}}>该模块暂无数据</p>;
  97. }
  98. getLegends(obj){
  99. let arr = [],temp='',doms=[];
  100. for(let i in obj){
  101. arr = [...arr,...obj[i]];
  102. }
  103. arr.map((it,ix)=>{
  104. doms.push(<div><img src={require('@common/images/'+(ix+1)+'.png')} /><span>{it}</span></div>);
  105. });
  106. return doms;
  107. }
  108. componentDidMount(){
  109. const {initFn,pindex,disName} = this.props;
  110. const type = config.chartDismen;
  111. const times = this.getXAxisArr(config.chartDismen);
  112. const startTime=times[0];
  113. const endTime=times[times.length-1];
  114. const range = [startTime,endTime];
  115. initFn&&initFn({range,disName,rangeType:type,pindex,getNew:true});
  116. }
  117. render() {
  118. const {title,data,names} = this.props;
  119. const range = this.getXAxisArr(config.chartDismen);
  120. const obj = data[range[0]+range[range.length-1]];
  121. const nameObj = names&&names[range[0]+range[range.length-1]];
  122. return <div className={style['assess-item']}>
  123. <h2>{title}</h2>
  124. <div className={style['legend']}>
  125. {this.getLegends(nameObj)}
  126. </div>
  127. <div className={style['item-content']}>
  128. {this.getContainers(obj)}
  129. </div>
  130. </div>;
  131. }
  132. }
  133. class Chart extends Component{
  134. constructor(props) {
  135. super(props);
  136. this.state={
  137. chartObj:null,
  138. //timeRange:'year',
  139. week:props.getXAxisArr('week'),
  140. month:props.getXAxisArr('month'),
  141. sixMonth:props.getXAxisArr('sixMonth'),
  142. year:props.getXAxisArr('year')
  143. };
  144. this.drawChart = this.drawChart.bind(this);
  145. this.timeSwitch = this.timeSwitch.bind(this);
  146. }
  147. drawChart(){
  148. const {index,data,getXAxisArr,type,endDate,len} = this.props;
  149. const xAxis = getXAxisArr(type);
  150. const id = endDate?'chart'+endDate+index:'chart'+index;
  151. let series = [],names=[],inx=-1;
  152. let myChart = echarts.init(document.getElementById(id) ,null, {renderer: 'svg'});
  153. let xAxisArr = [...xAxis];
  154. const strNum = type=='week'?13:10;
  155. const interval = {
  156. week:23,
  157. month:4,
  158. sixMonth:9,
  159. year:60
  160. };
  161. data&&data.map((it,j)=>{
  162. let values=new Array();
  163. let name='';
  164. it&&it.creatTime.map((x,i)=>{
  165. inx=xAxis.findIndex((a)=>{
  166. name=x.substr(0,strNum);
  167. return a.substr(0,strNum)==name;
  168. }); //日期对应横坐标的位置
  169. if(inx!=-1){
  170. if(type=='week'){
  171. xAxisArr[inx] = x;
  172. }
  173. values[inx] = it.indexValue[i]; //值对应横坐标的位置
  174. }
  175. });
  176. names.push(it.itemName);
  177. series.push({
  178. name: it.itemName,
  179. type: 'line',
  180. data: values,
  181. showAllSymbol:true,
  182. symbol:'image://'+require('@common/images/'+((len+j)%8+1)+'.png'),//config.chartSymbols[(len+j)%8],
  183. symbolSize:10,
  184. itemStyle:{
  185. color:config.chartColors[(len+j)%8]
  186. }
  187. });
  188. });
  189. // 指定图表的配置项和数据
  190. var option = {
  191. tooltip: {
  192. trigger: 'axis',
  193. textStyle:{
  194. fontSize:12
  195. }
  196. },
  197. legend: {
  198. show:false
  199. /*data:names,
  200. padding:0,
  201. itemHeight:16,*/
  202. },
  203. grid:{
  204. top:40
  205. },
  206. xAxis: {
  207. type: 'category',
  208. boundaryGap: false,
  209. data: xAxisArr,
  210. splitLine:{
  211. show:true
  212. },
  213. axisPointer:{
  214. //show:false
  215. },
  216. axisTick:{
  217. show:false,
  218. interval:interval[type]
  219. },
  220. axisLabel:{
  221. show:true,
  222. showMaxLabel:true,
  223. interval:interval[type],
  224. rotate:65,
  225. fontSize:10,
  226. formatter: function(value,index){
  227. return value.substr(0,13);
  228. }
  229. }
  230. },
  231. yAxis: {
  232. type: 'value',
  233. axisTick:{
  234. show:false
  235. },
  236. axisLabel:{
  237. show:true,
  238. showMaxLabel:true
  239. }
  240. },
  241. series: series
  242. };
  243. // 使用刚指定的配置项和数据显示图表。
  244. myChart.setOption(option);
  245. }
  246. timeSwitch(type){
  247. const {handleRangeChange,index} = this.props;
  248. handleRangeChange&&handleRangeChange(type,index);
  249. const that=this;
  250. setTimeout(()=>{
  251. that.drawChart();
  252. },300);
  253. }
  254. componentDidMount(){
  255. this.drawChart();
  256. }
  257. render(){
  258. const {type,index,endDate} = this.props;
  259. return <div className={style['cont']}>
  260. <div className={style['time-range']}>
  261. <span className={type=='year'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("year")}>近一年</span>
  262. <span className={type=='sixMonth'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("sixMonth")}>近六个月</span>
  263. <span className={type=='month'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("month")}>近一个月</span>
  264. <span className={type=='week'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("week")}>近一周</span>
  265. </div>
  266. <div className={style["chart-box"]} id={endDate?'chart'+endDate+index:'chart'+index}></div>
  267. </div>;
  268. }
  269. }
  270. export default ChartItem;