我正在使用chart.js库创建一个图表,我几乎完成了。最后一件事是将yAxis的标题放置在图例下面的顶部。
我现在有这样的东西。
https://i.stack.imgur.com/FD5s7.png
我所期待的结果是这样的。
https://i.stack.imgur.com/4T5l3.png
我真的很感激你的帮助。
代码:https://jsfiddle.net/w2qzjhcb/2/
- 超文本标记语言**
<canvas id="myChart"></canvas>
- 脚本语言**
var maxValue = 180000;
var ctx = document.getElementById("myChart").getContext('2d');
Chart.defaults.global.defaultFontFamily = "Arial";
Chart.defaults.global.defaultFontSize = 18;
Chart.defaults.global.defaultFontColor = 'black';
Chart.defaults.global.plugins.datalabels.font.size = '15';
Chart.defaults.global.plugins.datalabels.font.weight = 'bold';
function converToSpaced(item){
tisice = (Math.floor(item/1000)).toString();
stovky = (item-(Math.floor(item/1000))*1000).toString();
switch (stovky.length){
case 1:
stovky = "00" + stovky;
break;
case 2:
stovky = "0" + stovky;
break;
case 3:
break;
}
return tisice + ' ' + stovky;
};
var data = {
labels: ["2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018"],
datasets: [
{
label: 'zisk Veolia, a.s. v mil. Kč',
data: [
61743,
95242,
125991,
131192,
130616,
149573,
155744,
139540,
128046,
113326
],
backgroundColor: 'black'
},
{
label: 'zisk VHS OL',
data:[
633,
3425,
10636,
12130,
13708,
17095,
23804,
17787,
21820,
20179
],
backgroundColor: '#000e4b'
},
{
label: 'zisk VHS OL',
data:[
9123,
9123,
6748,
6435,
-12623,
5732,
7304,
11167,
9149,
20437
],
backgroundColor: '#009de0'
},
{
label: 'zisk VHS OL',
data:[
5202,
6153,
1803,
1480,
1845,
2403,
3258,
3020,
3343,
3726
],
backgroundColor: '#55d9ff'
},
{
label: 'zisk VHS OL',
data:[
14958,
12393,
19187,
20045,
2930,
25230,
34366,
31974,
34312,
44342
],
backgroundColor: 'white',
borderWidth: 1,
borderColor: 'black'
}
]
};
var stovky;
var tisice;
var myChart = new Chart(ctx, {
type: 'bar',
data: data,
options: {
scales:{
yAxes: [{
ticks: {
min: 0,
stepSize: 10000,
max: maxValue,
userCallback: function(item){
return converToSpaced(item);
}
},
scaleLabel:{
display: true,
labelString: "Kč | m" + ('\u00B3')
}
}],
xAxes: [{
barPercentage: 1,
categoryPercentage: 1,
}]
},
legend:{
display: true,
position:'top',
labels:{
fontColor:'#000',
fontStyle: 'bold'
}
},
tooltips:{
enabled: true,
callbacks: {
label: function(tooltipItems, data) {
if(tooltipItems.yLabel >= 1000){
tisice = (Math.floor(tooltipItems.yLabel/1000)).toString();
stovky = (tooltipItems.yLabel-(Math.floor(tooltipItems.yLabel/1000))*1000).toString();
switch (stovky.length){
case 1:
stovky = "00" + stovky;
break;
case 2:
stovky = "0" + stovky;
break;
case 3:
break;
}
return tisice + ' ' + stovky + ' Kč';
}
else if(tooltipItems.yLabel <= -1000){
tisice = (Math.ceil(tooltipItems.yLabel/1000)).toString();
stovky = (Math.abs(tooltipItems.yLabel)-Math.abs((Math.ceil(tooltipItems.yLabel/1000))*1000)).toString();
switch (stovky.length){
case 1:
stovky = "00" + stovky;
break;
case 2:
stovky = "0" + stovky;
break;
case 3:
break;
}
return tisice + ' ' + stovky + ' Kč';
}
else{
return tooltipItems.yLabel + ' Kč';
}
},
title: function(tooltipItems, data){
return data.datasets[tooltipItems[0].datasetIndex].label + " za rok " + tooltipItems[0].xLabel;//
}
}
},
plugins: {
datalabels: {
display: true,
align: 'end',
anchor: 'end',
rotation: 270,
color: function(value){
if(value.dataset.data[value.dataIndex] <= 0){
return 'red';
}
else{
return 'black';
}
},
formatter: function(value){
if(value >= 1000){
tisice = (Math.floor(value/1000)).toString();
stovky = (value-(Math.floor(value/1000))*1000).toString();
switch (stovky.length){
case 1:
stovky = "00" + stovky;
break;
case 2:
stovky = "0" + stovky;
break;
case 3:
break;
}
return tisice + ' ' + stovky;
}
else if(value <= -1000){
tisice = (Math.ceil(value/1000)).toString();
stovky = (Math.abs(value)-Math.abs((Math.ceil(value/1000))*1000)).toString();
switch (stovky.length){
case 1:
stovky = "00" + stovky;
break;
case 2:
stovky = "0" + stovky;
break;
case 3:
break;
}
return tisice + ' ' + stovky;
}
else{
return value;
}
}
}
}
}
});
3条答案
按热度按时间iq3niunx1#
您可以使用
options.scales.x.title.padding
参数使用自定义样式将轴标签移动到所需位置,如下例所示:wlwcrazw2#
不幸的是,至少目前,这是不可能的。刻度标题选项不支持这种位置更改。更多信息请参阅此处的文档:
https://www.chartjs.org/docs/latest/axes/labelling.html
也许在Chart.jsgithub上提出特性请求是有意义的?
https://github.com/chartjs/Chart.js/issues
l2osamch3#
original answer
我看到这个example,希望它能帮助短裤,创建一个插件,并在画布绘制后附加标题