Skip to content

Commit

Permalink
[TASK] Adjust chartjs implementation (#218)
Browse files Browse the repository at this point in the history
* [TASK] Make chartjs usable without endpoint
* [BUGFIX] Make requests work again
  • Loading branch information
benjaminkott authored Jun 23, 2022
1 parent 5a1ccf4 commit e19118c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 51 deletions.
94 changes: 48 additions & 46 deletions assets/js/libs/charts.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
(function() {
'use strict';
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);

const Selectors = {
chartContainer: 'canvas.js-chart'
};

const Charts = {
initialize: function () {
if (document.querySelectorAll(Selectors.chartContainer).length > 0) {
require('chart.js');
Charts.setup();
}
},
setup: function () {
const charts = Array.from(document.querySelectorAll(Selectors.chartContainer));
for (let canvas of charts) {
Charts.loadChart(canvas);
}
},
loadChart: function (canvas) {
const chartType = canvas.dataset.chartType;
const sourceUrl = canvas.dataset.src;
const options = JSON.parse(canvas.dataset.options || '{}');
const Charts = {
options: {
selector: 'canvas.js-chart'
},
initialize: function () {
if (document.querySelectorAll(this.options.selector).length > 0) {
Charts.setup();
}
},
setup: function () {
const charts = Array.from(document.querySelectorAll(this.options.selector));
for (let canvas of charts) {
Charts.loadChart(canvas);
}
},
loadChart: async function (canvas) {
let chartType = canvas.dataset.chartType;
let sourceUrl = canvas.dataset.src || null;
let dataSet = JSON.parse(canvas.dataset.dataset || '{}');
let options = JSON.parse(canvas.dataset.options || '{}');

Charts.fetchData(sourceUrl).done(function(response) {
new Chart(canvas, {
type: chartType,
data: response,
options: Object.assign({
responsive: true,
maintainAspectRatio: false,
tooltips: {
mode: 'index'
}
}, options)
});
});
},
fetchData: function (src) {
return $.ajax(src, {
dataType: 'json'
});
if (sourceUrl) {
dataSet = await this.fetchData(sourceUrl);
}
};

$(function () {
Charts.initialize();
});
})();
new Chart(canvas, {
type: chartType,
data: dataSet,
options: Object.assign({
responsive: true,
maintainAspectRatio: false,
tooltips: {
mode: 'index'
}
}, options)
});
},
fetchData: async function (src) {
let response = await fetch(src, {
headers: {
'Accept': 'application/json'
}
});
let data = await response.json();
return data;
}
};

Charts.initialize();
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/Resources/public/app.54fb8efd.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/Resources/public/app.9aaea0d2.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Resources/public/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"/bundles/template/app.11308dab.css"
],
"js": [
"/bundles/template/app.54fb8efd.js"
"/bundles/template/app.9aaea0d2.js"
]
},
"webfont": {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"bundles/template/app.css": "/bundles/template/app.11308dab.css",
"bundles/template/app.js": "/bundles/template/app.54fb8efd.js",
"bundles/template/app.js": "/bundles/template/app.9aaea0d2.js",
"bundles/template/webfont.css": "/bundles/template/webfont.eb64eebe.css",
"bundles/template/fonts/fa-solid-900.ttf": "/bundles/template/fonts/fa-solid-900.e615bbcb.ttf",
"bundles/template/fonts/fa-brands-400.ttf": "/bundles/template/fonts/fa-brands-400.b823fc0d.ttf",
Expand Down

0 comments on commit e19118c

Please sign in to comment.