Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. <?php
  2. // mysql database connection details
  3. $host = "localhost";
  4. $username = "user";
  5. $password = "pass";
  6. $dbname = "basedados";
  7.  
  8. // open connection to mysql database
  9. $connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
  10.  
  11. // fetch mysql table rows
  12. $sql = "SELECT `tempo`, `s1`, `s2`, `s3` FROM `tabela`";
  13. $result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
  14. $f = "perfiskwh.csv";
  15. $fp = fopen('perfiskwh.csv', 'w+');
  16.  
  17.  
  18. while($row = mysqli_fetch_assoc($result))
  19. {
  20. fputcsv($fp,$row);
  21. }
  22. // read into array
  23. $arr = file($f);
  24.  
  25. // edit first line
  26. $arr[0] = 'Data,KWh1,KWh2,KWh3,'. "n";
  27.  
  28. // write back to file
  29. file_put_contents($f, implode($arr));
  30. fclose($fp);
  31. //close the db connection
  32. mysqli_close($connection);
  33.  
  34. ?>
  35.  
  36. $.get('dados/perfiskwh.csv', function (csvFile) {
  37. var data = parseCSVData(csvFile);
  38. var highchartsOptions = Highcharts.setOptions({
  39. lang: {
  40. loading: 'Aguarde...',
  41. months: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
  42. weekdays: ['Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado'],
  43. shortMonths: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
  44. exportButtonTitle: "Exportar",
  45. printButtonTitle: "Imprimir",
  46. rangeSelectorFrom: "De",
  47. rangeSelectorTo: "Até",
  48. rangeSelectorZoom: "Periodo",
  49. downloadPNG: 'Descarregar gráfico como imagem PNG',
  50. downloadJPEG: 'Descarregar gráfico como imagem JPEG',
  51. downloadSVG: 'Descarregar gráfico como imagem SVG',
  52. downloadPDF: 'Salvar em documento PDF',
  53. contextButtonTitle: 'Opções',
  54. noData: 'Sem dados para mostrar',
  55. printChart: 'Imprimir gráfico',
  56. numericSymbols: null,
  57. // resetZoom: "Reset",
  58. // resetZoomTitle: "Reset,
  59. thousandsSep: ".",
  60. decimalPoint: ','
  61. }
  62. }
  63. );
  64. $('#container').highcharts('StockChart', {
  65. navigation: {
  66. buttonOptions: {
  67. enabled: true,
  68. }
  69. },
  70. colors:[
  71. '#993333',
  72. '#404040',
  73. '#003366',
  74. '#006600',
  75. '#CC3300'
  76. ],
  77. credits: {
  78. enabled: false
  79. },
  80. rangeSelector : {
  81. selected: 1,
  82. allButtonsEnabled: true,
  83. buttons: [{
  84. type: 'day',
  85. count: 1,
  86. text: '1 dia'
  87. }, {
  88. type: 'week',
  89. count: 1,
  90. text: '1 Semana'
  91. }, {
  92. type: 'all',
  93. text: 'Tudo'
  94. }],
  95. buttonTheme: {
  96. width: 75,
  97. style: {
  98. color: '#333',
  99. fontWeight: 'bold',
  100. fontSize: '14px',
  101. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  102.  
  103. },
  104. },
  105.  
  106. },
  107. chart: {
  108. renderTo: 'container',
  109. backgroundColor: {
  110. linearGradient: [500, 400, 250, 0],
  111. stops: [
  112. [0, '#F0F7FF'],
  113. [1, '#F0F7FF']
  114. ]
  115. },
  116. style: {
  117. color: '#333',
  118. fontWeight: 'bold',
  119. fontSize: '14px',
  120. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  121.  
  122. },
  123. type: 'spline'
  124. },
  125.  
  126. title: {
  127. text: 'Perfis de Consumos',
  128. style: {
  129. color: '#333',
  130. fontWeight: 'bold',
  131. fontSize: '20px',
  132. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  133.  
  134. }
  135. },
  136. xAxis: {
  137. labels: {
  138. style: {
  139. color: 'black',
  140. fontWeight: 'bold',
  141. fontSize: '12px',
  142. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  143. }
  144. },
  145. type: 'datetime',
  146. tickInterval: 3600 * 1000,
  147. categories: ['Mar','Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb']
  148.  
  149. },
  150. tooltip: {
  151. valueDecimals: 1,
  152. valueSuffix: ' KWh'
  153. },
  154. yAxis: {
  155. labels: {
  156. style: {
  157. color: 'black',
  158. fontWeight: 'bold',
  159. fontSize: '12px',
  160. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  161. }
  162. },
  163. opposite: false,
  164. title: {
  165. text: 'Consumos da Energia [KWh]',
  166. style: {
  167. color: '#333',
  168. fontWeight: 'bold',
  169. fontSize: '16px',
  170. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  171. }
  172. }
  173. },
  174. navigator:{
  175. xAxis: {
  176. style: {
  177. color: '#333',
  178. fontWeight: 'bold',
  179. fontSize: '16px',
  180. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  181. }
  182. },
  183. enabled: true,
  184. handles: {
  185. backgroundColor: 'Black',
  186. borderColor: 'White'
  187. },
  188. series: {
  189. color: '#0000FF',
  190. lineWidth: 1
  191. },
  192. outlineColor: 'black',
  193. outlineWidth: 1,
  194. },
  195. series: [{
  196. name:'Distribuidor',
  197. data: data[0],
  198. }, {
  199. name: 'Edificios',
  200. data: data[1]
  201. }, {
  202. name: 'Avac',
  203. data: data[2]
  204. }]
  205. });
  206. });
  207.  
  208. function parseCSVData(csvFile) {
  209. //Array para armazenar dados do Gráfico
  210. var Geral= [];
  211. var Distribuidor= [];
  212. var AVAC= [];
  213.  
  214. var Data_inicio = "January 1, 2014";
  215. var Data_hoje = new Date();
  216.  
  217. //Separar os dados do CSV em linhas
  218. var lines= csvFile.split("n");
  219.  
  220. //Verifica todas as linhas do CSV
  221. $.each(lines, function (lineNumber, line){
  222. if(lineNumber !=0){ //Salta a linha do cabeçalho
  223. var fields = line.split(",");
  224. if(fields.length==4 && Date.parse(fields[0]) >= Date.parse(Data_inicio) && Date.parse(fields[0]) <= Date.parse(Data_hoje)) {//Salta as linhas invalidas e começa na data especificada até ao presente dia
  225. var timestamp = Date.parse(fields[0]);
  226. var data1 = parseInt(fields[1]);
  227. var data2 = parseInt(fields[2]);
  228. var data3 = parseInt(fields[3]);
  229.  
  230. Geral.push([timestamp, data1]);
  231. Distribuidor.push([timestamp, data2]);
  232. AVAC.push([timestamp, data3]);
  233.  
  234. }
  235. }
  236. });
  237. return [Geral,Distribuidor,AVAC];
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement