我正在使用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
});
请注意,我没有添加width和height属性,因为我希望图表能够响应。
<canvas id="bar" class="chart chart-bar" data="data" labels="labels" legend="true" series="series"></canvas>
感谢任何帮助。
答案 0 :(得分: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开发者工具并选择适当的视口大小来查看效果。出于同样的原因,如果要调整窗口大小,这将不会产生任何影响 - 如果需要,只需用window.innerWidth替换screen.width。
将此作为新指令添加到角度图表非常简单。在上面的代码块之后(并且在包含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>
通知选项? 将需要在您的BarAlt选项中包含responsiveLabelLengths(否则它就像一个普通的条形图)。你这样做(在你的控制器中)
$scope.options = {
responsiveLabelLengths: [
{
uptoWidth: 480,
maxLabelLength: 5
},
{
uptoWidth: 768,
maxLabelLength: 10
},
{
uptoWidth: 992,
maxLabelLength: 20
},
{
uptoWidth: 1200,
maxLabelLength: undefined
},
]
};
请注意,您需要使用Chrome开发者工具并选择适当的视口大小来查看效果
小提琴 - http://jsfiddle.net/mqo24Lnx/
以下是修剪在不同屏幕尺寸上的工作原理