Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6881: Upgrade chart.js to 4.4.0 #961

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bundle/Resources/encore/ibexa.js.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const layout = [
path.resolve(__dirname, '../public/js/scripts/core/split.btn.js'),
path.resolve(__dirname, '../public/js/scripts/core/pie.chart.js'),
path.resolve(__dirname, '../public/js/scripts/core/bar.chart.js'),
path.resolve(__dirname, '../public/js/scripts/core/doughnut.chart.js'),
path.resolve(__dirname, '../public/js/scripts/core/adaptive.items.js'),
path.resolve(__dirname, '../public/js/scripts/core/popup.menu.js'),
path.resolve(__dirname, '../public/js/scripts/core/tag.view.select.js'),
Expand Down
12 changes: 5 additions & 7 deletions src/bundle/Resources/public/js/scripts/core/bar.chart.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
(function (global, doc, ibexa) {
const barDefaultOptions = {
scales: {
xAxes: [
{
display: true,
gridLines: {
display: false,
},
x: {
display: true,
grid: {
display: false,
},
],
},
},
};

Expand Down
17 changes: 13 additions & 4 deletions src/bundle/Resources/public/js/scripts/core/base.chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
const defaultOptions = {
responsive: true,
maintainAspectRatio: false,
legend: {
display: false,
plugins: {
legend: {
display: false,
},
},
tooltips: {
enabled: true,
Expand All @@ -26,11 +28,13 @@
},
},
};
const defaultPlugins = [];

class BaseChart {
constructor(data, options = {}) {
constructor(data, options = {}, plugins = []) {
this.setData(data);
this.setOptions(options);
this.setPlugins(plugins);
this.lang = document.documentElement.lang.replace('_', '-'); // TODO: Get this config from settings
}

Expand All @@ -46,6 +50,10 @@
};
}

setPlugins(plugins) {
this.plugins = [...defaultPlugins, ...plugins];
}

getType() {}

getLayoutOptions() {}
Expand Down Expand Up @@ -74,13 +82,14 @@
}

render() {
this.chart = new Chart(this.canvas.getContext('2d'), {
this.chart = new Chart(this.canvas, {
type: this.getType(),
data: {
labels: this.labels,
datasets: this.datasets,
},
options: this.options,
plugins: this.plugins,
});

this.updateChartMessageDisplay();
Expand Down
15 changes: 15 additions & 0 deletions src/bundle/Resources/public/js/scripts/core/doughnut.chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function (global, doc, ibexa) {
class DoughnutChart extends ibexa.core.BaseChart {
constructor(data, options = {}, plugins = []) {
super(data, options, plugins);

this.type = 'doughnut';
}

getType() {
return this.type;
}
}

ibexa.addConfig('core.chart.DoughnutChart', DoughnutChart);
})(window, window.document, window.ibexa);
56 changes: 19 additions & 37 deletions src/bundle/Resources/public/js/scripts/core/line.chart.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
(function (global, doc, ibexa, Chart) {
(function (global, doc, ibexa) {
const MAX_NUMBER_OF_LABELS = 16;
const lineDefaultOptions = {
elements: {
point: {
radius: 2,
},
line: {
tension: 0,
},
},
scales: {
xAxes: [
{
display: true,
gridLines: {
display: false,
},
ticks: {
maxRotation: 0,
autoSkip: false,
callback: (value, index, labels) => {
const labelsInterval = Math.max(Math.ceil(labels.length / MAX_NUMBER_OF_LABELS), 1);
const shouldDisplayLabel = !(index % labelsInterval);

return shouldDisplayLabel ? value : null;
},
},
x: {
display: true,
grid: {
display: false,
},
],
yAxes: [
{
display: true,
type: 'logarithmic',
ticks: {
callback: (...args) => {
const value = Chart.Ticks.formatters.logarithmic.call(this, ...args);

if (value.length) {
return Number(value).toLocaleString();
}

return value;
},
ticks: {
maxRotation: 0,
autoSkip: false,
callback: function (value, index, ticks) {
const label = this.getLabelForValue(value);
const labelsInterval = Math.max(Math.ceil(ticks.length / MAX_NUMBER_OF_LABELS), 1);
const shouldDisplayLabel = !(index % labelsInterval);

return shouldDisplayLabel ? label : null;
},
},
],
},
y: {
display: true,
},
},
};

Expand All @@ -70,4 +52,4 @@
}

ibexa.addConfig('core.chart.LineChart', LineChart);
})(window, window.document, window.ibexa, window.Chart);
})(window, window.document, window.ibexa);
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<script src="{{ asset('bundles/ibexaadminuiassets/vendors/moment-timezone/builds/moment-timezone-with-data.min.js') }}"></script>
<script src="{{ asset('bundles/bazingajstranslation/js/translator.min.js') }}"></script>
<script src="{{ asset('assets/translations/config.js') }}"></script>
<script src="{{ asset('bundles/ibexaadminuiassets/vendors/chart-js/dist/Chart.min.js') }}"></script>
<script src="{{ asset('bundles/ibexaadminuiassets/vendors/chart-js/dist/chart.umd.js') }}"></script>
<script src="{{ asset('bundles/ibexaadminuiassets/vendors/js-md5/build/md5.min.js') }}"></script>
</head>
<body class="{% block body_class %}{% endblock %}">
Expand Down
Loading