index.jsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. for(let i=0;i<7;i++){
  28. temp=i*1000*60*60*24;
  29. arr.unshift(...this.getDayHours(now-temp,true));
  30. }
  31. break;
  32. case 'month':
  33. for(let i=0;i<30;i++){
  34. temp=i*1000*60*60*24;
  35. arr.unshift(this.getDayHours(now-temp));
  36. }
  37. break;
  38. case 'sixMonth':
  39. for(let i=0;i<180;i++){
  40. temp=i*1000*60*60*24;
  41. arr.unshift(this.getDayHours(now-temp));
  42. }
  43. break;
  44. case 'year':
  45. for(let i=0;i<365;i++){
  46. temp=i*1000*60*60*24;
  47. arr.unshift(this.getDayHours(now-temp));
  48. }
  49. break;
  50. default:
  51. }
  52. return arr;
  53. }
  54. getDayHours(time,isHour){
  55. const year = new Date(time).getFullYear();
  56. const month = new Date(time).getMonth()+1;
  57. const date = new Date(time).getDate();
  58. const hour = new Date(time).getHours();
  59. let arr=[],temp='';
  60. const str= year+'-'+(month<10?'0'+month:month)+'-'+(date<10?'0'+date:date);
  61. if(isHour){
  62. for(let i=0;i<24;i++){
  63. temp = hour-i>-1?hour-i:hour-i+24;
  64. arr.unshift(str+" "+(temp<10?'0'+temp:temp));
  65. }
  66. return arr;
  67. }else{
  68. return str;
  69. }
  70. }
  71. rangChange(type,i){
  72. const {initFn,handleChange,data,pindex} = this.props;
  73. const times = this.getXAxisArr(type);
  74. const startTime=times[0];
  75. const endTime=times[times.length-1];
  76. const range = [startTime,endTime];
  77. const temp=this.props.timeTypes;
  78. if(!data[startTime+endTime]){
  79. initFn&&initFn({range,rangeType:type,index:i,pindex,getNew:false});
  80. }
  81. handleChange&&handleChange(Object.assign(temp,{[i]:type}));
  82. }
  83. getContainers(){
  84. const timeTypes = this.props.timeTypes;
  85. const range = this.getXAxisArr(config.chartDismen);
  86. const obj = this.props.data[range[0]+range[range.length-1]];
  87. const {endDate} = this.props;
  88. let arr = [];
  89. for(let i in obj){
  90. arr.push(<Chart data={obj[i]} endDate={endDate} type={timeTypes&&timeTypes[i]} index={i} getXAxisArr={this.getXAxisArr} handleRangeChange={this.rangChange}/>)
  91. }
  92. return arr;
  93. }
  94. componentDidMount(){
  95. const {initFn,pindex} = this.props;
  96. const type = config.chartDismen;
  97. const times = this.getXAxisArr(config.chartDismen);
  98. const startTime=times[0];
  99. const endTime=times[times.length-1];
  100. const range = [startTime,endTime];
  101. initFn&&initFn({range,rangeType:type,pindex,getNew:true});
  102. }
  103. render() {
  104. const {title} = this.props;
  105. return <div className={style['assess-item']}>
  106. <h2>{title}</h2>
  107. <div className={style['item-content']}>
  108. {this.getContainers()}
  109. </div>
  110. </div>;
  111. }
  112. }
  113. class Chart extends Component{
  114. constructor(props) {
  115. super(props);
  116. this.state={
  117. chartObj:null,
  118. //timeRange:'year',
  119. week:props.getXAxisArr('week'),
  120. month:props.getXAxisArr('month'),
  121. sixMonth:props.getXAxisArr('sixMonth'),
  122. year:props.getXAxisArr('year')
  123. };
  124. this.drawChart = this.drawChart.bind(this);
  125. this.timeSwitch = this.timeSwitch.bind(this);
  126. }
  127. drawChart(){
  128. const {index,data,getXAxisArr,type,endDate} = this.props;
  129. const xAxis = getXAxisArr(type);
  130. const id = endDate?'chart'+endDate+index:'chart'+index;
  131. let series = [],names=[],inx=-1;
  132. let myChart = echarts.init(document.getElementById(id) ,null, {renderer: 'svg'});
  133. const interval = {
  134. week:24,
  135. month:4,
  136. sixMonth:9,
  137. year:60
  138. };
  139. data&&data.map((it)=>{
  140. let values=new Array();
  141. let name='';
  142. it&&it.creatTime.map((x,i)=>{
  143. inx=xAxis.findIndex((a)=>{
  144. name=type=='week'?x.substr(0,13):x.substr(0,10);
  145. return a==name;
  146. }); //日期对应横坐标的位置
  147. if(inx!=-1){
  148. values[inx] = it.indexValue[i]; //值对应横坐标的位置
  149. }
  150. });
  151. names.push(it.itemName);
  152. series.push({
  153. name: it.itemName,
  154. type: 'line',
  155. data: values,
  156. showAllSymbol:true
  157. });
  158. });
  159. // 指定图表的配置项和数据
  160. var option = {
  161. tooltip: {
  162. trigger: 'axis'
  163. },
  164. legend: {
  165. data:names,
  166. bottom:210
  167. },
  168. grid:{
  169. top:80
  170. },
  171. xAxis: {
  172. type: 'category',
  173. boundaryGap: false,
  174. data: xAxis,
  175. splitLine:{
  176. show:false
  177. },
  178. axisPointer:{
  179. //show:false
  180. },
  181. axisTick:{
  182. show:false,
  183. interval:interval[type]
  184. },
  185. axisLabel:{
  186. show:true,
  187. showMaxLabel:true,
  188. interval:interval[type],
  189. rotate:65,
  190. fontSize:10
  191. }
  192. },
  193. yAxis: {
  194. type: 'value',
  195. axisTick:{
  196. show:false
  197. },
  198. axisLabel:{
  199. show:true,
  200. showMaxLabel:true
  201. }
  202. },
  203. series: series
  204. };
  205. // 使用刚指定的配置项和数据显示图表。
  206. myChart.setOption(option);
  207. }
  208. timeSwitch(type){
  209. const {handleRangeChange,index} = this.props;
  210. handleRangeChange&&handleRangeChange(type,index);
  211. const that=this;
  212. setTimeout(()=>{
  213. that.drawChart();
  214. },300);
  215. }
  216. componentDidMount(){
  217. this.drawChart();
  218. }
  219. render(){
  220. const {type,index,endDate} = this.props;
  221. return <div className={style['cont']}>
  222. <div className={style['time-range']}>
  223. <span className={type=='year'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("year")}>近一年</span>
  224. <span className={type=='sixMonth'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("sixMonth")}>近六个月</span>
  225. <span className={type=='month'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("month")}>近一个月</span>
  226. <span className={type=='week'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("week")}>近一周</span>
  227. </div>
  228. <div className={style["chart-box"]} id={endDate?'chart'+endDate+index:'chart'+index}></div>
  229. </div>;
  230. }
  231. }
  232. export default ChartItem;