-
-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils to handle chart clicks were added
- Loading branch information
Showing
10 changed files
with
306 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[ | ||
{ | ||
"path": "dist/index.js", | ||
"limit": "1.7 KB", | ||
"limit": "1.9 KB", | ||
"webpack": false, | ||
"running": false | ||
}, | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<script type="module" defer src="./index.ts"></script> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { createApp } from 'vue' | ||
import App from './src/App.vue' | ||
|
||
createApp(App).mount('#app') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"type": "module", | ||
"scripts": { | ||
"start": "vite" | ||
}, | ||
"dependencies": { | ||
"chart.js": "^4.0.0", | ||
"vue": "^3.2.31", | ||
"vue-chartjs": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@vitejs/plugin-vue": "^3.0.1", | ||
"typescript": "^4.9.3", | ||
"vite": "^3.2.4" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<template> | ||
<Chart | ||
ref="chartRef" | ||
type="bar" | ||
:data="data" | ||
:options="options" | ||
@click="onClick" | ||
/> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { ref } from 'vue' | ||
import { | ||
Chart as ChartJS, | ||
Title, | ||
Tooltip, | ||
Legend, | ||
BarElement, | ||
CategoryScale, | ||
LinearScale, | ||
InteractionItem | ||
} from 'chart.js' | ||
import { | ||
ChartComponentRef, | ||
Chart, | ||
getDatasetAtEvent, | ||
getElementAtEvent, | ||
getElementsAtEvent | ||
} from 'vue-chartjs' | ||
import * as chartConfig from './chartConfig.js' | ||
ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend) | ||
export default { | ||
name: 'App', | ||
components: { | ||
Chart | ||
}, | ||
setup() { | ||
const datasetAtEvent = (dataset: InteractionItem[]) => { | ||
if (!dataset.length) return | ||
const datasetIndex = dataset[0].datasetIndex | ||
console.log('dataset', chartConfig.data.datasets[datasetIndex].label) | ||
} | ||
const elementAtEvent = (element: InteractionItem[]) => { | ||
if (!element.length) return | ||
const { datasetIndex, index } = element[0] | ||
console.log( | ||
'element', | ||
chartConfig.data.labels[index], | ||
chartConfig.data.datasets[datasetIndex].data[index] | ||
) | ||
} | ||
const elementsAtEvent = (elements: InteractionItem[]) => { | ||
if (!elements.length) return | ||
console.log('elements', elements) | ||
} | ||
const chartRef = ref<ChartComponentRef>(null) | ||
const onClick = (event: MouseEvent) => { | ||
const { | ||
value: { chart } | ||
} = chartRef | ||
if (!chart) { | ||
return | ||
} | ||
datasetAtEvent(getDatasetAtEvent(chart, event)) | ||
elementAtEvent(getElementAtEvent(chart, event)) | ||
elementsAtEvent(getElementsAtEvent(chart, event)) | ||
} | ||
return { | ||
chartRef, | ||
onClick, | ||
...chartConfig | ||
} | ||
} | ||
} | ||
</script> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export const data = { | ||
labels: [ | ||
'January', | ||
'February', | ||
'March', | ||
'April', | ||
'May', | ||
'June', | ||
'July', | ||
'August', | ||
'September', | ||
'October', | ||
'November', | ||
'December' | ||
], | ||
datasets: [ | ||
{ | ||
label: 'Data One', | ||
backgroundColor: '#f87979', | ||
data: [40, 20, 12, 39, 10, 40, 39, 80, 40, 20, 12, 11] | ||
} | ||
] | ||
} | ||
|
||
export const options = { | ||
responsive: true, | ||
maintainAspectRatio: false | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig } from 'vite' | ||
import vue from '@vitejs/plugin-vue' | ||
|
||
export default defineConfig({ | ||
plugins: [vue()] | ||
}) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import 'chart.js/auto' | ||
import type { InteractionItem } from 'chart.js' | ||
import { ref } from 'vue' | ||
import { | ||
ChartComponentRef, | ||
Chart, | ||
getDatasetAtEvent, | ||
getElementAtEvent, | ||
getElementsAtEvent | ||
} from '../src/index.js' | ||
import * as barChartConfig from '../sandboxes/bar/src/chartConfig.js' | ||
|
||
export default { | ||
title: 'Chart', | ||
component: Chart, | ||
parameters: { | ||
layout: 'centered' | ||
} | ||
} | ||
|
||
export function Default(args) { | ||
return { | ||
components: { Chart }, | ||
template: '<Chart v-bind="args" />', | ||
setup() { | ||
return { args } | ||
} | ||
} | ||
} | ||
|
||
Default.args = { | ||
id: 'bar-chart', | ||
type: 'bar', | ||
width: 400, | ||
height: 400, | ||
...barChartConfig | ||
} | ||
|
||
export function Events(args) { | ||
return { | ||
components: { Chart }, | ||
template: '<Chart ref="chartRef" @click="onClick" v-bind="args" />', | ||
setup() { | ||
const datasetAtEvent = (dataset: InteractionItem[]) => { | ||
if (!dataset.length) return | ||
|
||
const datasetIndex = dataset[0].datasetIndex | ||
|
||
console.log('dataset', barChartConfig.data.datasets[datasetIndex].label) | ||
} | ||
|
||
const elementAtEvent = (element: InteractionItem[]) => { | ||
if (!element.length) return | ||
|
||
const { datasetIndex, index } = element[0] | ||
|
||
console.log( | ||
'element', | ||
barChartConfig.data.labels[index], | ||
barChartConfig.data.datasets[datasetIndex].data[index] | ||
) | ||
} | ||
|
||
const elementsAtEvent = (elements: InteractionItem[]) => { | ||
if (!elements.length) return | ||
|
||
console.log('elements', elements) | ||
} | ||
|
||
const chartRef = ref<ChartComponentRef>(null) | ||
|
||
const onClick = (event: MouseEvent) => { | ||
const { | ||
value: { chart } | ||
} = chartRef | ||
|
||
if (!chart) { | ||
return | ||
} | ||
|
||
datasetAtEvent(getDatasetAtEvent(chart, event)) | ||
elementAtEvent(getElementAtEvent(chart, event)) | ||
elementsAtEvent(getElementsAtEvent(chart, event)) | ||
} | ||
|
||
return { | ||
chartRef, | ||
args, | ||
onClick | ||
} | ||
} | ||
} | ||
} | ||
|
||
Events.args = { | ||
id: 'bar-chart', | ||
type: 'bar', | ||
width: 400, | ||
height: 400, | ||
...barChartConfig | ||
} |