Skip to content

Commit 12b31f7

Browse files
committed
PoC 32842 upgrade ChartJS
1 parent bf4cdad commit 12b31f7

File tree

6 files changed

+93
-10
lines changed

6 files changed

+93
-10
lines changed

app/code/Magento/Backend/view/adminhtml/web/js/dashboard/chart.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ define([
99
'jquery',
1010
'chartJs',
1111
'jquery-ui-modules/widget',
12+
'chartjs/chartjs-adapter-moment',
1213
'moment'
1314
], function ($, Chart) {
1415
'use strict';
@@ -74,7 +75,7 @@ define([
7475
$(this.element).toggle(response.data.length > 0);
7576
$(this.element).next('.dashboard-diagram-nodata').toggle(response.data.length === 0);
7677

77-
this.chart.options.scales.xAxes[0].time.unit = this.options.periodUnits[this.period] ?
78+
this.chart.options.scales.xAxis.time.unit = this.options.periodUnits[this.period] ?
7879
this.options.periodUnits[this.period] : 'hour';
7980
this.chart.data.datasets[0].data = response.data;
8081
this.chart.data.datasets[0].label = response.label;
@@ -89,6 +90,8 @@ define([
8990
type: 'bar',
9091
data: {
9192
datasets: [{
93+
yAxisID: 'yAxis',
94+
xAxisID: 'xAxis',
9295
data: [],
9396
backgroundColor: '#f1d4b3',
9497
borderColor: '#eb5202',
@@ -101,20 +104,19 @@ define([
101104
position: 'bottom'
102105
},
103106
scales: {
104-
xAxes: [{
107+
xAxis: {
105108
offset: true,
106109
type: 'time',
107110
ticks: {
108-
autoSkip: true,
109111
source: 'data'
110112
}
111-
}],
112-
yAxes: [{
113+
},
114+
yAxis: {
113115
ticks: {
114116
beginAtZero: true,
115117
precision: this.options.precision
116118
}
117-
}]
119+
}
118120
}
119121
}
120122
};

app/code/Magento/Ui/view/base/requirejs-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
var config = {
77
deps: [],
88
shim: {
9-
'chartjs/Chart.min': ['moment'],
9+
'chartjs/chartjs-adapter-moment': ['moment'],
1010
'tiny_mce_4/tinymce.min': {
1111
exports: 'tinyMCE'
1212
}
@@ -26,6 +26,7 @@ var config = {
2626
uiLayout: 'Magento_Ui/js/core/renderer/layout',
2727
buttonAdapter: 'Magento_Ui/js/form/button-adapter',
2828
chartJs: 'chartjs/Chart.min',
29+
'chart.js': 'chartjs/Chart.min',
2930
tinymce: 'tiny_mce_4/tinymce.min',
3031
wysiwygAdapter: 'mage/adminhtml/wysiwyg/tiny_mce/tinymce4Adapter'
3132
}

app/design/frontend/Magento/blank/etc/view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
</vars>
261261
<exclude>
262262
<item type="file">Lib::chartjs/Chart.min.js</item>
263+
<item type="file">Lib::chartjs/chartjs-adapter-moment.js</item>
263264
<item type="file">Lib::jquery/jquery.min.js</item>
264265
<item type="file">Lib::jquery/jquery-ui-1.9.2.js</item>
265266
<item type="file">Lib::jquery/jquery.details.js</item>

app/design/frontend/Magento/luma/etc/view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
</vars>
272272
<exclude>
273273
<item type="file">Lib::chartjs/Chart.min.js</item>
274+
<item type="file">Lib::chartjs/chartjs-adapter-moment.js</item>
274275
<item type="file">Lib::jquery/jquery.min.js</item>
275276
<item type="file">Lib::jquery/jquery-ui-1.9.2.js</item>
276277
<item type="file">Lib::jquery/jquery.details.js</item>

lib/web/chartjs/Chart.min.js

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*!
2+
* chartjs-adapter-moment v1.0.0
3+
* https://www.chartjs.org
4+
* (c) 2021 chartjs-adapter-moment Contributors
5+
* Released under the MIT license
6+
*/
7+
(function (global, factory) {
8+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('moment'), require('chart.js')) :
9+
typeof define === 'function' && define.amd ? define(['moment', 'chart.js'], factory) :
10+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.moment, global.Chart));
11+
}(this, (function (moment, chart_js) { 'use strict';
12+
13+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14+
15+
var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment);
16+
17+
const FORMATS = {
18+
datetime: 'MMM D, YYYY, h:mm:ss a',
19+
millisecond: 'h:mm:ss.SSS a',
20+
second: 'h:mm:ss a',
21+
minute: 'h:mm a',
22+
hour: 'hA',
23+
day: 'MMM D',
24+
week: 'll',
25+
month: 'MMM YYYY',
26+
quarter: '[Q]Q - YYYY',
27+
year: 'YYYY'
28+
};
29+
30+
chart_js._adapters._date.override(typeof moment__default['default'] === 'function' ? {
31+
_id: 'moment', // DEBUG ONLY
32+
33+
formats: function() {
34+
return FORMATS;
35+
},
36+
37+
parse: function(value, format) {
38+
if (typeof value === 'string' && typeof format === 'string') {
39+
value = moment__default['default'](value, format);
40+
} else if (!(value instanceof moment__default['default'])) {
41+
value = moment__default['default'](value);
42+
}
43+
return value.isValid() ? value.valueOf() : null;
44+
},
45+
46+
format: function(time, format) {
47+
return moment__default['default'](time).format(format);
48+
},
49+
50+
add: function(time, amount, unit) {
51+
return moment__default['default'](time).add(amount, unit).valueOf();
52+
},
53+
54+
diff: function(max, min, unit) {
55+
return moment__default['default'](max).diff(moment__default['default'](min), unit);
56+
},
57+
58+
startOf: function(time, unit, weekday) {
59+
time = moment__default['default'](time);
60+
if (unit === 'isoWeek') {
61+
weekday = Math.trunc(Math.min(Math.max(0, weekday), 6));
62+
return time.isoWeekday(weekday).startOf('day').valueOf();
63+
}
64+
return time.startOf(unit).valueOf();
65+
},
66+
67+
endOf: function(time, unit) {
68+
return moment__default['default'](time).endOf(unit).valueOf();
69+
}
70+
} : {});
71+
72+
})));

0 commit comments

Comments
 (0)