如何将工具提示转换为高图表中的onclick事件?

时间:2017-08-24 07:13:46

标签: javascript jquery css highcharts

I have an column highchart on mouseover entire month values are showing i want one place value on click event我正在使用鼠标功能。我需要在Highcharts中使用鼠标点击事件的工具提示。

Highcharts.chart('container', {
    title: {
        text: 'Mouse events demo'
    },
    subtitle: {
        text: 'On point mouse click, the values should be reported in top left'
    },
    plotOptions: {
        series: {
            point: {
                events: {
                    mouseOver: function() {
                        var chart = this.series.chart;
                        if (!chart.lbl) {
                            chart.lbl = chart.renderer.label('')
                                .attr({
                                    padding: 10,
                                    r: 10,
                                    fill: Highcharts.getOptions().colors[1]
                                })
                                .css({
                                    color: '#FFFFFF'
                                })
                                .add();
                        }
                        chart.lbl
                            .show()
                            .attr({
                                text: 'x: ' + this.x + ', y: ' + this.y
                            });
                    }
                }
            },
            events: {
                mouseOut: function() {
                    if (this.chart.lbl) {
                        this.chart.lbl.hide();
                    }
                }
            }
        }
    },
    tooltip: {
        enabled: false
    },
    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]
});

代码也可以在JSFiddle上找到。而不是鼠标悬停,我想要点击事件。

2 个答案:

答案 0 :(得分:0)

用下面的代码替换那个小提琴中的JavaScript部分

Highcharts.chart('container', {
title: {
    text: 'Mouse events demo'
},
subtitle: {
    text: 'On point mouse over or mouse out, the values should be reported in top left'
},
plotOptions: {
    series: {
        point: {
            events: {
               click: function () {
                    var chart = this.series.chart;
                    if (!chart.lbl) {
                        chart.lbl = chart.renderer.label('')
                            .attr({
                                padding: 10,
                                r: 10,
                                fill: Highcharts.getOptions().colors[1]
                            })
                            .css({
                                color: '#FFFFFF'
                            })
                            .add();
                    }
                    chart.lbl
                        .show()
                        .attr({
                            text: 'x: ' + this.x + ', y: ' + this.y
                        });
                }
            }
        },
        events: {
            mouseOut: function () {
                if (this.chart.lbl) {
                    this.chart.lbl.hide();
                }
            }
        }
    }
},

tooltip: {
    enabled: false
},

series: [{
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});

只需更改鼠标悬停功能即可点击

答案 1 :(得分:0)

在这里,你需要改变两件事。

  1. 点击事件:我在此处显示提醒,您可以根据要以格式显示工具提示的内容采取行动
  2. 禁用工具提示:这是为了隐藏鼠标移动时的默认工具提示
  3. 小提琴演示:http://jsfiddle.net/77mq9k3a/2/

    代码段:

    Highcharts.chart('container', {
    
      title: {
        text: 'Mouse events demo'
      },
      subtitle: {
        text: 'On point mouse click, the values should be reported in top left'
      },
      plotOptions: {
        series: {
          cursor: 'pointer',
          events: {
            click: function(event) {
              alert(
                this.name + ' clicked\n' +
                'Alt: ' + event.altKey + '\n' +
                'Control: ' + event.ctrlKey + '\n' +
                'Meta: ' + event.metaKey + '\n' +
                'Shift: ' + event.shiftKey
              );
            }
          }
        }
      },
    
      tooltip: {
        enabled: false
      },
    
      series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
      }]
    
    });
    

    您可以在此处找到highchart API。 http://api.highcharts.com/highcharts/plotOptions.series.events.click