-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Adjust chartjs implementation (#218)
* [TASK] Make chartjs usable without endpoint * [BUGFIX] Make requests work again
- Loading branch information
1 parent
5a1ccf4
commit e19118c
Showing
7 changed files
with
52 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters