array (native format) passed from external php script

928 views
Skip to first unread message

Clement

unread,
Oct 12, 2012, 4:50:20 AM10/12/12
to dygraph...@googlegroups.com
Hi Dan,
I had a difficulty sending the data  in array (native format) to xmlhttprequest's innerhtml.response.text in simple way.
The arrangement of data in PHP script do not look correct.
Please advise. Thanks

<?php
echo "[[1, 3.65, 5.00, 4.53, 5.01, 10.50, 0.03, 9.05, 5.05, 5.22, 6.23],
[7, 3.65, 5.03, 4.50, 5.02, 9.50, 0.05, 9.15, 5.55, 5.20, 6.23],
[8, 3.67, 5.00, 4.53, 4.99, 9.00, 0.04, 9.30, 5.10, 2.30, 6.22],
[12, 3.65, 5.04, 4.53, 4.99, 10.05, 0.35, 9.00, 5.23, 5.20, 6.21],
[16, 3.66, 5.00, 4.50, 4.98, 10.50, 1.01, 9.01, 5.20, 5.10, 6.24],
18, 3.65, 5.02, 4.70, 5.00, 9.80, 0.45, 9.14, 5.63, 5.15, 6.23]],
{["Batch","S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"]}";
?>

and

...
xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    z = new Dygraph
                     (       
                      document.getElementById("showrealchart").innerHTML=xmlhttp.responseText
                     );
               }
            }...

Clement

unread,
Oct 12, 2012, 5:54:43 AM10/12/12
to dygraph...@googlegroups.com
Oh ya that's right, I forgot to add keyword "labels" but it still does not work


<?php
echo "[[1, 3.65, 5.00, 4.53, 5.01, 10.50, 0.03, 9.05, 5.05, 5.22, 6.23],
[7, 3.65, 5.03, 4.50, 5.02, 9.50, 0.05, 9.15, 5.55, 5.20, 6.23],
[8, 3.67, 5.00, 4.53, 4.99, 9.00, 0.04, 9.30, 5.10, 2.30, 6.22],
[12, 3.65, 5.04, 4.53, 4.99, 10.05, 0.35, 9.00, 5.23, 5.20, 6.21],
[16, 3.66, 5.00, 4.50, 4.98, 10.50, 1.01, 9.01, 5.20, 5.10, 6.24],
18, 3.65, 5.02, 4.70, 5.00, 9.80, 0.45, 9.14, 5.63, 5.15, 6.23]],
{labels: ["Batch","S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"]}";

?>

regards
Clement

Dan Vanderkam

unread,
Oct 12, 2012, 11:11:21 AM10/12/12
to cleme...@gmail.com, dygraph...@googlegroups.com
You need to parse the response text at some point, probably via JSON.parse().

- dan

Clement

unread,
Oct 22, 2012, 5:29:01 AM10/22/12
to dygraph...@googlegroups.com, cleme...@gmail.com
Hi Dan.
I am not sure why my JSON script or JSONparse(0 did not work.
This time I am trying to convert JSON script to Javascript in HTML before I try again in PHP file so that I can understand  it better.

However I cannot figure it out why it did not work after studying JSON.

Please see my attached html.

And Please advise how to construct X, Y  values and headers in JSON format properly.

Thanks in advance
 Clement
json_js_dygraph.html

Clement

unread,
Oct 22, 2012, 11:31:47 PM10/22/12
to dygraph...@googlegroups.com, cleme...@gmail.com
I think the right way is :-
<script>
        var chart5_table ="{{"data": [ [1,10,100], [2,20,80], [3,50,60], [4,70,80] ]}, { "labels": [ "x", "A", "B" ]}}";
        e = new Dygraph
        (
            document.getElementById("chart5"),JSON.parse(chart5_table)
        );
    </script>

However, it does not seem to work.  The data and labels I reied bild are in JSON object,  however It does not work.
What am I missing?
Please advise
Clement

Clement

unread,
Oct 22, 2012, 11:39:55 PM10/22/12
to dygraph...@googlegroups.com, cleme...@gmail.com
Ok Dan, I guess the correct data format is shown  below  but there is no chart to display. So where did I go wrong?

var chart5_table = '{"data": [ [1,10,100], [2,20,80], [3,50,60], [4,70,80] ], "labels": [ "x", "A", "B" ]}';

Clement

Dan Vanderkam

unread,
Oct 23, 2012, 1:20:45 PM10/23/12
to cleme...@gmail.com, dygraph...@googlegroups.com
You're not passing parameters in correctly. The data is the second
parameter, the options are the third. You're passing in a single
second parameter which contains both of those.

- dan

Clement

unread,
Oct 23, 2012, 9:59:29 PM10/23/12
to dygraph...@googlegroups.com, cleme...@gmail.com
Hi Dan . Thank you for pointing out my mistake
So I removed the third parameter, the option as shown below.

<script>
var chart5_table = '{"data": [ [1,10,100], [2,20,80], [3,50,60], [4,70,80] ]}';

var chart5_tableA = JSON.parse(chart5_table); //Transform JSON string to javascript object

      
        e = new Dygraph
        (
            document.getElementById("chart5"), chart5_tableA
        );
</script>

Again, the chart will not be displayed. Please advise.
Thanks
Clement

Dan Vanderkam

unread,
Oct 23, 2012, 10:43:13 PM10/23/12
to cleme...@gmail.com, dygraph...@googlegroups.com
Please read http://dygraphs.com/data.html.

- dan

megana

unread,
Apr 9, 2014, 7:30:18 AM4/9/14
to dygraph...@googlegroups.com, cleme...@gmail.com

I think you can look at this tutorial for sending data in native array form.
http://geekynnotsogeeky.wordpress.com/tag/dygraphs/

Nalzam Ahmad

unread,
Apr 15, 2015, 6:23:06 AM4/15/15
to dygraph...@googlegroups.com
hello all, 

i retrieve dygraph data thru ajax and the return valus is in javascript variable. the format is shown below:

[[new Date('2015/02/01 00:00'),1099],[new Date('2015/02/01 00:01'),1071],[new Date('2015/02/01 00:02'),1099],[new Date('2015/02/01 00:03'),1078],[new Date('2015/02/01 00:04'),1062]],

the problem is dygraph not recognize the javascript variable that contains the data like above.

how to assign data to dygraph using javascript variable in proper way?

Dan Vanderkam

unread,
Apr 15, 2015, 9:59:52 AM4/15/15
to dygraph...@googlegroups.com
Please give a specific demo link, e.g. via dygraphs.com/fiddle.

--
You received this message because you are subscribed to the Google Groups "dygraphs-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dygraphs-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nalzam Ahmad

unread,
Apr 16, 2015, 10:21:38 AM4/16/15
to dygraph...@googlegroups.com
i paste my code here for your evaluation

<script LANGUAGE="JavaScript">    
function graphpower(x,y) {    
    $.ajax({
    async: true,
    url: "powerajax.php",
    type: "POST",
    data: {
        tfrom: $("#timefrom").val(),
        tto: $("#timeto").val(),
        },

    success: function (Str) {  
    document.getElementById("chart").innerHTML = "";
    plotgraph(Str);
       
}

the Str contains the data that will be use by dygraph to plot graph.

below is powerajax.php

<?php

$connection
= mysqli_connect($mysql_host, $mysql_user, $mysql_pw) or die("Verbindung zur Datenbank fehlgeschlagen.");
mysqli_select_db
($connection,$mysql_db) or die("Datenbank konnte nicht ausgewaehlt werden.");

$abfrage
= "SELECT DATE_FORMAT(datetime, '%Y/%m/%d %H:%i') as datetime2, value as value2 FROM building_power where datetime>'2015-02-01' and datetime<='2015-02-28' order by datetime";      

echo
"[";                                  // start of the 2 dimensional array

while ($row = $ergebnis->fetch_assoc()) {
    echo
"["."new Date('".$row["datetime2"]."')".",".$row["value2"]."],<br>";    
}

echo
"]";
?>

the problem is how can i parser Str variable to dygraph data?

i found more example dygraph data ia based on csv or hardcode data, not thru ajax.



Rajesh Natarajan

unread,
Dec 8, 2015, 3:19:48 AM12/8/15
to dygraphs-users
Hi Ahmad,  Have you fixed this one. Am also having same problem.
Reply all
Reply to author
Forward
0 new messages