Chart.js: generateLegend() results in an “undefined is not a function” error

Created on 5 Feb 2015  Â·  3Comments  Â·  Source: chartjs/Chart.js

The generatLegend() function only seems to work when a chart is created as follows:

var chart = new Chart(ctx).Line(data, ctxOptions);
var legend = chart.generateLegend();

The following does not work, and result in an "undefined is not a function" error:

var chart = new Chart(ctx);
chart.Line(data, ctxOptions);
var legend = chart.generateLegend();

See http://stackoverflow.com/questions/28236561/chart-js-generatelegend-call-results-in-undefined-is-not-a-function/28347373#28347373

needs test case

Most helpful comment

chart.Line(...) doesn't modify your chart object in place, and the proto isn't updated with generateLegend() until after you've called a chart type on your chart object.

var chart = new Chart(ctx);
var lineChart = chart.Line(data, ctxOptions);
var legend = lineChart.generateLegend();

All 3 comments

Thats is not working in my chart.
I make an example in codepen: http://codepen.io/Haxz/pen/RNmJeW

chart.Line(...) doesn't modify your chart object in place, and the proto isn't updated with generateLegend() until after you've called a chart type on your chart object.

var chart = new Chart(ctx);
var lineChart = chart.Line(data, ctxOptions);
var legend = lineChart.generateLegend();

@KuroJoe is correct.

Was this page helpful?
0 / 5 - 0 ratings