I want to render a line chart with a set of (x,y) values. Both x axis and y-axis need to scaled. I mean say, I have values (1,10),(2,20),(4,30),(8,40) The distance between 1 and 2 should be half of between 2 and 4. Which in turn should be half of 4 and 8.
When I try with linechart now, since labels are just text, it shows me 1,2,4,8 with same gap between them.
Please check the example
var ctx = document.getElementById("chart").getContext("2d");
var data = {
labels: [1, 2, 4, 8],
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [10,20,30,40]
}]
};
Is there any way to achieve this?
To show text instead of the number labels on the X axis, you need to add an additional series with the values 1 to 5. Add this series to the chart (copy the cells with the numbers 1 to 5, select the chart, use paste special and hit OK).
X-Axis. In line graphs, like the one above, the x-axis runs horizontally (flat). Typically, the x-axis has numbers representing different time periods or names of things being compared. In this line graph, the x-axis measured different school years.
If you wanted, you could use the 'scatter'
chart type (which was relatively recently introduced), or just override the default X-axis type to make it 'linear'
instead of the default 'category'
setting ('time'
and 'logarithmic'
are other options).
However, if you take this approach, you will need to change your data format:
var data = {
// No more "labels" field!
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [{x:1,y:10},{x:2,y:20},{x:4,y:30},{x:8,y:40}] // Note the structure change here!
}]
}
You will also need to make sure (if you continue using 'line'
as the chart type) that you override the options:
var options = {
scales: {
xAxes: [{
type: 'linear',
// ...
}]
}
}
There may be more steps required than are shown in this answer (I haven't mocked this up and I don't know your existing options), but these are the main steps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With