2

我正在构建一个使用angular-chart.js.

(回购不够,抱歉)
http://i.imgur.com/g648Iws.png
http://i.imgur.com/LK1eQCo.png

如您所见,一些文本太长(y 轴),它挤压了我的图表。我试图截断文本,但如果你有更大的设备,它看起来不太好。

有没有办法处理这种情况或(动态截断)?

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

请注意,我没有添加宽度和高度属性,因为我希望图形能够响应地适应。

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

任何帮助表示赞赏。

4

1 回答 1

1

您可以根据屏幕宽度修剪标签,如下所示

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
        },
    ]
});

由于我们使用的是屏幕宽度,您需要使用 Chrome 开发者工具并选择合适的视口大小来查看效果。出于同样的原因,如果您正在调整窗口大小,这不会有任何影响 - 如果您需要,只需将 screen.width 替换为 window.innerWidth。


将此作为新指令添加到 angular-chart 非常简单。在上述代码块之后(以及 angular-chart.js 的脚本包含之后)为 BarAlt 添加一个新指令,如下所示

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

然后像这样使用指令

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

注意选项?您需要在您的选项中包含 responsiveLabelLengths 才能启动 BarAlt(否则它就像一个普通的条形图)。你这样做(在你的控制器中)

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

请注意,您需要使用 Chrome 开发人员工具并选择适当的视口大小才能看到效果

小提琴 - http://jsfiddle.net/mqo24Lnx/


以下是修剪在不同屏幕尺寸上的工作方式

在此处输入图像描述

于 2015-07-29T11:40:14.463 回答