Reputation: 453
I created a very simple line chart, for use on mobile devices. I find that when I include the scale, it takes up a lot of space on the side of the chart.
Instead, what I've seen accomplished on other mobile charts is only showing the minimum and the maximum value. A user can use the tooltip to find specifics.
Anyway, I'm hoping someone can show me how to create a scale such as the one here:
The two main takeaways of this scale:
My thought process was possibly using the scaleOverride
, then just defining scaleSteps : 1
with a scaleStepWidth
equal to the difference between the min and max of my data. Even if this is on the right track, I don't know how I would override the positioning of the scale to be absolute and not take space away from the line itself.
Upvotes: 2
Views: 3122
Reputation: 8746
I've broken this answer down into the steps needed to get your chart to look like the provided image:
First, use JavaScript max()
and min()
methods to determine the highest and lowest values in the data array:
var min = Math.min(...data);
var max = Math.max(...data);
Then, add the following options to the yAxes
ticks configuration section:
max
and min
values to the data array max and min values.stepSize
to the difference between the max and min value. This will ensure that it only shows the highest and lowest numbers in the labels.This can be done in two steps:
position: "right"
to move the align the scale yAxes
to the right side of the chart.mirror: true
in the ticks configuration to flip the labels over the yAxes
, to get them to show inside the chart.To format the numbers with a dollar sign and two decimal places, add a callback
function to the ticks options:
callback: function(value) {
return '$' + value.toFixed(2);
}
See How To Format Scale Numbers in Chart.js v2 and Formatting a number with exactly two decimals in JavaScript for more information.
yAxes
grid lineAlthough your question is about changing the color of the grid line, based on the provided image, I am assuming you want to remove the yAxes
line. To remove the grid line, set drawBorder
to false
in the grid line configuration settings.
If you do want to change the grid line color to white, color: "#fff"
will work, you just need to make sure that you put that line in the gridLines
config section like:
gridLines: {
color: "#fff"
}
This wasn't in your questions, but if you want to change to label font color and sizing, simply use the fontColor
and fontSize
tick config options.
yAxes
code:yAxes: [{
id: 'share_price',
type: 'linear',
position: "right",
gridLines: {
display: false,
drawBorder: false,
//color: "#fff"
},
ticks: {
stepSize: max - min,
min: min,
max: max,
mirror: true,
fontColor: "#fff",
fontSize: 18,
callback: function(value) {
return '$' + value.toFixed(2);
}
}
}]
JSFiddle Demo: https://jsfiddle.net/ujsg9w8r/5/
Upvotes: 5