Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a point by ID using Highcharts

Using Highcharts, how can I select a point using it's id? For example, if I create a chart using the following code:

 chart1 = new Highcharts.Chart({
         chart: {
            renderTo: 'container',
            type: 'scatter'
         },
         title: {
            text: 'Fruit Consumption'
         },
         xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
         },
         yAxis: {
            title: {
               text: 'Fruit eaten'
            }
         },
         series: [{
            name: 'Jane',
            data: [{
                name: 'Point1',
                x: 1,
                y: 2
            }, {
                name: 'Point2',
                x: 2,
                y: 5
            }]
         }, {
            name: 'John',
            data: [5, 7, 3]
         }]
      });
   });

The tooltip tells me that when I hover over a point, what the id is. However, I can't figure out the syntax to identify that point. I know that chart1.series[0].name returns Jane. Also, chart1.series[0].data[0].namereturnspoint1` Is there an easy way that I can just select the point and change the color knowing only 'point1'?

I'm wondering if there is a more efficient way other than looping through all of the points each time.

like image 209
djq Avatar asked Oct 03 '12 22:10

djq


1 Answers

You can set an id for each point you want to get.

series: [{
    name: 'Jane',
    data: [{
        'name': 'Point1',
        'id': 'point1',
        'x': 1,
        'y': 2
    }, {
        'name': 'Point2',
        'id': 'point2',
        'x': 2,
        'y': 5
    }]
}, {
    name: 'John',
    data: [5, 7, 3]
}]

Then you can get the point by the following code.

// assuming that chart is your chart var
chart.get('point1');

demo

Or if you don't want to set an id you can simple loop thrue points to compare the name you want to find with the point name.

Reference:

  • http://api.highcharts.com/highstock#object-Chart
like image 195
Ricardo Alvaro Lohmann Avatar answered Oct 02 '22 14:10

Ricardo Alvaro Lohmann