Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { Line } from 'react-chartjs-2';
  3. import Utils from '../../utils';
  4. import Config from '../../config';
  5.  
  6. const brandPrimary = '#20a8d8';
  7. const brandSuccess = '#4dbd74';
  8. const brandInfo = '#63c2de';
  9. const brandDanger = '#f86c6b';
  10.  
  11. let group = null,
  12. chartOptions = {
  13. maintainAspectRatio: false,
  14. legend: {
  15. display: true
  16. },
  17. scales: {
  18. xAxes: [{
  19. type: 'time',
  20. gridLines: {
  21. drawOnChartArea: false,
  22. },
  23. }],
  24. yAxes: [{
  25. ticks: {},
  26. }]
  27. },
  28. elements: {
  29. point: {
  30. radius: 4,
  31. hitRadius: 10,
  32. hoverRadius: 4,
  33. hoverBorderWidth: 3,
  34. },
  35. },
  36. labelsFilter: function(val) {
  37. return 'foo';
  38. }
  39. },
  40. data = null,
  41. chart = '';
  42.  
  43. class Dashboard extends Component {
  44.  
  45. componentWillMount() {
  46. this.fetchData();
  47. }
  48.  
  49. render() {
  50. return (
  51. <div className="animated fadeIn">
  52. {chart}
  53. </div>
  54. )
  55. }
  56.  
  57. fetchData(date) {
  58. if (!date) {
  59. date = new Date();
  60. date.setHours(date.getHours() - 1);
  61. }
  62.  
  63. fetch(`${Config.API_URL}/group/${this.props.params.id}/values/${date.getTime()}`)
  64. .then(response => response.json())
  65. .then(response => {
  66. group = response;
  67. switch (group.units) {
  68. case 'ms':
  69. chartOptions.scales.yAxes[0].ticks.userCallback = function(value) {
  70. return Utils.msToString(value);
  71. };
  72. break;
  73. default:
  74. chartOptions.scales.yAxes[0].ticks.userCallback = null;
  75. break;
  76. }
  77. this.buildChart();
  78. });
  79. }
  80.  
  81. buildChart() {
  82. let data = {
  83. labels: [],
  84. datasets: [
  85. {
  86. label: 'Min',
  87. backgroundColor: convertHex(brandInfo,10),
  88. borderColor: brandInfo,
  89. pointHoverBackgroundColor: '#fff',
  90. borderWidth: 2,
  91. data: []
  92. },
  93. {
  94. label: 'Max',
  95. backgroundColor: 'transparent',
  96. borderColor: brandSuccess,
  97. pointHoverBackgroundColor: '#fff',
  98. borderWidth: 2,
  99. data: []
  100. },
  101. {
  102. label: 'Average',
  103. backgroundColor: 'transparent',
  104. borderColor: brandDanger,
  105. pointHoverBackgroundColor: '#fff',
  106. borderWidth: 1,
  107. borderDash: [8, 5],
  108. data: []
  109. }
  110. ]
  111. };
  112.  
  113. for (let i = 0; i < group.values.length; i++) {
  114. data.labels.push(group.values[i].time);
  115. data.datasets[0].data.push(group.values[i].min);
  116. data.datasets[1].data.push(group.values[i].max);
  117. data.datasets[2].data.push(group.values[i].average);
  118. }
  119.  
  120. if (group.values.length > 200) {
  121. chartOptions.elements.point.radius = 0;
  122. } else if (group.values.length > 100) {
  123. chartOptions.elements.point.radius = 1;
  124. } else if (group.values.length > 50) {
  125. chartOptions.elements.point.radius = 2;
  126. } else {
  127. chartOptions.elements.point.radius = 4;
  128. }
  129.  
  130. chart = (
  131. <div className="card">
  132. <div className="card-block">
  133. <div className="row">
  134. <div className="col-sm-5">
  135. <h4 className="card-title mb-0">{group.name}</h4>
  136. </div>
  137. <div className="col-sm-7 hidden-sm-down">
  138. <button type="button" className="btn btn-primary float-right"><i className="icon-cloud-download"></i></button>
  139. <div className="btn-toolbar float-right" role="toolbar" aria-label="Toolbar with button groups">
  140. <div className="btn-group mr-1" data-toggle="buttons" aria-label="First group">
  141. <label className="btn btn-outline-secondary">
  142. <input type="radio" name="options" id="toggle-2-days" onClick={() => {
  143. let date = new Date();
  144. date.setDate(date.getDate() - 2);
  145. this.fetchData(date);
  146. }}/> 2 Days
  147. </label>
  148. <label className="btn btn-outline-secondary">
  149. <input type="radio" name="options" id="toggle-1-day" onClick={() => {
  150. let date = new Date();
  151. date.setDate(date.getDate() - 1);
  152. this.fetchData(date);
  153. }}/> 1 Day
  154. </label>
  155. <label className="btn btn-outline-secondary">
  156. <input type="radio" name="options" id="toggle-1-day" onClick={() => {
  157. let date = new Date();
  158. date.setHours(date.getHours() - 6);
  159. this.fetchData(date);
  160. }}/> 6hr
  161. </label>
  162. <label className="btn btn-outline-secondary">
  163. <input type="radio" name="options" id="toggle-1-day" onClick={() => {
  164. let date = new Date();
  165. date.setHours(date.getHours() - 2);
  166. this.fetchData(date);
  167. }}/> 2hr
  168. </label>
  169. <label className="btn btn-outline-secondary">
  170. <input type="radio" name="options" id="toggle-1-hour" onClick={() => this.fetchData()}/> 1hr
  171. </label>
  172. </div>
  173. </div>
  174. </div>
  175. </div>
  176. <div className="chart-wrapper" style={{height: 300 + 'px', marginTop: 40 + 'px'}}>
  177. <Line data={data} options={chartOptions} height={300}/>
  178. </div>
  179. </div>
  180. <div className="card-footer">
  181. {group.description}
  182. </div>
  183. </div>
  184. );
  185.  
  186. this.setState({
  187. lastUpdate: new Date(),
  188. });
  189. }
  190. }
  191.  
  192. function convertHex(hex,opacity) {
  193. hex = hex.replace('#','');
  194. var r = parseInt(hex.substring(0,2), 16);
  195. var g = parseInt(hex.substring(2,4), 16);
  196. var b = parseInt(hex.substring(4,6), 16);
  197.  
  198. var result = 'rgba('+r+','+g+','+b+','+opacity/100+')';
  199. return result;
  200. }
  201.  
  202. export default Dashboard;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement