1

In the google chart api, the data for the chart is set by this line:

data.addRows([
  ['2004', 1000, 400],
  ['2005', 1170, 460],
  ['2006',  860, 580],
  ['2007', 1030, 540]
]);

I want to be able to set this data from data in my database. Below is an image of my database: enter image description here

I want to take all values of salePrice and unitPrice and display the values on a line chart that corresponds to the period they were created.

Here is my code:

<?php 

include("getteam.php");


    $saleprice = mysql_query("

    SELECT `outputValue` FROM `output` WHERE `teamID` = '$teamID' && `outputType` = 'salePrice'

    ")or die($saleprice."<br/><br/>".mysql_error());

    // set ID's = to a variable and now get Outputs for each variable(teamID)
    $salepriceNumR  = mysql_num_rows($saleprice);


            $sPrice = array();

            $i="0";


            while ($i<$salepriceNumR && $row = mysql_fetch_assoc($saleprice))
            {

            $sPrice[$i] = $row['outputValue'];
            $i++;

            }




    $unitprice = mysql_query("

    SELECT `outputValue` FROM `output` WHERE `teamID` = '$teamID' && `outputType` = 'unitPrice'

    ")or die($unitprice."<br/><br/>".mysql_error());

    // set ID's = to a variable and now get Outputs for each variable(teamID)
    $unitpriceNumR  = mysql_num_rows($unitprice);


            $uPrice = array();

            $i="0";


            while ($i<$unitpriceNumR && $row = mysql_fetch_assoc($unitprice))
            {

            $uPrice[$i] = $row['outputValue'];
            $i++;


            }



$chartrow = array();

for ($i = 0; $i < $unitpriceNumR; $i++ )
{

$chartrow[$i] = "['".$i."',".$sPrice[$i].", ".$uPrice[$i]."]";

}

switch ($currentStage) {

case "0":
case "1":
    $value = $chartrow[0]; 

    break;

case "2":
    $value = $chartrow[0].",".$chartrow[1];

    break;

case "3":
    $value = $chartrow[0].",".$chartrow[1].",".$chartrow[2];

    break;


default:
    $value = $chartrow[0]; 
    // You should have some default value, seriously!!!
}               




?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows(JSON.parse( [<?php echo json_encode($value); ?>] )); 

var options = {
  width: 400, height: 240,
  title: 'Company Performance'
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>

<div id="chart_div"></div>

The problem is the way I am setting $value it is a string and so the data is not outputted correctly. Is there a way I can set $value so I can use it as intended?

When $value is echoed ($currentStage being the value 3) this is outputted: ['0',0, 0],['1',65, 35],['2',88, 35]

However when I view source I am getting:

 data.addRows(JSON.parse( ["['0',0, 0],['1',65, 35],['2',88, 35]"] )); 

I need to get rid of the "".

2
  • When I view source I am getting data.addRows(JSON.parse( ["['0',0, 0],['1',65, 35],['2',88, 35]"] )); I need to get rid of the ". Commented Dec 10, 2011 at 13:56
  • Please see this link for more info: stackoverflow.com/questions/8452903/… Commented Dec 10, 2011 at 14:04

1 Answer 1

2

You have to parse your JSON string :

data.addRows(JSON.parse( <?php echo json_encode($value); ?> ));  

If I'm not mistaken that should solve your problem.

Edit In fact I'm not sure I understand why you're setting you $value variable to a string; the whole point of json_encode being that you can directly encode objects or arrays.

Edit 2 : Here's how I see it, but my php is more than rusty.

<?php

$chartrow = array(); 

for ($i = 0; $i < $unitpriceNumR; $i++ ) 
{  
    $chartrow[$i] = array( (string)$i, (int)$sPrice[$i], (int)$uPrice[$i] );
} 

switch ($currentStage) { 
    case "0": 
    case "1": 
        $value = $chartrow[0];  
    break; 

    case "2": 
        $value = array($chartrow[0], $chartrow[1]);
        break; 

    case "3": 
        $value = array($chartrow[0], $chartrow[1], $chartrow[2]); 
    break;
    default: 
        $value = $chartrow[0];  
}                

Edit 3 I edited the js snippet, I don't know why I left the brackets around the php tags.

Edit 4 Edited the php code to the working version.

Sign up to request clarification or add additional context in comments.

14 Comments

I have tried this but still nothing. When I view source I get this data.addRows(JSON.parse( ["['0',0, 0],['1',65, 35],['2',88, 35]"] )); The " should not be there.
Im using chrome, same result for view source in IE. Dont have firefox downloaded so can't check.
ok the reason I am doing this is because of this:stackoverflow.com/questions/8452903/…
Im just confused on how to set $value
I think that you should set $valueitself to be an array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.