dygraphs - Hide series from Legend

回眸只為那壹抹淺笑 提交于 2019-12-11 00:31:58

问题


i'm trying to build a dynamic chart with 3 series using Dygraphs, the problem is that i'm not able to make the chart shows only one of them in legend. I have 3 series named "Prec", "Val" and "Now", everytime a user passes with cursor over the chart my goal is to show in the legend only value from "Val" serie.

My series data are given in "native" format, in other words it's an array of value with date in the first element:

va data = {
           [ X, Open, Val, Now],
           [ 2015-11-26, 1002, 1024, 1026],
           [ 2015-11-27, 1002, 1025, 1026],
           [ 2015-11-28, 1002, 1027, 1025],
            ...
          };

I checked in docs (http://dygraphs.com/options.html) without any luck. Any suggestion would be appreciated.

UPDATE: I tried to add a custom valueFormatter to the option object like this:

axes:{
    y:{
        valueFormatter: function (y, opts, seriesName){
            if (seriesName == "Open" || seriesName == "Now") return "";
            else return y;
        }
    }
}

but it just hide the value, not the serie's Name so now my chart's legend shows:

2015-11-26 Open: Val:1024 Now:

回答1:


The solution is to add an attribute to the Dygraphs option object to activate the highlight feature, even empty if you want to use default settings:

highlightSeriesOpts: {}

it adds 'highlight' class to the serie's legend spans when you hover it.

then you have to add these rows to your CSS

.dygraph-legend > span { display: none; }
.dygraph-legend > span.highlight { display: inline; }

in this way only the highlighted series will be visible in the legend.

source: Dygraphs docs



来源:https://stackoverflow.com/questions/33940462/dygraphs-hide-series-from-legend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!