5

我希望在情节中选择性地隐藏某些系列的传说。这个http://www.jqplot.com/docs/files/jqplot-core-js.html#Series.showLabel看起来是正确的选择。

但是使用series:{showLabel:false}with 似乎不起作用。

在这方面的公开(旧)票:https ://bitbucket.org/cleonello/jqplot/issue/100/feature-request-individual-legend-control

我用对了吗?也赞赏任何其他解决方法来实现这一点。

更新:

showLabel与普通的 legendRenderer 一起工作正常。

4

3 回答 3

2

在你的 jqplot 下面添加这一行:

$($("#chartXXX .jqplot-table-legend").get(0)).hide();

玩弄 get() 中的数字,因为我不确定数字是如何映射到图例元素的。此解决方案比更改增强的LegendRenderer 代码要优雅得多。

于 2013-06-07T16:50:11.260 回答
1

draw文件中的函数中,jqplot.enhancedLegendRenderer.js我们在第 132 行看到了这一点:

if (idx < series.length && (series[idx].show || series[idx].showLabel)){

对于上下文,这段代码是创建图例表的功能的一部分。如果将系列设置为显示(默认为true)或要显示系列标签,则会创建一行。这意味着要防止为系列创建图例项目,您需要隐藏整个系列本身,以及设置showLabelfalse,这并不理想。

相反,请尝试将其设置||&&- 这在我的情况下有效。首先尝试在未缩小的版本上进行更改。

编辑:

首先要做的是进行我在原始答案中建议的更改。

接下来,我们需要防止tr创建图例中的元素 if showLabelis false。就在上面提到的 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.

于 2013-03-06T21:34:17.480 回答
0

好吧,我结合了 jbass 和 DaFrenk 的答案,结果是:

$($("#chartXXX tr.jqplot-table-legend").get(0)).hide();

中的数字get是基于零的索引,它定义了图例表中的行。这正是我想要的。

于 2013-09-12T08:22:37.720 回答