Skip to content

Commit 54a8734

Browse files
authored
IBX-6881: Upgraded chart.js to 4.4.0 (#961)
1 parent 503e855 commit 54a8734

File tree

6 files changed

+54
-49
lines changed

6 files changed

+54
-49
lines changed

src/bundle/Resources/encore/ibexa.js.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const layout = [
3232
path.resolve(__dirname, '../public/js/scripts/core/split.btn.js'),
3333
path.resolve(__dirname, '../public/js/scripts/core/pie.chart.js'),
3434
path.resolve(__dirname, '../public/js/scripts/core/bar.chart.js'),
35+
path.resolve(__dirname, '../public/js/scripts/core/doughnut.chart.js'),
3536
path.resolve(__dirname, '../public/js/scripts/core/adaptive.items.js'),
3637
path.resolve(__dirname, '../public/js/scripts/core/popup.menu.js'),
3738
path.resolve(__dirname, '../public/js/scripts/core/tag.view.select.js'),

src/bundle/Resources/public/js/scripts/core/bar.chart.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
(function (global, doc, ibexa) {
22
const barDefaultOptions = {
33
scales: {
4-
xAxes: [
5-
{
6-
display: true,
7-
gridLines: {
8-
display: false,
9-
},
4+
x: {
5+
display: true,
6+
grid: {
7+
display: false,
108
},
11-
],
9+
},
1210
},
1311
};
1412

src/bundle/Resources/public/js/scripts/core/base.chart.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
const defaultOptions = {
66
responsive: true,
77
maintainAspectRatio: false,
8-
legend: {
9-
display: false,
8+
plugins: {
9+
legend: {
10+
display: false,
11+
},
1012
},
1113
tooltips: {
1214
enabled: true,
@@ -26,11 +28,13 @@
2628
},
2729
},
2830
};
31+
const defaultPlugins = [];
2932

3033
class BaseChart {
31-
constructor(data, options = {}) {
34+
constructor(data, options = {}, plugins = []) {
3235
this.setData(data);
3336
this.setOptions(options);
37+
this.setPlugins(plugins);
3438
this.lang = document.documentElement.lang.replace('_', '-'); // TODO: Get this config from settings
3539
}
3640

@@ -46,6 +50,10 @@
4650
};
4751
}
4852

53+
setPlugins(plugins) {
54+
this.plugins = [...defaultPlugins, ...plugins];
55+
}
56+
4957
getType() {}
5058

5159
getLayoutOptions() {}
@@ -74,13 +82,14 @@
7482
}
7583

7684
render() {
77-
this.chart = new Chart(this.canvas.getContext('2d'), {
85+
this.chart = new Chart(this.canvas, {
7886
type: this.getType(),
7987
data: {
8088
labels: this.labels,
8189
datasets: this.datasets,
8290
},
8391
options: this.options,
92+
plugins: this.plugins,
8493
});
8594

8695
this.updateChartMessageDisplay();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(function (global, doc, ibexa) {
2+
class DoughnutChart extends ibexa.core.BaseChart {
3+
constructor(data, options = {}, plugins = []) {
4+
super(data, options, plugins);
5+
6+
this.type = 'doughnut';
7+
}
8+
9+
getType() {
10+
return this.type;
11+
}
12+
}
13+
14+
ibexa.addConfig('core.chart.DoughnutChart', DoughnutChart);
15+
})(window, window.document, window.ibexa);
Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
1-
(function (global, doc, ibexa, Chart) {
1+
(function (global, doc, ibexa) {
22
const MAX_NUMBER_OF_LABELS = 16;
33
const lineDefaultOptions = {
44
elements: {
55
point: {
66
radius: 2,
77
},
8-
line: {
9-
tension: 0,
10-
},
118
},
129
scales: {
13-
xAxes: [
14-
{
15-
display: true,
16-
gridLines: {
17-
display: false,
18-
},
19-
ticks: {
20-
maxRotation: 0,
21-
autoSkip: false,
22-
callback: (value, index, labels) => {
23-
const labelsInterval = Math.max(Math.ceil(labels.length / MAX_NUMBER_OF_LABELS), 1);
24-
const shouldDisplayLabel = !(index % labelsInterval);
25-
26-
return shouldDisplayLabel ? value : null;
27-
},
28-
},
10+
x: {
11+
display: true,
12+
grid: {
13+
display: false,
2914
},
30-
],
31-
yAxes: [
32-
{
33-
display: true,
34-
type: 'logarithmic',
35-
ticks: {
36-
callback: (...args) => {
37-
const value = Chart.Ticks.formatters.logarithmic.call(this, ...args);
38-
39-
if (value.length) {
40-
return Number(value).toLocaleString();
41-
}
42-
43-
return value;
44-
},
15+
ticks: {
16+
maxRotation: 0,
17+
autoSkip: false,
18+
callback: function (value, index, ticks) {
19+
const label = this.getLabelForValue(value);
20+
const labelsInterval = Math.max(Math.ceil(ticks.length / MAX_NUMBER_OF_LABELS), 1);
21+
const shouldDisplayLabel = !(index % labelsInterval);
22+
23+
return shouldDisplayLabel ? label : null;
4524
},
4625
},
47-
],
26+
},
27+
y: {
28+
display: true,
29+
},
4830
},
4931
};
5032

@@ -70,4 +52,4 @@
7052
}
7153

7254
ibexa.addConfig('core.chart.LineChart', LineChart);
73-
})(window, window.document, window.ibexa, window.Chart);
55+
})(window, window.document, window.ibexa);

src/bundle/Resources/views/themes/admin/ui/layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
<script src="{{ asset('bundles/ibexaadminuiassets/vendors/moment-timezone/builds/moment-timezone-with-data.min.js') }}"></script>
113113
<script src="{{ asset('bundles/bazingajstranslation/js/translator.min.js') }}"></script>
114114
<script src="{{ asset('assets/translations/config.js') }}"></script>
115-
<script src="{{ asset('bundles/ibexaadminuiassets/vendors/chart-js/dist/Chart.min.js') }}"></script>
115+
<script src="{{ asset('bundles/ibexaadminuiassets/vendors/chart-js/dist/chart.umd.js') }}"></script>
116116
<script src="{{ asset('bundles/ibexaadminuiassets/vendors/js-md5/build/md5.min.js') }}"></script>
117117
</head>
118118
<body class="{% block body_class %}{% endblock %}">

0 commit comments

Comments
 (0)