claude-sonnet-4-20250514· Anthropic LMArena Human Preference 140K
Use vue-chartjs
- User
Use vue-chartjs
- Assistant
Great choice! vue-chartjs is a Vue.js wrapper for Chart.js that provides a more Vue-friendly way to work with charts. Here's how to create synchronized charts using vue-chartjs:
🚀 Step-by-Step Guide: Synchronized Charts with vue-chartjs
✅ Prerequisites
First, install the required packages:
npm install vue-chartjs chart.js chartjs-plugin-zoom
1. 🔧 Create Base Chart Components
First, let's create reusable chart components using vue-chartjs.
src/components/BaseBarChart.vue<template> <Bar ref="chartRef" :data="chartData" :options="chartOptions" /> </template> <script setup> import { ref, computed, watch } from 'vue' import { Bar } from 'vue-chartjs' import Chart from 'chart.js/auto' import zoomPlugin from 'chartjs-plugin-zoom' // Register the zoom plugin Chart.register(zoomPlugin) const props = defineProps({ data: { type: Object, required: true }, zoomOptions: { type: Object, default: () => ({}) } }) const emit = defineEmits(['zoom', 'pan']) const chartRef = ref(null) const chartData = computed(() => props.data) const chartOptions = computed(() => ({ responsive: true, maintainAspectRatio: false, scales: { x: { // Will be updated dynamically for synchronization }, y: { beginAtZero: true } }, plugins: { zoom: { pan: { enabled: true, mode: 'x', onPan: ({ chart }) => { emit('pan', { min: chart.scales.x.min, max: chart.scales.x.max }) } }, zoom: { wheel: { enabled: true }, pinch: { enabled: true }, mode: 'x', onZoom: ({ chart }) => { emit('zoom', { min: chart.scales.x.min, max: chart.scales.x.max }) } }, ...props.zoomOptions } } })) // Method to update zoom from external source const updateZoom = (min, max) => { if (chartRef.value?.chart) { chartRef.value.chart.options.scales.x.min = min chartRef.value.chart.options.scales.x.max = max chartRef.value.chart.update('none') } } const resetZoom = () => { if (chartRef.value?.chart) { chartRef.value.chart.resetZoom() } } // Expose methods to parent component defineExpose({ updateZoom, resetZoom, chart: computed(() => chartRef.value?.chart) }) </script>src/components/BaseLineChart.vue<template> <Line ref="chartRef" :data="chartData" :options="chartOptions" /> </template> <script setup> import { ref, computed } from 'vue' import { Line } from 'vue-chartjs' import Chart from 'chart.js/auto' import zoomPlugin from 'chartjs-plugin-zoom' // Register the zoom plugin Chart.register(zoomPlugin) const props = defineProps({ data: { type: Object, required: true }, zoomOptions: { type: Object, default: () => ({}) } }) const emit = defineEmits(['zoom', 'pan']) const chartRef = ref(null) const chartData = computed(() => props.data) const chartOptions = computed(() => ({ responsive: true, maintainAspectRatio: false, scales: { x: { // Will be updated dynamically for synchronization }, y: { beginAtZero: true } }, plugins: { zoom: { pan: { enabled: true, mode: 'x', onPan: ({ chart }) => { emit('pan', { min: chart.scales.x.min, max: chart.scales.x.max }) } }, zoom: { wheel: { enabled: true }, pinch: { enabled: true }, mode: 'x', onZoom: ({ chart }) => { emit('zoom', { min: chart.scales.x.min, max: chart.scales.x.max }) } }, ...props.zoomOptions } } })) // Method to update zoom from external source const updateZoom = (min, max) => { if (chartRef.value?.chart) { chartRef.value.chart.options.scales.x.min = min chartRef.value.chart.options.scales.x.max = max chartRef.value.chart.update('none') } } const resetZoom = () => { if (chartRef.value?.chart) { chartRef.value.chart.resetZoom() } } // Expose methods to parent component defineExpose({ updateZoom, resetZoom, chart: computed(() => chartRef.value?.chart) }) </script>
2. 🔄 Create the Synchronized Dashboard Component
src/components/SynchronizedDashboard.vue<template> <div class="dashboard"> <h1>Synchronized Charts with vue-chartjs</h1> <!-- Chart 1: Sales (Bar Chart) --> <div class="chart-wrapper"> <h2>Monthly Sales</h2> <BaseBarChart ref="barChartRef" :data="salesData" @zoom="onChartInteraction" @pan="onChartInteraction" /> </div> <!-- Chart 2: Website Visits (Line Chart) --> <div class="chart-wrapper"> <h2>Website Visits</h2> <BaseLineChart ref="lineChartRef" :data="visitsData" @zoom="onChartInteraction" @pan="onChartInteraction" /> </div> <div class="controls"> <button @click="resetAllZooms" class="reset-button"> Reset All Zooms </button> <button @click="zoomToRange" class="zoom-button"> Zoom to Q2 (Apr-Jun) </button> </div> </div> </template> <script setup> import { ref, reactive } from 'vue' import BaseBarChart from './BaseBarChart.vue' import BaseLineChart from './BaseLineChart.vue' // Chart refs const barChartRef = ref(null) const lineChartRef = ref(null) // Shared labels const labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] // Chart data const salesData = reactive({ labels: labels, datasets: [{ label: 'Sales ($k)', data: [40, 20, 60, 50, 30, 70, 45, 80, 55, 65, 35, 90], backgroundColor: '#41B883', borderColor: '#369870', borderWidth: 1 }] }) const visitsData = reactive({ labels: labels, datasets: [{ label: 'Visits (k)', data: [10, 15, 8, 25, 18, 30, 22, 40, 28, 35, 15, 50], borderColor: '#00D8FF', backgroundColor: 'rgba(0, 216, 255, 0.1)', tension: 0.1, fill: true }] }) // Synchronization logic const onChartInteraction = (scaleInfo) => { const { min, max } = scaleInfo // Update all other charts with the new scale if (barChartRef.value) { barChartRef.value.updateZoom(min, max) } if (lineChartRef.value) { lineChartRef.value.updateZoom(min, max) } } // Reset all charts const resetAllZooms = () => { if (barChartRef.value) { barChartRef.value.resetZoom() } if (lineChartRef.value) { lineChartRef.value.resetZoom() } } // Programmatic zoom example const zoomToRange = () => { // Zoom to show April to June (indices 3-5) const min = 3 const max = 5 if (barChartRef.value) { barChartRef.value.updateZoom(min, max) } if (lineChartRef.value) { lineChartRef.value.updateZoom(min, max) } } </script> <style scoped> .dashboard { padding: 20px; max-width: 1200px; margin: 0 auto; } .chart-wrapper { width: 100%; height: 300px; margin-bottom: 30px; border: 1px solid #e0e0e0; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .chart-wrapper h2 { margin-top: 0; margin-bottom: 15px; color: #333; font-size: 1.2em; } .controls { display: flex; gap: 10px; justify-content: center; margin-top: 20px; } .reset-button, .zoom-button { padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; font-size: 0.9em; transition: all 0.2s ease; } .reset-button { background-color: #dc3545; color: white; } .reset-button:hover { background-color: #c82333; } .zoom-button { background-color: #28a745; color: white; } .zoom-button:hover { background-color: #218838; } </style>
3. 🔌 Update Your Main App
src/App.vue<template> <div id="app"> <SynchronizedDashboard /> </div> </template> <script setup> import SynchronizedDashboard from './components/SynchronizedDashboard.vue' </script> <style> #app { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: #2c3e50; background-color: #f8f9fa; min-height: 100vh; } * { box-sizing: border-box; } body { margin: 0; padding: 0; } </style>
4. 🏃 Run Your App
npm run devNow you have:
- Synchronized zoom and pan between the bar chart and line chart
- Reset functionality to return both charts to their original view
- Programmatic zoom example (zoom to Q2 button)
- Clean Vue.js architecture using vue-chartjs
🧠 Key Benefits of Using vue-chartjs
- Vue-friendly API: Uses Vue components instead of direct Chart.js instantiation
- Reactive data: Chart data updates automatically when your Vue reactive data changes
- Better component composition: Easier to create reusable chart components
- Template refs: Access chart instances through Vue's ref system
- Event handling: Use Vue's event system (
@zoom,@pan) for better integration
🎯 Features You Can Add Next
- Multiple chart types: Add pie charts, doughnut charts, etc.
- Real-time data: Connect to WebSocket or API for live updates
- Chart themes: Add dark/light mode toggle
- Export functionality: Add buttons to export charts as images
- Advanced synchronization: Sync Y-axis scales for related data
Let me know if you'd like to implement any of these additional features!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.