]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix: config.platform was ignored (#9442)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 20 Jul 2021 11:51:16 +0000 (14:51 +0300)
committerGitHub <noreply@github.com>
Tue, 20 Jul 2021 11:51:16 +0000 (07:51 -0400)
src/core/core.config.js
src/core/core.controller.js
src/helpers/helpers.dom.js
src/platform/index.js
test/specs/core.controller.tests.js

index ff97d5f7572b7056fe870a6fad8d2dad4b0cdfb7..255699bdf527c36dd67fa80cd3330168587b2679 100644 (file)
@@ -128,6 +128,10 @@ export default class Config {
     this._resolverCache = new Map();
   }
 
+  get platform() {
+    return this._config.platform;
+  }
+
   get type() {
     return this._config.type;
   }
index 3d1f6d4459816d64837d78d7c7327d985642b0aa..16c98cd97ffc3b6552f86bf01be23ba7ede67a72 100644 (file)
@@ -2,11 +2,11 @@ import animator from './core.animator';
 import defaults, {overrides} from './core.defaults';
 import Interaction from './core.interaction';
 import layouts from './core.layouts';
-import {BasicPlatform, DomPlatform} from '../platform';
+import {_detectPlatform} from '../platform';
 import PluginService from './core.plugins';
 import registry from './core.registry';
 import Config, {determineAxis, getIndexAxis} from './core.config';
-import {retinaScale} from '../helpers/helpers.dom';
+import {retinaScale, _isDomSupported} from '../helpers/helpers.dom';
 import {each, callback as callCallback, uid, valueOrDefault, _elementsEqual, isNullOrUndef, setsEqual} from '../helpers/helpers.core';
 import {clearCanvas, clipArea, unclipArea, _isPointInArea} from '../helpers/helpers.canvas';
 // @ts-ignore
@@ -44,16 +44,12 @@ function onAnimationProgress(context) {
   callCallback(animationOptions && animationOptions.onProgress, [context], chart);
 }
 
-function isDomSupported() {
-  return typeof window !== 'undefined' && typeof document !== 'undefined';
-}
-
 /**
  * Chart.js can take a string id of a canvas element, a 2d context, or a canvas element itself.
  * Attempt to unwrap the item passed into the chart constructor so that it is a canvas element (if possible).
  */
 function getCanvas(item) {
-  if (isDomSupported() && typeof item === 'string') {
+  if (_isDomSupported() && typeof item === 'string') {
     item = document.getElementById(item);
   } else if (item && item.length) {
     // Support for array based queries (such as jQuery)
@@ -76,10 +72,10 @@ const getChart = (key) => {
 class Chart {
 
   // eslint-disable-next-line max-statements
-  constructor(item, config) {
+  constructor(item, userConfig) {
     const me = this;
 
-    this.config = config = new Config(config);
+    const config = this.config = new Config(userConfig);
     const initialCanvas = getCanvas(item);
     const existingChart = getChart(initialCanvas);
     if (existingChart) {
@@ -91,7 +87,7 @@ class Chart {
 
     const options = config.createResolver(config.chartOptionScopes(), me.getContext());
 
-    this.platform = me._initializePlatform(initialCanvas, config);
+    this.platform = new (config.platform || _detectPlatform(initialCanvas))();
 
     const context = me.platform.acquireContext(initialCanvas, options.aspectRatio);
     const canvas = context && context.canvas;
@@ -206,18 +202,6 @@ class Chart {
     return me;
   }
 
-  /**
-        * @private
-        */
-  _initializePlatform(canvas, config) {
-    if (config.platform) {
-      return new config.platform();
-    } else if (!isDomSupported() || (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas)) {
-      return new BasicPlatform();
-    }
-    return new DomPlatform();
-  }
-
   clear() {
     clearCanvas(this.canvas, this.ctx);
     return this;
index 750af3907b0db5c1f8091827f5ecf17165d3f58f..8fe1f3f9b2b19b9a6c404298dc348ad5be83b3df 100644 (file)
@@ -1,5 +1,12 @@
 import {INFINITY} from './helpers.math';
 
+/**
+ * @private
+ */
+export function _isDomSupported() {
+  return typeof window !== 'undefined' && typeof document !== 'undefined';
+}
+
 /**
  * @private
  */
index 4d4136353a328e688337876f505a9f5f084fae98..7ae28f5b05e27f96b8cdfeb0d7eb1fc68b2475f4 100644 (file)
@@ -1,3 +1,13 @@
-export {default as BasePlatform} from './platform.base';
-export {default as BasicPlatform} from './platform.basic';
-export {default as DomPlatform} from './platform.dom';
+import {_isDomSupported} from '../helpers';
+import BasePlatform from './platform.base';
+import BasicPlatform from './platform.basic';
+import DomPlatform from './platform.dom';
+
+export function _detectPlatform(canvas) {
+  if (!_isDomSupported() || (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas)) {
+    return BasicPlatform;
+  }
+  return DomPlatform;
+}
+
+export {BasePlatform, BasicPlatform, DomPlatform};
index 393983852d2a8dc18841b5d380bc0194388599c5..ac0479b202b768a37986a43265143d2802441c63 100644 (file)
@@ -1935,4 +1935,14 @@ describe('Chart', function() {
       expect(active[0].element).toBe(meta.data[0]);
     });
   });
+
+  describe('platform', function() {
+    it('should use the platform constructor provided in config', function() {
+      const chart = acquireChart({
+        platform: Chart.platforms.BasicPlatform,
+        type: 'line',
+      });
+      expect(chart.platform).toBeInstanceOf(Chart.platforms.BasicPlatform);
+    });
+  });
 });