Skip to content

Instantly share code, notes, and snippets.

@heathdutton
Last active December 21, 2020 02:13
Show Gist options
  • Save heathdutton/23df219217cdac46683ee2b13b5b0673 to your computer and use it in GitHub Desktop.
Save heathdutton/23df219217cdac46683ee2b13b5b0673 to your computer and use it in GitHub Desktop.
Add sums to all charts in Mautic.
// Add sums to all chart labels in Mautic.
Mautic.chartLabelSum = function () {
if (
typeof Mautic.chartObjects !== 'undefined'
&& Mautic.chartObjects.length
) {
var total, numeric, updated, totalStr;
mQuery.each(Mautic.chartObjects, function (i, chart) {
updated = false;
if (
typeof chart.data.datasets !== 'undefined'
&& (
typeof chart.sumProcessed === 'undefined'
|| chart.sumProcessed === false
)
) {
mQuery.each(chart.data.datasets, function (i, dataset) {
total = 0;
numeric = false;
if (
typeof dataset.data !== 'undefined'
&& typeof dataset.label !== 'undefined'
) {
mQuery.each(dataset.data, function (i, value) {
if (mQuery.isNumeric(value)) {
numeric = true;
total += Number(value);
}
});
if (numeric) {
total = +total.toFixed(2);
totalStr = ' (' + total + ')';
if (dataset.label.indexOf(totalStr) === -1) {
dataset.label += totalStr;
updated = true;
}
}
}
});
}
if (updated) {
chart.update();
}
chart.sumProcessed = true;
});
}
};
mQuery(document).ready(function () {
Mautic.chartLabelSum();
});
mQuery(document).ajaxComplete(function (event, xhr, settings) {
setTimeout(function () {
Mautic.chartLabelSum();
}, 150);
});
@heathdutton
Copy link
Author

screen shot 2018-09-07 at 12 41 29 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment