【发布时间】:2019-03-22 13:53:51
【问题描述】:
我使用 chart.js 的代码
var ctx_1 = document.getElementById('non_200_pages').getContext('2d');
var myChart_1 = new Chart(ctx_1, {
type: 'horizontalBar',
data: {
labels: ["Total","301 Redirect","Broken Pages (4xx Errors)","Uncategorised HTTP Response Codes","5xx Errors","Unauthorised Pages","Non-301 Redirects"],
datasets: [{
data: [ {{ $array_non_200_pages[0] }}, {{ $array_non_200_pages[1] }}, {{ $array_non_200_pages[2] }}, {{ $array_non_200_pages[3] }}, {{ $array_non_200_pages[4] }}, {{ $array_non_200_pages[5] }}, {{ $array_non_200_pages[6]}} ],
backgroundColor: [
'rgba(237, 56, 98, 1.0)',
'rgba(237, 56, 98, 1.0)',
'rgba(237, 56, 98, 1.0)',
'rgba(237, 56, 98, 1.0)',
'rgba(237, 56, 98, 1.0)',
'rgba(237, 56, 98, 1.0)',
'rgba(237, 56, 98, 1.0)'
]
}]
},
options: {
showAllTooltips: true,
tooltips: {
enabled: true,
displayColors: false,
yPadding: 20,
xPadding: 30,
caretSize: 10,
backgroundColor: 'rgba(240, 240, 240, 1)',
bodyFontSize: 16,
bodyFontColor: 'rgb(50, 50, 50)',
borderColor: 'rgba(0,0,0,1)',
borderWidth: 1,
cornerRadius: 0,
yAlign: 'bottom',
xAlign: 'center',
position: 'custom',
custom: function(tooltip) {
if (!tooltip) return;
// disable displaying the color box;
tooltip.displayColors = false;
},
callbacks: {
// use label callback to return the desired label
label: function(tooltipItem, data) {
return tooltipItem.yLabel + " : " + tooltipItem.xLabel ;
},
// remove title
title: function(tooltipItem, data) {
return;
}
}
},
responsive: false,
legend: { display: false },
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
},
gridLines: {
display: false
},
}],
xAxes: [{
ticks: {
stepSize:5,
display: false
},
gridLines: {
drawBorder: false,
}
}],
},
plugins: {
datalabels: {
align: 'end',
anchor: 'end',
backgroundColor: function(context) {
return context.dataset.backgroundColor;
},
borderRadius: 4,
color: 'white',
formatter: Math.round
}
}
}
});
我的工作
我在如何将标签文本设置为左对齐时遇到问题。我想要做的是我希望我的标签与下一张图片相同,但我不知道Options:{ 文本对齐设置的位置。有没有人可以帮我解决它?我在https://www.chartjs.org/docs/latest/configuration/legend.html 上找不到任何内容。提前谢谢你。
【问题讨论】:
-
没有直接的方法可以做到这一点,您是否尝试过查看 stackoverflow 上的一些旧答案? stackoverflow.com/questions/43677197/…
-
@KunalKhivensara 我已经在尝试和研究,但从未发现任何与我的问题相似的问题。感谢您的宝贵时间,我真的很感激。
-
@KunalKhivensara 如何对齐图表的标题...
textAlign: "left"/horizontal-align不起作用 -
@ReyYoung 您好,您需要使用标题选项下的
position属性,如下所示:title:{display: true,text:"testTitle", position: "left" }。请参阅此小提琴 -> jsfiddle.net/xnom85ja/1 或文档 -> chartjs.org/docs/latest/configuration/title.html#position -
兄弟,我试过了..它给了我另一个位置,我只想将它对齐到左边..stackoverflow.com/questions/54396656/…
标签: javascript charts chart.js