Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Created August 17, 2022 03:47
Show Gist options
  • Select an option

  • Save nidhi-canopas/b701ea7bf9e100b908cd3255fb9363d1 to your computer and use it in GitHub Desktop.

Select an option

Save nidhi-canopas/b701ea7bf9e100b908cd3255fb9363d1 to your computer and use it in GitHub Desktop.
Medium like bar chart with customization
<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' },
tooltip: {
callbacks: {
labelColor: function (context) {
return {
backgroundColor: 'green',
};
},
labelTextColor: function (context) {
return 'yellow';
},
},
enabled: false,
},
},
scales: {
xAxis: {
ticks: {
display: false,
},
},
},
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