在文件jqplot.enhancedLegendRenderer.js 的draw 函数中,我们在大约第132 行看到:
if (idx < series.length && (series[idx].show || series[idx].showLabel)){
就上下文而言,这段代码是创建图例表的功能的一部分。如果将系列设置为显示(默认为true)或要显示系列标签,则会创建一行。这意味着要防止为系列创建图例项目,您需要隐藏整个系列本身,并将showLabel 设置为false,这并不理想。
相反,尝试将|| 设置为&& - 这在我的情况下有效。请先尝试在未缩小版本上进行更改。
编辑:
首先要做的是做出我在原始答案中建议的更改。
接下来,如果showLabel 是false,我们需要防止创建图例中的tr 元素。就在上面提到的 if 语句之前,您将看到以下代码:
tr = $(document.createElement('tr'));
tr.addClass('jqplot-table-legend');
if (reverse){
tr.prependTo(this._elem);
}
else{
tr.appendTo(this._elem);
}
把它改成这样(我们在这里所做的只是将它包装在我们之前使用的相同 if 语句中):
if (idx < series.length && (series[idx].show && series[idx].showLabel)){
tr = $(document.createElement('tr'));
tr.addClass('jqplot-table-legend');
if (reverse){
tr.prependTo(this._elem);
}
else{
tr.appendTo(this._elem);
}
}
向下滚动一些(大约第 212 行),您将看到以下代码:
if (this.showLabels) {
console.log(this._elem.find('tr').length - 1);
td2.bind('click', {series:s, speed:speed, plot: plot, replot:this.seriesToggleReplot }, handleToggle);
td2.addClass('jqplot-seriesToggle');
}
当在图例中单击其中一个系列时绑定事件处理程序。我们需要做的是向对象字面量添加一个附加属性,该属性封装了传递给click 方法的数据:
td2.bind('click', {series:s, speed:speed, plot: plot,
replot:this.seriesToggleReplot,
trIndex: this._elem.find('tr').length - 1 }, handleToggle);
trIndex 表示 HTML 表中行的实际索引。正是这一点确保了图例将删除正确的元素。
在doLegendToggle 声明中,您将看到如下代码:
if (s.canvas._elem.is(':hidden') || !s.show) {
// Not sure if there is a better way to check for showSwatches and showLabels === true.
// Test for "undefined" since default values for both showSwatches and showLables is true.
if (typeof plot.options.legend.showSwatches === 'undefined' || plot.options.legend.showSwatches === true) {
plot.legend._elem.find('td').eq(sidx * 2).addClass('jqplot-series-hidden');
}
if (typeof plot.options.legend.showLabels === 'undefined' || plot.options.legend.showLabels === true) {
plot.legend._elem.find('td').eq((sidx * 2) + 1).addClass('jqplot-series-hidden');
}
}
else {
if (typeof plot.options.legend.showSwatches === 'undefined' || plot.options.legend.showSwatches === true) {
plot.legend._elem.find('td').eq(sidx * 2).removeClass('jqplot-series-hidden');
}
if (typeof plot.options.legend.showLabels === 'undefined' || plot.options.legend.showLabels === true) {
plot.legend._elem.find('td').eq((sidx * 2) + 1).removeClass('jqplot-series-hidden');
}
}
查看这四个对sidx 变量的引用,更改它们以便代码使用trIndex 变量。为此,请将四个 sidx 引用替换为 d.trIndex。