]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix data in financial sample (#6244)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Wed, 15 May 2019 12:23:09 +0000 (05:23 -0700)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Wed, 15 May 2019 12:23:09 +0000 (14:23 +0200)
samples/scales/time/financial.html

index d5b5ac8d59b398947ded6816f722211e52f47b22..084bacc36229e412309d801d3404995b45089c57 100644 (file)
@@ -17,6 +17,7 @@
 
 <body>
        <div style="width:1000px">
+               <p>This example demonstrates a time series scale by drawing a financial line chart using just the core library. For more specific functionality for financial charts, please see <a href="https://github.com/chartjs/chartjs-chart-financial">chartjs-chart-financial</a></p>
                <canvas id="chart1"></canvas>
        </div>
        <br>
        <button id="update">update</button>
        <script>
                function generateData() {
+                       var unit = document.getElementById('unit').value;
+
+                       function unitLessThanDay() {
+                               return unit === 'second' || unit === 'minute' || unit === 'hour';
+                       }
+
+                       function beforeNineThirty(date) {
+                               return date.hour() < 9 || (date.hour() === 9 && date.minute() < 30);
+                       }
+
+                       // 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)) {
+                                       return true;
+                               }
+                               return false;
+                       }
+
                        function randomNumber(min, max) {
                                return Math.random() * (max - min) + min;
                        }
                        var date = moment('Jan 01 1990', 'MMM DD YYYY');
                        var now = moment();
                        var data = [];
-                       var unit = document.getElementById('unit').value;
-                       for (; data.length < 60 && date.isBefore(now); date = date.clone().add(1, unit)) {
-                               if (date.isoWeekday() <= 5) {
-                                       data.push(randomBar(date, data.length > 0 ? data[data.length - 1].y : 30));
+                       var lessThanDay = unitLessThanDay();
+                       for (; data.length < 60 && date.isBefore(now); date = date.clone().add(1, unit).startOf(unit)) {
+                               if (outsideMarketHours(date)) {
+                                       if (!lessThanDay || !beforeNineThirty(date)) {
+                                               date = date.clone().add(date.isoWeekday() >= 5 ? 8 - date.isoWeekday() : 1, 'day');
+                                       }
+                                       if (lessThanDay) {
+                                               date = date.hour(9).minute(30).second(0);
+                                       }
                                }
+                               data.push(randomBar(date, data.length > 0 ? data[data.length - 1].y : 30));
                        }
 
                        return data;