以条形图为例,
我的尝试是执行以下操作(这是在'load'事件中):
window.addEventListener('load',function(event){
var chartAreaHeight = (chartInstance.chartArea.bottom - chartInstance.chartArea.top);
var dataWidth = 40; // How about a default of 40 (for minimum chart area height)
// We don't want the chart to shrink past the length of it's longest scaleLabel.labelString
for (var i = 0; i < chartInstance.options.scales.yAxes.length; ++i)
{
if (chartInstance.options.scales.yAxes[i].display == false || chartInstance.options.scales.yAxes[i].scaleLabel.display == false)
{
continue;
}
// notice the need for currentDevicePixelRatio
var tempDataWidth = ctx.measureText(chartInstance.options.scales.yAxes[i].scaleLabel.labelString).width * chartInstance.currentDevicePixelRatio;
if (tempDataWidth > dataWidth)
{
dataWidth = tempDataWidth;
}
}
好的,我有(是的,我知道我把它命名为dataWidth,当它实际上是高度时) . 怎么办?
if (dataWidth > (chartAreaHeight * chartInstance.currentDevicePixelRatio))
{
// What do I do in here? To ensure that:
// (chartInstance.chartArea.bottom - chartInstance.chartArea.top)
// is not less than dataWidth?
}
相信我,这并不容易 . 我注意到xAxes与事物“混杂”(我有一个底部xAxes) .
我测量我的比例文本是202(好吧,101,浏览器设置为200%缩放等于202),我的chartArea高度是202(好吧,101,因为chart.js将大小保持在缩放前的水平,你必须使用currentDevicePixelRatio进行数学运算 .
但是,在此示例中,缩放文本比chartArea长 . 不应该将它们设置为相同的大小 . 它是什么,xScale与事物混淆 - 即使我占了这个高度 . 是因为在缩放前的计算中,xAxes会以单向显示,而后缩放则以另一种方式显示吗?无论如何,当我将xAxes显示关闭时,它与chartArea完全吻合 .
这是一个显示我的意思的jsfiddle(平均处理时间yAxes文本比chartArea高度长):http://jsfiddle.net/jmpxgufu/214/