index.jsx 6.7 KB

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