Output date as year-month

37 views
Skip to first unread message

Christian Schnellrieder

unread,
Jul 6, 2016, 6:46:26 AM7/6/16
to Google Visualization API
Hello.

Since some days i try to create a webpage with a google chart. The input is a json formated file.
Everything works except the display of the date. Target is "2016-01" or "2016-Jan" but what i get is only "Thu Dec 31 2016, etc etc"





























How can i change the date to year and month?
Thank you very much for any hint.
Christian

Source Code:
graph.html

<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">

var options = {
        animation: {
                duration: 3000,
                startup: true //This is the new option
            },
        title: 'Verbrauch',
        hAxis: {
          title: 'month',
          format: 'Y',
        },
        vAxis: {
          title: 'kWh'
        },
        width: 1200,
        height: 500

      };

    // Load the Visualization API and the piechart package.
//    google.charts.load('current', {'packages':['corechart']});
        google.charts.load('visualization', '1.1', {'packages': ['corechart']});


    // Set a callback to run when the Google Visualization API is loaded.
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
      var jsonData = $.ajax({
          url: "getData.php",
          dataType: "json",
          async: false
          }).responseText;

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(jsonData);

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));

        chart.draw(data, options);
    }

    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>


data.json:
{
        "cols": [
                {"label":"Date","type":"string"},
                {"label":"Basis Tarif","type":"number"},
                {"label":"H/N Tarif","type":"number"},

                ],

        "rows": [
                {"c":[{"v":"Date(1451601300000)"},{"v":"355.228"},{"v":"179.224"}] },
                {"c":[{"v":"Date(1451602800000)"},{"v":"373.213"},{"v":"290.455"}] },
                {"c":[{"v":"Date(1454281200000)"},{"v":"379.148"},{"v":"201.963"}] },
                {"c":[{"v":"Date(1456786800000)"},{"v":"374.395"},{"v":"178.306"}] },
                {"c":[{"v":"Date(1459461600000)"},{"v":"361.792"},{"v":"78.485"}] },
                {"c":[{"v":"Date(1462053600000)"},{"v":"347.246"},{"v":"51.025"}] },
                {"c":[{"v":"Date(1464732000000)"},{"v":"347.201"},{"v":"39.039"}] },
                ]

}



getData.php
<?php

// It reads a json formatted text file and outputs it.

$string = file_get_contents("data.json");
echo $string;

// Instead you can query your database and parse into JSON.

?>



Daniel LaLiberte

unread,
Jul 13, 2016, 7:15:58 PM7/13/16
to Google Visualization API
I'm not sure about the axis labels yet, but your date data is suspect, and moreover, your first column has type 'string'.  For 'date' values, you should probably use the format "Date(yyyy, mm, dd)".  If you use a single number, the number is now interpreted as a millisecond value.  But this is in Universal time, rather than in your local time, so that might be why you are getting a December 31 value.  Hope that helps.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/ee3d0654-b8f7-420d-b32d-16d6f97f8e96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Christian Schnellrieder

unread,
Jul 14, 2016, 10:34:41 AM7/14/16
to Google Visualization API
Thank you for your answer.

I was able to fix the problem:
Important was to change the line:
google.charts.load('visualization', '1.1', {'packages': ['corechart']});
to:
google.charts.load('visualization', 'current', {'packages': ['corechart','bar']});



Now i was able to set the first colum to "date"


{
       
"cols": [
               
{"label":"Date","type":"date"},

               
{"label":"Basis Tarif","type":"number"},


               
{"label":"H/N Tarif","type":"number"},


               
],


       
"rows": [

         
{"c":[{"v":"Date(1443650400000)"},{"v":"217.900"},{"v":"84.969"}]},
         
{"c":[{"v":"Date(1446332400000)"},{"v":"357.905"},{"v":"138.326"}]},
         
{"c":[{"v":"Date(1448924400000)"},{"v":"355.228"},{"v":"179.224"}]},
.....


The result:



Greetings from Austria,
Christian
Reply all
Reply to author
Forward
0 new messages