1

Im retrieving JSON Data and assigning those into Javascript and display Chart (im using Chart.js).

[{"name":"yasiru","col1":"300","col2":"50","col3":"100","col4":"40","col5":"120"}]

 var pieData = [
        {
          value: obj[0].col1,
          color:"#F7464A",
          highlight: "#FF5A5E",
          label: obj[0].name
        },
        {
          value: 50,
          color: "#46BFBD",
          highlight: "#5AD3D1",
          label: "Green"
        },
        {
          value: 100,
          color: "#FDB45C",
          highlight: "#FFC870",
          label: "Yellow"
        }
      ];

when i run this. label : obj[0].name correctly show the Yasiru name without any problem. but Value is not. so i tried just alert(obj[0].col1); then it shows 300 as i needed. am i doing something wrong?

Here PHP code im using. FYI

 $row = mysqli_fetch_array($result);
    $response = array('name' => $row['username'],
    'col1' => $row['first'],
    'col2' => $row['second'],
    'col3' => $row['third'],
    'col4' => $row['fourth'],
    'col5' => $row['five']
);

    $responses[]=$response;
3
  • 1
    Are you trying to get "value" or "Value"? They are going to be different properties. Commented Jan 25, 2015 at 7:34
  • 1
    This works fine for me. Are you sure you're using pieData[0].value, not pieData[0].Value? The capitalization matters. Commented Jan 25, 2015 at 7:34
  • @SearchAndResQ i dont know how to remove it. im retrieving it from PHP server side. ill add that code as well Commented Jan 25, 2015 at 7:35

2 Answers 2

1

obj[0].col1 is a string, try checking the docs to see if chart values can be defined as strings, or only numbers. In this case, it seems like it might only accept numbers.

In the meantime, using parseInt should work fine:

value: parseInt(obj[0].col1)
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is not working because your values are strings and Chart.js expects only integers. Try the following JSON code instead:

[{"name":"yasiru", "col1": 300, "col2": 50, "col3": 100, "col4": 40,"col5": 120}]

By the way, prefer using the console when debugging, instead of alert(): console.log()

1 Comment

How to remove "" when i retrieve the Data from PHP? please check my PHP Code

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.