Use vue-chartjs
- User
Use vue-chartjs
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Use vue-chartjs
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's how to use vue-chartjs (the official Vue wrapper for Chart.js) to create synchronized charts with zoom/pan:
npm install @vue-chartjs/core chart.js chartjs-plugin-zoom
Create src/components/SyncedCharts.vue:
<template>
<div class="chart-container">
<Bar
:data="barData"
:options="chartOptions"
ref="barChart"
/>
<Line
:data="lineData"
:options="chartOptions"
ref="lineChart"
/>
<button @click="resetZoom" class="reset-btn">
🔄 Reset Zoom
</button>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { Bar, Line } from 'vue-chartjs'
import { Chart as ChartJS, registerables } from 'chart.js'
import zoomPlugin from 'chartjs-plugin-zoom'
// Register Chart.js components and plugins
ChartJS.register(...registerables, zoomPlugin)
// Shared chart configuration
const chartOptions = reactive({
responsive: true,
maintainAspectRatio: false,
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'xy',
group: 'shared' // <-- Critical for syncing
},
zoom: {
wheel: { enabled: true },
pinch: { enabled: true },
mode: 'xy',
group: 'shared' // <-- Critical for syncing
}
}
},
scales: {
y: { beginAtZero: true }
}
})
// Bar Chart Data
const barData = reactive({
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
datasets: [{
label: 'Monthly Sales',
backgroundColor: '#42b983',
data: [65, 59, 80, 81, 56, 55]
}]
})
// Line Chart Data
const lineData = reactive({
labels: [...barData.labels],
datasets: [{
label: 'Revenue Trend',
borderColor: '#ff6384',
backgroundColor: 'rgba(255,99,132,0.2)',
data: [28, 48, 40, 19, 86, 27],
tension: 0.4
}]
})
// Chart references
const barChart = ref(null)
const lineChart = ref(null)
// Reset zoom for both charts
const resetZoom = () => {
if (barChart.value?.chart) barChart.value.chart.resetZoom()
if (lineChart.value?.chart) lineChart.value.chart.resetZoom()
}
</script>
<style scoped>
.chart-container {
width: 90%;
max-width: 800px;
margin: 20px auto;
display: flex;
flex-direction: column;
gap: 20px;
}
.reset-btn {
align-self: center;
padding: 8px 16px;
background: #35495e;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
</style>
In src/App.vue:
<template>
<div id="app">
<h1>Vue Chart.js Synchronized Zoom</h1>
<SyncedCharts />
</div>
</template>
<script setup>
import SyncedCharts from './components/SyncedCharts.vue'
</script>
<style>
#app { text-align: center; font-family: sans-serif; }
</style>
vue-chartjs Integration
Bar and Line components from vue-chartjsSynchronized Zoom/Pan
group: 'shared' in zoom configReset Functionality
npm run dev
You should now see two synchronized charts that:
vue-chartjs Advantages:
Synchronization:
The group: 'shared' parameter in the zoom plugin config tells Chart.js to synchronize all charts with the same group ID.
Let me know if you need to add more chart types or customize the interaction!