Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Chart JS Pie Chart Example

#1
Chart JS Pie Chart Example

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/12/chart-js-pie-chart-example.jpg" width="550" height="561" title="" alt="" /></div><div><div class="modified-on" readability="7.1304347826087"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on December 7th, 2022.</div>
<p>In this tutorial, we are going to learn how to create a pie chart using JavaScript libraries. We have used Chart.js library for the generating the pie charts. As an alternate option, I have also presented a 3d pie chart example using Google charts library.</p>
<p>Let us see the following examples of creating a pie chart using JavaScript.</p>
<ul>
<li>Quick example – Simple pie chart example via ChartJS.</li>
<li>3D pie chart with Google Charts library.</li>
<li>Responsive ChartJS pie chart.</li>
</ul>
<h2>Quick example – Simple pie chart example via ChartJS</h2>
<div class="post-section-highlight" readability="53">
<pre class="prettyprint"><code class="language-php-template">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Chart JS Pie Chart&lt;/title&gt;
&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;
&lt;/head&gt;
&lt;body&gt; &lt;div class="phppot-container"&gt; &lt;h1&gt;Responsive Pie Chart&lt;/h1&gt; &lt;div&gt; &lt;canvas id="pie-chart"&gt;&lt;/canvas&gt; &lt;/div&gt; &lt;/div&gt; &lt;script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js"&gt;&lt;/script&gt; &lt;script&gt; new Chart(document.getElementById("pie-chart"), { type : 'pie', data : { labels : [ "Lion", "Horse", "Elephant", "Tiger", "Jaguar" ], datasets : [ { backgroundColor : [ "#51EAEA", "#FCDDB0", "#FF9D76", "#FB3569", "#82CD47" ], data : [ 418, 263, 434, 586, 332 ] } ] }, options : { title : { display : true, text : 'Chart JS Pie Chart Example' } } }); &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
</div>
<p>Creating a ChartJS pie chart is a three-step process as shown below.</p>
<ol>
<li>Add the ChartJS library include to the head section of your HTML.</li>
<li>Add a canvas element to the HTML.</li>
<li>Add the ChartJS class initiation and invoking script before closing the HTML body tag.</li>
</ol>
<h3>About the ChartJS pie chart script</h3>
<p>The script sets the following properties to initiate the ChartJS library.</p>
<ul>
<li><em>type</em> – The type of the chart supported by the ChartJS library.</li>
<li><em>data</em> – It sets the chart labels and datasets. The dataset contains the data array and the display properties.</li>
<li><em>options</em> – It sets the chart title text and its display flag as a boolean true to show it on the browser.</li>
</ul>
<p><strong>Output:</strong></p>
<p><img loading="lazy" class="alignnone size-large wp-image-20136" src="https://phppot.com/wp-content/uploads/2022/12/chartjs-pie-chart-550x561.jpg" alt="chartjs pie chart" width="550" height="561" srcset="https://phppot.com/wp-content/uploads/2022/12/chartjs-pie-chart-550x561.jpg 550w, https://phppot.com/wp-content/uploads/20...94x300.jpg 294w, https://phppot.com/wp-content/uploads/20...68x783.jpg 768w, https://phppot.com/wp-content/uploads/20...-chart.jpg 1000w" sizes="(max-width: 550px) 100vw, 550px"></p>
<p>In a previous tutorial, we have seen the various ways of <a href="https://phppot.com/javascript/chartjs-line-chart/">creating line charts using the Chart JS library</a>.</p>
<p><a class="demo" href="https://phppot.com/demo/chartjs-pie-chart/">View Demo</a></p>
<h2>Creating 3D pie chart</h2>
<p>There is no option for a 3D pie chart using chart JS. For those users who have landed here looking for a 3D pie chart, you may try Google Charts.</p>
<p>This example uses Google Charts to create a 3D pie chart for a webpage. In a previous code, we use <a href="https://phppot.com/php/how-to-render-attendance-graph-using-google-charts/">Google Charts to render a bar chart</a> to show students’ attendance statistics.</p>
<p>The Google Charts JavaScript code prepares the array of animal distribution data. This array is for sending it to the chart data table which helps to draw the pie chart.</p>
<p>The Google Charts library accepts the <strong>is3D</strong> with a boolean&nbsp;<em>true</em> to output a 3D pie chart.</p>
<p>It creates a chart visualization object with the reference with respect to the UI element target. Then, it calls the Google Charts library function to draw and render the chart.</p>
<pre class="prettyprint"><code class="language-php-template">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;3d Pie Chart JavaScript with Google Charts&lt;/title&gt;
&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt; &lt;script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt; google.charts.load("current", { packages : [ "corechart" ] }); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ [ 'Animal', 'Distribution' ], [ 'Horse', 11 ], [ 'Elephant', 2 ], [ 'Tiger', 2 ], [ 'Lion', 2 ], [ 'Jaguar', 7 ] ]); var options = { title : '3d Pie Chart JavaScript with Google Charts', is3D : true, }; var chart = new google.visualization.PieChart(document .getElementById('3d-pie-chart')); chart.draw(data, options); }
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt; &lt;div class="phppot-container"&gt; &lt;h1&gt;3d Pie Chart JavaScript with Google Charts&lt;/h1&gt; &lt;div id="3d-pie-chart" style="width: 700px; height: 500px;"&gt;&lt;/div&gt; &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img loading="lazy" class="alignnone size-large wp-image-20144" src="https://phppot.com/wp-content/uploads/2022/12/3d-pie-chart-550x444.jpg" alt="3d pie chart" width="550" height="444" srcset="https://phppot.com/wp-content/uploads/2022/12/3d-pie-chart-550x444.jpg 550w, https://phppot.com/wp-content/uploads/20...00x242.jpg 300w, https://phppot.com/wp-content/uploads/20...-chart.jpg 600w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>Responsive pie chart using Chart JS</h2>
<p>The Chart JS library provides JavaScript options to make the output pie chart responsive.</p>
<p>This example script uses those options to render a responsive pie chart in a browser.</p>
<p>The JavaScript code to render a responsive pie chart is the same as we have seen in the quick example above.</p>
<p>The difference is nothing but to set&nbsp;<strong>responsive: true</strong> in the ChartJS&nbsp;<em>options</em> properties.</p>
<p>If you want to <a href="https://phppot.com/php/draw-responsive-charts-pie-bar-column-using-google-charts/">create a responsive chart using Google Charts</a>, then the linked article has an example.</p>
<pre class="prettyprint"><code class="language-php-template">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Responsive Pie Chart&lt;/title&gt;
&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt; &lt;div class="phppot-container"&gt; &lt;h1&gt;Responsive Pie Chart&lt;/h1&gt; &lt;div&gt; &lt;canvas id="pie-chart"&gt;&lt;/canvas&gt; &lt;/div&gt; &lt;/div&gt; &lt;script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js"&gt;&lt;/script&gt; &lt;script&gt; new Chart(document.getElementById("pie-chart"), { type : 'pie', data : { labels : [ "Lion", "Horse", "Elephant", "Tiger", "Jaguar" ], datasets : [ { backgroundColor : [ "#51EAEA", "#FCDDB0", "#FF9D76", "#FB3569", "#82CD47" ], data : [ 418, 263, 434, 586, 332 ] } ] }, options : { title : { display : true, text : 'Responsive Pie Chart' }, responsive : true } }); &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><a class="demo" href="https://phppot.com/demo/chartjs-pie-chart/">View Demo</a><a class="download" href="https://phppot.com/downloads/javascript/chartjs-pie-chart.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/javascript/chartjs-pie-chart/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2022/12/...t-example/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016