index.jsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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)+":00:00");
  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. let xAxisArr = [...xAxis];
  134. const interval = {
  135. week:23,
  136. month:4,
  137. sixMonth:9,
  138. year:60
  139. };
  140. data&&data.map((it)=>{
  141. let values=new Array();
  142. let name='';
  143. it&&it.creatTime.map((x,i)=>{
  144. inx=xAxis.findIndex((a)=>{
  145. name=type=='week'?x.substr(0,13):x.substr(0,10);
  146. return a.substr(0,13)==name;
  147. }); //日期对应横坐标的位置
  148. if(inx!=-1){
  149. if(type=='week'){
  150. xAxisArr[inx] = x;
  151. }
  152. values[inx] = it.indexValue[i]; //值对应横坐标的位置
  153. }
  154. });
  155. names.push(it.itemName);
  156. series.push({
  157. name: it.itemName,
  158. type: 'line',
  159. data: values,
  160. showAllSymbol:true
  161. });
  162. });
  163. // 指定图表的配置项和数据
  164. var option = {
  165. tooltip: {
  166. trigger: 'axis',
  167. textStyle:{
  168. fontSize:12
  169. }
  170. },
  171. legend: {
  172. type:'scroll',
  173. /*formatter: function (name) {
  174. return name.split("-")[name.split("-").length-1];
  175. },*/
  176. data:names,
  177. padding:0,
  178. itemHeight:16,
  179. },
  180. grid:{
  181. top:80
  182. },
  183. xAxis: {
  184. type: 'category',
  185. boundaryGap: false,
  186. data: xAxisArr,
  187. splitLine:{
  188. show:true
  189. },
  190. axisPointer:{
  191. //show:false
  192. },
  193. axisTick:{
  194. show:false,
  195. interval:interval[type]
  196. },
  197. axisLabel:{
  198. show:true,
  199. showMaxLabel:true,
  200. interval:interval[type],
  201. rotate:65,
  202. fontSize:10,
  203. formatter: function(value,index){
  204. return value.substr(0,10);
  205. }
  206. }
  207. },
  208. yAxis: {
  209. type: 'value',
  210. axisTick:{
  211. show:false
  212. },
  213. axisLabel:{
  214. show:true,
  215. showMaxLabel:true
  216. }
  217. },
  218. series: series
  219. };
  220. // 使用刚指定的配置项和数据显示图表。
  221. myChart.setOption(option);
  222. }
  223. timeSwitch(type){
  224. const {handleRangeChange,index} = this.props;
  225. handleRangeChange&&handleRangeChange(type,index);
  226. const that=this;
  227. setTimeout(()=>{
  228. that.drawChart();
  229. },300);
  230. }
  231. componentDidMount(){
  232. this.drawChart();
  233. }
  234. render(){
  235. const {type,index,endDate} = this.props;
  236. return <div className={style['cont']}>
  237. <div className={style['time-range']}>
  238. <span className={type=='year'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("year")}>近一年</span>
  239. <span className={type=='sixMonth'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("sixMonth")}>近六个月</span>
  240. <span className={type=='month'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("month")}>近一个月</span>
  241. <span className={type=='week'?style['range']+" "+style['on']:style['range']} onClick={()=>this.timeSwitch("week")}>近一周</span>
  242. </div>
  243. <div className={style["chart-box"]} id={endDate?'chart'+endDate+index:'chart'+index}></div>
  244. </div>;
  245. }
  246. }
  247. export default ChartItem;