Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript][PHP] Problem z prasowaniem daty dla amCharts
Forum PHP.pl > Forum > Przedszkole
krzysiekkurowski
Witam, chciałbym zczytać dane z bazy mysql i wyswietlić je używając skryptu amCharts.

Niestety problem w tym ze nie mogę poradzić sobie z prasowaniem daty z mysql i po wczytaniu strony nic się nie pojawia.

Problem dokładnie jest z linijką:
  1. year:<?php print ($row['dattim']); ?>,
jak mogę użyć new Date() aby skrypt zaczął działać ?

Proszę o pomoc.

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2.  
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>amCharts examples</title>
  6. <link rel="stylesheet" href="style.css" type="text/css">
  7. <script src="amcharts/amcharts.js" type="text/javascript"></script>
  8. <script type="text/javascript">
  9. var chart;
  10. var graph;
  11.  
  12.  
  13. // months in JS are zero-based, 0 means January
  14. var chartData = [
  15. <?php foreach($last24hourValues as $row) { ?>
  16. {
  17. year:<?php print ($row['dattim']); ?>,
  18. value: <?php print number_format($row['Aussen'], 2); ?>
  19. }
  20. <?php } ?>
  21.  
  22. ];
  23.  
  24.  
  25. AmCharts.ready(function () {
  26. // SERIAL CHART
  27. chart = new AmCharts.AmSerialChart();
  28. chart.pathToImages = "amcharts/images/";
  29. chart.dataProvider = chartData;
  30. chart.marginLeft = 10;
  31. chart.categoryField = "year";
  32. chart.zoomOutButton = {
  33. backgroundColor: '#000000',
  34. backgroundAlpha: 0.15
  35. };
  36.  
  37. // listen for "dataUpdated" event (fired when chart is inited) and call zoomChart method when it happens
  38. chart.addListener("dataUpdated", zoomChart);
  39.  
  40. // AXES
  41. // category
  42. var categoryAxis = chart.categoryAxis;
  43. categoryAxis.parseDates = true; // as our data is date-based, we set parseDates to true
  44. categoryAxis.minPeriod = "YYYY"; // our data is yearly, so we set minPeriod to YYYY
  45. categoryAxis.gridAlpha = 0;
  46.  
  47. // value
  48. var valueAxis = new AmCharts.ValueAxis();
  49. valueAxis.axisAlpha = 0;
  50. valueAxis.inside = true;
  51. chart.addValueAxis(valueAxis);
  52.  
  53. // GRAPH
  54. graph = new AmCharts.AmGraph();
  55. graph.type = "smoothedLine"; // this line makes the graph smoothed line.
  56. graph.lineColor = "#d1655d";
  57. graph.negativeLineColor = "#637bb6"; // this line makes the graph to change color when it drops below 0
  58. graph.bullet = "round";
  59. graph.bulletSize = 5;
  60. graph.lineThickness = 2;
  61. graph.valueField = "value";
  62. chart.addGraph(graph);
  63.  
  64. // CURSOR
  65. var chartCursor = new AmCharts.ChartCursor();
  66. chartCursor.cursorAlpha = 0;
  67. chartCursor.cursorPosition = "mouse";
  68. chartCursor.categoryBalloonDateFormat = "YYYY";
  69. chart.addChartCursor(chartCursor);
  70.  
  71. // SCROLLBAR
  72. var chartScrollbar = new AmCharts.ChartScrollbar();
  73. chartScrollbar.graph = graph;
  74. chartScrollbar.backgroundColor = "#DDDDDD";
  75. chartScrollbar.scrollbarHeight = 30;
  76. chartScrollbar.selectedBackgroundColor = "#FFFFFF";
  77. chart.addChartScrollbar(chartScrollbar);
  78.  
  79. // WRITE
  80. chart.write("chartdiv");
  81. });
  82.  
  83. // this method is called when chart is first inited as we listen for "dataUpdated" event
  84. function zoomChart() {
  85. // different zoom methods can be used - zoomToIndexes, zoomToDates, zoomToCategoryValues
  86. chart.zoomToDates(new Date(2000, 0), new Date(2003, 0));
  87. }
  88. </script>
  89. </head>
  90.  
  91. <body>
  92. <div id="chartdiv" style="width:100%; height:400px;"></div>
  93. </body>
  94.  
  95. </html>
