Comments (25)
It's been 6 years and this is still a valid issue :( why was it closed? If your data only has small integers (I believe less than 10 per data point) then it will display floats instead of integers in the axis
from chart.js.
really annoying :-( i always got 2 decimal points in each of my bar graphs (looks very strange cause it represent "minutes") An update would be amazing 👍
from chart.js.
After some more testing I found the initial patch caused an infinite loop when using data sets containing zero values.
I created an alternative patch just after the while loop in the calculateScale function:
if (stepValue < 1) {
stepValue = 1;
} else {
stepValue = Math.round(stepValue);
}
Would be great if you could specify "integers: true" in the options.
from chart.js.
var max = <?php echo json_encode(max(array_values($data))) ?>;
var option = {
scale: {
ticks: {
beginAtZero: true,
steps: max/10 >= 1 ? max/10 : 1,
stepValue: max/10 >= 1 ? max/10 : 1,
max: max >=10 ? max : 10
}
}
}
}
and you will get only integers
from chart.js.
Waiting for an update, you can do these changes :
replace in the calculateScale function
stepValue /= 2;
with
stepValue = Math.round(stepValue / 2);
And replace in the getValueBounds() functions for Bar & Line
var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
with
var minSteps = (upperValue < Math.floor((scaleHeight / labelHeight*0.5))) ? upperValue : Math.floor((scaleHeight / labelHeight*0.5));
This work great for me.
Hope it help.
Cheers.
Val
from chart.js.
I had this issue but I'm just precalculating the ticks in code (PHP) before passing the data off to chartjs
function get_steps($numberOfTicks, $dataPoints) { $max = round(max($dataPoints)); $steps = ($max + ($numberOfTicks - $max % $numberOfTicks)) / $numberOfTicks; return $steps; }
from chart.js.
Resolved my problem! Thanks Val (uBaze)
+1
from chart.js.
+1 "integers: true" in options but could we bulk this up a bit by passing in the float position as a variable.
stepValueX = integer; ( 20.000 = 20 )
stepValueX = float(2); ( 724 = 7.34 )
...and decouple X & Y treatments...
stepValueY = float(2);
As this codebase matures this will become increasingly viable.
from chart.js.
@uBaze @psyose Can you please paste your js file, i tried modifications but still causing infinite loop.
from chart.js.
I found this piece of code very helpful, worked for me.
var steps = 3;
new Chart(ctx).Bar(plotData, {
scaleOverride: true,
scaleSteps: steps,
scaleStepWidth: Math.ceil(max / steps),
scaleStartValue: 0
});
from chart.js.
@seyhunak I've pasted my file here : http://jsfiddle.net/Ase7V/
from chart.js.
I've just add Math.floor to the label drawing function (you need to change for every type of char you want to round the value)
labels.push(tmpl(labelTemplateString, {value: Math.floor((graphMin + (stepValue * i))) }));
@populateLabels
from chart.js.
I keep getting infinite loop no matter what. Can anyone gist their entire Chart.js solution?
from chart.js.
@Netherdrake : The gist of my file, don't know if helpfull https://gist.github.com/uBaze/6962655
from chart.js.
+1
from chart.js.
I have charts with highly variable values. This is how I set the scales so they are always integer values and ~10 steps...
var steps = new Number(model.maxValue);
var stepWidth = new Number(1);
if (model.maxValue > 10) {
stepWidth = Math.floor(model.maxValue / 10);
steps = Math.ceil(model.maxValue / stepWidth);
}
var options = { scaleOverride: true, scaleSteps: steps, scaleStepWidth: stepWidth, scaleStartValue: 0 };
from chart.js.
@uBaze This worked well, but gets stuck in an infinite loop for small numbers <= 1.
I included a simple IF to make it work again =)
stepValue = (stepValue <= 1) ? stepValue : Math.round(stepValue / 2);
from chart.js.
Ahh okay, nice !
In my case I don't work with small numbers <=1 that's why I didn't see that !
Well done ! :)
from chart.js.
Was this issue ever fixed in the master Chart.js code? I'm using the latest version, but I'm still having this issue with decimal numbers on the axis labels. I would imagine this issue must effect a high percent of users who want to us Chart.js.
from chart.js.
Here is a function that also does the work to calculate the max value (based on MardyGit's code)...
function wholeNumberAxisFix(data) {
var maxValue = false;
for (datasetIndex = 0; datasetIndex < data.datasets.length; ++datasetIndex) {
var setMax = Math.max.apply(null, data.datasets[datasetIndex].data);
if (maxValue === false || setMax > maxValue) maxValue = setMax;
}
var steps = new Number(maxValue);
var stepWidth = new Number(1);
if (maxValue > 10) {
stepWidth = Math.floor(maxValue / 10);
steps = Math.ceil(maxValue / stepWidth);
}
return { scaleOverride: true, scaleSteps: steps, scaleStepWidth: stepWidth, scaleStartValue: 0 };
}
So that way you can do something like "new Chart(ctx).Line(data, wholeNumberAxisFix(data))".
from chart.js.
@orrd That works great, thanks so much!
from chart.js.
is this function (wholeNumberAxisFix() ) really work? i just test and seem like start with 0
from chart.js.
@orrd Works perfectly! Thanks!
from chart.js.
I am not able to reproduce this problem using the new version release candidate.
Could you please check if this has been fixed and close or create a JSBin showing the issue.
Thank you
from chart.js.
I think it may have been fixed? This is the code from the original example (I just fixed the source since GitHub disabled hotlinking of JS): http://jsfiddle.net/SNLAw/19/
from chart.js.
Related Issues (20)
- Official Discord Server ? HOT 4
- Cannot see ticks lines when grid > display = false HOT 2
- Dynamic data not updating HOT 1
- Lines partially missing after upgrade from 4.3.0 to 4.3.1 HOT 1
- Moving from 4.3.0 to 4.3.1 break most of my already existing code due to types HOT 11
- Non-static registry HOT 2
- Doughnut chart legends issue in chart.js
- Partially missing line on time axis, regression in 4.3.1 HOT 11
- Toggle doughnut vs. label resizing HOT 4
- BorderRadius of Bar Chart issue on Firefox 116 HOT 5
- Release 4.3.3 missing tgz file HOT 7
- Rendering flowchart is wrong
- Superfluous value copies inside plugin options
- Vite giving chart.js ActiveElement import error HOT 1
- PackedCircle HOT 4
- Multiple charts freeze Firefox tab with maintainAspectRatio: false HOT 8
- The bars in the graphs which are having the exact values included in the tooltips are not accessible with keyboard keys. HOT 1
- chart.cjs blocked by CDN HOT 5
- How to use the Colors plugin? No docs/info :( HOT 1
- chart.hide(datasetIndex, dataIndex) not hiding bar
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chart.js.