]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add userCallback to category scale + demo file
authorEvert Timberg <evert.timberg@gmail.com>
Mon, 22 Jun 2015 23:03:01 +0000 (19:03 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Mon, 22 Jun 2015 23:03:01 +0000 (19:03 -0400)
samples/line-x-axis-filter.html [new file with mode: 0644]
src/scales/scale.category.js

diff --git a/samples/line-x-axis-filter.html b/samples/line-x-axis-filter.html
new file mode 100644 (file)
index 0000000..3b5b82f
--- /dev/null
@@ -0,0 +1,138 @@
+<!doctype html>
+<html>
+
+<head>
+    <title>Chart with xAxis Filtering</title>
+    <script src="../Chart.js"></script>
+    <script src="../node_modules/jquery/dist/jquery.min.js"></script>
+    <style>
+    canvas {
+        -webkit-box-shadow: 0 0 20px 0 rgba(0, 0, 0, .5);
+    }
+    </style>
+</head>
+
+<body>
+    <div style="width:50%;">
+        <canvas id="canvas" style="width:100%;height:100%"></canvas>
+    </div>
+    <br>
+    <br>
+    <button id="randomizeData">Randomize Data</button>
+    <button id="addDataset">Add Dataset</button>
+    <button id="removeDataset">Remove Dataset</button>
+    <button id="addData">Add Data</button>
+    <button id="removeData">Remove Data</button>
+    <script>
+    var randomScalingFactor = function() {
+        return Math.round(Math.random() * 50 * (Math.random() > 0.5 ? 1 : 1)) + 50;
+    };
+    var randomColorFactor = function() {
+        return Math.round(Math.random() * 255);
+    };
+    var randomColor = function(opacity) {
+        return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
+    };
+
+    var config = {
+        type: 'line',
+        data: {
+            labels: ["January", "February", "March", "April", "May", "June", "July"],
+            datasets: [{
+                label: "My First dataset",
+                data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
+                fill: false,
+                borderDash: [5, 5],
+            }, {
+                label: "My Second dataset",
+                data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
+            }]
+        },
+        options: {
+            responsive: true,
+            scales: {
+                xAxes: [{
+                    display: true,
+                    labels: {
+                        userCallback: function(dataLabel, index) {
+                            return index % 2 === 0 ? dataLabel : '';
+                        }
+                    }
+                }],
+                yAxes: [{
+                    display: true,
+                    beginAtZero: false
+                }]
+            }
+        }
+    };
+
+    $.each(config.data.datasets, function(i, dataset) {
+        dataset.borderColor = randomColor(0.4);
+        dataset.backgroundColor = randomColor(0.5);
+        dataset.pointBorderColor = randomColor(0.7);
+        dataset.pointBackgroundColor = randomColor(0.5);
+        dataset.pointBorderWidth = 1;
+    });
+
+    console.log(config.data);
+
+    window.onload = function() {
+        var ctx = document.getElementById("canvas").getContext("2d");
+        window.myLine = new Chart(ctx, config);
+    };
+
+    $('#randomizeData').click(function() {
+        $.each(config.data.datasets, function(i, dataset) {
+            dataset.data = dataset.data.map(function() {
+                return randomScalingFactor();
+            });
+
+        });
+
+        window.myLine.update();
+    });
+
+    $('#addDataset').click(function() {
+        var newDataset = {
+            label: 'Dataset ' + config.data.datasets.length,
+            borderColor: randomColor(0.4),
+            backgroundColor: randomColor(0.5),
+            pointBorderColor: randomColor(0.7),
+            pointBackgroundColor: randomColor(0.5),
+            pointBorderWidth: 1,
+            data: [],
+        };
+
+        for (var index = 0; index < config.data.labels.length; ++index) {
+            newDataset.data.push(randomScalingFactor());
+        }
+
+        window.myLine.addDataset(newDataset);
+    });
+
+    $('#addData').click(function() {
+        if (config.data.datasets.length > 0) {
+            config.data.labels.push('dataset #' + config.data.labels.length);
+
+            for (var index = 0; index < config.data.datasets.length; ++index) {
+                window.myLine.addData(randomScalingFactor(), index);
+            }
+        }
+    });
+
+    $('#removeDataset').click(function() {
+        window.myLine.removeDataset(0);
+    });
+
+    $('#removeData').click(function() {
+        config.data.labels.splice(-1, 1); // remove the label first
+
+        config.data.datasets.forEach(function(dataset, datasetIndex) {
+            window.myLine.removeData(datasetIndex, -1);
+        });
+    });
+    </script>
+</body>
+
+</html>
index 7e861dfd7c22d2f46602059e2e3d1091a7cef4a8..2b233048f853ed1988971c36de53c84e38ffdd36 100644 (file)
                isHorizontal: function() {
                        return this.options.position == "top" || this.options.position == "bottom";
                },
+               buildLabels: function(index) {
+                       this.labels = [];
+
+                       if (this.options.labels.userCallback) {
+                               this.data.labels.forEach(function(labelString, index) {
+                                       this.labels.push(this.options.labels.userCallback(labelString, index));
+                               }, this);
+                       } else {
+                               this.labels = this.data.labels;
+                       }
+               },
                getPixelForValue: function(value, index, datasetIndex, includeOffset) {
                        // This must be called after fit has been run so that 
                        //      this.left, this.top, this.right, and this.bottom have been defined
                        if (this.isHorizontal()) {
                                var isRotated = (this.labelRotation > 0);
                                var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
-                               var valueWidth = innerWidth / Math.max((this.data.labels.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
+                               var valueWidth = innerWidth / Math.max((this.labels.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
                                var valueOffset = (valueWidth * index) + this.paddingLeft;
 
                                if (this.options.gridLines.offsetGridLines && includeOffset) {
@@ -52,7 +63,7 @@
 
                                return this.left + Math.round(valueOffset);
                        } else {
-                               return this.top + (index * (this.height / this.data.labels.length));
+                               return this.top + (index * (this.height / this.labels.length));
                        }
                },
                getPointPixelForValue: function(value, index, datasetIndex) {
                        var labelFont = helpers.fontString(this.options.labels.fontSize, this.options.labels.fontStyle, this.options.labels.fontFamily);
                        this.ctx.font = labelFont;
 
-                       var firstWidth = this.ctx.measureText(this.data.labels[0]).width;
-                       var lastWidth = this.ctx.measureText(this.data.labels[this.data.labels.length - 1]).width;
+                       var firstWidth = this.ctx.measureText(this.labels[0]).width;
+                       var lastWidth = this.ctx.measureText(this.labels[this.labels.length - 1]).width;
                        var firstRotated;
                        var lastRotated;
 
                        this.labelRotation = 0;
 
                        if (this.options.display) {
-                               var originalLabelWidth = helpers.longestText(this.ctx, labelFont, this.data.labels);
+                               var originalLabelWidth = helpers.longestText(this.ctx, labelFont, this.labels);
                                var cosRotation;
                                var sinRotation;
                                var firstRotatedWidth;
                                this.height = maxHeight;
                        }
 
+                       this.buildLabels();
                        this.calculateLabelRotation(maxHeight, margins);
 
                        var minSize = {
                        };
 
                        var labelFont = helpers.fontString(this.options.labels.fontSize, this.options.labels.fontStyle, this.options.labels.fontFamily);
-                       var longestLabelWidth = helpers.longestText(this.ctx, labelFont, this.data.labels);
+                       var longestLabelWidth = helpers.longestText(this.ctx, labelFont, this.labels);
 
                        // Width
                        if (this.isHorizontal()) {
                                        var yTickEnd = this.options.position == "bottom" ? this.top + 10 : this.bottom;
                                        var isRotated = this.labelRotation !== 0;
 
-                                       helpers.each(this.data.labels, function(label, index) {
+                                       helpers.each(this.labels, function(label, index) {
                                                var xLineValue = this.getPixelForValue(label, index, null, false); // xvalues for grid lines
                                                var xLabelValue = this.getPixelForValue(label, index, null, true); // x values for labels (need to consider offsetLabel option)