aras785
zapodaj caly kod php smile.gif
krzysiekkurowski
  1. <?php
  2. // Make sure you set the database connection on the next line:
  3. $mysqli = new mysqli("localhost", "***", "***", "***");
  4.  
  5. /* check connection */
  6. if ($mysqli->connect_errno) {
  7. header("HTTP/1.0 500 Internal Server Error");
  8. exit();
  9. }
  10.  
  11. $lastTemps = getLastTemps($mysqli);
  12. $lastAVGTemps = getAVGTemps($mysqli);
  13. $last24hourValues = last24hourValues($mysqli);
  14.  
  15. $mysqli->close();
  16.  
  17. if($_GET['format'] == json) {
  18. header('Content-Type: application/json');
  19. print json_encode($lastTemps);
  20. }
  21.  
  22. header('Refresh: 150; url=index1.php');
  23.  
  24. function getLastTemps($mysqli) {
  25. $stmt = $mysqli->prepare("SELECT Aussen, dattim FROM dlogtab ORDER BY dattim DESC LIMIT 1");
  26. $stmt->execute();
  27. $stmt->bind_result($res['Aussen'], $res['dattim']);
  28. $stmt->fetch();
  29. return $res;
  30. }
  31.  
  32. function getAVGTemps($mysqli) {
  33. $stmt = $mysqli->prepare("SELECT AVG(Aussen) FROM dlogtab WHERE dattim >= SYSDATE() - INTERVAL 1 DAY");
  34. $stmt->execute();
  35. $stmt->bind_result($res['Aussen']);
  36. $stmt->fetch();
  37. return $res;
  38. }
  39.  
  40. function last24hourValues($mysqli) {
  41. $stmt = $mysqli->prepare("SELECT Aussen, dattim FROM dlogtab WHERE dattim >= SYSDATE() - INTERVAL 1 DAY");
  42. $stmt->execute();
  43. $stmt->bind_result($res['Aussen'], $res['dattim']);
  44. $rows = array();
  45.  
  46. $i = 0;
  47. while($stmt->fetch()) {
  48. $rows[$i] = array();
  49. foreach($res as $k=>$v)
  50. $rows[$i][$k] = $v;
  51. $i++;
  52. }
  53. return $rows;
  54. }
  55.  
  56. function tempParts($temp, $index) {
  57. $parts = explode('.', number_format($temp, 1));
  58. return $parts[$index];
  59. }
  60.  
  61. ?>
  62.  
  63.  
  64. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  65. <html>
  66.  
  67. <head>
  68. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  69. <title>amCharts examples</title>
  70. <link rel="stylesheet" href="style.css" type="text/css">
  71. <script src="amcharts/amcharts.js" type="text/javascript"></script>
  72. <script type="text/javascript">
  73. var chart;
  74. var graph;
  75.  
  76.  
  77. // months in JS are zero-based, 0 means January
  78. var chartData = [
  79. <?php foreach($last24hourValues as $row) { ?>
  80. {
  81. year:new Date(<?php print ($row['dattim']); ?>,0),
  82. value: <?php print number_format($row['Aussen'], 2); ?>
  83. }
  84. <?php } ?>
  85.  
  86. ];
  87.  
  88.  
  89. AmCharts.ready(function () {
  90. // SERIAL CHART
  91. chart = new AmCharts.AmSerialChart();
  92. chart.pathToImages = "amcharts/images/";
  93. chart.dataProvider = chartData;
  94. chart.marginLeft = 10;
  95. chart.categoryField = "year";
  96. chart.zoomOutButton = {
  97. backgroundColor: '#000000',
  98. backgroundAlpha: 0.15
  99. };
  100.  
  101. // listen for "dataUpdated" event (fired when chart is inited) and call zoomChart method when it happens
  102. chart.addListener("dataUpdated", zoomChart);
  103.  
  104. // AXES
  105. // category
  106. var categoryAxis = chart.categoryAxis;
  107. categoryAxis.parseDates = true; // as our data is date-based, we set parseDates to true
  108. categoryAxis.minPeriod = "YYYY"; // our data is yearly, so we set minPeriod to YYYY
  109. categoryAxis.gridAlpha = 0;
  110.  
  111. // value
  112. var valueAxis = new AmCharts.ValueAxis();
  113. valueAxis.axisAlpha = 0;
  114. valueAxis.inside = true;
  115. chart.addValueAxis(valueAxis);
  116.  
  117. // GRAPH
  118. graph = new AmCharts.AmGraph();
  119. graph.type = "smoothedLine"; // this line makes the graph smoothed line.
  120. graph.lineColor = "#d1655d";
  121. graph.negativeLineColor = "#637bb6"; // this line makes the graph to change color when it drops below 0
  122. graph.bullet = "round";
  123. graph.bulletSize = 5;
  124. graph.lineThickness = 2;
  125. graph.valueField = "value";
  126. chart.addGraph(graph);
  127.  
  128. // CURSOR
  129. var chartCursor = new AmCharts.ChartCursor();
  130. chartCursor.cursorAlpha = 0;
  131. chartCursor.cursorPosition = "mouse";
  132. chartCursor.categoryBalloonDateFormat = "YYYY";
  133. chart.addChartCursor(chartCursor);
  134.  
  135. // SCROLLBAR
  136. var chartScrollbar = new AmCharts.ChartScrollbar();
  137. chartScrollbar.graph = graph;
  138. chartScrollbar.backgroundColor = "#DDDDDD";
  139. chartScrollbar.scrollbarHeight = 30;
  140. chartScrollbar.selectedBackgroundColor = "#FFFFFF";
  141. chart.addChartScrollbar(chartScrollbar);
  142.  
  143. // WRITE
  144. chart.write("chartdiv");
  145. });
  146.  
  147. // this method is called when chart is first inited as we listen for "dataUpdated" event
  148. function zoomChart() {
  149. // different zoom methods can be used - zoomToIndexes, zoomToDates, zoomToCategoryValues
  150. chart.zoomToDates(new Date(2000, 0), new Date(2003, 0));
  151. }
  152. </script>
  153. </head>
  154.  
  155. <body>
  156. <div id="chartdiv" style="width:100%; height:400px;"></div>
  157. </body>
  158.  
  159. </html>
