Highcharts => Getting the id of a point when clicking on a line chart

后端 未结 6 1421
既然无缘
既然无缘 2020-12-29 07:25

I am building a line chart and I would like, when I click on a point of the line, to display a popup containing some data about this point. The issue I try to resolve is to

相关标签:
6条回答
  • 2020-12-29 07:45

    I just did this by passing 3 objects into the series data array and then pulling it out of the object's config attribute from the click.

    So you can construct your series data something like this:

     series: [{
      name: 'Example',
      yAxis: 0,
      type: 'spline',
      data: [[1294099200000,220.0,37],[1296432000000,190.0,40],[1297036800000,184.4,5]]
    }]
    

    In the data attribute above the 1st element is the date (x), the 2nd element is another data point (y), and the 3rd is the id of the object that represent that data object. This "z" will not show up on the graph but will show up as the 3rd element in the config array. For example: using plotOptions point attribute to capture the click, the ID of the object is in the alert as this.config[2]

      plotOptions: {
        series: {
          cursor: 'pointer',
          point: {events: {click: function() {console.log(this); alert('Category: '+ this.category +', value: '+ this.y + 'Series: ' +  this.series.name + ' ID: ' + this.config[2])}}}
         }
       },
    
    0 讨论(0)
  • 2020-12-29 07:50

    According to the docs, event.point holds a pointer to the nearest point on the graph.

    So I'd write the event.point to the console, and see what's available.

    console.log(event.point);
    

    From the docs:

    click: Fires when the series is clicked. The this keyword refers to the series object itself. One parameter, event, is passed to the function. This contains common event information based on jQuery or MooTools depending on which library is used as the base for Highcharts. Additionally, event.point holds a pointer to the nearest point on the graph.

    Example based on the example from the docs: http://jsfiddle.net/5nTYd/

    Click a point, and check the console.

    0 讨论(0)
  • 2020-12-29 07:54

    i found this old post in my search to ==>add a marker to a point when i click a Highcharts "Trend Line" [in examples: "line-time-series"] chart[when i click anywhere on the drawn line itself]. well, without showing you too much code, look in the

      cursor: 'pointer',
                        point: {
                          events: {
                                 click: function(e) {
                                    alert("X("+this.x+"),Y("+this.y+")");
                                    }//click
                              }//events
                               }//point
    

    if you would like more detail, i'm happy to provide!

    0 讨论(0)
  • 2020-12-29 07:54
     plotOptions: {
       series: {
         cursor: 'pointer',
         point: {
           events: {
             click: function() {
               console.log(this); 
               alert('Category: '+ this.category +', value: '+ this.y + 'Series: ' +  this.series.name + ' ID: ' + this.config[2])
             }
           }
         }
       }
     },
    
    0 讨论(0)
  • 2020-12-29 07:58

    I had the same problem ... if I understand correctly. My solution is this, to get the id of the series ... See if it helps ...

    plotOptions{
     series:{
      cursor: 'pointer',
        events: {
          click: function(event) {
            console.log(event.point.series.userOptions.id);
          }
        }
      }
    
    0 讨论(0)
  • 2020-12-29 08:06

    To return the 'ID' of the selected point on the chart use the 'X' value:

    plotOptions: {
        series: {
            cursor: 'pointer',
            events: {
                click: function(event) {
                       // Log to console
                    console.log(event.point);
                    alert(this.name +' clicked\n'+
                          'Alt: '+ event.altKey +'\n'+
                          'Control: '+ event.ctrlKey +'\n'+
                          'Shift: '+ event.shiftKey +'\n'+
                          'Index: '+ event.point.x);
                }
            }
        }
    },
    

    See an example here: http://jsfiddle.net/engemasa/mxRwg/

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