简体   繁体   English

轴上的比例不会自动变窄

[英]The scale on the axis doesn't automatically narrow

I'm using chart.js but on some and only some of the graphs it creates the y-axis scale goes from 0-100 when a more appropriate scale might be 80-100. 我正在使用chart.js,但在某些图表中,当更合适的比例可能是80-100时,它创建的y轴比例从0-100开始。 This means all the lines are bunched up at the top. 这意味着所有行都聚集在顶部。

You can see what I mean if you visit mbi.dajubox.com and select '14 days' under waiting times. 如果您访问mbi.dajubox.com并在等待时间下选择“ 14天”,您会明白我的意思。 When the results come up beneath click the first entry (Calderdale And Huddersfield NHS Foundation Trust) and the graph appears. 当结果出现在下面时,单击第一个条目(Calderdale And Huddersfield NHS Foundation Trust),然后将显示图形。 But the lines are bunched at the top. 但是这些线在顶部排列。

If I go down to number 15 though (Stockport NHS Foundation Trust) it scales the axis ok. 如果我下降到第15位(Stockport NHS Foundation Trust),它将缩放轴为ok。

The code that generates them is the same 生成它们的代码是相同的

var ctx = document.getElementById("myChart_"+provID).getContext("2d");
var myLineChart = new Chart(ctx).Line(data, {bezierCurve: false, multiTooltipTemplate: "<%= datasetLabel %> - <%= value %>"});

Can any one help me out? 谁能帮我吗?

This is because in your data you receive null values to display in the chart. 这是因为在您的数据中,您会收到要在图表中显示的null值。 Chartjs's min function is just a wrapper for the Math.min which will treat null as 0 . Chartjs的min函数只是Math.min的包装,它将把null视为0

A fix for this can be to override the helper function calculateScaleRange 解决此问题的方法可以是覆盖辅助函数calculateScaleRange

Just declare this after you have Chart.js (or apply the small change straight to your Chart.js) 在拥有Chart.js之后,只需声明一下即可(或将较小的更改直接应用于Chart.js)

Chart.helpers.calculateScaleRange = function (valuesArray, drawingSize, textSize, startFromZero, integersOnly) {
     //Set a minimum step of two - a point at the top of the graph, and a point at the base
     var minSteps = 2,
         maxSteps = Math.floor(drawingSize / (textSize * 1.5)),
         skipFitting = (minSteps >= maxSteps);

     var maxValue =  Chart.helpers.max(valuesArray),
         minValue = Chart.helpers.min(valuesArray.map(function(value){
             //using map to create a new array where all nulls are mapped to Infinity so they do not pull the result down to 0
             return value === null ? Infinity: value;
         }));
        ................ //lots more code that is part of calculateScaleRange

here is a full example http://jsfiddle.net/leighking2/L9kLxpe1/ 这是一个完整的示例http://jsfiddle.net/leighking2/L9kLxpe1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM