diff --git a/src/components/Charts/BarChart.js b/src/components/Charts/BarChart.js index a7851df..62e0c6d 100644 --- a/src/components/Charts/BarChart.js +++ b/src/components/Charts/BarChart.js @@ -1,57 +1,57 @@ -import { Bar } from 'vue-chartjs'; +import { Bar, mixins } from 'vue-chartjs'; export default { name: 'bar-chart', extends: Bar, + mixins: [mixins.reactiveProp], props: { - labels: Array, - datasets: Array, - data: [Array, Object], - color: { - type: String, - default: '' - }, extraOptions: Object, - title: { - type: String, - default: '' - }, gradientColors: { type: Array, - deafault: () => ['rgba(72,72,176,0.2)', 'rgba(72,72,176,0.0)', 'rgba(119,52,169,0)'], + default: () => ['rgba(72,72,176,0.2)', 'rgba(72,72,176,0.0)', 'rgba(119,52,169,0)'], validator: val =>{ return val.length > 2; } }, + gradientStops: { + type: Array, + default: () => [1, 0.4, 0], + validator: val =>{ + return val.length > 2; + } + } + }, + methods: { + updateGradients() { + const ctx = document.getElementById(this.chartId).getContext('2d'); + const gradientStroke = ctx.createLinearGradient(0, 230, 0, 50); + + gradientStroke.addColorStop(this.gradientStops[0], this.gradientColors[0]); + gradientStroke.addColorStop(this.gradientStops[1], this.gradientColors[1]); + gradientStroke.addColorStop(this.gradientStops[2], this.gradientColors[2]); + if(this.chartData){ + this.chartData.datasets.forEach(set => { + set.backgroundColor = gradientStroke; + }); + } + } }, mounted() { - let fallBackColor = '#d048b6'; - let color = this.color || fallBackColor; - const ctx = document.getElementById(this.chartId).getContext('2d'); - const gradientStroke = ctx.createLinearGradient(0, 230, 0, 50); - - gradientStroke.addColorStop(1, this.gradientColors[0]); - gradientStroke.addColorStop(0.2, this.gradientColors[1]); - gradientStroke.addColorStop(0, this.gradientColors[2]); //purple colors - + this.updateGradients(); this.renderChart( - { - labels: this.labels || [], - datasets: this.datasets - ? this.datasets - : [{ - label: this.title || '', - fill: true, - backgroundColor: gradientStroke, - hoverBackgroundColor: gradientStroke, - borderColor: color, - borderWidth: 2, - borderDash: [], - borderDashOffset: 0.0, - data: this.data || [] - }] - }, + this.chartData, this.extraOptions ); + }, + watch: { + chartData(newVal, oldVal) { + this.updateGradients(); + if(oldVal === null) { + this.renderChart( + this.chartData, + this.extraOptions + ); + } + } } }; diff --git a/src/components/Charts/LineChart.js b/src/components/Charts/LineChart.js index 90d89f3..04761f6 100644 --- a/src/components/Charts/LineChart.js +++ b/src/components/Charts/LineChart.js @@ -1,65 +1,57 @@ -import { Line } from 'vue-chartjs'; +import { Line, mixins } from 'vue-chartjs'; export default { name: 'line-chart', extends: Line, + mixins: [mixins.reactiveProp], props: { - labels: Array, - datasets: Array, - data: [Array, Object], - color: { - type: String, - default: '' - }, extraOptions: Object, - title: { - type: String, - default: '' - }, gradientColors: { type: Array, - deafault: () => ['rgba(72,72,176,0.2)', 'rgba(72,72,176,0.0)', 'rgba(119,52,169,0)'], + default: () => ['rgba(72,72,176,0.2)', 'rgba(72,72,176,0.0)', 'rgba(119,52,169,0)'], validator: val =>{ return val.length > 2; } }, + gradientStops: { + type: Array, + default: () => [1, 0.4, 0], + validator: val =>{ + return val.length > 2; + } + } + }, + methods: { + updateGradients() { + const ctx = document.getElementById(this.chartId).getContext('2d'); + const gradientStroke = ctx.createLinearGradient(0, 230, 0, 50); + + gradientStroke.addColorStop(this.gradientStops[0], this.gradientColors[0]); + gradientStroke.addColorStop(this.gradientStops[1], this.gradientColors[1]); + gradientStroke.addColorStop(this.gradientStops[2], this.gradientColors[2]); + if(this.chartData){ + this.chartData.datasets.forEach(set => { + set.backgroundColor = gradientStroke; + }); + } + } }, mounted() { - let fallBackColor = '#d048b6'; - let color = this.color || fallBackColor; - const ctx = document.getElementById(this.chartId).getContext('2d'); - const gradientStroke = ctx.createLinearGradient(0, 230, 0, 50); - - gradientStroke.addColorStop(1, this.gradientColors[0]); - gradientStroke.addColorStop(0.2, this.gradientColors[1]); - gradientStroke.addColorStop(0, this.gradientColors[2]); //purple colors - + this.updateGradients(); this.renderChart( - { - labels: this.labels || [], - datasets: this.datasets - ? this.datasets - : [ - { - label: this.title || '', - fill: true, - backgroundColor: gradientStroke, - borderColor: color, - borderWidth: 2, - borderDash: [], - borderDashOffset: 0.0, - pointBackgroundColor: color, - pointBorderColor: 'rgba(255,255,255,0)', - pointHoverBackgroundColor: color, - pointBorderWidth: 20, - pointHoverRadius: 4, - pointHoverBorderWidth: 15, - pointRadius: 4, - data: this.data || [] - } - ] - }, + this.chartData, this.extraOptions ); + }, + watch: { + chartData(newVal, oldVal) { + this.updateGradients(); + if(oldVal === null) { + this.renderChart( + this.chartData, + this.extraOptions + ); + } + } } }; diff --git a/src/pages/Dashboard.vue b/src/pages/Dashboard.vue index 0dd34c2..7ed858c 100644 --- a/src/pages/Dashboard.vue +++ b/src/pages/Dashboard.vue @@ -1,10 +1,499 @@