Create Chart.js pie chart data from PHP

Create Chart.js pie chart data from PHP

chart js php mysql tutorial
how to create line chart in php and mysql
create bar graph in php using data from mysql
php charts free
how to create pie chart in php using database values
how to create graph in php using database
chart.js examples
chart.js download

I am working with chart.js and I am trying to get data from PHP but I'm having an issue.

I have the following code:

$dataArray = $this->getBlankSupportArray("New");
            $dataArray = $this->getBlankSupportArray("In Progress");
            $dataArray = $this->getBlankSupportArray("Neutralised Parked");
            $dataArray = $this->getBlankSupportArray("Neutralised Pending Shout");
            $dataArray = $this->getBlankSupportArray("Neutralised Pending Shout");
            $dataArray = $this->getBlankSupportArray("Ready To Close");

The getBlankSupportArray loos like the following:

private function getBlankSupportArray($issueStatus)
        {
            $array[$issueStatus]["value"] = 0;
            $array[$issueStatus]["color"] = "#ffffff";
            $array[$issueStatus]["highlight"] = "#ffffff";
            $array[$issueStatus]["label"] = $issueStatus;
            return $array;
        }

Below is the data I am looping round from a mysql result and adding to the array

while ($myrow = $result->fetch_array())
                {
                    $issueStatus = $myrow["IssueStatus"];
                    $supportOverview[$issueStatus]["value"] = $myrow["COUNT(*)"];
                    $supportOverview[$issueStatus]["color"] = "#F7464A";
                    $supportOverview[$issueStatus]["highlight"] = "#FF5A5E";
                    $supportOverview[$issueStatus]["label"] = $issueStatus;
                }

I am then json_encoding this array.

I think the json_encoding is returning the data in a format not suitable chart.js to process.

The documentation for chart.js says that the data object should be in the following format

var data = [
    {
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red"
    },
    {
        value: 50,
        color: "#46BFBD",
        highlight: "#5AD3D1",
        label: "Green"
    },
    {
        value: 100,
        color: "#FDB45C",
        highlight: "#FFC870",
        label: "Yellow"
    }
]

So my question is, how do I get PHP to generate an object that can be sent back to javascript and decoded to be passed into chart.js.

Thans for any help you can provide.


You need to convert the values in the JSON to numbers, JSON encodes them as strings.

parseInt() 

in Javascript should do the trick.

I got confused with this as well, kept me occupied for weeks.

For PHP Var

var pie = [{ value: parseInt(<?PHP VARIABLE?>), color: "#F7464A" }]

Creating Dynamic Data Graph using PHP and Chart.js, Understanding data becomes easy and obvious with the use of graphs. There are various charting libraries like Google Charts, Highcharts, Chart. So does that mean I need to create an object separately and loop around the json to create the array myself. I was hoping PHP could send the response and I could just put this response straight into chart.js without needing to manually parse the json – Boardy Apr 10 '15 at 20:12


Hi I was facing the same problem and i found solution. which is working absolutly fine.

My json data [{"week_name":"18","taskcount":"4"},{"week_name":"16","taskcount":"17"},{"week_name":"15","taskcount":"11"},{"week_name":"13","taskcount":"20"}] so,

$obj = json_decode($jsonData);

var PieData = [<?php for($i=0;$i<count($obj);$i++){ ?>
{

    value: <?php echo $obj[$i]->taskcount;?>,

    label: 'Week '+<?php echo $obj[$i]->week_name;?>

  },
          <?php } ?>
];

Chart Js Using PHP, MySql And Javascript, In this tutorial we will learn to create doughnut chart using data from MySQL (​MariaDB) table and PHP. Using ChartJS 2.x Download. Note! You can get the code  I am creating a pie chart using chart.js; however, the value I want in the pie chart is a PHP variable. How do I add it to the pie chart? Currently I have this for the data for the pie chart: var pieData = [ { value: 40, color:"green" }, { value : 40, color : "red" } ]; However, I need to replace the two 40s with PHP variables.


For all those still wondering how to pass an integer PHP variable as a value in Chartjs javascript here's how:

var PieData        = [
      {
        value    : parseInt(<?php echo $variable; ?>),
        color    : '#00c0ef',
        highlight: '#00c0ef',
        label    : 'Blue'
      },

I used this with Adminlte 3 - Hope it helps!

ChartJS, In this tutorial we will learn to draw pie chart using ChartJS and some static data. In this post, we will cover the following topics: 1.0 Dummy Database for Dynamic Pie Chart 2.0 Using LibChart to Create Dynamic Charts (PHP) 3.0 Using the Google Chart Tools (JavaScript) The first way I will give you is generating a chart using PHP, and in the second way, we will use JavaScript.


How to create a pie chart using ChartJS - ChartJS, Pie and doughnut charts are probably the most commonly used charts. the arc of each segment shows the proportional value of each piece of data. We can also change these default values for each Doughnut type that is created, this  Customizing the look and feel of chart is really easy in CanvasJS. Given example shows Pie Chart with index / data labels placed inside the slice of Pie. It also includes PHP source code that you can try running locally.


Doughnut & Pie · Chart.js documentation, These are available on all charts created with Chart.js, but for the examples, let's use a line chart we've This can be safely called after updating the data object. In this tutorial, we will learn how to create bar charts in Chart.js with php mysql Codeigniter. In PHP codeigniter or other framework Using chart js, we can draw many types of charts, like bar chart, area chart, line chart, pie chart etc. We provide demo link at the end of article. Checkout the link. Codeigniter Bar Chart Using Chart Js Contents


Chart.js API · Chart.js documentation, <?php; $dataPoints = array();; //Best practice is to create a separate file for handling connection to database; try{; // Creating a new connection. // Replace  Let us create an example for creating graph view with the use of Chart.js library. View Demo. Creating graph view using Chart.js is simple and easy. I have created the graph output for dynamic data retrieved from the database. I have a MySQL database table tbl_marks containing student marks. I read the mark data and supplied it to the Chart.js