From 462439a5ab121e2b91bcdc32826c71f430228f76 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 3 Dec 2017 09:27:54 +0100 Subject: [PATCH] Better chart display for #1034 --- public/js/ff/charts.defaults.js | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/public/js/ff/charts.defaults.js b/public/js/ff/charts.defaults.js index 04ab8201ad..a3395ceac0 100644 --- a/public/js/ff/charts.defaults.js +++ b/public/js/ff/charts.defaults.js @@ -20,6 +20,60 @@ /** global: accounting */ + +/** + * Takes a string phrase and breaks it into separate phrases no bigger than 'maxwidth', breaks are made at complete words. + * https://stackoverflow.com/questions/21409717/chart-js-and-long-labels + * + * @param str + * @param maxwidth + * @returns {Array} + */ +function formatLabel(str, maxwidth){ + var sections = []; + var words = str.split(" "); + var temp = ""; + + words.forEach(function(item, index){ + if(temp.length > 0) + { + var concat = temp + ' ' + item; + + if(concat.length > maxwidth){ + sections.push(temp); + temp = ""; + } + else{ + if(index === (words.length-1)) + { + sections.push(concat); + return; + } + else{ + temp = concat; + return; + } + } + } + + if(index === (words.length-1)) + { + sections.push(item); + return; + } + + if(item.length < maxwidth) { + temp = item; + } + else { + sections.push(item); + } + + }); + + return sections; +} + var defaultChartOptions = { elements: { line: { @@ -31,6 +85,12 @@ var defaultChartOptions = { { gridLines: { display: false + }, + ticks: { + // Include a dollar sign in the ticks + callback: function (value, index, values) { + return formatLabel(value, 20); + } } } ],