]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Simplify financial sample (#7070)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Sat, 8 Feb 2020 13:10:20 +0000 (05:10 -0800)
committerGitHub <noreply@github.com>
Sat, 8 Feb 2020 13:10:20 +0000 (08:10 -0500)
samples/samples.js
samples/scales/financial.html [moved from samples/advanced/financial.html with 71% similarity]

index b93cc773e562f3ac0ecbf8742b8c2973dfab0e4c..7811c8690d4f0232b8f1bb542cdf131f530569de 100644 (file)
                }, {
                        title: 'Center Positioning',
                        path: 'scales/axis-center-position.html'
+               }, {
+                       title: 'Custom major ticks',
+                       path: 'scales/financial.html'
                }]
        }, {
                title: 'Legend',
        }, {
                title: 'Advanced',
                items: [{
-                       title: 'Custom minor and major ticks',
-                       path: 'advanced/financial.html'
-               }, {
                        title: 'Progress bar',
                        path: 'advanced/progress-bar.html'
                }, {
similarity index 71%
rename from samples/advanced/financial.html
rename to samples/scales/financial.html
index 64a42655895942e38b91eee4fa390b438208db85..32171add098cb59639ac290dd9ea716138c2f06a 100644 (file)
                                return date.hour() < 9 || (date.hour() === 9 && date.minute() < 30);
                        }
 
+
+                       function after4pm(date) {
+                               return date.hour() > 16 || (date.hour() === 16 && date.minute() > 0);
+                       }
+
                        // Returns true if outside 9:30am-4pm on a weekday
                        function outsideMarketHours(date) {
                                if (date.isoWeekday() > 5) {
                                        return true;
                                }
-                               if (unitLessThanDay() && (beforeNineThirty(date) || date.hour() > 16)) {
+                               if (unitLessThanDay() && (beforeNineThirty(date) || after4pm(date))) {
                                        return true;
                                }
                                return false;
                                                        fontStyle: function(context) {
                                                                return context.tick.major ? 'bold' : undefined;
                                                        },
-                                                       source: 'labels', // We provided no labels. Generate no ticks. We'll make our own
+                                                       source: 'data',
                                                        autoSkip: true,
                                                        autoSkipPadding: 75,
                                                        maxRotation: 0,
                                                        sampleSize: 100
                                                },
-                                               // Custom logic that chooses ticks from dataset timestamp by choosing first timestamp in time period
+                                               // Custom logic that chooses major ticks by first timestamp in time period
+                                               // E.g. if March 1 & 2 are missing from dataset because they're weekends, we pick March 3 to be beginning of month
                                                afterBuildTicks: function(scale) {
-                                                       // Determine units according to our own logic
-                                                       // Make sure there's at least 10 ticks generated. autoSkip will remove any extras
                                                        const units = ['second', 'minute', 'hour', 'day', 'month', 'year'];
-                                                       const duration = moment.duration(moment(scale.max).diff(scale.min));
                                                        const unit = document.getElementById('unit').value;
-                                                       let minorUnit = unit;
-                                                       for (let i = units.indexOf(minorUnit); i < units.length; i++) {
-                                                               const periods = duration.as(units[i]);
-                                                               if (periods < 10) {
-                                                                       break;
-                                                               }
-                                                               minorUnit = units[i];
-                                                       }
                                                        let majorUnit;
-                                                       if (units.indexOf(minorUnit) !== units.length - 1) {
-                                                               majorUnit = units[units.indexOf(minorUnit) + 1];
-                                                       }
-
-                                                       // Generate ticks according to our own logic
-                                                       const data = scale.chart.data.datasets[0].data;
-                                                       const firstDate = moment(data[0].t);
-
-                                                       function findIndex(ts) {
-                                                               // Note that we could make this faster by doing a binary search
-                                                               // However, Chart.helpers.collection._lookup requires key and it's already pretty fast
-                                                               let result = -1;
-                                                               for (let i = 0; i < data.length; i++) {
-                                                                       if (data[i].t >= ts) {
-                                                                               result = i;
-                                                                               break;
-                                                                       }
-                                                               }
-                                                               if (result === 0) {
-                                                                       return isFirstUnitOfPeriod(firstDate, unit, minorUnit) ? 0 : 1;
-                                                               }
-                                                               return result;
-                                                       }
-
-                                                       // minor ticks
-                                                       let start = moment(scale.min).startOf(minorUnit);
-                                                       const end = moment(scale.max);
-                                                       const values = new Set();
-                                                       for (let date = start; date.isBefore(end); date.add(1, minorUnit)) {
-                                                               const index = findIndex(+date);
-                                                               if (index !== -1) {
-                                                                       values.add(data[index].t);
-                                                               }
+                                                       if (units.indexOf(unit) !== units.length - 1) {
+                                                               majorUnit = units[units.indexOf(unit) + 1];
                                                        }
-                                                       const ticks = Array.from(values, value => ({value}));
 
                                                        // major ticks
+                                                       const ticks = scale.ticks;
                                                        for (let i = 0; i < ticks.length; i++) {
-                                                               if (!majorUnit || isFirstUnitOfPeriod(moment(ticks[i].value), unit, majorUnit)) {
-                                                                       ticks[i].major = true;
-                                                               }
+                                                               ticks[i].major = !majorUnit || isFirstUnitOfPeriod(moment(ticks[i].value), unit, majorUnit);
                                                        }
                                                        scale.ticks = ticks;
                                                }