Highcharts Demo
author(s):
Torstein Hønsi
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
<button id="log-scale-toggle">Log</button>
CSS
#container {
min-width: 320px;
max-width: 800px;
margin: 0 auto;
}
JavaScript
/**
* Custom Axis extension to allow emulation of negative values on a logarithmic
* Y axis. Note that the scale is not mathematically correct, as a true
* logarithmic axis never reaches or crosses zero.
*/
(function (H) {
H.addEvent(H.Axis, 'afterInit', function () {
const logarithmic = this.logarithmic;
if (logarithmic && this.options.custom.allowNegativeLog) {
// Avoid errors on negative numbers on a log axis
this.positiveValuesOnly = false;
// Override the converter functions
logarithmic.log2lin = num => {
const isNegative = num < 0;
let adjustedNum = Math.abs(num);
if (adjustedNum < 10) {
adjustedNum += (10 - adjustedNum) / 10;
}
const result = Math.log(adjustedNum) / Math.LN10;
return isNegative ? -result : result;
};
logarithmic.lin2log = num => {
const isNegative = num < 0;
let result = Math.pow(10, Math.abs(num));
if (result < 10) {
result = (10 * (result - 1)) / (10 - 1);
}
return isNegative ? -result : result;
};
}
});
}(Highcharts));
Highcharts.chart('container', {
chart: {
type: 'column',
},
title: {
text: 'Logarithmic axis with custom conversion allows negative values'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
type: 'logarithmic',
custom: {
allowNegativeLog: true
}
},
series: [{
data: [-1000, -100, -10, -1, -0.1, 0, 0.1, 1, 10, 100, 1000]
}]
});
document.getElementById('log-scale-toggle').addEventListener('click', () => {
var x = document.getElementById('log-scale-toggle');
chart.update({
yAxis: {
...