question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue in styling legend in Pie charts

See original GitHub issue

Hi All,

I’m trying to style my pie chart as first image below. But with below code I’m getting as shown in second image. Even I tried the solution as mentioned here 2253. Then no legends are displayed. Also I don’t want white border between pies.

I’m using ChartJs 2.4.0 version. Please help me in resolving the issue.

var legend = chartInstance.generateLegend();
document.getElementById("legend").innerHTML = legend; 

community admin google drive guwha

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
madmoizocommented, Feb 7, 2017

A solution with an independent legend

Controller

Use your chart conf to create a list of legend items with color & label :

# Chart conf
scope.chart = []
scope.chart.labels = ['Food', 'Fuel']
scope.chart.colors = ['red', 'yellow']

# Legend
scope.legend = []
for label, i in scope.chart.labels
    scope.legend[i] =
      title: label
      color: scope.chart.colors[i]

(it’s coffee script, use http://js2.coffee/ if you want a translation)

Template

Loop on legend items to display them and you can do whatever you want with styles

 <div class="widget-body">
    <div class="row">
      <div class="col-md-12 col-lg-7">
        <div class="widget-pie-chart">
          <canvas id="monthlyExpensesChart" class="chart chart-pie" chart-data="communitytotalCostArray" chart-labels="communityCategoryNamesArray" chart-options="options" height="250" chart-colors="colors"></canvas>
        </div>
      </div>

      <div class="col-md-12 col-lg-5">
        <div class="widget-pie-legend">
          <a ng-repeat="item in ::legend" href="">
            <div class="pie-legend-item">
              <span class="legend-item-box" ng-style="::{'color': item.color}"></span>
              <span class="legend-item-label">{{::item.title}}</span>
            </div>
          </a>
        </div>
      </div>
    </div>
  </div>

Result

You should see something like that image

0reactions
nutanladecommented, Feb 9, 2017

Thanks @etimberg. Issue resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pie chart legend position issues - Google Groups
The legend is placed at the top since it is lengthen when you have lots of items, so aligning it to the center...
Read more >
How to resolve Legends formatting Issue in SSRS pie Chart
If you are trying to have same legend text value, then try this, Right click on Series (in char data box) ---> Series...
Read more >
How to set the legend of a pie chart right - Mendix Forum
Hello everyone, I'm facing an issue with the pie-chart legend. It overlaps at the border (see 130) and I haven't had any success...
Read more >
Pie Chart Legend Height Issue - PTC Community
Hello, I placed a Pie Chart in a Responsive container, but the Height of the Legend takes around 30-40% of the container; how...
Read more >
How to customize legendShape and can we apply styles to ...
2) I need to style legends using flex or any css property how to arrange two legends in columns.. 3)in pie chart I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found