简体   繁体   中英

jQuery flot graph plot tooltip

I have a graph that displays like so:

图形

I'd like the tooltip to display "£1500 sales on 31/07" rather than 4.

My x-axis is generated like so:

xaxis: {
    tickColor: 'transparent',
    tickDecimals: 0,
    ticks: [[1,'27/07'],[2,'28/07'],[3,'29/07'],[4,'30/07'],[5,'31/07'],[6,'01/08'],[7,'02/08']]
},

And the code to generate the tooltip is:

function showTooltip(x, y, contents) {
    jQuery('<div id="tooltip">' + contents + '</div>').css({
        top: y - 16,
        left: x + 20
    }).appendTo('body').fadeIn();
}

var previousPoint = null;

jQuery('#graph-lines, #graph-bars').bind('plothover', function (event, pos, item) {
    if (item) {
        if (previousPoint != item.dataIndex) {
            previousPoint = item.dataIndex;
            jQuery('#tooltip').remove();
            var x = item.datapoint[0],
                y = item.datapoint[1];
                showTooltip(item.pageX, item.pageY, '&pound;' + y + ' sales on ' + x);
        }
    } else {
        jQuery('#tooltip').remove();
        previousPoint = null;
    }
});

The important line is:

var x = item.datapoint[0]

How can I get this to read the tick string rather than value?

Thanks

I've figured this out, I realised that I need to save the x-axis label within the data points:

var graphData = [{
        // Shop
        data: [ [1, 1300, '27/07'], [2, 1600, '28/07'], [3, 1700, '29/07'], [4, 1500, '30/07'], [5, 1500, '31/07'], [6, 1200, '01/08'], [7, 1000, '02/08'] ],
        color: '#71c73e'
    }, {
        // Net
        data: [ [1, 900, '27/07'], [2, 1100, '28/07'], [3, 1550, '29/07'], [4, 1600, '30/07'], [5, 1800, '31/07'], [6, 1400, '01/08'], [7, 1300, '02/08'] ],
        color: '#77b7c5',
        points: { radius: 4, fillColor: '#77b7c5' }
    }
];

And then replace this:

var x = item.datapoint[0]

With this:

var x = item.series.data[item.dataIndex][2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM