Create a Pie Chart Using HighCharts

This article describes how to make a pie chart in js using Highchart. To create pie charts, utilise pie charts. Pie charts are a fairly common way to present a brief summary of a composition or comparison. They continue to be a common option for tiny datasets even if they might be more challenging

CDN for using High chart
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
Create a piechart.html file and insert the following code:
<figure class="highcharts-figure">
    <div id="container"></div>
    <p class="highcharts-BrahmaDescription">
        This is a pie chart created by Prinkesha Lathidadiya.
    </p>
</figure>
Create a CSS file now so that the pie chart can be styled
.highcharts-figure,
.highcharts-data-table table {
    min-width: 320px;
    max-width: 800px;
    margin: 1em auto;
}

.highcharts-data-table table {
    font-family: Verdana, sans-serif;
    border-collapse: collapse;
    border: 1px solid #ebebeb;
    margin: 10px auto;
    text-align: center;
    width: 100%;
    max-width: 500px;
}

.highcharts-data-table caption {
    padding: 1em 0;
    font-size: 1.2em;
    color: #555;
}

.highcharts-data-table th {
    font-weight: 600;
    padding: 0.5em;
}

.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
    padding: 0.5em;
}

.highcharts-data-table thead tr,
.highcharts-data-table tr:nth-child(even) {
    background: #f8f8f8;
}

.highcharts-data-table tr:hover {
    background: #f1f7ff;
}

input[type="number"] {
    min-width: 50px;
}
Create a JS file for displaying data in a pie chart after that
Highcharts.chart('container', {
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    title: {
        text: 'Example Of Pie Chart.'
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    accessibility: {
        point: {
            valueSuffix: '%'
        }
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %'
            }
        }
    },
    series: [{
        name: 'Brands',
        colorByPoint: true,
        data: [{
            name: 'x',
            y: 70.67,
            sliced: true,
            selected: true
        }, {
            name: 'y',
            y: 14.77
        },  {
            name: 'z',
            y: 4.86
        }, {
            name: 'a',
            y: 2.63
        }, {
            name: 'b',
            y: 1.53
        },  {
            name: 'c',
            y: 1.40
        }, {
            name: 'd',
            y: 0.84
        }, {
            name: 'e',
            y: 0.51
        }, {
            name: 'f',
            y: 2.6
        }]
    }]
});
Your chart will now appear as follows:

Another Pie Chart
<html>
   <head>
      <title>Another Example Of Pie Chart</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script src = "https://code.highcharts.com/highcharts.js"></script>
   </head>

   <body>
      <div id = "container" style = "width: 600px; height: 450px; margin: 0 auto"></div>
      <script language = "JavaScript">
         $(document).ready(function() {
            var chart = {
               plotBackgroundColor: null,
               plotBorderWidth: null,
               plotShadow: false
            };
            var title = {
               text: 'Basic Example Of Pie Chart'
            };
            var tooltip = {
               pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            };
            var plotOptions = {
               pie: {
                  allowPointSelect: true,
                  cursor: 'pointer',

                  dataLabels: {
                     enabled: true,
                     format: '<b>{point.name}%</b>: {point.percentage:.1f} %',
                     style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor)||
                        'black'
                     }
                  }
               }
            };
            var series = [{
               type: 'pie',
               name: 'Basic Example',
               data: [
                  ['A',   45.0],
                  ['B',       26.8],
                  {
                     name: 'C',
                     y: 12.8,
                     sliced: true,
                     selected: true
                  },

                  ['D',    8.5],
                  ['E',     6.2],
                  ['F',   0.7]
               ]
            }];
            var json = {};
            json.chart = chart;
            json.title = title;
            json.tooltip = tooltip;
            json.series = series;
            json.plotOptions = plotOptions;
            $('#container').highcharts(json);
         });
      </script>
   </body>

</html>
Your pie chart will appear as-

This article should assist you in making a pie chart. I’ll explain several chart kinds in another article using highchart.

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories