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

Generate legend not found #6

Closed
youknowriad opened this issue Nov 29, 2014 · 13 comments
Closed

Generate legend not found #6

youknowriad opened this issue Nov 29, 2014 · 13 comments
Milestone

Comments

@youknowriad
Copy link

Hi,

I have a small issue with legends, Chart.js has a generateLegend method to generate an html legend (based on legends templates). When I try this in nchart i get a not found function. Can this be added to nchart ?

Thanks

@fundon
Copy link
Contributor

fundon commented Dec 15, 2014

@youknowriad Please PR 😄

@Tommassissimo
Copy link

I met the same problem with legend, could some one help to figure out or fix the issue

@fundon fundon added this to the 2.0.0 milestone Jan 30, 2015
@fundon
Copy link
Contributor

fundon commented Jan 30, 2015

@Tommassissimo

The nchart 1.0.0 will be updated to the latest APIs of chart.js.

fundon added a commit that referenced this issue Jan 30, 2015
@fundon
Copy link
Contributor

fundon commented Jan 30, 2015

@youknowriad @Tommassissimo
Currently, used the latest Chart.js, try it.

@youknowriad
Copy link
Author

It worked perfectly with last version. Thanks a lot :)

@jgato
Copy link

jgato commented Feb 27, 2015

Please, could I have an example of how to use it? Is it possible to see the legend in the generated png?

@fundon
Copy link
Contributor

fundon commented Feb 27, 2015

@jgato see examples

@jgato
Copy link

jgato commented Feb 27, 2015

I have seen all the examples @fundon but I dont see anything about legends (or I am missing something). About legends I mean, in a linear graph with three data sets, a legend with titles for each data set (line).

@youknowriad
Copy link
Author

@jgato In my use case, I generate an image from the canvas and us wkhtmtopdf to generate a pdf with the image and the html of the legend with a code like this

// imageFile contains the filename of the image generated from nchart canvas
$ = cheerio.load('<div><style>' +
            '.chartjs-legend li {display: inline-block;margin: 0 5px;} ' +
            '.chartjs-legend .pill {display: inline-block;width: 8px;height: 8px;border-radius: 2px;margin-right: 3px;} '+
            '.chartjs-legend {list-style: none;text-align: center;} ' +
          '</style></div>');
$('div').append('<img src="file://'+imageFile+'" />');
$('div').append(chart.generateLegend());

and the generated pdf looks like

capture d ecran 2015-02-27 a 17 49 24

@jgato
Copy link

jgato commented Feb 27, 2015

But, is it possible to generate the image containing the labels?

Using object.generateLegend() I get the html code regarding the lables. How can I include that in the picture? Do I need to insert that into the canvas? Sorry because I have never used canvas before.

@youknowriad
Copy link
Author

I think the best you could do, is using a trick like me and use wkhtmltoimage : First generate an html containing the image of the graph and the html of the legend than transform this html to an image using wkhtmltoimage

@jgato
Copy link

jgato commented Feb 27, 2015

Thanks @youknowriad cheerion was the piece that I was missing, now I almost got it.

I have the legend, but not with the colours of the series. How can I get that? I guess I will need to modify the legendTemplate option, but I dont know how.

responsetimes

By the way the code to get that, just if it is useful for other people:

var myLineChartResponseTimes = new Chart(ctx).Line(data);

    canvas.toBuffer(function (err, buf) {
        if (err) throw err;

        // fs.writeFile(directory + "/" + title + ".png", buf);
        fs.writeFile(directory + "/" + title + ".png", buf, function (err){

            if (err) 
                throw err;

            var cheerio = require('cheerio');

            $ = cheerio.load('<div><style>' +
                             '.chartjs-legend li {display: inline-block;margin: 0 5px;} ' +
                             '.chartjs-legend .pill {display: inline-block;width: 8px;height: 8px;border-radius: 2px;margin-right: 3px;} '+
                             '.chartjs-legend {list-style: none;text-align: center;} ' +
                             '</style></div>');
            $('div').append('<img src="' + __dirname + '/' + directory + '/' + title + '.png" ></img>');

            $('div').append(myLineChartResponseTimes.generateLegend());

            var wkhtmltopdf = require('wkhtmltopdf');

            wkhtmltopdf($.html()).pipe(fs.createWriteStream('out.pdf'));
        });
    });

@youknowriad
Copy link
Author

@jgato you're absolutely right I use something like this for the legendTemplate

chartOptions = {
    legendTemplate : '<ul class="chartjs-legend <%=name.toLowerCase()%>-legend"><% for (var i=0; i<datasets.length; i++){%><li><span class="pill" style="background-color:<%=datasets[i].strokeColor%>"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>'
};

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

No branches or pull requests

4 participants