score:22
i ended up finding a way to do this through configuration after digging even further into the highcharts api. each axis has a configuration option called tickpositioner
for which you provide a function which returns an array. this array contains the exact values where you want ticks to appear on the axis. here is my new tickpositioner
configuration, which places five ticks on my y axis, with zero neatly in the middle and the max at both extremes :
yaxis: {
tickpositioner: function () {
var maxdeviation = math.ceil(math.max(math.abs(this.datamax), math.abs(this.datamin)));
var halfmaxdeviation = math.ceil(maxdeviation / 2);
return [-maxdeviation, -halfmaxdeviation, 0, halfmaxdeviation, maxdeviation];
},
...
}
score:0
just in case someone is searching,
one option more. i ended up in a similar situation. follows my solution:
tickpositioner: function () {
var datamin,
datamax = this.datamax;
var positivepositions = [], negativepositions = [];
if(this.datamin<0) datamin = this.datamin*-1;
if(this.datamax<0) datamax = this.datamax*-1;
for (var i = 0; i <= (datamin)+10; i+=10) {
negativepositions.push(i*-1)
}
negativepositions.reverse().pop();
for (var i = 0; i <= (datamax)+10; i+=10) {
positivepositions.push(i)
}
return negativepositions.concat(positivepositions);
},
score:0
it is an old question but recently i have had the same problem, and here is my solution which might be generalized:
const tick_precision = 2;
const axis_max_expand_rate = 1.2;
function setaxisticks(axis, tickcount) {
// first you calc the max from the data, then multiply with 1.1 or 1.2
// which can expand the max a little, in order to leave some space from the bottom/top to the max value.
// toprecision decide the significant number.
let maxdeviation = (math.max(math.abs(axis.datamax), math.abs(axis.datamin)) * axis_max_expand_rate).toprecision(tick_precision);
// in case it is not a whole number
let wholemaxdeviation = maxdeviation * 10 ** tick_precision;
// halfcount will be the tick counts on each side of 0
let halfcount = math.floor(tickcount / 2);
// look for the nearest larger number which can mod the halfcount
while (wholemaxdeviation % halfcount != 0) {
wholemaxdeviation++;
}
// calc the unit tick amount, remember to divide by the precision
let unittick = (wholemaxdeviation / halfcount) / 10 ** tick_precision;
// finally get all ticks
let tickpositions = [];
for (let i = -halfcount; i <= halfcount; i++) {
// there are problems with the precision when multiply a float, make sure no anything like 1.6666666667 in your result
let tick = parsefloat((unittick * i).tofixed(tick_precision));
tickpositions.push(tick);
}
return tickpositions;
}
so in your chart axis tickpositioner you may add :
tickpositioner: function () {
return setaxisticks(this, 7);
},
score:1
you can do this with the getextremes and setextremes methods
- http://api.highcharts.com/highcharts#axis.getextremes%28%29
- http://api.highcharts.com/highcharts#axis.setextremes%28%29
example:
http://jsfiddle.net/jlbriggs/j3ntm/1/
var ext = chart.yaxis[0].getextremes();
score:1
here is my solution. the nice thing about this is that you can maintain the tickinterval
.
tickpositioner(min, max) {
let { tickpositions, tickinterval } = this;
tickpositions = _.map(tickpositions, (tickpos) => math.abs(tickpos));
tickpositions = tickpositions.sort((a, b) => (b - a));
const maxtickposition = _.first(tickpositions);
let mintickposition = maxtickposition * -1;
let newtickpositions = [];
while (mintickposition <= maxtickposition) {
newtickpositions.push(mintickposition);
mintickposition += tickinterval;
}
return newtickpositions;
}
score:3
i know this is an old post, but thought i would post my solution anyway (which is inspired from the one macserv suggested above in the accepted answer) as it may help others who are looking for a similar solution:
tickpositioner: function (min, max) {
var maxdeviation = math.ceil(math.max(math.abs(this.datamax), math.abs(this.datamin)));
return this.getlineartickpositions(this.tickinterval, -maxdeviation, maxdeviation);
}
Source: stackoverflow.com
Related Query
- Highcharts - Keep Zero Centered on Y-Axis with Negative Values
- Highcharts - best way to handle and display zero (or negative) values in a line chart series with logarithmic Y axis
- Highcharts - Column With Negative Values - Column Color
- Highcharts - Issue with negative values when displaying multiple axes
- HighCharts scatter plot with Datetime on X Axis not plotting values correctly
- Highcharts - Area Chart - Stacking with series containing negative and positive values
- Highcharts - Cannot set x-axes categories in column charts with negative values
- Column chart with negative values and categories on xAxis in HighCharts
- Highcharts - Pie chart with negative values
- highcharts gauge use with negative values
- Y axis values merging with candles in highcharts
- nGradient effect with negative values on highcharts
- Display negative Y values above zero instead below in highcharts
- highchart axis position with negative values
- Highcharts - Stacked Bar Column - Total Value of Stacked Bar not Correct with Negative and Positive Values
- Highcharts - set maximum range for yAxis but keep axis dynamic within that range
- Highcharts - with datetime axis labels overlap
- highcharts - removing decimal places on Y axis with only one point
- Highcharts blank chart with x and y axis
- Highcharts label format with tickPositioner in a datetime x Axis
- highcharts zero values results in graph half way instead at the bottom
- Highcharts Line - When Y axis min is set to 0, connecting line isn't shown for consecutive 0 values
- Highcharts How to get decimal point values on y-axis with big numbers
- How to show only specific x-axis values on datetime axis in Highcharts
- Highcharts - Global configuration with common code and unique data & Headings
- How to populate a Highcharts axis with string formatted data from a PHP array
- Highcharts: having trouble recreating stacked area chart from Excel with positive and negative values
- Highcharts Solid Gauge with min number NOT zero
- how to pass values to tooltip which not in x and y axis in highcharts
- Highcharts line with null values
More Query from same tag
- Displaying Volume profile on a javascript chart
- How to pass an argument into a Highcharts-Function
- Bring marker to front of grid line in Highcharts
- How do I create a correctly scaled x-axis?
- Highchart yAxis opposite is not working in type datetime
- How to get series's id in Highcharts / Highstock
- Highchart: Irrelevent YAxis Scale at the end
- Highcharts - Exporting options for print to match export to png
- How can I remove the legend symbol for a SINGLE legend in Highcharts?
- Have an issue with JavaScript, AJAX code displaying data
- How to add synchronized-charts in drilldown?
- Highcharts Highstock enabledThreshold feature not working
- Saiku query fetch
- Highcharts: Difference between onMouseOver vs onContainerPointerMove?
- Highcharts How to update all series with ajax
- Setting Order of R Highcharter Categories
- Highmaps from sharepoint data
- Gradient background bars in Highchart bar chart
- Highcharts organization chart (org chart) with React Native
- Highcharts: render color to series dynamically
- ASP.Net Chart controls Vs Highcharts
- Highcharts: how to dynamically update text inside of the label?
- How to add another series into my highchart
- Highcharts series showing different data for 'column' and 'line' chart
- Error in ordering the X axis in Highchart
- highcharts putting ajax/json data at 1970
- How can I prevent tooltip to appears when I hover another element?
- highcharts from HTML table does not getting the x axis key
- JS Highcharts table - How to set Category name
- Highcharts histogram drilldown: set options for drilldown + first and last column cut-off