Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highcharts : labels inside columnrange bar

I am using the columnrange type from Highcharts-more. The default data labels formatter puts the min / max data at both ends of the range, which is nice.

Is there a way to attach another label to each point? (for example : the data name, right in the middle of the bar)

Please see my work at: JSFiddle

This is a family tree with life ranges. I'd like to display the names shown in tooltips at the center of bars.

(FYI I tried to overload the chart with text using the renderer : it worked... but the texts were attached to the chart, not to the points, and zooming left them in place, while the points moved around. Is there maybe a way to attach text labels to the each point on the chart onLoad event?)

Thank you.

$(function () {

$('#container').highcharts({

    chart: {
        type: 'columnrange',
        inverted: true,
        marginLeft: 40,
        zoomType: 'xy'
    },

    title: {
        text: 'My family tree'
    },

    xAxis: {

        labels: {
            enabled: false
        },
        min: 0,
        max: 10
    },

    yAxis: {
        type: 'datetime',
        min: new Date().setYear(1900).valueOf(),
        max: new Date().valueOf(),
        title: {
            text: 'Year'
        },
        endOnTick: false
    },

    legend: {
        enabled: false
    },

    plotOptions: {
        columnrange: {
            grouping: false,
            pointPadding: 0,
            dataLabels: {
                enabled: true,
                useHTML: true,
                formatter: function () {
                    if (new Date().setHours(0, 0, 0, 0) !== new Date(this.y).setHours(0, 0, 0, 0)) return '<span style="color:black">' + Highcharts.dateFormat('%Y', this.y) + '</span>';
                    else return '';
                }
            }
        }
    },

    tooltip: {
        headerFormat: '{series.name}<br/>',
        formatter: function () {
            var l = this.point.name + '<br/>' + Highcharts.dateFormat('%e/%m/%Y', this.point.low);
            if (new Date().setHours(0, 0, 0, 0) !== new Date(this.point.high).setHours(0, 0, 0, 0)) l += '<br/>' + Highcharts.dateFormat('%e/%m/%Y', this.point.high);
            return l;
        }
    },

    series: [{
        color: 'rgb(100,100,255)',
        pointWidth: 150,
        data: [{
            name: 'Yann B',
            low: Date.UTC(1976, 1, 27),
            high: new Date().valueOf(),
            x: 1 * 10 / 2
        }]
    }, {
        color: 'rgb(150,150,255)',
        pointWidth: 70,
        data: [{
            name: 'Jean-Yves B',
            low: Date.UTC(1947, 3, 26),
            high: Date.UTC(2006, 2, 10),
            x: 1 * 10 / 4
        }, {
            name: 'Josiane M',
            low: Date.UTC(1946, 8, 21),
            high: Date.UTC(1998, 11, 26),
            x: 3 * 10 / 4
        }]
    }, {
        color: 'rgb(200,200,255)',
        pointWidth: 30,
        data: [{
            name: 'Guillaume B',
            low: Date.UTC(1907, 7, 4),
            high: Date.UTC(1988, 1, 11),
            x: 1 * 10 / 8
        }, {
            name: 'Marie-Jeanne S',
            low: Date.UTC(1911, 7, 17),
            high: Date.UTC(1986, 2, 3),
            x: 3 * 10 / 8
        }, {
            name: 'Joseph M',
            low: Date.UTC(1921, 3, 11),
            high: Date.UTC(1996, 4, 23),
            x: 5 * 10 / 8
        }, {
            name: 'Marie K',
            low: Date.UTC(1925, 4, 4),
            high: new Date().valueOf(),
            x: 7 * 10 / 8
        }]
    }]

});

});
like image 813
user2590995 Avatar asked Feb 16 '23 17:02

user2590995


1 Answers

The use of the "inside" propriety is indeed relevant here. But I had to bypass the bug inherent to its use (see comment to last answer).

Here is a piece of code that works for datalabels. There still is a zIndex issue with the tool tips, and I'll try to post a more complete solution soon. http://jsfiddle.net/SineDie/TREwr/1/

dataLabels: {
        inside: true,
        enabled: true,
        useHTML: true,
        formatter: function () {
             if (this.y === this.point.low) {
                    var l = '<div style="text-align:center;color:black;width:' + (this.point.plotLow - this.point.plotHigh) + 'px">' 
                        + Highcharts.dateFormat('%Y', this.point.low) + ' - '
                        + this.point.name;
                    // to avoid marking as dead if still living... 
                    if (new Date().setHours(0, 0, 0, 0) !== new Date(this.point.high).setHours(0, 0, 0, 0))
                         l += ' - ' + Highcharts.dateFormat('%Y', this.point.high);
                    l += '</div>';
                    return l;
                }
             else return '';
        }
   }
like image 50
user2590995 Avatar answered Feb 18 '23 10:02

user2590995