2016-02-01 4 views
0

Мы можем получить любую сюжетные из массива по индексу:Highcharts получить сюжетный по идентификатору

var plotLine = $('#container').highcharts().xAxis[0].plotLinesAndBands[0]; 

Но этот путь не просто, когда мы имеем много динамических сюжетные. Есть ли способ легко получить PlotLine по id?

Как удаление:

chart.xAxis[0].removePlotLine('plotline-1'); 

ответ

0

Вы можете определить сюжетный Id с добавлением дел. поскольку все div будут иметь разные id, plotLine всегда будет иметь уникальный идентификатор.

посмотрит, подобная функциональность я реализовал в моем проекте:

var plotId = divId + 'plot-line-scatter'; 
     var chartX = $('#' + divId + '').highcharts(); 
     if(null != chartX){ 
      chartX.xAxis[0].removePlotLine(plotId); 
      chartX.xAxis[0].addPlotLine({ 
       value : time, 
       color : '#FF0000', 
       width : 1, 
       id : plotId 
      }); 
+0

Спасибо за ответ. Все мои PlotLines получают уникальный идентификатор при создании. Но как я могу получить объект PlotLine из диаграммы по id? Что-то вроде этого - chartX.xAxis [0] .getPlotLine (plotId); – r00tGER

2

Я нашел только полный спектр итерационного метода:

for (var i = 0; i < axis.plotLinesAndBands.length; i++) { 
    if (axis.plotLinesAndBands[i].id === plotLineId) { 
    return axis.plotLinesAndBands[i]; 
    } 
} 
0

Я искал решение, чтобы получить plotBand пути id, чтобы скрыть его с помощью серии. Основываясь на фрагменте r00tGER я сделал функцию, чтобы позвонить:

/** 
* Get the plotBand or plotLine with given id out of all plotBands and plotLines of one Axis 
* @param id The id of the plotBand or plotLine you are looking for 
* @param axis The axis where the plotBand/plotLine is bound 
*/ 
function getPlotBandOrLineById(id, axis) { 
    // loop through all plotBands/plotLines and check their id 
    for (var i = 0; i < axis.plotLinesAndBands.length; i++) { 
    if (axis.plotLinesAndBands[i].id === id) { 
     return axis.plotLinesAndBands[i]; 
    } 
    } 
} 

С этой функцией вы можете скрыть и показать на plotBand (или сюжетный) с их серией:

$(function() { 
    $('#chart_container').highcharts('StockChart', { 
    ... 
    series: [ 
     { 
     name: 'Carbs', 
     ... 
     events: { 
      show: function() { 
      var plotBand = getPlotBandOrLineById('goal_carbs', this.chart.yAxis[0]); 
      plotBand.hidden = false; 
      plotBand.svgElem.show(); 
      }, 
      hide: function() { 
      var plotBand = getPlotBandOrLineById('goal_carbs', this.chart.yAxis[0]); 
      plotBand.hidden = true; 
      plotBand.svgElem.hide(); 
      }, 
     }, 
     }, 
    ], 
    ... 
    }); 
}); 
0

Вы могли бы использовать часть логику функции removePlotBandOrLine() прототипа Axis для создания и добавления прототипа новой функции, называемой getPlotLineOrBand. Он будет циклически перемещаться по всем полосам линий/линиям и возвращать тот, где id является правильным.

$(function(H) { 
    H.AxisPlotLineOrBandExtension.getPlotLineOrBand = function(id) { 
    var plotLinesAndBands = this.plotLinesAndBands, 
    i = plotLinesAndBands.length; 

    while (i--) { 
     if (plotLinesAndBands[i].id === id) { 
     return plotLinesAndBands[i]; 
     } 
    } 

    return; 
    }; 

    H.extend(H.Axis.prototype, H.AxisPlotLineOrBandExtension); 
}(Highcharts)); 

Пример: http://jsfiddle.net/d_paul/g9sf75ju/