From 01b882e63de0fd360bc68fb5246715afe377d19b Mon Sep 17 00:00:00 2001 From: bre1470 <40056287+bre1470@users.noreply.github.com> Date: Mon, 10 Aug 2020 13:53:16 +0200 Subject: [PATCH] Reverted fix #13914, negative logarithmic axis. --- js/Core/Axis/Axis.js | 5 +- js/Core/Axis/LogarithmicAxis.js | 35 +------------ .../yaxis/type-log-negative/demo.js | 50 +++++++++++++++++-- ts/Core/Axis/Axis.ts | 6 +-- ts/Core/Axis/LogarithmicAxis.ts | 42 +--------------- 5 files changed, 53 insertions(+), 85 deletions(-) diff --git a/js/Core/Axis/Axis.js b/js/Core/Axis/Axis.js index 1d50555ac17..da1c2fe8a7e 100644 --- a/js/Core/Axis/Axis.js +++ b/js/Core/Axis/Axis.js @@ -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); /** diff --git a/js/Core/Axis/LogarithmicAxis.js b/js/Core/Axis/LogarithmicAxis.js index eeb957fbaad..8f62b1dcafe 100644 --- a/js/Core/Axis/LogarithmicAxis.js +++ b/js/Core/Axis/LogarithmicAxis.js @@ -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; @@ -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; @@ -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 diff --git a/samples/highcharts/yaxis/type-log-negative/demo.js b/samples/highcharts/yaxis/type-log-negative/demo.js index 1797d96b28a..da731c7407b 100644 --- a/samples/highcharts/yaxis/type-log-negative/demo.js +++ b/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: { @@ -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] }] -}); \ No newline at end of file +}); diff --git a/ts/Core/Axis/Axis.ts b/ts/Core/Axis/Axis.ts index 72f758e83f8..9b5bb4f90fc 100644 --- a/ts/Core/Axis/Axis.ts +++ b/ts/Core/Axis/Axis.ts @@ -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); diff --git a/ts/Core/Axis/LogarithmicAxis.ts b/ts/Core/Axis/LogarithmicAxis.ts index 36e424f3fc0..6781d800961 100644 --- a/ts/Core/Axis/LogarithmicAxis.ts +++ b/ts/Core/Axis/LogarithmicAxis.ts @@ -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; - } } } @@ -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; } @@ -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 */ @@ -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