javascript - Dygraphs - 同时突出显示两个系列

标签 javascript dygraphs

我知道之前已经有人问过这个问题,但我对 Dygraphs 还很陌生,正在努力寻找答案。 我在 javascript 中有以下数据结构:

x , Label1, Label2, label3.... label1_2, label1_3, etc...
new Date(...), 1.23,1.45,.... , .... , ....,
new Date(...), null, null, ......., 1.23,1.434
new Date(....), 1.4656, 1.6765.......,null, null,null

整个想法是绘制一条线的某一部分是虚线而其余部分不是虚线的图。我最初有 7 个时间序列,我将每个时间序列分成两部分(虚线部分和非虚线部分),现在我想突出显示整个时间序列(所以 2 个不同的系列 Dygraphs 是虚线序列,和我一分为二的非虚线)当我将鼠标移到虚线区域或非虚线区域时。

我看到人们规定使用 HihlightCallback,但我正在努力将其付诸实践。

我现在有什么:

data =[new Date(), ..,..,.,,.,,.]
labels= {'A','B', ..... }
series= {'A': {strokePattern: [10, 20] }, 'B': .......}

g = new Dygraph( demo, data, {width: 1000,height: 700,labelsDivStyles: { 'textAlign': 'right' }, labels: labels,series:series, visibility: visibility, gridLineColor: 'red', gridLinePattern: [5,5], highlightCircleSize: 2,strokeWidth: 1, strokeBorderWidth: 1,highlightSeriesOpts: { strokeWidth: 3,strokeBorderWidth: 1,highlightCircleSize: 5}});

我相信我的结构应该如下:

g.updateOptions({ highlightCallback: function(event, x, points, row, seriesName) {
//1)here I need to somehow reference the other series whose label is situated N columns from the highlighted serie ( I can also reference it by its name).
// 2) Hilight the other serie                                                 
                         }});

我尝试了很多不同的语法,但似乎没有什么能正常工作。 谁能帮我解决这个问题,我迷路了。

这是我想要实现的目标:

http://www.google.co.uk/publicdata/explore?ds=k3s92bru78li6_#!ctype=l&strail=false&bcs=d&nselm=h&met_y=ggxwdn_ngdp&scale_y=lin&ind_y=false&rdim=world&idim=world:Earth&idim=country:AR:DZ:AU:AZ&ifdim=world&tstart=343382400000&tend=1574064000000&hl=en_US&dl=en_US&ind=false

非常感谢!

最佳答案

如果我没理解错的话,你设置的是这样的:jsbin

通常,您使用 highlightSeriesOpts 为突出显示的系列设置样式,但前提是只有一个突出显示的系列。

如果您想以这种方式对数据建模(作为实际和预测的单独系列),您需要使用 highlightCallback 自行设置系列的样式。我将在下面提到一些关于此的粗俗事情,但这是可行的。

演示:jsbin

g = new Dygraph(document.getElementById("graph"),
                 "X,Y,Y projected,Z,Z projected\n" +
                 "2006,0,,3,\n" +
                 "2008,2,,6,\n" +
                 "2010,4,,8,\n" +
                 "2012,6,,9,\n" +
                 "2014,8,8,9,9\n" +
                 "2016,,10,,8\n" +
                 "2018,,12,,6\n" +
                 "2020,,14,,3\n",
                 {
                     colors: ['blue', 'blue', 'red', 'red'],
                     series: {
                       'Y': { },
                       'Y projected': { strokePattern: [5, 5] },
                       'Z': { },
                       'Z projected': { strokePattern: [5, 5] }
                     },
                     highlightCallback: function(_, _, _, row, seriesName) {
                       update(seriesName, row);
                     },
                     unhighlightCallback: function() {
                       update();
                     },
                     highlightSeriesOpts: {},
                     highlightSeriesBackgroundAlpha: 1.0
                 });

function update(selectedSeries, row) {
  var newOptions = {};
  var seriesNames = g.getLabels().slice(1);
  seriesNames.forEach(function(label) {
    newOptions[label] = {strokeWidth: 1};
  });

  if (selectedSeries == 'Y' || selectedSeries == 'Y projected') {
    newOptions['Y'] = newOptions['Y projected'] = {strokeWidth: 3};
  } else if (selectedSeries == 'Z' || selectedSeries == 'Z projected') {
    newOptions['Z'] = newOptions['Z projected'] = {strokeWidth: 3};
  }
  g.updateOptions({series: newOptions});
  if (typeof(row) !== 'undefined') {
    g.setSelection(row);
  }
}

想法是在 highlightCallback 中调用 updateOptions,根据是否(或其配对)为每个系列设置 strokeWidth 属性系列)被选中。

关于这件事有一些严重的事情:

  • 您必须为要传递给 highlightCallbackseriesName 参数设置 highlightSeriesOpts
  • 您需要通过设置 highlightSeriesBackgroundAlpha 来抵消 highlightSeriesOpts 的默认淡入淡出行为。
  • 调用 updateOptions 会清除选择,因此您必须显式调用 setSelection 才能重新选择。

如果您愿意将测量值和预测值建模为一个系列,那么您可以通过编写 custom plotter 来更干净地完成此操作在某些时候从实线切换到虚线。

这是一个演示:jsbin

g = new Dygraph(document.getElementById("graph"),
                 "X,Y,Z\n" +
                 "2004,0,3\n" +
                 "2006,2,6\n" +
                 "2008,4,8\n" +
                 "2010,6,9\n" +
                 "2012,8,9\n" +
                 "2014,10,8\n" +
                 "2016,12,6\n" +
                 "2018,14,3\n",
                 {
                     plotter: function(e) {
                       var ctx = e.drawingContext;
                       ctx.beginPath();
                       ctx.moveTo(e.points[0].canvasx, e.points[0].canvasy);
                       for (var i = 1; i < e.points.length; i++) {
                         var p = e.points[i];
                         ctx.lineTo(p.canvasx, p.canvasy);
                         if (p.xval == 2014) {
                           ctx.stroke();
                           ctx.beginPath();
                           ctx.moveTo(p.canvasx, p.canvasy);
                           ctx.setLineDash([5]);
                         }
                       }
                       ctx.stroke();
                       ctx.setLineDash([]);
                     },
                     highlightSeriesOpts: {
                       strokeWidth: 3
                     }
                 });

因为您的数据是一个系列,您不再需要同时突出显示多个系列,因此您可以使用 highlightSeriesOpts

关于javascript - Dygraphs - 同时突出显示两个系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006633/

相关文章:

javascript - 使用 Dygraphs 绘制无线图

javascript - 调整大小时 jQuery 的小错误?

javascript - 调用(动态)JS函数从Kotlin返回Promise

javascript - 当我放大/缩小 dygraphs 图表时,我可以更改 Y 轴标签显示吗?

r - 有没有办法突出显示 R dygraphs 中最接近的系列?

javascript - dygraph 1.1.1 似乎错误地检测数据源的范围值

javascript - 将 php 变量作为函数参数传递

javascript - 使 gridster.js 瓦片粘在特定的网格位置(捕捉到网格)

来自选择框的 JavaScript if 语句

r - 如何从 dygraph 中保存交互式图表