Chr*_*min 7 javascript charts chart.js
我的代码使用 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
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
我的工作
我在如何设置标签文本左对齐方面遇到问题。我想要做的是我希望我的标签与下一张图片相同,但我不知道Options:{文本对齐设置的位置。有没有人可以帮助我如何解决它?我在https://www.chartjs.org/docs/latest/configuration/legend.html上找不到任何内容 。先感谢您。
您可以通过使用刻度配置上的 crossAlignment 选项来控制标签对齐。共有三个可能的值:“ near”、“ center”和“ far”。
在你的情况下,你只需使用near
options: {
...
scales: {
y: {
ticks: {
crossAlign: "far",
},
},
},
}
Run Code Online (Sandbox Code Playgroud)
该yAxis.ticks标签可以通过定义左对齐mirror: true与添加一些结合padding。
yAxes: [{
ticks: {
mirror: true,
padding: 220
},
Run Code Online (Sandbox Code Playgroud)
要使标签在图表区域可见,需要在图表布局左侧定义相同的填充。
layout: {
padding: {
left: 220
}
},
Run Code Online (Sandbox Code Playgroud)
请在下面查看您更改的代码。
yAxes: [{
ticks: {
mirror: true,
padding: 220
},
Run Code Online (Sandbox Code Playgroud)
layout: {
padding: {
left: 220
}
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8049 次 |
| 最近记录: |