Flot charts- toggling a series on/off

后端 未结 1 2052
無奈伤痛
無奈伤痛 2021-01-16 13:43

I was successfully established my flot chart, based on this previous post

I want to enable to viewer to show/hide the series with a click. I found bunch of solutions

相关标签:
1条回答
  • 2021-01-16 14:06

    1) The onClick directly in the HTML is a bad idea when the scope of the Chart object is not global. I changed it to a jquery event handler:

    $('body').on 'click', 'a.legendtoggle', (event) ->
        Chart.togglePlot($(this).data('index'))
        return false
    

    2) The series object in the labelFormatter function has no idx property, so I used a variable inside the Chart object:

    labelFormatter: (label, series) ->
        "<a href=\"#\" class=\"legendtoggle\" data-index=\"" + Chart.legendindex++ + "\">" + label + "</a>"
    

    3) I also put your plot object inside Chart so that it can be accessed inside the togglePlot function. And I changed the lines to points since your data has only one datapoint per series:

    togglePlot: (seriesIdx) ->
        someData = this.plot.getData()
        someData[seriesIdx].points.show = not someData[seriesIdx].points.show
        this.plot.setData someData
        this.plot.draw()
        return
    

    That should be all I changed, but compare for yourself if I got everything.
    Here is a working fiddle: http://jsfiddle.net/jhpgtxz1/2/

    PS: Never again CoffeeScript for me :-(

    0 讨论(0)
提交回复
热议问题