Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Google Pie chart with Custom Legend (Large Legend Without Pagination)

1.00/5 (1 vote)
29 Sep 2017CPOL1 min read 11.1K   170  
Customised large legends for Google pie chart without pagination

Introduction

While adding Google charts in web page, most of us have noticed that if number of legends are large, then chart will show you pagination for legends. But in some cases, pagination for legends is not much useful for us. For example, if you are exporting chart HTML to any other format like PDF, then you are not able to show full legend in exported content. This article will help you to create simple customised HTML legends for Google pie chart. You can modify and use it as per your needs and chart type.

Using the Code

I have added one separate div element for legend exactly below the chart div element. You can plave it anywhere as per your needs. 

HTML
// <div id="chartLegend" class="legendMain"></div>

my.js is having code for creating legends and bind chart. In my.js, 'chartLegend' function is created for custom legends, which has two parameters of type array, one for chart data and one for colors. 

JavaScript
// function chartLegend(chartData,colorsArray)

In this function, legend elements are created using custom HTML objects created in funcion. 

We are creating separate div for number of legend elements with respect to container width so that we can center align them.

JavaScript
// var $divOuter = $("<div>", {id: "legendOuter"+i, 
// "class": "legendOuter","title":((chartData[i][1] / totalVal) * 100 ).toFixed(1) + " %"});

Please refer to the attached sample code for further reference and actual integration.

For any suggestions and modifications, please write in comments.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)