
| Current Path : /var/www/html/rocksensor/web/modules/contrib/charts/modules/charts_chartjs/js/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/rocksensor/web/modules/contrib/charts/modules/charts_chartjs/js/charts_chartjs.js |
/**
* @file
* JavaScript integration between Chart.js and Drupal.
*/
(function (Drupal, once) {
'use strict';
function copyAttributes(source, target) {
return Array.from(source.attributes).forEach(attribute => {
target.setAttribute(
attribute.nodeName === 'id' ? 'data-id' : attribute.nodeName,
attribute.nodeValue,
);
});
}
Drupal.behaviors.chartsChartjs = {
attach: function (context) {
const contents = new Drupal.Charts.Contents();
once('load-charts-chartjs', '.charts-chartjs', context).forEach(function (element) {
const chartId = element.id;
// Switching div for canvas element.
const parent = element.parentNode;
const canvas = document.createElement('canvas');
// Transferring the attributes of our source element to the canvas.
copyAttributes(element, canvas);
canvas.id = chartId;
parent.replaceChild(canvas, element);
// Initializing the chart item.
const chart = contents.getData(chartId);
const options = chart['options'];
const enabled_plugins = [];
// If options.plugins.dataLabels.display is set to true, we need to add the plugin to the chart.
if (options.plugins && options.plugins.dataLabels && options.plugins.dataLabels.display) {
enabled_plugins.push(ChartDataLabels);
}
new Chart(chartId, {
type: chart['type'],
data: chart['data'],
plugins: enabled_plugins,
options: options,
});
if (canvas.nextElementSibling && canvas.nextElementSibling.hasAttribute('data-charts-debug-container')) {
canvas.nextElementSibling.querySelector('code').innerText = JSON.stringify(chart, null, ' ');
}
});
}
};
}(Drupal, once));