buliq
Kod
year:new Date("<?php print ($row['dattim']); ?>"),
?
W jakiej formie masz te pole dattim ? Wyciągnij z niego sam rok po stronie php i z głowy
krzysiekkurowski
Hej,

W bazie mam jako timestamp czyli (2013-07-29 10:10:10), ja wlasnie chce wczytac dane z do poziomu minut (dane w bazie beda dodawane co 10 minut).

buliq
no to dla przykładu

  1. $year = date("Y", strtotime($row['dattim']));


itd aż do minut, tylko musisz odpowiednio to ustawić w js po tym( jest w dokumentacji, obecnie twój skrypt ustawia tylko lata)
krzysiekkurowski
Witam, mogę prosić o przykład, próbuje twoje sugestie ale niestety nie działa.
brzanek
Witam sory za odkopanie ale mam podobny problem.
Tak u mnie wygląda plik odpowiedzialny za pobieranie danych z bazy danych mysql
dane.php
  1. <?php
  2. ini_set( 'display_errors', 'On' );
  3. error_reporting( E_ALL );
  4.  
  5. $con=@mysql_connect('localhost','login','haslo');
  6. mysql_select_db("nazwa_bazy_danych", $con);
  7. mysql_query("SET CHARSET utf8");
  8. $result = mysql_query("SELECT htime, htemperature FROM prognoza_godzinowa WHERE hid_miasto =1") or die(mysql_error());
  9. while($row = mysql_fetch_array($result)) {
  10. $rows[] = array(
  11. "htime" => date("d.m.Y H:i", $row['htime']),
  12. "htemperature" => $row[ 'htemperature' ]
  13. );
  14. }
  15. echo json_encode( $rows );


A tak wygląda plik wyświetlający wykres
  1. <!-- Styles -->
  2. <style>
  3. #chartdiv {
  4. width : 100%;
  5. height : 500px;
  6. }
  7.  
  8. </style>
  9.  
  10. <!-- Resources -->
  11. <script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
  12. <script src="https://www.amcharts.com/lib/3/serial.js"></script>
  13. <script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
  14. <link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
  15. <script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
  16. <!-- Chart code -->
  17. <script>
  18. var chartData = ('http://brzanek.webd.pl/charts2/dane.php');
  19. var chart = AmCharts.makeChart("chartdiv", {
  20. "type": "serial",
  21. "theme": "light",
  22. "marginRight": 40,
  23. "marginLeft": 40,
  24. "autoMarginOffset": 20,
  25. "mouseWheelZoomEnabled":true,
  26. "dataDateFormat": "YYYY-MM-DD JJ:NN",
  27. "valueAxes": [{
  28. "id": "v1",
  29. "axisAlpha": 0,
  30. "position": "left",
  31. "ignoreAxisWidth":true
  32. }],
  33. "balloon": {
  34. "borderThickness": 1,
  35. "shadowAlpha": 0
  36. },
  37. "graphs": [{
  38. "id": "g1",
  39. "balloon":{
  40. "drop":true,
  41. "adjustBorderColor":false,
  42. "color":"#ffffff"
  43. },
  44. "bullet": "round",
  45. "bulletBorderAlpha": 1,
  46. "bulletColor": "#FFFFFF",
  47. "bulletSize": 5,
  48. "hideBulletsCount": 50,
  49. "lineThickness": 2,
  50. "title": "red line",
  51. "useLineColorForBulletBorder": true,
  52. "valueField": "htemperature",
  53. "balloonText": "<span style='font-size:18px;'>[[htemperature]]</span>"
  54. }],
  55. "chartScrollbar": {
  56. "graph": "g1",
  57. "oppositeAxis":false,
  58. "offset":30,
  59. "scrollbarHeight": 80,
  60. "backgroundAlpha": 0,
  61. "selectedBackgroundAlpha": 0.1,
  62. "selectedBackgroundColor": "#888888",
  63. "graphFillAlpha": 0,
  64. "graphLineAlpha": 0.5,
  65. "selectedGraphFillAlpha": 0,
  66. "selectedGraphLineAlpha": 1,
  67. "autoGridCount":true,
  68. "color":"#AAAAAA"
  69. },
  70. "chartCursor": {
  71. "pan": true,
  72. "valueLineEnabled": true,
  73. "valueLineBalloonEnabled": true,
  74. "cursorAlpha":1,
  75. "cursorColor":"#258cbb",
  76. "limitToGraph":"g1",
  77. "valueLineAlpha":0.2,
  78. "valueZoomable":true
  79. },
  80. "valueScrollbar":{
  81. "oppositeAxis":false,
  82. "offset":50,
  83. "scrollbarHeight":10
  84. },
  85. "categoryField": "htime",
  86. "categoryAxis": {
  87. "parseDates": true,
  88. "dashLength": 1,
  89. "minorGridEnabled": true
  90. },
  91. "export": {
  92. "enabled": true
  93. },
  94. "dataProvider": chartData,
  95.  
  96.  
  97. });
  98.  
  99. chart.addListener("rendered", zoomChart);
  100.  
  101. zoomChart();
  102.  
  103. function zoomChart() {
  104. chart.zoomToIndexes(chart.dataProvider.length - 10, chart.dataProvider.length - 1);
  105. }
  106. </script>
  107.  
  108. <!-- HTML -->
  109. <div id="chartdiv"></div>

Problem w tym, że nic nie pojawia się na wykresie. Kto podpowie dlaczego tak się dzieje.
gitbejbe
Przecież widać na pierwszy rzut oka, że Twoim problemem jest brak konsoli w przeglądarce.

Ps. spoko masz syf na hostingu
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.