I've been trying for two days to find a way to set the maximum value of the yAxis on Highcharts.
I got a percentage column graphic, but the highest value in the series is 60, so it adjusts the axis to top at 70, I found a way to choose the start point, but no way to choose the end point.
What I really need is to set the yAxis from 0 to 100, so the data will be more accurately viewed as percentage
Try this:
yAxis: {min: 0, max: 100}
See this jsfiddle example
Alternatively one can use the setExtremes method also,
yAxis.setExtremes(0, 100);
Or if only one value is needed to be set, just leave other as null
yAxis.setExtremes(null, 100);
Taking help from above answer link mentioned in the above answer sets the max value with option
yAxis: { max: 100 },
On similar line min value can be set.So if you want to set min-max value then
yAxis: {
min: 0,
max: 100
},
If you are using HighRoller php library for integration if Highchart graphs then you just need to set the option
$series->yAxis->min=0;
$series->yAxis->max=100;
Related
I have certain requirement where I need to override the auto calculated values for Y axis in highcharts. For eg.
Here, the gridlines plot area is equally divided into 100. I wanted to override this so that the negative plot area should be at a max of let's say 50 and the positive ones can remain the same. Even if I try the max, min, softMax, softMin, ceiling and floor properties, the result is the same. I was thinking of using a secondary axis but then there is only one data in the series which would render the second one useless. I don't think using setExtremes() will be helpful either. I'm hoping to avoid modifying the library itself to add a certain option but it'll be helpful if such an option already exists in highcharts. Any suggestions?
Use tickPositions or tickPositioner property:
yAxis: {
tickPositions: [-50, 0, 100, 200, 300, 400]
}
Live demo: http://jsfiddle.net/BlackLabel/6m4e8x0y/4883/
API Reference:
https://api.highcharts.com/highcharts/yAxis.tickPositions
https://api.highcharts.com/highcharts/yAxis.tickPositioner
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:
Only the min and max values are shown
They are shown with absolute positioning approximately in the top left and bottom right corners.
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.
I've broken this answer down into the steps needed to get your chart to look like the provided image:
1. Only show max and min labels
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:
Set the max and min values to the data array max and min values.
Set 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.
2. Move labels to inside the right side of the chart
This can be done in two steps:
Add position: "right" to move the align the scale yAxes to the right side of the chart.
Set mirror: true in the ticks configuration to flip the labels over the yAxes, to get them to show inside the chart.
3. Format number with dollar sign and 2 decimal places
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.
4. Change color or hide the yAxes grid line
Although 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"
}
5. Change label font formatting
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.
Final 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/
I've used AddPoint repeatedly on a scatter Highchart to show some data evolution, using
setinterval()
When the chart updates the axes' ranges as a consequence of the presence of new data points, the chart sometimes "twitches" as the data distribution is not longer well balanced around the origin:
http://jsfiddle.net/dhigger/qfxe87tr/
I would like to keep the redrawing of the axes to accommodate various data distributions, so one solution would be to keep the origin at the centre of the graph at all times, and scale positive and negative of an axis together, and always scale up. But I can't see how to achieve these.
Is there a way to avoid the twitch?
Those "twitches" are coming from Highcharts' native axis auto-scaling. You can fix the xAxis so your chart won't move anymore. For example, I managed to have your JSFiddle working by adding min: -15 for the xAxis:
xAxis: {
plotLines: [{
color: '#FA5858',
width: 1,
value: 0,
zIndex: 2
}],
minRange: 30,
min: -15,
gridLineWidth: 1
}
See the JSFiddle working with not "twitches" here.
After setting the limits of the axes (per #Kabulan0lak's answer), I found that I could monotonically increase both positive and negative axis limits by keep track of the largest-magnitude data values and then re-set the max and min limits of the axes manually using setExtremes (manual).
e.g. chart.xAxis[0].setExtremes(min_x,max_x);
Updated jsfiddle: http://jsfiddle.net/dhigger/qfxe87tr/3/
I am working on flot jquery library, and I used to use this code in order to tell flot to inference the x-axis itself
xaxis: {
},
now i have a case in which I need to show the values starting from zero in x-axis, i don't care about last value, so I need flot to calculate it dinamically. it is possible to set just the begining ?
what I tried
I couldn't find such an option using google so I calculated the max value my self and I set the x-axis manually between zero and that max value. that approaches works in some cases, but i do need to handle all the senarios so i thought let me ask here first to see if there is a built-in option for that
You can set the minimum or maximum extent of the axes like this:
xaxis: {min: 0, max: 999}
See https://github.com/flot/flot/blob/master/API.md#customizing-the-axes
We're attempting to display two series of data on the same chart. The first series (ie: number of quotations) can only contain positive integers (and 0). The second series (ie: sales value) can contain both positive and negative float values (in case you're wondering, the negative values result from the issuing of credit notes).
As indicated on the attached image, the problem we're having is that when the second series contains negative values, the 0-point "base line" of the two series of data is no longer shared.
We've tried setting the "min" options of the Y axes to 0, but then we lose out the insight on negative values.
We've also tried setting the min of the first series to be equal to the minimum extreme of the second series, but unfortunately this doesn't scale the columns very nicely (since the values of each Y axis are on completely different scales, ie: 10s vs 1000s).
How can we configure the chart so that the 0-point "base line" is shared? In other words, how would we make the blue columns start at the same base line as the green 0 point?
Update
Linger's answer is a step in the right direction. Setting the min and tickinterval for both axes does the trick. However the values need to be determined dynamically based on variable data, which is where I am now stuck.
Any tips on how these min and tickinterval values can be determined before I generate the chart?
I've done some thinking about it in the mean time. The values associated with the left axis (blue / quotations) are always positive and start from zero. Thus it is the right axis (green / sales) that dictates the number of ticks to show below the zero point. Now since highcharts automatically determines the best scale for both blue and green, all I need to do is find a way to set the left axis' minimum value like so (excuse the pseudo-code):
var factor = right_axis.min / right_axis.tickinterval;
left_axis.min = factor * left.tickinterval;
Note: I have seen the reference API has a setExtremes() method under Axis, but this would require me to first initialise the chart, and then go back and update its left axis. I am hoping to do this before drawing the chart. Perhaps I'm overlooking something obvious?
Thanks.
You can control what you are asking for with a combination of tickInterval and min on the yaxis as demonstrated in this jsfiddle mock up.
EDIT
In your code while reading in the XML you will have to keep track of what the lowest value is and also the highest value on the sales side. Then decide how you want to display those values if they meet a certain value. After that use if statements to set the values. Here is a couple of examples.
Low less than -50 and greater -90
With High less than 400
Primary Axis: min: -2, tickInterval: 1,
Secondary Axis: min: -100, tickInterval: 50
Example
Low less than -50 and greater -90
With High greater than 400
Primary Axis: min: -1, tickInterval: 1,
Secondary Axis: min: min: -100, tickInterval: 100,
Example
To figure out what the min on the Primary Axis should be you simply divide the min on the Secondary Axis by its tickInterval. Then the tickInterval for the Primary Axis would always be 1.
You can also calculate second axis tick positions based on the max series value related to this axis and the first axis ticks positions to get the same amount of ticks and 0 line shared.
Using axis.update() method update tick positions in the second axis using property tickPositions.
Example with a load event. Notice, that when you add points dynamically you have to make those calculations and update tick positions each time your first axis ticks will change:
chart: {
type: 'column',
events: {
load: function() {
var chart = this,
yAxis = this.yAxis,
max = yAxis[1].dataMax,
positiveTicksAmount = 0,
negativeTicksAmount = 0,
tickPositions = [],
tickInterval,
min,
i;
yAxis[0].tickPositions.forEach(function(tick) {
if (tick > 0) {
positiveTicksAmount++;
} else if (tick < 0) {
negativeTicksAmount++;
}
});
tickInterval = max / positiveTicksAmount;
for (i = negativeTicksAmount; i > 0; i--) {
tickPositions.push(-tickInterval * i);
}
for (i = 0; i <= positiveTicksAmount; i++) {
tickPositions.push(
tickInterval * i
);
}
yAxis[1].update({
tickPositions: tickPositions
});
}
}
},
Jsfiddle examples:
https://jsfiddle.net/wchmiel/84q0sxrt/
http://jsfiddle.net/wchmiel/nuj7fagh/
Api reference:
https://api.highcharts.com/class-reference/Highcharts.Axis#update
https://api.highcharts.com/highcharts/yAxis.tickPositions