我用图表。js(版本:2.7.2)和结果行中的一些标签相当长
var barCanvas = document.getElementById("canvasVoteNames");
var ctx = barCanvas.getContext('2d');
var numberWithCommas = function(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
var self = this;
var myChart = new Chart(ctx, { // stacked bar report https://jsfiddle.net/sdfx/hwx9awgn/
type: 'bar',
data: {
labels:monthsXCoordItems,
datasets: [
{
label: 'Correct Votes',
data: voteValuesCorrect,
borderWidth: 1, // The stroke width of the bar in pixels.
backgroundColor : formatColor('#05b932'), //rgba(0, 0, 0, 0.1), // The fill color of the bar. See Colors
borderColor: formatColor('#05b932'),// rgba(255, 0, 0, 0.1) // The color of the bar border.
hoverBackgroundColor : formatColor('#05b932'), // The fill colour of the bars when hovered.
hoverBorderColor: formatColor('#05b932'), // The stroke colour of the bars when hovered.
hoverBorderWidth : 1 // The stroke width of the bars when hovered.
},
{
label: 'Incorrect Votes',
data: voteValuesNoneCorrect,
borderWidth: 1, // The stroke width of the bar in pixels.
backgroundColor : formatColor('#b1a19a'), //rgba(0, 0, 0, 0.1), // The fill color of the bar. See Colors
borderColor: formatColor('#b1a19a'),// rgba(255, 0, 0, 0.1) // The color of the bar border.
hoverBackgroundColor : formatColor('#b1a19a'), // The fill colour of the bars when hovered.
hoverBorderColor: formatColor('#b1a19a'), // The stroke colour of the bars when hovered.
hoverBorderWidth : 1 // The stroke width of the bars when hovered.
},
]
},
options: { // options of Report By Vote Names
animation: {
duration: 10,
},
tooltips: { // tooltip text of Report By Vote Days ( 'bar' report )
mode: 'label',
callbacks: {
label: function(tooltipItem, data) {
return data.datasets[tooltipItem.datasetIndex].label + ": " + numberWithCommas(tooltipItem.yLabel);
}
}
}, // tooltips: { // tooltip text of Report By Vote Days ( 'bar' report )
scales: { // options for x and y scales
xAxes: [{
stacked: true, // Stacked bar charts can be used to show how one data series i
gridLines: { display: false },
}],
yAxes: [{
stacked: true, // Stacked bar charts can be used to show how one data series i
ticks: {
callback: function(value) { // on Y scale show only integer without decimals
if (Math.floor(value) === value) {
return value;
}
}, // callback: function(value) { return numberWithCommas(value); },
},
}],
}, // scales: { // options for x and y scales
legend: {display: true}
} // options: { // options of Report By Vote Names
}); // var myChart = new Chart(ctx, { // stacked bar report https://jsfiddle.net/sdfx/hwx9awgn/
}
我得到的图表是我所需要的,https://imgur.com/a/n1SsW7w,但任何酒吧的标签都很长,看起来不太好,我不知道是否有办法解决这个问题?为什么标签有大的边距,而不是相对的条?
xAxes 或附加图例的一些选项?
谢谢
您在JSFiddle中使用的是ChartJs版本2.1.3,它似乎不能处理多行标签
您可以将多行标签与以下解决方案一起使用:
var dates = [["Some l-o-o-o-o-", "o-o-o-o-o-o-o-", "n-n-n-n-n-n-g-g-g-", "g-g-g-g label"], "DDD", ["EEE", "FFF", "GGG"], "HHH", "III"];
你可以用一个数组代替一个标签,数组中的每个元素都将被视为一个新行(见js fiddle):https://jsfiddle.net/cyuwxh3q/
如果您的标签是随机生成的,您可以使用图表配置中的插件来拆分它们:
type: 'bar',
data: {...},
options: {...},
plugins: [{
beforeInit: function (chart) {
chart.data.labels.forEach(function (value, index, array) {
var a = [];
a.push(value.slice(0, 5));
var i = 1;
while(value.length > (i * 5)){
a.push(value.slice(i * 5, (i + 1) * 5));
i++;
}
array[index] = a;
})
}
}]
此函数将每个标签转换为一个长度小于或等于给定值(此处为5)的元素数组(参见JSIDLE):https://jsfiddle.net/jhr5nm17/
这是用多行标签替换长标签的两种简单方法,希望有所帮助。
我试图在堆叠条形图中“稳健地”居中数据标签。下面给出了一个简单的代码示例和结果。如您所见,数据标签并不是在所有矩形中都居中。我错过了什么?
我的问题类似于Chart.js 2.1.6中图例如何显示条形标签? 我想有相同的输出饼图给出,但我不想创建多个数据集。我设法做到了这一点,但现在我找不到如何做到。 这是我的代码示例: 这是一种方法吗?
我有一个数据框架,df,就像: 我想绘制两个垂直条形图,每个大陆一个,并排,共享相同的y轴。我已经完成了90%的工作,除了将条的高度添加到子图中。到目前为止,我的代码: 我在这里和那里看到过带有标签的示例,但这些往往只适用于一个条形图,而不是几个子图。
问题内容: 我试图在一个堆积条形图中“稳健地”居中数据标签。A下面给出了简单的代码和结果。如您所见,数据标签并不是所有的矩形都居中。我错过了什么? 问题答案: 你为什么写“va=”bottom“。
这是关于以下条款:https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data在条形图标题下。在下面的给定代码中: 产出是: 此处不显示X值。点击这里查看截图 如何将X轴值设置为月份(1月1日,1月2日,1月3日...),如文章中所示。
我正在尝试使用我自己的标签制作Seaborn条形图,代码如下: 但是,我得到一个错误: 有什么好处?