Chart.js and long labels

前端 未结 6 668
难免孤独
难免孤独 2020-12-30 22:45

I use Chart.js to display a Radar Chart. My problem is that some labels are very long : the chart can\'t be display or it appears very small.

So, is there a way to b

6条回答
  •  生来不讨喜
    2020-12-30 23:11

    With ChartJS 2.1.6 and using @ArivanBastos answer

    Just pass your long label to the following function, it will return your label in an array form, each element respecting your assigned maxWidth.

    /* takes a string phrase and breaks it into separate phrases 
       no bigger than 'maxwidth', breaks are made at complete words.*/
    
    function formatLabel(str, maxwidth){
        var sections = [];
        var words = str.split(" ");
        var temp = "";
    
        words.forEach(function(item, index){
            if(temp.length > 0)
            {
                var concat = temp + ' ' + item;
    
                if(concat.length > maxwidth){
                    sections.push(temp);
                    temp = "";
                }
                else{
                    if(index == (words.length-1))
                    {
                        sections.push(concat);
                        return;
                    }
                    else{
                        temp = concat;
                        return;
                    }
                }
            }
    
            if(index == (words.length-1))
            {
                sections.push(item);
                return;
            }
    
            if(item.length < maxwidth) {
                temp = item;
            }
            else {
                sections.push(item);
            }
    
        });
    
        return sections;
    }
    

提交回复
热议问题