score:4

Accepted answer

chart.js uses canvas filltext for the tooltips in it's default tooltip function. filltext unfortunately doesn't support word wrapping.

so you'll have to write your own custom tooltip function. there again, the labels are also used for the x axis. the easiest way would be to use \b (it's just ignored in your axis filltext) and swap it out in your custom tooltip function.


preview

enter image description here


code

var mylinechart = new chart(ctx).bar(data, {
    customtooltips: function (tooltip) {
        var tooltipel = $('#chartjs-tooltip');

        if (!tooltip) {
            tooltipel.css({
                opacity: 0
            });
            return;
        }

        // split out the label and value and make your own tooltip here
        var parts = tooltip.text.split(":");
        var re = new regexp('\b', 'g');
        var innerhtml = '<span>' + parts[0].trim().replace(re, '<br/>') + '</span> : <span><b>' + parts[1].trim() + '</b></span>';
        tooltipel.html(innerhtml);

        tooltipel.css({
            opacity: 1,
            left: tooltip.chart.canvas.offsetleft + tooltip.x + 'px',
            top: tooltip.chart.canvas.offsettop + tooltip.y + 'px',
            fontfamily: tooltip.fontfamily,
            fontsize: tooltip.fontsize,
            fontstyle: tooltip.fontstyle,
        });
    }
});

with the following markup added (your tooltip wrapper)

<div id="chartjs-tooltip"></div>

and the following css (for positioning your tooltip)

 #chartjs-tooltip {
     opacity: 0;
     position: absolute;
     background: rgba(0, 0, 0, .7);
     color: white;
     padding: 3px;
     border-radius: 3px;
     -webkit-transition: all .1s ease;
     transition: all .1s ease;
     pointer-events: none;
     -webkit-transform: translate(-50%, -120%);
     transform: translate(-50%, -120%);
 }

and your labels would look like

labels: ["jan\bua\bry", "february", "mar\bch", "april", "may", "june", "july"],

with \b standing for breaks. note that you \n, \r, \t, \f... won't work if you don't want spaces in your x axis labels. if you actually want there to be spaces just use \n or something and change the regex accordingly

fiddle - http://jsfiddle.net/5h1r71g8/


Related Query

More Query from same tag