Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a legend on angular-chart.js doughnut chart

I've followed angular-chart.js documentation, and created a chart, but am unable to render a legend with it. I don't understand why its not working.

Documentation: http://jtblin.github.io/angular-chart.js/
Similar SO question: How to color legend in angular-chart.js

<div class="panel-body" ng-controller="CircleCtrl" style="display: block;">
    <div class="chart-container" style="width:400px; height:200px;">
        <canvas id="doughnut"
            class="chart chart-doughnut"
            chart-data="data"
            chart-labels="labels"
            chart-colours="colours"
            chart-legend="true">
        </canvas> 
    </div>
</div>  

enter image description here

I've also tried defining an array for legend in the controller,

$scope.legend = ["complete", "incomplete"]

Per the accepted answer in the other SO question, chart-legend="true" should be enough to make it work.

Does anyone have experience with this library and have an idea how to solve this issue?

like image 700
tim_xyz Avatar asked May 09 '16 04:05

tim_xyz


People also ask

How do you display a legend in Chartjs?

js (3.6. 0), you can control the Legend display with the following code: const options = { plugins: { ... legend: { position: "right", // by default it's top }, ... }, };

What is legend in donut chart?

Legends in Pie Chart are shown for each data point instead of data series. This is because each slice in a pie graph are proportional to their contribution towards the total sum. You can also attach event to chart legends. Given example shows a Pie Chart with clickable Legends.

What is a legend in chart JS?

The chart legend displays data about the datasets that are appearing on the chart.

How do you hide a legend in Chartjs?

To remove legend on charts with Chart. js v2 and JavaScript, we can set the options. legend to false . const chart1 = new Chart(canvas, { type: "pie", data: data, options: { legend: { display: false, }, tooltips: { enabled: false, }, }, });


2 Answers

If you're using the 1.0.0-alpha branch based on chart.js 2, the correct syntax is:

$scope.options = {legend: {display: true}};

and in your html

<canvas id="pie"
            class="chart chart-pie"                
            chart-data="data"
            chart-labels="labels"
            chart-options="options">
 </canvas>
like image 52
Livie Avatar answered Sep 20 '22 10:09

Livie


I had Similar Problem So I have extended the width of the Pie Chart and Placed Legends Right Side of the Graph and Looks Pretty Well

You can add this in the Controller

 $scope.options = {
    legend: {
      display: true,
      position: 'right'
    }
  };

In HTML you can add

<canvas id="pie" class="chart chart-pie" chart-data="data" chart-series="series" chart-labels="labels" chart-options="options" chart-colors="colors" chart-dataset-override="datasetOverride">
        chart-series="series"
      </canvas>

This How it Looks Like

like image 33
Mahesh G Avatar answered Sep 21 '22 10:09

Mahesh G