{"id":16916,"date":"2023-09-28T08:09:21","date_gmt":"2023-09-28T08:09:21","guid":{"rendered":"https:\/\/www.anychart.com\/blog\/?p=16916"},"modified":"2023-09-29T12:53:30","modified_gmt":"2023-09-29T12:53:30","slug":"js-pie-charts","status":"publish","type":"post","link":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/","title":{"rendered":"How to Build Pie Charts with JavaScript"},"content":{"rendered":"<p><a href=\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/\"><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png\" alt=\"Pie Chart in JavaScript\" width=\"100%\" class=\"alignnone size-full wp-image-16927\" srcset=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png 1366w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-300x169.png 300w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-768x432.png 768w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a>The pie chart, a <a href=\"https:\/\/www.anychart.com\/chartopedia\/chart-type\/pie-chart\/\">widely used chart type<\/a> yet also a topic of debate <a href=\"https:\/\/scc.ms.unimelb.edu.au\/resources\/data-visualisation-and-exploration\/no_pie-charts\" target=\"_blank\" rel=\"nofollow\">here<\/a> and <a href=\"https:\/\/www.businessinsider.com\/pie-charts-are-the-worst-2013-6\" target=\"_blank\" rel=\"nofollow\">there<\/a>, has cemented its place in the realm of data visualization. When used <a href=\"https:\/\/randalolson.com\/2016\/03\/24\/the-correct-way-to-use-pie-charts\/\" target=\"_blank\" rel=\"nofollow\">appropriately<\/a>, it provides an intuitive insight into the composition of data, with each slice of the pie representing a distinct component. In this tutorial, I&#8217;ll guide you through a straightforward path of creating interactive pie charts using JavaScript.<\/p>\n<p>Every chart begins with data. To demonstrate the concepts in this tutorial, I decided to visualize the composition of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Indian_Premier_League\" target=\"_blank\" rel=\"nofollow\">Indian Premier League<\/a> (IPL) winnership over its 16-season history.<\/p>\n<p>Whether you&#8217;re a novice or an experienced coder, get ready to immediately acquire the valuable skills of crafting visually appealing JS-based pie charts from scratch.<\/p>\n<p><!--more Read the JS charting tutorial \u00bb--><\/p>\n<p><a href=\"https:\/\/playground.anychart.com\/mNqpdOXb\" target=\"_blank\" rel=\"nofollow\"><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-example-5.png\" alt=\"JavaScript Pie Chart Preview\" width=\"100%\" class=\"alignnone size-full wp-image-16929\" srcset=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-example-5.png 1200w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-example-5-300x162.png 300w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-example-5-768x415.png 768w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-example-5-1024x554.png 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h2>Steps to Create a JS Pie Chart<\/h2>\n<p>The toss is done, and it&#8217;s time to step onto the field. Come along as I guide you through crafting a functional, interactive pie chart with JavaScript in just four straightforward steps.<\/p>\n<h3>1. Setting the stage<\/h3>\n<p>Before we dive into code, let&#8217;s prepare our canvas. Every chart needs a container, so we&#8217;ll start by creating a simple HTML page. Within it, we&#8217;ll define a container element with a unique ID to host our pie chart. If you want the chart to fill the entire page, set the container&#8217;s width and height parameters in CSS to 100%.<\/p>\n<pre><code class=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;Pie Chart in JS&lt;\/title&gt;\r\n    &lt;style type=\"text\/css\"&gt;      \r\n      html, body, #container { \r\n        width: 100%; height: 100%; margin: 0; padding: 0; \r\n        } \r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>2. Gathering tools<\/h3>\n<p>Building a JavaScript pie graph from scratch can be a daunting task, but we&#8217;re in luck! There are plenty of JS charting libraries that provide pre-built functions and features, making data visualization development seamless. For this tutorial, I\u2019ve chosen the <a href=\"https:\/\/www.anychart.com\">AnyChart<\/a> JS library, suitable for beginners and experts alike, but feel free to use your favorite one as the logic of the process will be the same.<\/p>\n<p>We\u2019ll connect the necessary library files from a content delivery network (CDN). These scripts will reside in the <code>&lt;head&gt;<\/code> section of our HTML page, setting the stage for our pie chart. The JavaScript code itself will be placed between the <code>&lt;script&gt;<\/code> and <code>&lt;\/script&gt;<\/code> tags, which can be added to either the <code>&lt;head&gt;<\/code> or the <code>&lt;body&gt;<\/code> sections.<\/p>\n<pre><code class=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;Pie Chart in JS&lt;\/title&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.1\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.1\/js\/anychart-pie.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;style type=\"text\/css\"&gt;      \r\n      html, body, #container { \r\n        width: 100%; height: 100%; margin: 0; padding: 0; \r\n      } \r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;  \r\n    &lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n      \/\/ All the code for the JS Pie Chart will come here\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>3. Adding data<\/h3>\n<p>Charts rely on data, and we&#8217;ve got options for how to incorporate it. In this tutorial, we&#8217;ll manually input the data directly into our JavaScript charting code. However, keep in mind that you can, of course, also fetch data from databases or files.<\/p>\n<pre><code class=\"javascript\">let data = anychart.data.set([\r\n  [\"Chennai Super Kings\", 5],\r\n  [\"Mumbai Indians\", 5],\r\n  [\"Kolkatta Knight Riders\", 2],\r\n  [\"Deccan Chargers\", 1],\r\n  [\"Gujarat Titans\", 1],\r\n  [\"Rajasthan Royal\", 1],\r\n  [\"Sunrisers Hyderabad\", 1],\r\n]);<\/code><\/pre>\n<h3>4. Writing a JavaScript charting code<\/h3>\n<p>Now, let&#8217;s transition from scoring singles to hitting those thrilling fours and sixes. You&#8217;ll see how just a few lines of code can create an interactive JS pie chart in no time.<\/p>\n<p>Firstly, to ensure everything runs smoothly, we enclose all our code within a function. This guarantees that our chart-building process only begins after the page has fully loaded. The data goes directly there as the first ball.<\/p>\n<pre><code class=\"javascript\">anychart.onDocumentReady(function () {\r\n  \/\/ add the data\r\n  let data = anychart.data.set([\r\n    [\"Chennai Super Kings\", 5],\r\n    [\"Mumbai Indians\", 5],\r\n    [\"Kolkatta Knight Riders\", 2],\r\n    [\"Deccan Chargers\", 1],\r\n    [\"Gujarat Titans\", 1],\r\n    [\"Rajasthan Royal\", 1],\r\n    [\"Sunrisers Hyderabad\", 1]\r\n  ]);\r\n  \/\/ The upcoming JS code goes here...\r\n});<\/code><\/pre>\n<p>Now, for the game-changing moment \u2014 we create a pie chart instance with a single line of code.<\/p>\n<pre><code class=\"javascript\">let chart = anychart.pie(data);<\/code><\/pre>\n<p>To provide context to our pie graphic and help viewers understand its purpose, let\u2019s add a descriptive title.<\/p>\n<pre><code class=\"javascript\">chart.title(\"IPL Winnership Over 16 Seasons\");<\/code><\/pre>\n<p>Lastly, we refer to the HTML element designated earlier as the chart container and, with a simple command, draw the resulting pie chart.<\/p>\n<pre><code class=\"javascript\">chart.container(\"container\");\r\nchart.draw();<\/code><\/pre>\n<p>And there you have it! A nice chart that vividly visualizes the composition of the IPL winnership over the entire span of 16 seasons. You&#8217;ll notice that the top two teams dominate, accounting for almost two-thirds of all IPL titles during this period. Feel free to explore the initial version <a href=\"https:\/\/playground.anychart.com\/HlRubIg8\" target=\"_blank\" rel=\"nofollow\">here<\/a> with the code; the complete HTML\/CSS\/JS code is also below.<\/p>\n<p><iframe loading=\"lazy\" class=\"anychart-embed anychart-embed-HlRubIg8\" src=\"https:\/\/playground.anychart.com\/HlRubIg8\/iframe\" width=\"300\" height=\"150\" frameborder=\"0\" sandbox=\"allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-modals allow-forms\" allowfullscreen=\"allowfullscreen\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-HlRubIg8{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<pre><code class=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;Pie Chart in JS&lt;\/title&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.1\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.1\/js\/anychart-pie.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;style type=\"text\/css\"&gt;      \r\n      html, body, #container { \r\n        width: 100%; height: 100%; margin: 0; padding: 0; \r\n      } \r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;  \r\n    &lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n      anychart.onDocumentReady(function () {\r\n        \/\/ add the data\r\n        let data = anychart.data.set([\r\n          [\"Chennai Super Kings\", 5],\r\n          [\"Mumbai Indians\", 5],\r\n          [\"Kolkatta Knight Riders\", 2],\r\n          [\"Deccan Chargers\", 1],\r\n          [\"Gujarat Titans\", 1],\r\n          [\"Rajasthan Royal\", 1],\r\n          [\"Sunrisers Hyderabad\", 1],\r\n        ]);\r\n        \/\/ create a pie chart with the data\r\n        let chart = anychart.pie(data);\r\n        \/\/ set the chart title\r\n        chart.title(\"IPL Winnership Over 16 Seasons\");\r\n        \/\/ set container id for the chart\r\n        chart.container(\"container\");\r\n        \/\/ initiate chart drawing\r\n        chart.draw();\r\n      });\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h2>Some Options to Customize a Pie Chart<\/h2>\n<p>Now that we&#8217;ve created a basic JS pie chart, let&#8217;s delve into possible ways of customizing it to elevate its visual appeal.<\/p>\n<h3>1. Modify the color palette<\/h3>\n<p>A smart way to enhance a chart is by using colors that are relevant to the data it represents. In our case, since we&#8217;re plotting IPL teams, why not incorporate the primary colors from each team&#8217;s logo?<\/p>\n<p>To achieve this, we&#8217;ll create a customized color palette and apply it to the initial pie chart.<\/p>\n<pre><code class=\"javascript\">\/\/ create a custom palette\r\nlet palette = anychart.palettes.distinctColors();\r\n\/\/ set the colors according to the team brands\r\npalette.items([\r\n  {color: \"#004f91\"},\r\n  {color: \"#fcce06\"},\r\n  {color: \"#3a225d\"},\r\n  {color: \"#9a1c22\"},\r\n  {color: \"#254aa5\"},\r\n  {color: \"#D9E3EF\"},\r\n  {color: \"#02183c\"}\r\n]);\r\n\/\/ set the palette to the chart\r\nchart.palette(palette);<\/code><\/pre>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-lWhhy128\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/lWhhy128\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-lWhhy128{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<h3>2. Format the labels and disable the legend<\/h3>\n<p>Let&#8217;s enhance the readability of the pie chart by improving the labels on the slices. We can display information about both the team and the number of titles won in an explicit manner.<\/p>\n<p>Using abbreviations instead of full team names can be a wise choice to maintain chart conciseness and visual appeal. So, first, we\u2019ll incorporate the abbreviations into the data.<\/p>\n<pre><code class=\"javascript\">\/\/ add the data\r\nlet data = anychart.data.set([\r\n  [\"Chennai Super Kings\", 5, \"CSK\"],\r\n  [\"Mumbai Indians\", 5, \"MI\"],\r\n  [\"Kolkatta Knight Riders\", 2, \"KKR\"],\r\n  [\"Deccan Chargers\", 1, \"DC\"],\r\n  [\"Gujarat Titans\", 1, \"GT\"],\r\n  [\"Rajasthan Royal\", 1, \"RR\"],\r\n  [\"Sunrisers Hyderabad\", 1, \"SRH\"]\r\n]);\r\n\/\/ create a pie chart with the data, naming the values in the dataset\r\nlet chart = anychart.pie(data.mapAs({name: 0, value: 1, abbr: 2}));<\/code><\/pre>\n<p>Next, we&#8217;ll proceed to configure the labels themselves. Our goal is to format them, displaying the team abbreviation alongside the number of titles won. Additionally, we&#8217;ll specify a font size larger than the default to ensure the labels have a more pronounced visual impact. To guarantee that all labels, including those exceeding the pie slice boundaries, remain easily visible, we&#8217;ll position them outside the chart; adjusting the label position to &#8220;outside&#8221; ensures each label is automatically linked to its corresponding slice with a connecting line.<\/p>\n<pre><code class=\"javascript\">chart\r\n  .labels()\r\n  .format(\"{%abbr} \u2014 {%value}\")\r\n  .fontSize(14)\r\n  .position(\"outside\");<\/code><\/pre>\n<p>Since all labels are now readily visible, let\u2019s disable the legend to avoid redundancy.<\/p>\n<pre><code class=\"javascript\">chart.legend(false);<\/code><\/pre>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-3znzpqff\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/3znzpqff\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-3znzpqff{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<h3>3. Improve the tooltip<\/h3>\n<p>Using a tooltip is an effective approach to providing additional information within a chart. While our pie visualization already presents the necessary information, we can further enhance it by modifying the tooltip using the <code>format()<\/code> function.<\/p>\n<pre><code class=\"javascript\">chart\r\n  .tooltip()\r\n  .format(\"Percent of total wins: {%PercentValue}{decimalsCount: 1}%\");<\/code><\/pre>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-lgfZt5nV\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/lgfZt5nV\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-lgfZt5nV{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<h3>4. Enhance the chart title<\/h3>\n<p>A title can be an important element that complements a data graphic. For example, let\u2019s customize the pie chart\u2019s one and include a subtitle to highlight the main takeaway.<\/p>\n<pre><code class=\"javascript\">chart\r\n  .title()\r\n  .enabled(true)\r\n  .useHtml(true)\r\n  .text(\r\n    '&lt;span style = \"color: #111; font-size:20px; margin-bottom:10px; dy:20px\"&gt;IPL Winnership Over 16 Seasons&lt;\/span&gt;' +\r\n      '&lt;br\/&gt;&lt;span style=\"color:#173685; font-size: 15px;\"&gt;Among the 10 current IPL teams, 2 dominate, and 3 have never won the title&lt;\/span&gt;'\r\n  );<\/code><\/pre>\n<p>Observe the sophisticated and refined appearance of the JavaScript pie chart now! You can access the interactive version along with the code for this tutorial\u2019s final chart <a href=\"https:\/\/playground.anychart.com\/mNqpdOXb\" target=\"_blank\" rel=\"nofollow\">here<\/a>; the complete HTML\/CSS\/JS code is also below.<\/p>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-mNqpdOXb\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/mNqpdOXb\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-mNqpdOXb{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<pre><code class=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;Pie Chart in JS&lt;\/title&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.1\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.1\/js\/anychart-pie.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;style type=\"text\/css\"&gt;      \r\n      html, body, #container { \r\n        width: 100%; height: 100%; margin: 0; padding: 0; \r\n      } \r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;  \r\n    &lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n      anychart.onDocumentReady(function () {\r\n        \/\/ add the data\r\n        let data = anychart.data.set([\r\n          [\"Chennai Super Kings\", 5, \"CSK\"],\r\n          [\"Mumbai Indians\", 5, \"MI\"],\r\n          [\"Kolkatta Knight Riders\", 2, \"KKR\"],\r\n          [\"Deccan Chargers\", 1, \"DC\"],\r\n          [\"Gujarat Titans\", 1, \"GT\"],\r\n          [\"Rajasthan Royal\", 1, \"RR\"],\r\n          [\"Sunrisers Hyderabad\", 1, \"SRH\"]\r\n        ]);\r\n        \/\/ create a pie chart with the data, naming the values in the dataset\r\n        let chart = anychart.pie(data.mapAs({name: 0, value: 1, abbr: 2}));\r\n        \/\/ create a custom palette\r\n        let palette = anychart.palettes.distinctColors();\r\n        \/\/ set the colors according to the team brands\r\n        palette.items([\r\n          {color: \"#004f91\"},\r\n          {color: \"#fcce06\"},\r\n          {color: \"#3a225d\"},\r\n          {color: \"#9a1c22\"},\r\n          {color: \"#254aa5\"},\r\n          {color: \"#D9E3EF\"},\r\n          {color: \"#02183c\"}\r\n        ]);\r\n        \/\/ set the palette to the chart\r\n        chart.palette(palette);\r\n        \/\/ configure the labels\r\n        chart\r\n          .labels()\r\n          .format(\"{%abbr} \u2014 {%value}\")\r\n          .fontSize(14)\r\n          .position(\"outside\");\r\n        \/\/ disable the chart legend\r\n        chart.legend(false);\r\n        \/\/ format the tooltip\r\n        chart\r\n          .tooltip()\r\n          .format(\"Percent of total wins: {%PercentValue}{decimalsCount: 1}%\");\r\n        \/\/ set chart title text settings\r\n        chart\r\n          .title()\r\n          .enabled(true)\r\n          .useHtml(true)\r\n          .text(\r\n            '&lt;span style = \"color: #111; font-size:20px; margin-bottom:10px; dy:20px\"&gt;IPL Winnership Over 16 Seasons&lt;\/span&gt;' +\r\n              '&lt;br\/&gt;&lt;span style=\"color:#173685; font-size: 15px;\"&gt;Among the 10 current IPL teams, 2 dominate, and 3 have never won the title&lt;\/span&gt;'\r\n          );\r\n        \/\/ set container id for the chart\r\n        chart.container(\"container\");\r\n        \/\/ initiate chart drawing\r\n        chart.draw();\r\n      });\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Wrapping up, I hope this JS pie charting tutorial will be helpful to you. Feel free to adapt what you&#8217;ve learned here to your projects and datasets. Should you have any questions or innovative ideas, don&#8217;t hesitate to get in touch.<\/p>\n<p>Creating engaging charts can be as captivating as watching IPL matches. Start your visualization journey and tell compelling data stories.<\/p>\n<hr \/>\n<p><strong><em>Published with the permission of Shachee Swadia. Originally appeared on <a href=\"https:\/\/www.codementor.io\/@shacheeswadia\/creating-a-pie-chart-with-javascript-291oohnrfq\" target=\"_blank\" rel=\"nofollow\">Codementor<\/a> with the title &#8220;Creating a Pie Chart with JavaScript&#8221; on September 27, 2023.<\/p>\n<p>You may also like to read the <a href=\"https:\/\/www.anychart.com\/blog\/2017\/12\/06\/pie-chart-create-javascript\/\">JavaScript Pie Chart Tutorial<\/a> originally published on our blog earlier.<\/p>\n<p>Check out more <a href=\"https:\/\/www.anychart.com\/blog\/category\/javascript-chart-tutorials\/\">JavaScript charting tutorials<\/a> and continue mastering interactive data visualization.<\/p>\n<p>Got an idea for some nice guest post? <a href=\"https:\/\/www.anychart.com\/support\/\">Let us know<\/a>.<\/strong><\/em><\/p>\n<hr \/>\n<p><!-- SyntaxHighlighter --><link rel=\"stylesheet\" href=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/styles\/default.min.css\"><link rel=\"stylesheet\" href=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/styles\/atom-one-light.min.css\"><script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/highlight.min.js\"><\/script><script>hljs.initHighlightingOnLoad();<\/script><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>The pie chart, a widely used chart type yet also a topic of debate here and there, has cemented its place in the realm of data visualization. When used appropriately, it provides an intuitive insight into the composition of data, with each slice of the pie representing a distinct component. In this tutorial, I&#8217;ll guide [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,23,13,279,4],"tags":[2656,2385,3499,1292,193,3352,509,54,844,805,2013,2014,32,55,61,167,146,433,152,2949,151,3607,36,907,141,249,3111,81,57,60,3586,58,65,56,59,64,84,172,2816,804,3407],"class_list":["post-16916","post","type-post","status-publish","format-standard","hentry","category-anychart-charting-component","category-html5","category-javascript","category-javascript-chart-tutorials","category-tips-and-tricks","tag-codementor","tag-coding","tag-cricket","tag-data-chart","tag-data-composition","tag-data-graphic","tag-data-graphics","tag-data-visualization","tag-data-visualization-tutorial","tag-front-end-development","tag-html","tag-html-charts","tag-html5","tag-html5-charts","tag-html5-pie","tag-interactive-charts","tag-interactive-data-visualization","tag-interactive-graphics","tag-interactive-infographic","tag-interactive-infographics","tag-interactive-visualizations","tag-ipl","tag-javascript","tag-javascript-chart-tutorial","tag-javascript-charting","tag-javascript-charting-api","tag-javascript-charting-features","tag-javascript-charting-library","tag-javascript-charts","tag-javascript-pie-chart","tag-js","tag-js-chart","tag-js-charting","tag-js-charts","tag-js-pie-chart","tag-pie-chart","tag-pie-charts","tag-tutorial","tag-web-design","tag-web-development","tag-website-development","wpautop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating Pie Charts with JavaScript Step by Step<\/title>\n<meta name=\"description\" content=\"Learn to effortlessly build cool interactive pie charts using JavaScript as we visualize the composition of the IPL winnership over 16 seasons step by step.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Pie Charts with JavaScript Step by Step\" \/>\n<meta property=\"og:description\" content=\"Learn to effortlessly build cool interactive pie charts using JavaScript as we visualize the composition of the IPL winnership over 16 seasons step by step.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/\" \/>\n<meta property=\"og:site_name\" content=\"AnyChart News\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/AnyCharts\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-28T08:09:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-29T12:53:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-og.png\" \/>\n<meta name=\"author\" content=\"Shachee Swadia\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Creating Pie Charts with JavaScript Step by Step\" \/>\n<meta name=\"twitter:description\" content=\"Learn to effortlessly build cool interactive pie charts using JavaScript as we visualize the composition of the IPL winnership over 16 seasons step by step.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-og.png\" \/>\n<meta name=\"twitter:creator\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:site\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shachee Swadia\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/\"},\"author\":{\"name\":\"Shachee Swadia\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51\"},\"headline\":\"How to Build Pie Charts with JavaScript\",\"datePublished\":\"2023-09-28T08:09:21+00:00\",\"dateModified\":\"2023-09-29T12:53:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/\"},\"wordCount\":1106,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png\",\"keywords\":[\"Codementor\",\"coding\",\"cricket\",\"data chart\",\"data composition\",\"data graphic\",\"data graphics\",\"Data Visualization\",\"data visualization tutorial\",\"front-end development\",\"HTML\",\"HTML charts\",\"HTML5\",\"html5 charts\",\"html5 pie\",\"interactive charts\",\"interactive data visualization\",\"interactive graphics\",\"interactive infographic\",\"interactive infographics\",\"interactive visualizations\",\"IPL\",\"JavaScript\",\"javascript chart tutorial\",\"javascript charting\",\"javascript charting api\",\"JavaScript charting features\",\"JavaScript charting library\",\"javascript charts\",\"javascript pie chart\",\"js\",\"js chart\",\"js charting\",\"js charts\",\"js pie chart\",\"pie chart\",\"pie charts\",\"tutorial\",\"web design\",\"web development\",\"website development\"],\"articleSection\":[\"AnyChart Charting Component\",\"HTML5\",\"JavaScript\",\"JavaScript Chart Tutorials\",\"Tips and Tricks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/\",\"url\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/\",\"name\":\"Creating Pie Charts with JavaScript Step by Step\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png\",\"datePublished\":\"2023-09-28T08:09:21+00:00\",\"dateModified\":\"2023-09-29T12:53:30+00:00\",\"author\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51\"},\"description\":\"Learn to effortlessly build cool interactive pie charts using JavaScript as we visualize the composition of the IPL winnership over 16 seasons step by step.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#primaryimage\",\"url\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png\",\"contentUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png\",\"width\":1366,\"height\":768},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.anychart.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Build Pie Charts with JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\",\"url\":\"https:\/\/www.anychart.com\/blog\/\",\"name\":\"AnyChart News\",\"description\":\"AnyChart JS Charts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.anychart.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51\",\"name\":\"Shachee Swadia\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/50ebd5c61e4e46e052898a5ddbcef5996666132cf07c614be4725f028ec7eae3?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/50ebd5c61e4e46e052898a5ddbcef5996666132cf07c614be4725f028ec7eae3?s=96&r=g\",\"caption\":\"Shachee Swadia\"},\"url\":\"https:\/\/www.anychart.com\/blog\/author\/shachee-swadia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating Pie Charts with JavaScript Step by Step","description":"Learn to effortlessly build cool interactive pie charts using JavaScript as we visualize the composition of the IPL winnership over 16 seasons step by step.","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:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/","og_locale":"en_US","og_type":"article","og_title":"Creating Pie Charts with JavaScript Step by Step","og_description":"Learn to effortlessly build cool interactive pie charts using JavaScript as we visualize the composition of the IPL winnership over 16 seasons step by step.","og_url":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/","og_site_name":"AnyChart News","article_publisher":"https:\/\/www.facebook.com\/AnyCharts","article_published_time":"2023-09-28T08:09:21+00:00","article_modified_time":"2023-09-29T12:53:30+00:00","og_image":[{"url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-og.png","type":"","width":"","height":""}],"author":"Shachee Swadia","twitter_card":"summary_large_image","twitter_title":"Creating Pie Charts with JavaScript Step by Step","twitter_description":"Learn to effortlessly build cool interactive pie charts using JavaScript as we visualize the composition of the IPL winnership over 16 seasons step by step.","twitter_image":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart-og.png","twitter_creator":"@AnyChart","twitter_site":"@AnyChart","twitter_misc":{"Written by":"Shachee Swadia","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#article","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/"},"author":{"name":"Shachee Swadia","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51"},"headline":"How to Build Pie Charts with JavaScript","datePublished":"2023-09-28T08:09:21+00:00","dateModified":"2023-09-29T12:53:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/"},"wordCount":1106,"commentCount":0,"image":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png","keywords":["Codementor","coding","cricket","data chart","data composition","data graphic","data graphics","Data Visualization","data visualization tutorial","front-end development","HTML","HTML charts","HTML5","html5 charts","html5 pie","interactive charts","interactive data visualization","interactive graphics","interactive infographic","interactive infographics","interactive visualizations","IPL","JavaScript","javascript chart tutorial","javascript charting","javascript charting api","JavaScript charting features","JavaScript charting library","javascript charts","javascript pie chart","js","js chart","js charting","js charts","js pie chart","pie chart","pie charts","tutorial","web design","web development","website development"],"articleSection":["AnyChart Charting Component","HTML5","JavaScript","JavaScript Chart Tutorials","Tips and Tricks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/","url":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/","name":"Creating Pie Charts with JavaScript Step by Step","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#primaryimage"},"image":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png","datePublished":"2023-09-28T08:09:21+00:00","dateModified":"2023-09-29T12:53:30+00:00","author":{"@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51"},"description":"Learn to effortlessly build cool interactive pie charts using JavaScript as we visualize the composition of the IPL winnership over 16 seasons step by step.","breadcrumb":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#primaryimage","url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png","contentUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/09\/pie-chart.png","width":1366,"height":768},{"@type":"BreadcrumbList","@id":"https:\/\/www.anychart.com\/blog\/2023\/09\/28\/js-pie-charts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.anychart.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Build Pie Charts with JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.anychart.com\/blog\/#website","url":"https:\/\/www.anychart.com\/blog\/","name":"AnyChart News","description":"AnyChart JS Charts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.anychart.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51","name":"Shachee Swadia","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/50ebd5c61e4e46e052898a5ddbcef5996666132cf07c614be4725f028ec7eae3?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/50ebd5c61e4e46e052898a5ddbcef5996666132cf07c614be4725f028ec7eae3?s=96&r=g","caption":"Shachee Swadia"},"url":"https:\/\/www.anychart.com\/blog\/author\/shachee-swadia\/"}]}},"_links":{"self":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/comments?post=16916"}],"version-history":[{"count":15,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16916\/revisions"}],"predecessor-version":[{"id":16935,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16916\/revisions\/16935"}],"wp:attachment":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/media?parent=16916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/categories?post=16916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/tags?post=16916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}