If a point is clicked then add a marker in highcharts

ぐ巨炮叔叔 提交于 2019-12-13 06:24:30

问题


I want to add a maker when a point is clicked same like alerting a selected point...the code for selecting a point :

cursor: 'pointer',
            events: {
                click: function(event) {
                    alert('x: ' + event.xAxis[0].value+' y: '+event.yAxis[0].value);
                }
            }},

Now what should I write in place of alert to mark the point when it is clicked?


回答1:


instead of click event for this you can use marker states

plotOptions > series > marker > states > select > radius: 10

  plotOptions: {
    series: {
      allowPointSelect: true,
      marker: {
        radius: 1,
        states: {
          select: {
            radius: 10,
            fillColor: 'red'
          }
        }
      }
    }
  }

here is a working example

API reference : http://api.highcharts.com/highcharts#plotOptions.line.marker.states.select

Hope this will help you to achieve what you need.




回答2:


I assume that is correct with that scenario:

http://jsfiddle.net/rV7As/

plotOptions: {
        series: {
            allowPointSelect: true,
            point: {
                events: {
                    click: function (event) {
                        alert('aaa');
                    }
                }

            }
        }
    },



回答3:


Thanks to all for your help...I got it what I wanted. I created a js fiddle accordingly.

 $(function () {


            $('#container').highcharts({
                chart: {

                    type: 'line',
                    margin: [70, 50, 60, 80],

                    events: {
                        click: function(e) {
                            var ren = this.renderer;

                            // find the clicked values and the series
                            var x1 = e.xAxis[0].value;
                            x1 = this.xAxis[0].toPixels(x1);
                                y1 = e.yAxis[0].value;
                            y1 = this.yAxis[0].toPixels(y1);
                               series = this.series[0];

                    ren.circle(x1, y1, 5).attr({
                    'stroke-width': 2,
                    stroke: 'red',
                    fill: 'yellow',
                    zIndex: 3
                })
                .add();

                            // Add it
                           // series.addPoint([x, y]);

                        }
                    }
                },
                title: {
                    text: 'User supplied data'
                },
                subtitle: {
                    text: 'Click the plot area to add a point. Click a point to remove it.'
                },
                xAxis: {
                    minPadding: 0.2,
                    maxPadding: 0.2,
                    maxZoom: 60
                },
                yAxis: {
                    title: {
                        text: 'Value'
                    },
                    minPadding: 0.2,
                    maxPadding: 0.2,
                    maxZoom: 60,
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                legend: {
                    enabled: false
                },
                exporting: {
                    enabled: false
                },
                plotOptions: {
                    series: {
                        lineWidth: 1,
                        point: {
                            events: {
                                'click': function() {
                                    if (this.series.data.length > 1) this.remove();
                                }
                            }
                        }
                    }
                },
                series: [{
                    data: [[20, 20], [80, 80]]
                }]
            });
        });

http://jsfiddle.net/das_palash89/WN3XC/3/



来源:https://stackoverflow.com/questions/21569611/if-a-point-is-clicked-then-add-a-marker-in-highcharts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!