Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6911639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:58:59+00:00 2026-05-27T08:58:59+00:00

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

  • 0

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 “”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:59:00+00:00Added an answer on May 27, 2026 at 8:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the two implementations below, data.addRows([ ['2004', 1000], ['2005', 1170], ['2006', 660], ['2007', 1030]
Why doesn't this Google Chart API URL render both data sets on this XY
I want to create a Line Chart using data received by Google Analytics API.
I see this link to use google chart api for putting multiple line charts
I am trying to set Google Chart using API generated from below link Google
I am having issue with Google line chart API. When I reload the div,
I am trying to implement an Annotated Time Line Chart via Google Charts API.
I'm updating an older data visualization module which used the Google Image Chart API
Is there a way to plot a chart with Google chart API so that
I want to show concentric pie chart from google chart api but the imageData

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.