Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

legendCallback() doesn't render a custom legend #144

Closed
MOKuper opened this issue Jul 7, 2017 · 9 comments
Closed

legendCallback() doesn't render a custom legend #144

MOKuper opened this issue Jul 7, 2017 · 9 comments

Comments

@MOKuper
Copy link

MOKuper commented Jul 7, 2017

Expected Behavior

The legendCallback() as explained here chartjs docs html legend
should generate a custom legend

Actual Behavior

The new chart options, of which legendCallback() is one, is being fired, but the legend doesn't end up in the chart

I made a codepen with a little example
https://codepen.io/MOKuper/pen/QgVmNq

Environment

  • vue.js version: 2.3.4
  • vue-chart.js version: 2.7.1

Do I love chart.js

yes I do

@k-rumi
Copy link
Contributor

k-rumi commented Jul 8, 2017

I think that is because legend.display is set to false.
If set to true, I see legends. See this URL(https://codepen.io/kutrumi/pen/eRPmNa?editors=1111).

And legendCallback does not automatically substitute the default legends.
See this issue will help.
chartjs/Chart.js#3262
chartjs/Chart.js#2177

Did I answer your question?

@MOKuper
Copy link
Author

MOKuper commented Jul 10, 2017

Not really, but I took a better look at the implementation, and the legend should be set to false, but indeed you need to generate the legend in a separate div. For me this isn't apparent from the documentation, and replacing the default legend seems more obvious to me.

Anyways, I am going to try out the new method and will post my findings :-)

@ssuess
Copy link

ssuess commented Oct 4, 2017

@MOKuper any updates on your findings? I need to generate a custom legend to but am having some problems getting it to work...

@apertureless
Copy link
Owner

You can generate a complete custom legend, if you want.

In the options you can set legendTemplate but you have to set legend.display: false

scaleBeginAtZero: true,
          legendTemplate: '<ul class="<%=name.toLowerCase()%>-legend"><% for (var i=0; i<segments.length; i++){%><li class="chapter__item"><span style="background-color:<%=segments[i].fillColor%>"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>',
          legend: {
            display: false,
            position: 'left',
            fullWidth: true,
            labels: {
              fontSize: isXsScreen ? 13 : 18,
              fontFamily: "'Open Sans', 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
            }
          },

Then you can generate it and save if in a local var

this.legendMarkup = this.$data._chart.generateLegend()

And in your template you can do

<div class="legend__list" ref="legend" v-html="legendMarkup"></div>

@alexisraca
Copy link

@apertureless where in the docs says that? when are the owners going to update the docs

@apertureless
Copy link
Owner

With the current version of chart.js they removed legendTemplate like it seems and replaced it with legendCallback.

Just check out the Docs.

http://www.chartjs.org/docs/latest/configuration/legend.html#html-legends

@johndezr
Copy link

johndezr commented Jun 18, 2018

Yes, you need to use the legendCallback but don't forget to call this.$data._chart.generateLegend()

@simonwebdev000000001
Copy link

simonwebdev000000001 commented Oct 12, 2020

iam using with react

<Line
            ref={(chart: any) => {
              if (chart && chart.chartInstance) {
                chart.chartInstance.generateLegend();
              }
            }}
options={{
legend: {
            display: false
        },
legendCallback(chart ) {
   return `<h1>Test</h1>`
}}

and i am not able to see generated dom legend. Any thoughts?

@codeBasePlusPlus
Copy link

legend: {
display: false,
},
legendCallback: function(chart) {
return <p>hellos</p>;
},
.....
thisComponent.$nextTick(() => {
let d = bubbleExample.generateLegend();
document.getElementById('scatterLegend').innerHTML = d;
});
....
where 'scatterLegend' is a div id u manually create in template.
This works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants