Skip to content

Commit

Permalink
Add customCanvasBackgroundColor plugin to LineChart.vue
Browse files Browse the repository at this point in the history
  • Loading branch information
devincowan committed Apr 26, 2024
1 parent 99ddc30 commit 78d856b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 13 additions & 0 deletions frontend/src/_helpers/charts/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const customCanvasBackgroundColor = {
id: 'customCanvasBackgroundColor',
beforeDraw: (chart, args, options) => {
const { ctx } = chart
ctx.save()
ctx.globalCompositeOperation = 'destination-over'
ctx.fillStyle = options.color || '#99ffff'
ctx.fillRect(0, 0, chart.width, chart.height)
ctx.restore()
}
}

export { customCanvasBackgroundColor }
8 changes: 6 additions & 2 deletions frontend/src/components/LineChart.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<v-sheet>
<v-theme-provider theme="light" with-background>
<Line :data="chartData" :options="options" ref="line" />
<Line :data="chartData" :options="options" ref="line" :plugins="plugin" />
<!-- <Line :data="props.data" :options="options" ref="line" /> -->
</v-theme-provider>
<v-btn @click="downloadChart()">Download Chart</v-btn>
Expand All @@ -24,12 +24,13 @@ import 'chartjs-adapter-date-fns';
import { enUS } from 'date-fns/locale';
import { useChartsStore } from '@/stores/charts'
import { ref, onMounted } from 'vue'
import { customCanvasBackgroundColor } from '@/_helpers/charts/plugins'
const chartStore = useChartsStore()
const props = defineProps({ data: Object, chosenVariable: Object })
const line = ref(null)
ChartJS.register(LinearScale, TimeScale, PointElement, LineElement, Title, Tooltip, Legend)
ChartJS.register(LinearScale, TimeScale, PointElement, LineElement, Title, Tooltip, Legend, customCanvasBackgroundColor)
// TODO: might need a more efficient way of doing this instead of re-mapping the data
// Ideally use the store directly instead of passing it as a prop
let chartData = ref(props.data)
Expand All @@ -54,6 +55,9 @@ const options = {
title: {
display: false,
text: 'Chart Title'
},
customCanvasBackgroundColor: {
color: 'white',
}
},
scales: {
Expand Down

0 comments on commit 78d856b

Please sign in to comment.