Highcharts:当point = null时,如何显示工具提示? [英] Highcharts: how display tooltip, when point = null?

查看:115
本文介绍了Highcharts:当point = null时,如何显示工具提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们将鼠标悬停在解决方案

据我所知,这没有通用的选择,因为highcharts会忽略显示的空值.

另一方面,我们可以将null点替换为假"点,这些点的平均值在2个最接近点之间(这将导致图表流保持不变),并且具有自定义属性isNull,以后可以用作标记.

完成此操作后,我们可以对工具提示使用formatter函数,并以所需的方式操作工具提示,例如,当点isNull时仅显示序列名称.

$(function () {
    $('#container').highcharts({
        title: {
            text: 'The line is connected from April to Juni, despite the null value in May'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },

        plotOptions: {
            series: {
                connectNulls: true
            }
        },
        tooltip: {
            formatter: function(){
                if(this.point.isNull == undefined)
                    return this.series.name+"<br>"+"<b>value:</b>"+this.y;
                return this.series.name;
            }
        },
        series: [{
            data: [null, 71.5, 106.4, 129.2, null, 176.0, 135.6, null, 216.4, 194.1, 95.6, 54.4]
        }]

    }, function(chart){
        var series = chart.series[0];
        var data = series.data;
        $.each(data,function(i){
            var point = data[i];
            var prevPoint = data[i-1] != undefined ? data[i-1].y : 0;
            var nextPoint = data[i+1] != undefined ? data[i+1].y : 0;
            if(point.y == null){
                series.addPoint({x:point.x,y:(prevPoint+nextPoint)/2, isNull:true});
            }
        });
    });
});

http://jsfiddle.net/zb5ksxtc/1/

我希望这就是你的意思.

更新 我们还可以做一些不那么怪异的事情……(我猜在这种不寻常的情况下,进行一点点黑客就可以了)

我在这里所做的是创建一个伪"散布序列,其点位于真实序列的空值所在的位置(使用相同的平均逻辑). 该系列具有隐藏的标记,并且具有唯一的名称.

在那之后,我使用了shared工具提示和formatter来显示正确的工具提示. 我使用了name属性来确定要关注的系列:

$(function () {
    $('#container').highcharts({
        title: {
            text: 'The line is connected from April to Juni, despite the null value in May'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },

        plotOptions: {
            series: {
                connectNulls: true,

            },
        },
        tooltip: {
             shared:true,

            formatter: function(){
                var points = this.points;
                var seriesName = points[0].series.name;
                if(seriesName == 'fake')
                    return "Null tooltip";
                return seriesName+"<br>value: <b>"+this.y+"</b>";
            }
        },
        series: [{
            type:'areaspline',
            data: [null, 71.5, 106.4, 129.2, null, 176.0, 135.6, null, 216.4, 194.1, 95.6, 54.4]
        },
                 {
                     type:'spline',
                     lineWidth:0,
                     name:'fake',
                     showInLegend:false,
                     marker:{
                         symbol:'circle',
                         radius:0,
                     }
                 }
           ]

    }, function(chart){
        var series = chart.series[0];
        var fake_series = chart.series[1];
        $.each(series.data, function(i){
            var point = this;
            var nextPoint = series.data[i+1] != undefined ? series.data[i+1].y : 0;
            var prevPoint = series.data[i-1] != undefined ? series.data[i-1].y : 0;
            if(series.data[i].y == null)
                fake_series.addPoint([series.data[i].x, (nextPoint+prevPoint)/2]);
        });
    });
});

和课程设置: http://jsfiddle.net/zb5ksxtc/8/

When we hover on may In example, we don't see tooltip for this month, because data is null. Can I set the settings to see the tooltip when data is null?

解决方案

Well as far as I know there is no generic option for that since highcharts ignores null values from showing.

On the other hand, we can replace the null points with "fake" ones, that have an average value between the 2 closest points (this will cause the chart flow remain the same), and with a custom property isNull which can be used as a flag later.

After doing that, we can use a formatter function for the tooltip, and manipulate the tooltip the way we want, for example displaying only the series name when a point isNull.

$(function () {
    $('#container').highcharts({
        title: {
            text: 'The line is connected from April to Juni, despite the null value in May'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },

        plotOptions: {
            series: {
                connectNulls: true
            }
        },
        tooltip: {
            formatter: function(){
                if(this.point.isNull == undefined)
                    return this.series.name+"<br>"+"<b>value:</b>"+this.y;
                return this.series.name;
            }
        },
        series: [{
            data: [null, 71.5, 106.4, 129.2, null, 176.0, 135.6, null, 216.4, 194.1, 95.6, 54.4]
        }]

    }, function(chart){
        var series = chart.series[0];
        var data = series.data;
        $.each(data,function(i){
            var point = data[i];
            var prevPoint = data[i-1] != undefined ? data[i-1].y : 0;
            var nextPoint = data[i+1] != undefined ? data[i+1].y : 0;
            if(point.y == null){
                series.addPoint({x:point.x,y:(prevPoint+nextPoint)/2, isNull:true});
            }
        });
    });
});

http://jsfiddle.net/zb5ksxtc/1/

I hope this is what you meant.

UPDATE We can also do something not less hacky... (I guess a little hacking could work fine in this unusuall case)

What I did here was creating a "fake" scatter series, with points located where null values from the real series (used same average logic). The series has hidden markers, and has a unique name.

After that, I used a shared tooltip and a formatter to display the right tooltip. I used the name attribute to determine what series is focused:

$(function () {
    $('#container').highcharts({
        title: {
            text: 'The line is connected from April to Juni, despite the null value in May'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },

        plotOptions: {
            series: {
                connectNulls: true,

            },
        },
        tooltip: {
             shared:true,

            formatter: function(){
                var points = this.points;
                var seriesName = points[0].series.name;
                if(seriesName == 'fake')
                    return "Null tooltip";
                return seriesName+"<br>value: <b>"+this.y+"</b>";
            }
        },
        series: [{
            type:'areaspline',
            data: [null, 71.5, 106.4, 129.2, null, 176.0, 135.6, null, 216.4, 194.1, 95.6, 54.4]
        },
                 {
                     type:'spline',
                     lineWidth:0,
                     name:'fake',
                     showInLegend:false,
                     marker:{
                         symbol:'circle',
                         radius:0,
                     }
                 }
           ]

    }, function(chart){
        var series = chart.series[0];
        var fake_series = chart.series[1];
        $.each(series.data, function(i){
            var point = this;
            var nextPoint = series.data[i+1] != undefined ? series.data[i+1].y : 0;
            var prevPoint = series.data[i-1] != undefined ? series.data[i-1].y : 0;
            if(series.data[i].y == null)
                fake_series.addPoint([series.data[i].x, (nextPoint+prevPoint)/2]);
        });
    });
});

and the fiddle ofcourse: http://jsfiddle.net/zb5ksxtc/8/

这篇关于Highcharts:当point = null时,如何显示工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