如何处理angular-chart.js在y轴上的史诗长文本? [英] How to handle epic long text at y-axis for angular-chart.js?

查看:128
本文介绍了如何处理angular-chart.js在y轴上的史诗长文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 angular-chart.js 构建一个响应式应用程序。



(没有足够的仓库,对不起)


I'm building a responsive app that using angular-chart.js.

(Not enough repo, sorry)
http://i.imgur.com/g648Iws.png
http://i.imgur.com/LK1eQCo.png

As you can see, some of the text were too long (y-axis) and it squeezed my chart. I tried to truncate the text, but it doesn't look nice if you have larger devices.

Is there any way to handle this situation or (truncate dynamically) ?

ChartJsProvider.setOptions({
    responsive: true,
    barShowStroke: false,
    maintainAspectRatio: false  // I tried with 'true' but still looks the same
});

Note that I did not add the width and height attribute because I want the graph to fit responsively.

<canvas id="bar" class="chart chart-bar" data="data" labels="labels" legend="true" series="series"></canvas>

Any help is appreciated.

解决方案

You can trim the labels depending on the screen width, like so

Chart.types.Bar.extend({
    name: "BarAlt",
    initialize: function (data) {
        var originalLabels;
        // figure out the best label length for the current window size
        if (this.options.responsiveLabelLengths !== undefined) {
            var maxLabelLength;
            this.options.responsiveLabelLengths
                .sort(function (a, b) {
                    return b.uptoWidth - a.uptoWidth;
                })
                .forEach(function (spec) {
                    if (screen.width < spec.uptoWidth)
                        maxLabelLength = spec.maxLabelLength;
                })

            // update the labels if required
            if (maxLabelLength !== undefined) {
                originalLabels = Chart.helpers.clone(data.labels);
                data.labels.forEach(function (label, i) {
                    if (data.labels[i].length > maxLabelLength)
                        data.labels[i] = data.labels[i].substring(0, maxLabelLength) + '...';
                })
            }
        }

        Chart.types.Bar.prototype.initialize.apply(this, arguments);

        // optional - now restore the original labels for the tooltips
        if (originalLabels !== undefined) {
            this.datasets.forEach(function (dataset) {
                dataset.bars.forEach(function (bar, i) {
                    bar.label = originalLabels[i];
                })
            })
        }
    }
});



var data = {
    labels: ["Really Really Really Really Really Long Text", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.5)",
            strokeColor: "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.5)",
            strokeColor: "rgba(151,187,205,0.8)",
            highlightFill: "rgba(151,187,205,0.75)",
            highlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};

var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).BarAlt(data, {
    responsiveLabelLengths: [
        {
            uptoWidth: 480,
            maxLabelLength: 5
        },
        {
            uptoWidth: 768,
            maxLabelLength: 10
        },
        {
            uptoWidth: 992,
            maxLabelLength: 20
        },
        {
            uptoWidth: 1200,
            maxLabelLength: undefined
        },
    ]
});

Since we are using screen width you need to use Chrome Developer tools and choose a proper viewport sizing to see the effect. For the same reason, this is not going to have any effect if you are resizing your window - if you need that, just replace screen.width by window.innerWidth.


To add this as a new directive to angular-chart is pretty simple. After the above block of code (and after the script include for angular-chart.js) add a new directive for BarAlt, like so

angular.module('chart.js')
    .directive('chartBarAlt', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('BarAlt'); }]);

Then use the directive like so

<canvas id="line" class="chart chart-bar-alt" data="data" options="options"
        labels="labels"></canvas> 

Notice options? You will need to include the responsiveLabelLengths in your options for BarAlt to kick in (otherwise it just acts like a normal bar chart). You do that like this (in your controller)

$scope.options = {
    responsiveLabelLengths: [
        {
            uptoWidth: 480,
            maxLabelLength: 5
        },
        {
            uptoWidth: 768,
            maxLabelLength: 10
        },
        {
            uptoWidth: 992,
            maxLabelLength: 20
        },
        {
            uptoWidth: 1200,
            maxLabelLength: undefined
        },
    ]
};


Note that you need to use Chrome Developer tools and choose a proper viewport sizing to see the effect

Fiddle - http://jsfiddle.net/mqo24Lnx/


Here's how the trimming works on different screen sizes

这篇关于如何处理angular-chart.js在y轴上的史诗长文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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