Dygraphs:系列的一部分分为单独的图例
Dygraphs: part of series into separate legends
我正在尝试为一个图表实现多个图例。例如,我在一张图表上有 10 个系列,但我希望其中 5 个出现在图表左侧的图例中,另外 5 个出现在图表右侧的图例中。我们有很多具有不同测量单位的系列,因此我们想分开,例如,左轴和左图例中的百分比值,右轴和右图例中的电压值。
这可能吗?
您可以使用 legendFormatter
. The intent of this option is to format the standard legend (the one that appears in the top right of the chart or in the labelsDiv
)。但是没有理由不能将它用于副作用。
像这样的东西应该做你想要的:
function legendFormatter(data) {
const leftDiv = document.getElementById('left');
const rightDiv = document.getElementById('right');
if (data.x == null) {
// no selection
leftDiv.textContent = '';
rightDiv.textContent = '';
} else {
const percentSeries = data.series.filter(v => v.contains('Percentage'));
const voltageSeries = data.series.filter(v => v.contains('Voltage'));
leftDiv.innerHTML = data.xHTML + percentSeries.map(v => v.labelHTML + ': ' + v.yHTML).join(' ');
rightDiv.innerHTML = data.xHTML + voltageSeries.map(v => v.labelHTML + ': ' + v.yHTML).join(' ');
}
return '';
}
有一些 data
参数 here 的文档。
我正在尝试为一个图表实现多个图例。例如,我在一张图表上有 10 个系列,但我希望其中 5 个出现在图表左侧的图例中,另外 5 个出现在图表右侧的图例中。我们有很多具有不同测量单位的系列,因此我们想分开,例如,左轴和左图例中的百分比值,右轴和右图例中的电压值。
这可能吗?
您可以使用 legendFormatter
. The intent of this option is to format the standard legend (the one that appears in the top right of the chart or in the labelsDiv
)。但是没有理由不能将它用于副作用。
像这样的东西应该做你想要的:
function legendFormatter(data) {
const leftDiv = document.getElementById('left');
const rightDiv = document.getElementById('right');
if (data.x == null) {
// no selection
leftDiv.textContent = '';
rightDiv.textContent = '';
} else {
const percentSeries = data.series.filter(v => v.contains('Percentage'));
const voltageSeries = data.series.filter(v => v.contains('Voltage'));
leftDiv.innerHTML = data.xHTML + percentSeries.map(v => v.labelHTML + ': ' + v.yHTML).join(' ');
rightDiv.innerHTML = data.xHTML + voltageSeries.map(v => v.labelHTML + ': ' + v.yHTML).join(' ');
}
return '';
}
有一些 data
参数 here 的文档。