Improve charts and dynamic chart updates with gradients

This commit is contained in:
cristij 2018-09-02 15:26:30 +03:00
parent 5ea2826db0
commit e3105b4ad2
3 changed files with 36 additions and 39 deletions

View File

@ -21,37 +21,34 @@ export default {
} }
} }
}, },
data() {
return {
ctx: null
};
},
methods: { methods: {
updateGradients() { updateGradients(chartData) {
const ctx = document.getElementById(this.chartId).getContext('2d'); if(!chartData) return;
const ctx = this.ctx || document.getElementById(this.chartId).getContext('2d');
const gradientStroke = ctx.createLinearGradient(0, 230, 0, 50); const gradientStroke = ctx.createLinearGradient(0, 230, 0, 50);
gradientStroke.addColorStop(this.gradientStops[0], this.gradientColors[0]); gradientStroke.addColorStop(this.gradientStops[0], this.gradientColors[0]);
gradientStroke.addColorStop(this.gradientStops[1], this.gradientColors[1]); gradientStroke.addColorStop(this.gradientStops[1], this.gradientColors[1]);
gradientStroke.addColorStop(this.gradientStops[2], this.gradientColors[2]); gradientStroke.addColorStop(this.gradientStops[2], this.gradientColors[2]);
if(this.chartData){ chartData.datasets.forEach(set => {
this.chartData.datasets.forEach(set => {
set.backgroundColor = gradientStroke; set.backgroundColor = gradientStroke;
}); });
} }
}
}, },
mounted() { mounted() {
this.updateGradients(); this.$watch('chartData', (newVal, oldVal) => {
this.renderChart( this.updateGradients(this.chartData);
this.chartData, if (oldVal === null) {
this.extraOptions
);
},
watch: {
chartData(newVal, oldVal) {
this.updateGradients();
if(oldVal === null) {
this.renderChart( this.renderChart(
this.chartData, this.chartData,
this.extraOptions this.extraOptions
); );
} }
} }, { immediate: true });
} }
}; };

View File

@ -9,49 +9,46 @@ export default {
gradientColors: { gradientColors: {
type: Array, type: Array,
default: () => ['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 =>{ validator: val => {
return val.length > 2; return val.length > 2;
} }
}, },
gradientStops: { gradientStops: {
type: Array, type: Array,
default: () => [1, 0.4, 0], default: () => [1, 0.4, 0],
validator: val =>{ validator: val => {
return val.length > 2; return val.length > 2;
} }
} }
}, },
data() {
return {
ctx: null
};
},
methods: { methods: {
updateGradients() { updateGradients(chartData) {
const ctx = document.getElementById(this.chartId).getContext('2d'); if(!chartData) return;
const ctx = this.ctx || document.getElementById(this.chartId).getContext('2d');
const gradientStroke = ctx.createLinearGradient(0, 230, 0, 50); const gradientStroke = ctx.createLinearGradient(0, 230, 0, 50);
gradientStroke.addColorStop(this.gradientStops[0], this.gradientColors[0]); gradientStroke.addColorStop(this.gradientStops[0], this.gradientColors[0]);
gradientStroke.addColorStop(this.gradientStops[1], this.gradientColors[1]); gradientStroke.addColorStop(this.gradientStops[1], this.gradientColors[1]);
gradientStroke.addColorStop(this.gradientStops[2], this.gradientColors[2]); gradientStroke.addColorStop(this.gradientStops[2], this.gradientColors[2]);
if(this.chartData){ chartData.datasets.forEach(set => {
this.chartData.datasets.forEach(set => {
set.backgroundColor = gradientStroke; set.backgroundColor = gradientStroke;
}); });
} }
}
}, },
mounted() { mounted() {
this.updateGradients(); this.$watch('chartData', (newVal, oldVal) => {
this.renderChart( this.updateGradients(this.chartData);
this.chartData, if (oldVal === null) {
this.extraOptions
);
},
watch: {
chartData(newVal, oldVal) {
this.updateGradients();
if(oldVal === null) {
this.renderChart( this.renderChart(
this.chartData, this.chartData,
this.extraOptions this.extraOptions
); );
} }
} }, { immediate: true });
} }
}; };

View File

@ -29,6 +29,7 @@
</template> </template>
<div class="chart-area"> <div class="chart-area">
<line-chart style="height: 100%" <line-chart style="height: 100%"
ref="bigChart"
:chart-data="bigLineChart.chartData" :chart-data="bigLineChart.chartData"
:gradient-color="bigLineChart.gradientColors" :gradient-color="bigLineChart.gradientColors"
:gradient-stops="bigLineChart.gradientStops" :gradient-stops="bigLineChart.gradientStops"
@ -223,7 +224,7 @@
}, },
methods: { methods: {
initBigChart(index) { initBigChart(index) {
this.bigLineChart.chartData = { let chartData = {
datasets: [{ datasets: [{
label: "My First dataset", label: "My First dataset",
fill: true, fill: true,
@ -242,6 +243,8 @@
}], }],
labels: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'], labels: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
} }
this.$refs.bigChart.updateGradients(chartData);
this.bigLineChart.chartData = chartData;
this.bigLineChart.activeIndex = index; this.bigLineChart.activeIndex = index;
} }
}, },