/** * * ----------------------------------------------------------------------------- * A button with a built-in loading spinner. * * @type {Component} * * ----------------------------------------------------------------------------- */ parasails.registerComponent('barChart', { // ╔═╗╦═╗╔═╗╔═╗╔═╗ // ╠═╝╠╦╝║ ║╠═╝╚═╗ // ╩ ╩╚═╚═╝╩ ╚═╝ props: [ 'type', // Required: The type of bar chart to display. either 'stacked' (values are combined onto a single line) or 'divided' (each value is displayed as a seperate line) 'chartData', // Required: an array of objects, each containing a 'label', 'percent', and 'color' 'title', // Required: the title of the chart 'subtitle', // Optional: if provided, a subtitle will be added the chart 'maxRange', // Required for 'divided' type, the lowest number for the scale to display 'minRange', // Required for 'divided' type, the highest number for the scale to display 'incrementScaleBy', // Optional: if provided the scale will increment by this number, otherwise the scale will increment by 1, 2, 5, or 10 ], // ╦╔╗╔╦╔╦╗╦╔═╗╦ ╔═╗╔╦╗╔═╗╔╦╗╔═╗ // ║║║║║ ║ ║╠═╣║ ╚═╗ ║ ╠═╣ ║ ║╣ // ╩╝╚╝╩ ╩ ╩╩ ╩╩═╝ ╚═╝ ╩ ╩ ╩ ╩ ╚═╝ data: function (){ let range = this.maxRange - this.minRange; let incrementBy = undefined; // Determine how the range should increment, if a number was provided as a prop (`incrementScaleBy`), // we'll use that, otherwise we'll set this value based on the range of the bar chart if(this.incrementScaleBy){ incrementBy = this.incrementScaleBy; } else if (range >= 50) { incrementBy = 10; } else if (range >= 20) { incrementBy = 5; } else if(range > 10) { incrementBy = 2; } else { incrementBy = 1; } return { range, incrementBy, chartScale: undefined, //… }; }, // ╦ ╦╔╦╗╔╦╗╦ // ╠═╣ ║ ║║║║ // ╩ ╩ ╩ ╩ ╩╩═╝ template: `
{{title}} {{subtitle}}
{{item.percent}}% {{item.label}}
{{title}} {{subtitle}}
{{item.percent}}% {{item.label}}
{{value}}%
`, // ╦ ╦╔═╗╔═╗╔═╗╦ ╦╔═╗╦ ╔═╗ // ║ ║╠╣ ║╣ ║ ╚╦╝║ ║ ║╣ // ╩═╝╩╚ ╚═╝╚═╝ ╩ ╚═╝╩═╝╚═╝ beforeMount: function() { }, mounted: async function() { if(this.type === undefined){ throw new Error('Incomplete usage of : Please provide a `type`, either "divided" or "stacked". For example: ``'); } else if (this.type !== 'divided' && this.type !== 'stacked'){ throw new Error(' received an invalid `type`. `type` should be either "divided" or "stacked"'); } if(this.chartData === undefined){ throw new Error('Incomplete usage of : Please provide an array of objects as `chartData`. For example: ``'); } else if (!_.isArray(this.chartData)){ throw new Error(' received an invalid `chartData`. `chartData` should be an array of objects. Each object should containing a `label` (string), `percent` (string), and `color` (string).'); } if(this.title === undefined){ throw new Error('Incomplete usage of : Please provide a `title`. For example: ``'); } if(this.type === 'divided') { if(this.maxRange === undefined) { throw new Error('Incomplete usage of : When using the `divided` type a `maxRange` is required. For example: ``'); } if(this.minRange === undefined) { throw new Error('Incomplete usage of : When using the `divided` type a `minRange` is required. For example: ``'); } } // Adjusting the range for divided bar charts if(this.type === 'divided') { if(this.maxRange && this.minRange){ this.chartScale = Array.from({length: ((this.range)/this.incrementBy + 1)}, (_, i) => (i * this.incrementBy) + parseInt(this.minRange)); } } }, watch: { type: function(unused) { throw new Error('Changes to `type` are not currently supported in !'); }, chartData: function(unused) { throw new Error('Changes to `chartData` are not currently supported in !'); }, title: function(unused) { throw new Error('Changes to `title` are not currently supported in !'); }, subtitle: function(unused) { throw new Error('Changes to `subtitle` are not currently supported in !'); }, maxRange: function(unused) { throw new Error('Changes to `maxRange` are not currently supported in !'); }, minRange: function(unused) { throw new Error('Changes to `minRange` are not currently supported in !'); }, incrementScaleBy: function(unused) { throw new Error('Changes to `incrementScaleBy` are not currently supported in !'); }, }, beforeDestroy: function() { //… }, // ╦╔╗╔╔╦╗╔═╗╦═╗╔═╗╔═╗╔╦╗╦╔═╗╔╗╔╔═╗ // ║║║║ ║ ║╣ ╠╦╝╠═╣║ ║ ║║ ║║║║╚═╗ // ╩╝╚╝ ╩ ╚═╝╩╚═╩ ╩╚═╝ ╩ ╩╚═╝╝╚╝╚═╝ methods: { } });