我创建了一个带有图表的网站,为了生成图表,我使用了dygraphs库。图表显示三系列数据(测量值和公差)
如何禁用公差的图例?一个或多个系列的dygraphs图例是否可行?
发布于 2017-01-31 16:03:05
是。如果你使用的是dygraph 2.0或更高版本,你可以用legendFormatter
来做这件事。您可以使用legendFormatter
自定义图例的格式以满足您的需求。但是,如果您只想隐藏两个系列,最简单的方法是从series
数组中删除Min和Max系列,并将其交还给默认格式化程序:
g = new Dygraph(
document.getElementById("graph"),
"X,Y,min,max\n" +
"1,6,3,12\n" +
"2,3,3,12\n" +
"3,4,3,12\n" +
"4,6,3,12\n" +
"5,8,3,12\n" +
"6,10,3,12\n" +
"7,12,3,12\n" +
"8,10,3,12\n",
{
legend: 'always',
series: {
min: { color: 'red' },
max: { color: 'red' },
},
legendFormatter: function(data) {
data.series = [data.series[0]]; // pick whichever series you want to keep
return Dygraph.Plugins.Legend.defaultFormatter.call(this, data);
}
});
参见fiddle。
发布于 2019-11-21 07:51:26
您可以使用this函数设置图例的格式-
LegendFormatter(data) {
if (data.x == null) {
// This happens when there's no selection and {legend: 'always'} is set.
return data.series.map(function(series) {
if(series.labelHTML != 'counter_label')
return series.dashHTML + ' ' + series.labelHTML }).join(' ');
}
var html = data.xHTML;
return html;
}
上面的代码将返回除标签为'counter_ label‘的系列之外的所有图例。在代码中进行必要的修改,以便为您的要求应用两个条件。
在定义图表时,请使用以下格式来指定图表:
this.graph = new Dygraph(document.getElementById("graph_div"),
this.data,
{
legend: 'always',
drawPoints: true,
animatedZooms: true,
showRoller: true,
rollPeriod: 14,
legendFormatter: this.LegendFormatter,
});
这里将根据需要使用legendformatter函数来格式化图例。
https://stackoverflow.com/questions/41946680
复制相似问题