Chart.js/docs/scripts/log2.js
Evert Timberg 66ee0fecaf
Vuepress samples (#8756)
* Generate API docs with vuepress-plugin-typedoc

* Links, fixes, cleanup

* Convert bar samples to Vuepress

* Some line chart samples moved over

* Fix lint issues

* Derived axis type sample

* LineAreaStacked chart created in vuepress

* added radar area axample

* Line dataset added sample

* final area example added

* Add derived-chart-type

* Bar scriptable sample

* Scriptable samples

* Clean lint errors

* added linear axis samples to vuepress

* change tab to spaces to fix lint error

* Convert the rest of the scale samples

* Scale option samples

* Fix typo

* Fixes

* Legend samples

* Title samples

* Change the title of the tip block to Note (#8758)

* Convert bar samples to Vuepress

* Some line chart samples moved over

* Fix lint issues

* Derived axis type sample

* LineAreaStacked chart created in vuepress

* added radar area axample

* Line dataset added sample

* final area example added

* Add derived-chart-type

* Bar scriptable sample

* Scriptable samples

* Clean lint errors

* added linear axis samples to vuepress

* change tab to spaces to fix lint error

* Convert the rest of the scale samples

* Scale option samples

* Fix typo

* Fixes

* Legend samples

* Advanced samples

* Remove extra section

* Animation samples

* Hide legend from progressive line

* Add a comment on what `from` does

* Tooltip samples

* Ädd other charts to vuepress samples

* enable plugin again since all samples have been converted

* fix skip radar example, middle skip was not calculated correctly

* lint error

* Progressive-line: add 2nd line

* Fix lint errors

Co-authored-by: Jukka Kurkela <jukka.kurkela@gmail.com>
Co-authored-by: Jacco van den Berg <jaccoberg2281@gmail.com>
Co-authored-by: Jacco van den Berg <39033624+LeeLenaleee@users.noreply.github.com>
2021-04-02 08:04:39 -04:00

68 lines
1.6 KiB
JavaScript

import {Scale, LinearScale} from 'chart.js';
export default class Log2Axis extends Scale {
constructor(cfg) {
super(cfg);
this._startValue = undefined;
this._valueRange = 0;
}
parse(raw, index) {
const value = LinearScale.prototype.parse.apply(this, [raw, index]);
return isFinite(value) && value > 0 ? value : null;
}
determineDataLimits() {
const {min, max} = this.getMinMax(true);
this.min = isFinite(min) ? Math.max(0, min) : null;
this.max = isFinite(max) ? Math.max(0, max) : null;
}
buildTicks() {
const ticks = [];
let power = Math.floor(Math.log2(this.min));
let maxPower = Math.ceil(Math.log2(this.max));
while (power <= maxPower) {
ticks.push({value: Math.pow(2, power)});
power += 1;
}
this.min = ticks[0].value;
this.max = ticks[ticks.length - 1].value;
return ticks;
}
/**
* @protected
*/
configure() {
const start = this.min;
super.configure();
this._startValue = Math.log2(start);
this._valueRange = Math.log2(this.max) - Math.log2(start);
}
getPixelForValue(value) {
if (value === undefined || value === 0) {
value = this.min;
}
return this.getPixelForDecimal(value === this.min ? 0
: (Math.log2(value) - this._startValue) / this._valueRange);
}
getValueForPixel(pixel) {
const decimal = this.getDecimalForPixel(pixel);
return Math.pow(2, this._startValue + decimal * this._valueRange);
}
}
Log2Axis.id = 'log2';
Log2Axis.defaults = {};
// The derived axis is registered like this:
// Chart.register(Log2Axis);