Last active
August 16, 2022 11:41
-
-
Save nidhi-canopas/2f4bafdcdfa08129e7d38bec85f19749 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <div style="width: 800px" @mouseleave="getBackOriginalChart"> | |
| <Bar | |
| ref="chart1" | |
| :chart-data="chartData" | |
| :chart-options="{ | |
| responsive: true, | |
| maintainAspectRatio: false, | |
| plugins: { | |
| legend: { display: false }, | |
| title: { display: true, text: 'Customised chart title' }, | |
| }, | |
| onHover: (event, elements) => updateChart(elements[0]), | |
| }" | |
| /> | |
| </div> | |
| </template> | |
| <script > | |
| const chartData = { | |
| labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"], | |
| datasets: [ | |
| { | |
| label: "Dataset 1", | |
| data: [2, 5, 4, 8, 10, 4, 7], | |
| backgroundColor: "#DB4C77", | |
| }, | |
| ], | |
| }; | |
| import { Bar } from "vue-chartjs"; | |
| import { | |
| Chart as ChartJS, | |
| Title, | |
| Tooltip, | |
| Legend, | |
| BarElement, | |
| CategoryScale, | |
| LinearScale, | |
| } from "chart.js"; | |
| ChartJS.register( | |
| Title, | |
| Tooltip, | |
| Legend, | |
| BarElement, | |
| CategoryScale, | |
| LinearScale | |
| ); | |
| export default { | |
| components: { | |
| Bar, | |
| }, | |
| data() { | |
| return { | |
| chartData: chartData, | |
| }; | |
| }, | |
| methods: { | |
| updateChart(elem) { | |
| if (elem) { | |
| this.$refs.chart1.chart.data.datasets[0].backgroundColor = "#D3D3D3"; | |
| this.$refs.chart1.updateChart(); | |
| for ( | |
| let i = 0; | |
| i < this.$refs.chart1.chart.data.datasets[0].data.length; | |
| i++ | |
| ) { | |
| if (i == elem.index) { | |
| this.$refs.chart1.chart.data.datasets[0].hoverBackgroundColor = | |
| "#DB4C77"; | |
| } | |
| } | |
| } | |
| }, | |
| getBackOriginalChart() { | |
| if (this.$refs.chart1.chart.data == null) { | |
| return; | |
| } | |
| this.$refs.chart1.chart.data.datasets[0].backgroundColor = "#DB4C77"; | |
| this.$refs.chart1.updateChart(); | |
| }, | |
| }, | |
| }; | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment