Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import * as d3 from "d3";
  3.  
  4.  
  5.  
  6.  
  7. let clock = ["", "", "00:00", "", "", "", "", "", "06:00", "", "", "", "", "", "12:00", "", "", "", "", "", "18:00", "", "", "", "", "23:00"];
  8.  
  9. // let legend = [{txt:"0-50", limit:50}, {txt:"51-100", limit:100}, {txt:"101-150", limit:150}, {txt:"151+", limit:200}];
  10.  
  11. class AQI extends Component{
  12.  
  13.  
  14. constructor(props) {
  15. super(props);
  16. this.state = {width:0};
  17. this.wrapper = {};
  18. }
  19.  
  20.  
  21.  
  22. componentDidUpdate(prevProps,prevState) {
  23.  
  24. if (prevState.width!=this.wrapper.offsetWidth) {
  25. this.setState({
  26. width:this.wrapper.offsetWidth
  27. });
  28. this.drawChart()
  29. }
  30.  
  31. }
  32.  
  33. componentDidMount() {
  34.  
  35. this.setState({
  36. width:this.wrapper.offsetWidth
  37. });
  38.  
  39. }
  40.  
  41.  
  42. drawChart(){
  43. d3.select("svg").remove();
  44. const aqi24 = ["", ""].concat(this.props.aqi24).concat([""]);
  45. const w = this.state.width;
  46. const h = this.props.height;
  47. const brickWidth = w/27;
  48. const coeff = h/200;
  49.  
  50.  
  51. const svg = d3.select("#aqiChartFrame").append("svg").attr("id", "aqiChart").style("margin", "5px").style("background", "aliceblue")
  52. .attr("width", this.props.width)
  53. .attr("height", h);
  54.  
  55. svg.selectAll("rect")
  56. .data(aqi24)
  57. .enter()
  58. .append("rect")
  59. .attr("x", (d, i) => i * brickWidth)
  60. .attr("y", (d)=>{return (d>155) ? h-155*coeff-50 : h-d*coeff-50;})
  61. .attr("height", (d)=>{return (d>155) ? 155*coeff : d*coeff})
  62. .attr("width", brickWidth)
  63. .attr("fill", (d)=>{
  64. if(d>0 && d<=15)
  65. return "#28a745";
  66. else if(d>15 && d<=30)
  67. return "#8cc440";
  68. else if(d>30 && d<=50)
  69. return "#cbe942";
  70. else if(d>50 && d<=65)
  71. return "#f1c40f";
  72. else if(d>65 && d<=80)
  73. return "#fdd038";
  74. else if(d>81 && d<=100)
  75. return "#fda930";
  76. else if(d>100 && d<=150)
  77. return "#fd7e14";
  78. else if(d>150 && d<=175)
  79. return "#e64f3d";
  80. else if(d>175 && d<=200)
  81. return "#dc3545";
  82. else if(d>200 && d<=300)
  83. return "#6f42c1";
  84. else return "#6610f2";
  85.  
  86. })
  87. .attr("rx", "20")
  88. .attr("ry", "10")
  89. .attr("opacity", "0.85")
  90. .on("mouseover", function(d){
  91. d3.select(this).style("stroke", "white").style("stroke-width", "0.2em").attr("opacity","1");
  92. })
  93. .on("mouseout", function(d){
  94. d3.select(this).style("stroke", "none").attr("opacity","0.85");
  95. });
  96.  
  97.  
  98. svg.selectAll("text")
  99. .data(aqi24)
  100. .enter()
  101. .append("text")
  102. .text((d) => d)
  103. .attr("x", (d, i) => i * brickWidth + brickWidth/8)
  104. .attr("y", (d)=>{return (d>155) ? h-155*coeff-55 : h-d*coeff-55;})
  105. .style("font-size", "0.7em");
  106.  
  107. const svgClock = d3.select("#aqiChart").append("svg").attr("width", w)
  108. .attr("height", 50).attr("y", h-50)
  109. .selectAll("text")
  110. .data(clock)
  111. .enter()
  112. .append("text")
  113. .text((d) => d)
  114. .attr("x", (d, i) => i * brickWidth + brickWidth/8)
  115. .attr("y", (d, i) => 20)
  116. .style("font-size", "0.7em");
  117.  
  118. const svg2 = d3.select("#aqiChart").append("svg").attr("width", w)
  119. .attr("height", 50).attr("y", h-50);
  120. svg2.selectAll("line")
  121. .data(aqi24)
  122. .enter()
  123. .append("line")
  124. .attr("x1", (d, i) => i * brickWidth)
  125. .attr("y1", (d, i) => 30)
  126. .attr("x2", (d, i) => i * brickWidth + brickWidth)
  127. .attr("y2", (d, i) => 30)
  128. .style("stroke","black")
  129. .style("stroke-width", "1px");
  130.  
  131. const svg3 = d3.select("#aqiChart").append("svg").attr("width", w)
  132. .attr("height", 50).attr("y", h-50);
  133. svg3.selectAll("line")
  134. .data(aqi24)
  135. .enter()
  136. .append("line")
  137. .attr("x1", (d, i) => i * brickWidth + brickWidth/2)
  138. .attr("y1", (d, i) => 25)
  139. .attr("x2", (d, i) => i * brickWidth + brickWidth/2)
  140. .attr("y2", (d, i) => 35)
  141. .style("stroke","black")
  142. .style("stroke-width", "1px");
  143.  
  144.  
  145. // const svgLegend = d3.select("#aqiChart").append("svg").attr("width", 2* brickWidth)
  146. // .attr("height", 50)
  147. // .selectAll("text")
  148. // .data(legend)
  149. // .enter()
  150. // .append("text")
  151. // .text((d) => d.txt)
  152. // .attr("x", brickWidth/4)
  153. // .attr("y", (d)=>{return h - d.limit*coeff + 50});
  154.  
  155.  
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. render(){
  163. return (
  164. <div ref={(c)=> {this.wrapper=c}} id={"aqiChartFrame"}></div>
  165. );
  166. }
  167. }
  168.  
  169. export default AQI;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement