Legend text color in accordance with line color in

2019-01-26 01:56发布

问题:

I am using jqplot to draw multiple lines of different line colors.

Also, I have legends whose colors should be in accordance with the corresponding line colors.

I seem to find no way to cope with the legend color.

So any hint?

回答1:

Taken from the question title I understand you want to change the color of legend labels to correspond to the color of series, right?

For this reason, since the swatches which are just in front of the labels, we can use them to grab the color which we then set for the labels.

This is the bit of the code you need. You need to remember to put it before you draw your plot.

$.jqplot.postDrawHooks.push(function() {
    var swatches = $('table.jqplot-table-legend tr td.jqplot-table-legend-swatch');
    var labels = $('table.jqplot-table-legend tr td.jqplot-table-legend-label');
    labels.each(function(index) {
        //turn the label's text color to the swatch's color
        var color = $(swatches[index]).find("div div").css('background-color');
        $(this).css('color',color );
    });
});

You could see the code running live here.