]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Clean lint errors from samples by refactoring (#7440)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Thu, 28 May 2020 21:32:00 +0000 (00:32 +0300)
committerGitHub <noreply@github.com>
Thu, 28 May 2020 21:32:00 +0000 (17:32 -0400)
* Clean lint errors from samples by refactoring
* Review

samples/scriptable/bubble.html
samples/tooltips/custom-line.html
samples/utils.js

index 23f4f30fb9b3c92542ed5ad86af3b17566b06dce..87cd8a20b40f0e93f7ee742fe4f8300174ec7d8f 100644 (file)
 
                utils.srand(110);
 
+               function channelValue(x, y, values) {
+                       return x < 0 && y < 0 ? values[0] : x < 0 ? values[1] : y < 0 ? values[2] : values[3];
+               }
+
                function colorize(opaque, context) {
                        var value = context.dataset.data[context.dataIndex];
                        var x = value.x / 100;
                        var y = value.y / 100;
-                       var r = x < 0 && y < 0 ? 250 : x < 0 ? 150 : y < 0 ? 50 : 0;
-                       var g = x < 0 && y < 0 ? 0 : x < 0 ? 50 : y < 0 ? 150 : 250;
-                       var b = x < 0 && y < 0 ? 0 : x > 0 && y > 0 ? 250 : 150;
+                       var r = channelValue(x, y, [250, 150, 50, 0]);
+                       var g = channelValue(x, y, [0, 50, 150, 250]);
+                       var b = channelValue(x, y, [0, 150, 150, 250]);
                        var a = opaque ? 1 : 0.5 * value.v / 1000;
 
                        return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
index c4626d872451005e6b192420d9eaaa1f20428c6b..b2c7798fc8ad747f437e387bb02fab4e4ce8c3d2 100644 (file)
@@ -40,8 +40,7 @@
        <script>
                Chart.defaults.pointHitDetectionRadius = 1;
 
-               var customTooltips = function(tooltip) {
-                       // Tooltip Element
+               var getOrCreateTooltip = function() {
                        var tooltipEl = document.getElementById('chartjs-tooltip');
 
                        if (!tooltipEl) {
                                this._chart.canvas.parentNode.appendChild(tooltipEl);
                        }
 
+                       return tooltipEl;
+               };
+
+               var customTooltips = function(tooltip) {
+                       // Tooltip Element
+                       var tooltipEl = getOrCreateTooltip();
+
                        // Hide if no tooltip
                        if (tooltip.opacity === 0) {
                                tooltipEl.style.opacity = 0;
index a750e01172488afea0486d2bde8653c44c3fb90b..69a3a82763814563a9956d9a041e0e4483e7718f 100644 (file)
@@ -41,6 +41,18 @@ window.chartColors = {
        var Samples = global.Samples || (global.Samples = {});
        var Color = Chart.helpers.color;
 
+       function applyDefaultNumbers(config) {
+               var cfg = config || {};
+               cfg.min = cfg.min || 0;
+               cfg.max = cfg.max || 1;
+               cfg.from = cfg.from || [];
+               cfg.count = cfg.count || 8;
+               cfg.decimals = cfg.decimals || 8;
+               cfg.continuity = cfg.continuity || 1;
+
+               return cfg;
+       }
+
        Samples.utils = {
                // Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
                srand: function(seed) {
@@ -56,20 +68,14 @@ window.chartColors = {
                },
 
                numbers: function(config) {
-                       var cfg = config || {};
-                       var min = cfg.min || 0;
-                       var max = cfg.max || 1;
-                       var from = cfg.from || [];
-                       var count = cfg.count || 8;
-                       var decimals = cfg.decimals || 8;
-                       var continuity = cfg.continuity || 1;
-                       var dfactor = Math.pow(10, decimals) || 0;
+                       var cfg = applyDefaultNumbers(config);
+                       var dfactor = Math.pow(10, cfg.decimals) || 0;
                        var data = [];
                        var i, value;
 
-                       for (i = 0; i < count; ++i) {
-                               value = (from[i] || 0) + this.rand(min, max);
-                               if (this.rand() <= continuity) {
+                       for (i = 0; i < cfg.count; ++i) {
+                               value = (cfg.from[i] || 0) + this.rand(cfg.min, cfg.max);
+                               if (this.rand() <= cfg.continuity) {
                                        data.push(Math.round(dfactor * value) / dfactor);
                                } else {
                                        data.push(null);