{"id":5297,"date":"2023-03-29T15:41:25","date_gmt":"2023-03-29T10:11:25","guid":{"rendered":"https:\/\/designcollection.in\/blog\/?p=5297"},"modified":"2023-05-11T10:18:37","modified_gmt":"2023-05-11T04:48:37","slug":"how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js","status":"publish","type":"post","link":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/","title":{"rendered":"How to Create Dynamic Stacked Bar, Doughnut and Pie charts in PHP with Chart.js"},"content":{"rendered":"<h1 class=\"my-2\">How to Create Dynamic Stacked Bar, Doughnut and Pie charts in PHP with Chart.js <\/h1>\r\n<p class=\"my-2\">Chart.js is an open-source JavaScript library that allows developers to create various types of charts and graphs easily. In this blog post, we will explore how to create dynamic stacked bar, doughnut, and pie charts in PHP using Chart.js.<\/p>\r\n<p class=\"my-2\">Prerequisites<\/p>\r\n<p class=\"my-2\">Before we start, make sure you have the following:<\/p>\r\n<ol>\r\n<li class=\"my-2\">A basic understanding of PHP and HTML.<\/li>\r\n<li class=\"my-2\">Chart.js library.<\/li>\r\n<li class=\"my-2\">A web server like XAMPP, WAMP, or MAMP.<\/li>\r\n<li class=\"my-2\">A text editor like Visual Studio Code, Sublime Text, or Notepad++.<\/li>\r\n<\/ol>\r\n<h4 class=\"my-2 text-center\">Step 1: Download Chart.js<\/h4>\r\n<p class=\"my-2\">The first step is to download Chart.js from the official website, or you can use the CDN link to include the Chart.js library.<\/p>\r\n<img decoding=\"async\" src=\"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2023\/03\/js.png\" alt=\"code-js\"  class=\"img-fluid img-thumbnail mr-2\" \/>\r\n\r\n<h4 class=\"my-2 text-center\">Initialize Chart.js and load data into dynamic charts<\/h4>\r\n<p class=\"my-2 \">The following JavaScript contains four separate functions. It creates bar, doughnut, pie and stacked-column charts.<\/p>\r\n<p class=\"my-2 \">Each function requests the PHP endpoint to get the dynamic chart data.It uses the jQuery post method to call the PHP file from JavaScript. In a previous tutorial, we have seen how to use jQuery load method to render HTML dynamicallyThe endpoint URL includes the chart type in its query string. Based on this param the response data will vary.<\/p>\r\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2023\/03\/How-to-Create-Dynamic-Stacked-Bar-Doughnut-and-Pie-charts-in-PHP-with-Chart.js-1.jpg\" alt=\"graph-2\" width=\"750\" height=\"375\" class=\"img-fluid img-thumbnail my-3\" \/>\r\n\r\n<p class=\"my-2 \">This script loads the dynamic response data into the chart. The chart legends and labels are also dynamic from the database fetched using PHP.<\/p>\r\n<p class=\"my-2 text-center\">graph.php<\/p>\r\n<pre class=\"scrollspy-example-2 bg-light my-2\">\r\nfunction showBarChart() {\r\n\t{\r\n\t\t$.post(\"ajax-endpoint\/get-chart-data.php?chart_type=bar\",\r\n\t\t\tfunction(data) {\r\n\t\t\t\tconsole.log(data);\r\n\t\t\t\tvar name = [];\r\n\t\t\t\tvar marks = [];\r\n\r\n\t\t\t\tfor (var i in data) {\r\n\t\t\t\t\tname.push(data[i].student_name);\r\n\t\t\t\t\tmarks.push(data[i].marks);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tvar chartdata = {\r\n\t\t\t\t\tlabels: name,\r\n\t\t\t\t\tdatasets: [\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tlabel: 'Student Marks',\r\n\t\t\t\t\t\t\tbackgroundColor: '#49e2ff',\r\n\t\t\t\t\t\t\tborderColor: '#46d5f1',\r\n\t\t\t\t\t\t\thoverBackgroundColor: '#CCCCCC',\r\n\t\t\t\t\t\t\thoverBorderColor: '#666666',\r\n\t\t\t\t\t\t\tdata: marks\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t]\r\n\t\t\t\t};\r\n\r\n\t\t\t\tvar graphTarget = $(\"#bar-chart\");\r\n\r\n\t\t\t\tvar graph = new Chart(graphTarget, {\r\n\t\t\t\t\ttype: 'bar',\r\n\t\t\t\t\tdata: chartdata\r\n\t\t\t\t});\r\n\t\t\t});\r\n\t}\r\n}\r\n\r\nfunction showPieChart() {\r\n\t{\r\n\t\t$.post(\"ajax-endpoint\/get-chart-data.php?chart_type=pie\",\r\n\t\t\tfunction(data) {\r\n\t\t\t\tvar name = [];\r\n\t\t\t\tvar marks = [];\r\n\t\t\t\tvar bgColor = [];\r\n\r\n\t\t\t\tfor (var i in data) {\r\n\t\t\t\t\tname.push(data[i].label);\r\n\t\t\t\t\tmarks.push(data[i].data);\r\n\t\t\t\t\tbgColor.push(data[i].backgroundColor);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tvar chartdata = {\r\n\t\t\t\t\tlabels: name,\r\n\t\t\t\t\tdatasets: [\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tlabel: 'Student Marks',\r\n\t\t\t\t\t\t\tbackgroundColor: bgColor,\r\n\t\t\t\t\t\t\tdata: marks\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t]\r\n\t\t\t\t};\r\n\r\n\t\t\t\tvar graphTarget = $(\"#pie-chart\");\r\n\r\n\t\t\t\tvar graph = new Chart(graphTarget, {\r\n\t\t\t\t\ttype: 'pie',\r\n\t\t\t\t\tdata: chartdata\r\n\t\t\t\t});\r\n\t\t\t});\r\n\t}\r\n}\r\n\r\nfunction showDoughnutChart() {\r\n\t{\r\n\t\tChart.pluginService.register({\r\n\t\t\tbeforeDraw: function(chart) {\r\n\t\t\t\tvar width = chart.chart.width,\r\n\t\t\t\t\theight = chart.chart.height + 35,\r\n\t\t\t\t\tctx = chart.chart.ctx;\r\n\r\n\t\t\t\tctx.save();\r\n\t\t\t\tctx.font = \"bold 1.5em sans-serif\";\r\n\t\t\t\tctx.textBaseline = \"middle\";\r\n\r\n\r\n\t\t\t\tvar text = \"100\",\r\n\t\t\t\t\ttextX = Math.round((width - ctx.measureText(text).width) \/ 2),\r\n\t\t\t\t\ttextY = height \/ 2;\r\n\t\t\t\tctx.fillStyle = 'rgba(0, 0, 0, 1)';\r\n\t\t\t\tctx.fillText(text, textX, textY);\r\n\t\t\t\tctx.restore();\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\t$.post(\"ajax-endpoint\/get-chart-data.php?chart_type=doughnut\",\r\n\t\t\tfunction(data) {\r\n\t\t\t\tvar name = [];\r\n\t\t\t\tvar marks = [];\r\n\t\t\t\tvar bgColor = [];\r\n\r\n\t\t\t\tfor (var i in data) {\r\n\t\t\t\t\tname.push(data[i].label);\r\n\t\t\t\t\tmarks.push(data[i].data);\r\n\t\t\t\t\tbgColor.push(data[i].backgroundColor);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tvar chartdata = {\r\n\t\t\t\t\tlabels: name,\r\n\t\t\t\t\tdatasets: [\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tlabel: 'Student Marks',\r\n\t\t\t\t\t\t\tbackgroundColor: bgColor,\r\n\t\t\t\t\t\t\tdata: marks\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t]\r\n\t\t\t\t};\r\n\r\n\t\t\t\tvar graphTarget = $(\"#doughnut-chart\");\r\n\r\n\t\t\t\tvar graph = new Chart(graphTarget, {\r\n\t\t\t\t\ttype: 'doughnut',\r\n\t\t\t\t\tdata: chartdata,\r\n\t\t\t\t\toptions: {\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\t\t\t});\r\n\t}\r\n}\r\n\r\n\r\nfunction showStackedVerticalChart() {\r\n\t{\r\n\t\t$.post(\"ajax-endpoint\/get-chart-data.php?chart_type=vertical-bar\",\r\n\t\t\tfunction(data) {\r\n\t\t\t\tvar chartdata = {\r\n\t\t\t\t\tlabels: ['Marks'],\r\n\t\t\t\t\tdatasets: data\r\n\t\t\t\t};\r\n\r\n\t\t\t\tvar graphTarget = $(\"#stacked-vertical-chart\");\r\n\r\n\t\t\t\tvar graph = new Chart(graphTarget, {\r\n\t\t\t\t\ttype: 'bar',\r\n\t\t\t\t\tdata: chartdata,\r\n\t\t\t\t\toptions: {\r\n\t\t\t\t\t\tscales: {\r\n\t\t\t\t\t        xAxes: [{\r\n\t\t\t\t\t        \tbarPercentage: 0.3,\r\n            \t\t\t\t\tstacked: true\r\n\t\t\t\t\t        }],\r\n\t\t\t\t\t        yAxes: [{\r\n\t\t\t\t\t        \tstacked: true\r\n\t\t\t\t\t        }]\r\n\t\t\t\t\t    }\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\t\t\t});\r\n\t}\r\n}\r\n<\/pre>\r\n<h4 class=\"my-2 text-center\">Create PHP Model, Utils to prepare chart data<\/h4>\r\n<p class=\"my-2\">When we create a chart in PHP with dynamic data, it depends on the model or service handers on the server-side.<\/p>\r\n\r\n<p class=\"my-2\">The model prepares the dynamic data format from an external source like files or a database.<\/p>\r\n\r\n<p class=\"my-2\">In this model, the handlers read the chart data from the database. The chart.tbl_marks table maps the student identity data with the marks one-to-one.<\/p>\r\n<p class=\"my-2 text-center\">Student.php<\/p>\r\n<pre class=\" scrollspy-example-2\">\r\n\r\nnamespace Phppot;\r\n\r\nclass Student\r\n{\r\n\r\n    private $dbConn;\r\n\r\n    public function __construct()\r\n    {\r\n        require_once __DIR__ . '\/DataSource.php';\r\n        $this->dbConn = new DataSource();\r\n    }\r\n\r\n    function getStudentMark()\r\n    {\r\n        $query = \"SELECT student_id,student_name,marks FROM tbl_marks ORDER BY student_id\";\r\n        $result = $this->dbConn->select($query);\r\n        return $result;\r\n    }\r\n}\r\n<\/pre>\r\n<p class=\"my-2\">And, it has the utils class to dynamically supply the background color to the chart.The Util class generates the random color code to the chart slices and the stacked bars.When we see how to create charts using Google Charts, it has a fixed array of colors supplied to the chart slices.The Google Charts services also provide very good results for showing statistical graphs.<\/p>\r\n<p class=\"my-2\">Util.php<\/p>\r\n<pre class=\" scrollspy-example-2\">\r\n\r\nnamespace Phppot;\r\n\r\nclass Util\r\n{\r\n\r\n    function getRGBRandom()\r\n    {\r\n        $rgbRandom = str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT);\r\n        return $rgbRandom;\r\n    }\r\n\r\n    function getRandomColor()\r\n    {\r\n        $randomColor = \"#\";\r\n        for ($i = 0; $i < 3; $i ++) {\r\n            $randomColor .= $this->getRGBRandom();\r\n        }\r\n        return $randomColor;\r\n    }\r\n}\r\n<\/pre>\r\n<h4 class=\"my-2 text-center\">Database script<\/h4>\r\n<p class=\"my-2\">This database script shows the statements to create the table structure. It also has the sample data to load into the chart initialization process. Download the code and import the script to run this chart example.In a previous article, we have created a database to keep attendance data. Thereby, it created charts using Google charts.<\/p>\r\n<pre class=\" scrollspy-example-2\">\r\n-- Table structure for table `tbl_marks`\r\n--\r\n\r\nCREATE TABLE `tbl_marks` (\r\n  `student_id` int(10) UNSIGNED NOT NULL,\r\n  `student_name` varchar(35) NOT NULL,\r\n  `marks` int(11) DEFAULT 0\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n\r\n--\r\n-- Dumping data for table `tbl_marks`\r\n--\r\n\r\nINSERT INTO `tbl_marks` (`student_id`, `student_name`, `marks`) VALUES\r\n(1, 'Matthew', 39),\r\n(2, 'Hannah', 46),\r\n(3, 'Merlin', 65),\r\n(4, 'Jelin', 90),\r\n(5, 'Hilda', 75);\r\n\r\n--\r\n-- Indexes for dumped tables\r\n--\r\n\r\n--\r\n-- Indexes for table `tbl_marks`\r\n--\r\nALTER TABLE `tbl_marks`\r\n  ADD PRIMARY KEY (`student_id`);\r\n\r\n<\/pre>\r\n<h4 class=\"my-2 text-center\">Conclusion<\/h4>\r\n<p class=\"my-2\">This article described how to create a chart in PHP with JavaScript library Chart.js. The example code has structured dynamic blocks and the backend logic.We have displayed the marks statistics in the form of a pie chart, doughnut chart and stacked bar chart. In the doughnut chart, it displays the text to state the mark out of which the student marks are shown.The separate graph initializations script made the chart rendering process easier. It contains code to supply JSON response into the JavaScript to plot the metrics into the chart UI.With dynamic data loading, it is possible to make use of this code in a real-time application easily.<\/p>\r\n\r\n<div class=\"d-flex text-center align-items-center justify-content-center flex-wrap my-3\">\r\n   \r\n    <h5 class=\"p-2 text-center\"><a class=\"text-decoration-none btn btn-outline-info\" target=\"blank\" href=\"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2023\/03\/how-to-draw-dynamic-data-graph-with-chart-js.zip\" rel=\"noopener\">Download<\/a><\/h5>\r\n    \r\n    <\/div>\r\n<h5><span class=\"badge bd-red-200 mx-1 text-capitalize text-muted fw-normal\">PHP <\/span><span class=\"badge bd-green-200 mx-1 text-capitalize text-muted fw-normal\">AJAX  <\/span><span class=\"badge bd-yellow-200 mx-1 text-capitalize text-muted fw-normal\">Dynamic Graph<\/span><span class=\"badge bd-cyan-200 text-capitalize mx-1 text-muted fw-normal\">Javascript <\/span><span class=\"badge bd-orange-200 mx-1 text-capitalize text-muted fw-normal\">Lead Generation <\/span><\/h5>\r\n   \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>How to Create Dynamic Stacked Bar, Doughnut and Pie charts in PHP with Chart.js Chart.js is an open-source JavaScript library that allows developers to create various types of charts and graphs easily. In this blog post, we will explore how to create dynamic stacked bar, doughnut, and pie charts in PHP using Chart.js. Prerequisites Before [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5298,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[282],"tags":[430,461,372,397,284],"class_list":["post-5297","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-ajax","tag-dynamic-graph","tag-javascript","tag-lead-generation","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Pie Charts in PHP with Chart.j<\/title>\n<meta name=\"description\" content=\"Learn how to create dynamic and interactive stacked bar, doughnut, and pie charts in PHP using Chart.js, a powerful JavaScript library for data visualization and charting.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pie Charts in PHP with Chart.j\" \/>\n<meta property=\"og:description\" content=\"Learn how to create dynamic and interactive stacked bar, doughnut, and pie charts in PHP using Chart.js, a powerful JavaScript library for data visualization and charting.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog is an ocean of knowledge\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/designcollection.in\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-29T10:11:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-11T04:48:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2023\/03\/How-to-Create-Dynamic-Stacked-Bar-Doughnut-and-Pie-charts-in-PHP-with-Chart.js.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1300\" \/>\n\t<meta property=\"og:image:height\" content=\"1000\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"designcollection\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"designcollection\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/\"},\"author\":{\"name\":\"designcollection\",\"@id\":\"https:\/\/designcollection.in\/blog\/#\/schema\/person\/612562f8760f1b42e074b2a2e84bdd67\"},\"headline\":\"How to Create Dynamic Stacked Bar, Doughnut and Pie charts in PHP with Chart.js\",\"datePublished\":\"2023-03-29T10:11:25+00:00\",\"dateModified\":\"2023-05-11T04:48:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/\"},\"wordCount\":570,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/designcollection.in\/blog\/#organization\"},\"keywords\":[\"Ajax\",\"Dynamic Graph\",\"Javascript\",\"Lead Generation\",\"PHP\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/\",\"url\":\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/\",\"name\":\"Pie Charts in PHP with Chart.j\",\"isPartOf\":{\"@id\":\"https:\/\/designcollection.in\/blog\/#website\"},\"datePublished\":\"2023-03-29T10:11:25+00:00\",\"dateModified\":\"2023-05-11T04:48:37+00:00\",\"description\":\"Learn how to create dynamic and interactive stacked bar, doughnut, and pie charts in PHP using Chart.js, a powerful JavaScript library for data visualization and charting.\",\"breadcrumb\":{\"@id\":\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/designcollection.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create Dynamic Stacked Bar, Doughnut and Pie charts in PHP with Chart.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/designcollection.in\/blog\/#website\",\"url\":\"https:\/\/designcollection.in\/blog\/\",\"name\":\"Design Collection\",\"description\":\"Technology Tips, Tutorials, Learning &amp; How to Guides\",\"publisher\":{\"@id\":\"https:\/\/designcollection.in\/blog\/#organization\"},\"alternateName\":\"IT Services | Best Lead Generation Company In Indore - Design Collection\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/designcollection.in\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/designcollection.in\/blog\/#organization\",\"name\":\"Design Collection\",\"alternateName\":\"IT Services | Best Lead Generation Company In Indore - Design Collection\",\"url\":\"https:\/\/designcollection.in\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/designcollection.in\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2022\/12\/design-collection.png\",\"contentUrl\":\"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2022\/12\/design-collection.png\",\"width\":240,\"height\":41,\"caption\":\"Design Collection\"},\"image\":{\"@id\":\"https:\/\/designcollection.in\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/designcollection.in\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/designcollection.in\/blog\/#\/schema\/person\/612562f8760f1b42e074b2a2e84bdd67\",\"name\":\"designcollection\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/designcollection.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e791d735751a16b72683b42279ea62b4bcfcbe6dd200da2f7240cdeae878c3ff?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e791d735751a16b72683b42279ea62b4bcfcbe6dd200da2f7240cdeae878c3ff?s=96&d=mm&r=g\",\"caption\":\"designcollection\"},\"sameAs\":[\"https:\/\/designcollection.in\/blog\/\"],\"url\":\"https:\/\/designcollection.in\/blog\/author\/designcollection\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pie Charts in PHP with Chart.j","description":"Learn how to create dynamic and interactive stacked bar, doughnut, and pie charts in PHP using Chart.js, a powerful JavaScript library for data visualization and charting.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/","og_locale":"en_US","og_type":"article","og_title":"Pie Charts in PHP with Chart.j","og_description":"Learn how to create dynamic and interactive stacked bar, doughnut, and pie charts in PHP using Chart.js, a powerful JavaScript library for data visualization and charting.","og_url":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/","og_site_name":"Blog is an ocean of knowledge","article_publisher":"https:\/\/www.facebook.com\/designcollection.in","article_published_time":"2023-03-29T10:11:25+00:00","article_modified_time":"2023-05-11T04:48:37+00:00","og_image":[{"width":1300,"height":1000,"url":"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2023\/03\/How-to-Create-Dynamic-Stacked-Bar-Doughnut-and-Pie-charts-in-PHP-with-Chart.js.jpg","type":"image\/jpeg"}],"author":"designcollection","twitter_card":"summary_large_image","twitter_misc":{"Written by":"designcollection","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/#article","isPartOf":{"@id":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/"},"author":{"name":"designcollection","@id":"https:\/\/designcollection.in\/blog\/#\/schema\/person\/612562f8760f1b42e074b2a2e84bdd67"},"headline":"How to Create Dynamic Stacked Bar, Doughnut and Pie charts in PHP with Chart.js","datePublished":"2023-03-29T10:11:25+00:00","dateModified":"2023-05-11T04:48:37+00:00","mainEntityOfPage":{"@id":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/"},"wordCount":570,"commentCount":0,"publisher":{"@id":"https:\/\/designcollection.in\/blog\/#organization"},"keywords":["Ajax","Dynamic Graph","Javascript","Lead Generation","PHP"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/","url":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/","name":"Pie Charts in PHP with Chart.j","isPartOf":{"@id":"https:\/\/designcollection.in\/blog\/#website"},"datePublished":"2023-03-29T10:11:25+00:00","dateModified":"2023-05-11T04:48:37+00:00","description":"Learn how to create dynamic and interactive stacked bar, doughnut, and pie charts in PHP using Chart.js, a powerful JavaScript library for data visualization and charting.","breadcrumb":{"@id":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/designcollection.in\/blog\/how-to-create-dynamic-stacked-bar-doughnut-and-pie-charts-in-php-with-chart-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/designcollection.in\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create Dynamic Stacked Bar, Doughnut and Pie charts in PHP with Chart.js"}]},{"@type":"WebSite","@id":"https:\/\/designcollection.in\/blog\/#website","url":"https:\/\/designcollection.in\/blog\/","name":"Design Collection","description":"Technology Tips, Tutorials, Learning &amp; How to Guides","publisher":{"@id":"https:\/\/designcollection.in\/blog\/#organization"},"alternateName":"IT Services | Best Lead Generation Company In Indore - Design Collection","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/designcollection.in\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/designcollection.in\/blog\/#organization","name":"Design Collection","alternateName":"IT Services | Best Lead Generation Company In Indore - Design Collection","url":"https:\/\/designcollection.in\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/designcollection.in\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2022\/12\/design-collection.png","contentUrl":"https:\/\/designcollection.in\/blog\/wp-content\/uploads\/2022\/12\/design-collection.png","width":240,"height":41,"caption":"Design Collection"},"image":{"@id":"https:\/\/designcollection.in\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/designcollection.in"]},{"@type":"Person","@id":"https:\/\/designcollection.in\/blog\/#\/schema\/person\/612562f8760f1b42e074b2a2e84bdd67","name":"designcollection","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/designcollection.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e791d735751a16b72683b42279ea62b4bcfcbe6dd200da2f7240cdeae878c3ff?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e791d735751a16b72683b42279ea62b4bcfcbe6dd200da2f7240cdeae878c3ff?s=96&d=mm&r=g","caption":"designcollection"},"sameAs":["https:\/\/designcollection.in\/blog\/"],"url":"https:\/\/designcollection.in\/blog\/author\/designcollection\/"}]}},"_links":{"self":[{"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/posts\/5297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/comments?post=5297"}],"version-history":[{"count":20,"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/posts\/5297\/revisions"}],"predecessor-version":[{"id":5548,"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/posts\/5297\/revisions\/5548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/media\/5298"}],"wp:attachment":[{"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/media?parent=5297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/categories?post=5297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/designcollection.in\/blog\/wp-json\/wp\/v2\/tags?post=5297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}