Chart Js change y axis to real number - javascript

I am using latest chart js and i want to change Y axis value to real number
0,1,2,3,....
i want to change y axis in to positive real numbers where do i put scales value to change above format to real number format
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: allDate,
datasets:jsonArray,
},
options: {
scales: {
xAxes: [{
display:true
}],
yAxes: [{
display:true,
type: "linear",
ticks: {
userCallback: function(value, index, values) {
return value;
}
}
}]
}
}
});

Related

I want to show decimal values on my y axis but my chart doesn't show data how can i fix it?

This is my chart code my x axis value correct but y axis value is not working
<script>
const config = {
type: 'line',
data: {
datasets: [{
data: <%=lineData%>
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 2
}
}]
}
}
};
</script>
<script>
const myChart = new Chart(
document.getElementById('myChart'),
config
);
</script>
and this is my chart,
debugged value is correct but they does not on my chart
The problem is ',' on the y-axis value, change it to '.'
data: [{x: 17.05.2022 19:44:47, y: 28.28}]

ChartJs - Labelling minimum and maximum value of the Y Axis

I found a code to add a label to each value in the Y axis and I have tried it. This is the code
var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
scales: {
yAxes: [{
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
return '$' + value;
}
}
}]
}
}
});
But, can I give it only for minimum and maximum values? If so, how?
The following is an example chart that I mean. The minimum and maximum labels I give a red mark.
Example chart that I mean
You can specify min and max directly, Please check https://www.chartjs.org/docs/latest/axes/cartesian/linear.html#step-size
e.g
var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
scales: {
yAxes: [{
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
return '$' + value;
},
min: 1,
max: 100
}
}]
}
}});
You can also use axis range settings, It is only change the data values that are used to scale the axis and useful for extending the range of the axis while maintaining the auto fit behaviour.
https://www.chartjs.org/docs/latest/axes/cartesian/linear.html#axis-range-settings
e.g.
var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
scales: {
yAxes: [{
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
return '$' + value;
},
suggestedMin: 1,
suggestedMax: 100
}
}]
}
}});

How to change the display of an axis Chart.js

I want to change only the values shown on the x-axis, but not the values of the dataset,which can be seen by hovering on the curve. I've tried:
callback: function(value) {return Math.round(value);}
in the xAxes ticks, but it also changes the dataset.
Is there any way to only change the display?
Here is my curve:
curve
You should change the options for to xAxes of your chart.
const chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
scales: {
xAxes: [{
ticks: {
callback: function(value, index, values) {
return Math.round(value).toString();
}
}
}]
},
tooltips: {
callbacks: {
title: function(tooltipItem, data) {
return data.labels[tooltipItem[0].index];
}
}
}
}});
I think that this won't change the dataset.

Chart.js Y axis label, reverse tooltip order, shorten X axis labels

I am creating a stacked bar chart using Chart.js. However, I cannot find in the documentation how to change some things.
How to add a label on the Y axis.
How to shorten the label on the X axis so it displays only
the first 10 letters.
How to reverse the order in which the values are shown in
the tooltip.
Are these thigs are possible to implement?
I have marked what I want to change here.
Here are what my chart options look like now:
var ctx = $("#newchart");
var barGraph = new Chart(ctx, {
type: 'bar',
data: chartdata,
options: {
barValueSpacing: 20,
tooltips: {
enable: true,
mode: 'label',
callbacks: {
label: function(tooltipItem, data){
var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || '';
return datasetLabel + ': ' + Number(tooltipItem.yLabel) + ' Users';
}
}
},
responsive: true,
segmentShowStroke: true,
scales: {
xAxes: [{
display: false,
stacked: true,
ticks:{
stepSize : 10,
},
gridLines: {
lineWidth: 0,
color: "#9E9E9E"
}
}],
yAxes: [{
stacked: true,
ticks: {
min: 0,
stepSize: 10,
},
gridLines: {
lineWidth: 0,
color: "#9E9E9E"
}
}]
}
}
});
1. Adding a Y-axis label
in the yAxes object give it a scaleLabel object that takes in labelString (example fiddle)
yAxes: [{
scaleLabel: {
labelString: 'Value'
}
}]
2. Shortening xAxis category labels
For this, you can pass a userCallback function to the xAxis ticks object that can return your desired output. The function will take in the original label in its first parameter so you can just return a substring at your desired length, example fiddle
xAxes: [{
ticks: {
userCallback: function(label, index, labels) {
if(typeof label === "string")
{
return label.substring(0,1)
}
return label;
},
}
}],
3. reverse tooltip order
The tooltips object accepts a function called itemSort that can be passed to Array.prototype.sort.
So you could so something like below, but you may also need to compare the objects index as well as the datasetIndex to get your desired result. (example fiddle)
tooltips: {
mode: 'label',
itemSort: function(a, b) {
return b.datasetIndex - a.datasetIndex
},
},

Chart.js how define max tick of polarArea?

I look for way to use polarArea but with limit scale (line of tick) defined at 100. Actualy the scale display scale based in max value, but I want max fixed.
var data = {
datasets: [{
data: [10,20,30,40],
}],
labels: ["Red","Green","Yellow","Grey","Blue"]
};
var polarArea = new Chart(ctx, {
data: data,
type: 'polarArea',
options: {
scale: {
ticks: {
beginAtZero: true
}
}
}
});
This marked with arrow green as I want the graph.
Can you help me?
var data = {
datasets: [{
data: [10,20,30,40],
}],
labels: ["Red","Green","Yellow","Grey"]
};
var polarArea = new Chart(ctx, {
data: data,
type: 'polarArea',
options: {
scale: {
ticks: {
min: 0,
max: 100
}
}
}
});
A JSFiddle would be super helpful for your exact situation, but based on the information provided, this should do it for you. http://jsfiddle.net/andrewborem/vup9o5fx/

Categories