Skip to content

Commit

Permalink
Reverted fix #13914, negative logarithmic axis.
Browse files Browse the repository at this point in the history
  • Loading branch information
bre1470 committed Aug 10, 2020
1 parent 4584082 commit 01b882e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 85 deletions.
5 changes: 1 addition & 4 deletions js/Core/Axis/Axis.js
Expand Up @@ -433,10 +433,7 @@ var Axis = /** @class */ (function () {
// Placeholder for plotlines and plotbands groups
axis.plotLinesAndBandsGroups = {};
// Shorthand types
axis.positiveValuesOnly = !!(axis.logarithmic &&
!options.allowNegativeLog &&
!axis.allowNegativeLog // #13914 backwards compatibility
);
axis.positiveValuesOnly = !!axis.logarithmic;
// Flag, if axis is linked to another axis
axis.isLinked = defined(options.linkedTo);
/**
Expand Down
35 changes: 2 additions & 33 deletions js/Core/Axis/LogarithmicAxis.js
Expand Up @@ -109,25 +109,9 @@ var LogarithmicAxisAdditions = /** @class */ (function () {
return positions;
};
LogarithmicAxisAdditions.prototype.lin2log = function (num) {
if (this.axis.options.allowNegativeLog) {
var isNegative = num < 0;
num = Math.abs(num);
if (num < 1) {
return 0;
}
return (isNegative ? -1 : 1) * Math.pow(10, num);
}
return Math.pow(10, num);
};
LogarithmicAxisAdditions.prototype.log2lin = function (num) {
if (this.axis.options.allowNegativeLog) {
var isNegative = num < 0;
num = Math.abs(num);
if (num < 1) {
return 0;
}
return (isNegative ? -1 : 1) * Math.log(num) / Math.LN10;
}
return Math.log(num) / Math.LN10;
};
return LogarithmicAxisAdditions;
Expand All @@ -147,12 +131,8 @@ var LogarithmicAxis = /** @class */ (function () {
// @todo Remove this in next major
var axisProto = AxisClass.prototype;
var logAxisProto = LogarithmicAxisAdditions.prototype;
axisProto.log2lin = function (num) {
return logAxisProto.log2lin.call({ axis: this }, num);
};
axisProto.lin2log = function (num) {
return logAxisProto.lin2log.call({ axis: this }, num);
};
axisProto.log2lin = logAxisProto.log2lin;
axisProto.lin2log = logAxisProto.lin2log;
/* eslint-disable no-invalid-this */
addEvent(AxisClass, 'init', function (e) {
var axis = this;
Expand Down Expand Up @@ -194,14 +174,3 @@ var LogarithmicAxis = /** @class */ (function () {
}());
LogarithmicAxis.compose(Axis); // @todo move to factory functions
export default LogarithmicAxis;
/**
* Activates rendering of negative logarithmic values.
*
* @sample highcharts/yaxis/type-log-negative
* Rendering negative logarithmic
*
* @type {boolean}
* @since next
* @apioption xAxis.allowNegativeLog
*/
''; // keeps doclets above in transpiled file
50 changes: 47 additions & 3 deletions samples/highcharts/yaxis/type-log-negative/demo.js
@@ -1,3 +1,45 @@
/**
* 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', {

title: {
Expand All @@ -11,11 +53,13 @@ Highcharts.chart('container', {

yAxis: {
type: 'logarithmic',
allowNegativeLog: true
custom: {
allowNegativeLog: true
}
},

series: [{
data: [-1000, -100, -10, -1, -0.1, 0, 0.1, 1, 10, 100, 1000, 10000]
data: [-1000, -100, -10, -1, -0.1, 0, 0.1, 1, 10, 100, 1000]
}]

});
});
6 changes: 1 addition & 5 deletions ts/Core/Axis/Axis.ts
Expand Up @@ -4120,11 +4120,7 @@ class Axis implements AxisComposition, AxisLike {
axis.plotLinesAndBandsGroups = {};

// Shorthand types
axis.positiveValuesOnly = !!(
axis.logarithmic &&
!options.allowNegativeLog &&
!axis.allowNegativeLog // #13914 backwards compatibility
);
axis.positiveValuesOnly = !!axis.logarithmic;

// Flag, if axis is linked to another axis
axis.isLinked = defined(options.linkedTo);
Expand Down
42 changes: 2 additions & 40 deletions ts/Core/Axis/LogarithmicAxis.ts
Expand Up @@ -29,16 +29,11 @@ const {
declare global {
namespace Highcharts {
interface Axis {
/** @deprecated */
allowNegativeLog?: boolean;
/** @deprecated */
lin2log(num: number): number;
/** @deprecated */
log2lin(num: number): number;
}
interface XAxisOptions {
allowNegativeLog?: boolean;
}
}
}

Expand Down Expand Up @@ -209,26 +204,10 @@ class LogarithmicAxisAdditions {
}

public lin2log(num: number): number {
if (this.axis.options.allowNegativeLog) {
const isNegative = num < 0;
num = Math.abs(num);
if (num < 1) {
return 0;
}
return (isNegative ? -1 : 1) * Math.pow(10, num);
}
return Math.pow(10, num);
}

public log2lin(num: number): number {
if (this.axis.options.allowNegativeLog) {
const isNegative = num < 0;
num = Math.abs(num);
if (num < 1) {
return 0;
}
return (isNegative ? -1 : 1) * Math.log(num) / Math.LN10;
}
return Math.log(num) / Math.LN10;
}

Expand All @@ -250,12 +229,8 @@ class LogarithmicAxis {
// @todo Remove this in next major
const axisProto = AxisClass.prototype;
const logAxisProto = LogarithmicAxisAdditions.prototype;
axisProto.log2lin = function (num: number): number {
return logAxisProto.log2lin.call({ axis: this }, num);
};
axisProto.lin2log = function (num: number): number {
return logAxisProto.lin2log.call({ axis: this }, num);
};
axisProto.log2lin = logAxisProto.log2lin;
axisProto.lin2log = logAxisProto.lin2log;

/* eslint-disable no-invalid-this */

Expand Down Expand Up @@ -307,16 +282,3 @@ interface LogarithmicAxis extends Axis {
LogarithmicAxis.compose(Axis); // @todo move to factory functions

export default LogarithmicAxis;

/**
* Activates rendering of negative logarithmic values.
*
* @sample highcharts/yaxis/type-log-negative
* Rendering negative logarithmic
*
* @type {boolean}
* @since next
* @apioption xAxis.allowNegativeLog
*/

''; // keeps doclets above in transpiled file

0 comments on commit 01b882e

Please sign in to comment.