0

I am using charts.js plugin to create charts. It is using a javascript array data to generate chart.

var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.2)",
            strokeColor: "rgba(220,220,220,1)",
            pointColor: "rgba(220,220,220,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.2)",
            strokeColor: "rgba(151,187,205,1)",
            pointColor: "rgba(151,187,205,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};

I want to use ajax to return results for different range of time. How can i generate a response with php so that when i read it in ajax success i can use it the same way i am using var datato generate initial chart.

        $.ajax('/chart.php', {
            type: 'POST',
            data: {range: range},
            dataType: 'json',
            success: function(data) {
                console.log(data);
            }
        });
2
  • How does the data in your success() function differ from the structure you've shown above? Commented May 28, 2015 at 10:26
  • It would just have different values in first array "labels" and sub arrays "data" will have the same number of values as "labels" first array. Commented May 28, 2015 at 10:30

1 Answer 1

1

What you want is to create an array of objects, then use json_encode and it should produce what you need. Try this:

$array = array();
$dataset = new stdClass;
$dataset->label = "My first dataset";
// repeat for each field
$array[] = $dataset;
echo json_encode($array);

What we can do to shorten this is rather than declare a new dataset each time we can use an array and typecast it to an object like this:

$datasets = array();
$datasets[] = (object) array(
    'label' => "My First dataset",
    'fillColor' => "rgba(220,220,220,0.2)",
    'strokeColor' => "rgba(220,220,220,1)",
    'pointColor' => "rgba(220,220,220,1)",
    'pointStrokeColor' => "#fff",
    'pointHighlightFill' => "#fff",
    'pointHighlightStroke' => "rgba(220,220,220,1)",
    'data' => [65, 59, 80, 81, 56, 55, 40]
);

Check out this eval.in for a working example.

Update

For your complete structure you need:

$data = (object) [
    'labels' => ["January", "February", "March", "April", "May", "June", "July"],
    datasets => []
];

$data->datasets[] = (object) [
    'label' => "My First dataset",
    'fillColor' => "rgba(220,220,220,0.2)",
    'strokeColor' => "rgba(220,220,220,1)",
    'pointColor' => "rgba(220,220,220,1)",
    'pointStrokeColor' => "#fff",
    'pointHighlightFill' => "#fff",
    'pointHighlightStroke' => "rgba(220,220,220,1)",
    'data' => [65, 59, 80, 81, 56, 55, 40]
];

echo json_encode($data);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for reply. What do i do with labels: ["January", "February", "March", "April", "May", "June", "July"], and how to put both "labels" array and multiple "datasets" in one 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.