Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Chart.js中将Y轴值从实数更改为整数_Javascript_Html_Charts_Chart.js - Fatal编程技术网

Javascript 在Chart.js中将Y轴值从实数更改为整数

Javascript 在Chart.js中将Y轴值从实数更改为整数,javascript,html,charts,chart.js,Javascript,Html,Charts,Chart.js,我有一个图表,我想包括在我的网站使用。在Y轴上,它给出的是实数而不是整数。如何将数字更改为整数 这是我现在拥有的一张照片: 这是代码: var lineChartData = { labels : ["2013/04/01","2013/03/31", "2013/03/30", "2013/03/29", "2013/03/28","2013/03/27", "2013/03/26"], datasets : [ { fillCol

我有一个图表,我想包括在我的网站使用。在Y轴上,它给出的是实数而不是整数。如何将数字更改为整数

这是我现在拥有的一张照片:

这是代码:

var lineChartData = {

    labels : ["2013/04/01","2013/03/31", "2013/03/30", "2013/03/29", "2013/03/28","2013/03/27", "2013/03/26"],

    datasets : [
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,1)",
            pointColor : "rgba(151,187,205,1)",
            pointStrokeColor : "#fff",
            data : ["0", "2","1", "0", "1","0","1"]
        }
    ]

}

var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(lineChartData);

试试这个,其中max是数据的最高值

var steps = 3;
new Chart(ctx).Bar(plotData, {
    scaleOverride: true,
    scaleSteps: steps,
    scaleStepWidth: Math.ceil(max / steps),
    scaleStartValue: 0
});

如果你想从一个不同于零的数字开始,你必须考虑到这一点:

var step  = 5;
var max   = 90
var start = 40;
new Chart(income).Bar(barData, {
    scaleOverride: true,
    scaleSteps: Math.ceil((max-start)/step),
    scaleStepWidth: step,
    scaleStartValue: start
});
查看“全局配置”部分中的文档:

//布尔-比例是否应保持为整数,而不是浮动(即使存在绘图空间) scaleIntegerly:true,


在使用Chart.js的新版本2时,我无法获得适用于我的现有答案,因此我在V2中找到了解决此问题的方法:

new Chart(ctx, {type: 'bar', data: barChartData,
  options:{ 
    scales: {
      yAxes: [{
        ticks: {
          stepSize: 1
        }
      }]
    }
  }
});

我在新版本中是这样处理的:

new Chart(ctx, {
  type: 'bar',
  data: chartData,
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true,
          callback: function(value) {if (value % 1 === 0) {return value;}}
        }
      }]
    }
  }
});

我知道这是一个老问题,但在当前版本(v2.9.3)中,您只需将y轴刻度的精度设置为零即可获得整数:

options: {  
    scales: {
        yAxes: [{
            ticks: {
                precision: 0
            }
        }]
    }
}

@我根本不想显示y轴标签。我怎么能这么做?一定有选择权,我找到了。如果有人想完全删除标签,您可以将
scaleShowLabels:false
添加到
选项
属性中。如果不起作用,您可能没有
scaleOverride:true
。可能图表版本1已过时请注意,这将显示y轴中的所有整数。如果您的图形从0到300运行,所有301整数和网格线都将显示。@apfz非常正确,只有当您对数据的比例有了很好的了解,并且可以提前计算出合理的步长时,这项功能才有效。非常感谢您的朋友!工作得很有魅力。我想知道为什么这不是最好的答案。这是一个很好的答案。值得注意的是,我建议将此代码作为解决方案。那太好了!这应该是可以接受的答案,因为这不需要额外考虑(查找最大值、声明步骤等)。这表明在使用typescript时出现编译错误,属性似乎没有声明。