Skip to content

Commit

Permalink
feat: add typings
Browse files Browse the repository at this point in the history
  • Loading branch information
caro3801 committed Jun 3, 2024
1 parent ecb3102 commit 1c005bd
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/datavisualisations/BarChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import identity from 'lodash/identity'
import sortBy from 'lodash/sortBy'
import { defineComponent, computed, ref, watch } from 'vue'
import { chartProps, getChartProps, useChart } from '@/composables/chart'
import { ComponentPublicInstance } from 'vue'
type Datum = {value:number|number[],highlight?:boolean,label?:string}
type Bar = {width:number,height:number, x:number,y:number} & Datum;
export default defineComponent({
name: 'BarChart',
props: {
Expand Down Expand Up @@ -81,13 +84,12 @@ export default defineComponent({
},
...chartProps()
},
emits: ['loaded', 'resized'],
setup(props, { emit }) {
const el = ref(null)
const el = ref<ComponentPublicInstance<HTMLElement> |null>(null)
const width = ref(0)
const isLoaded = ref(false)
const { loadedData, elementsMaxBBox, dataHasHighlights, d3Formatter } =
useChart(el, getChartProps(props), { emit }, isLoaded, onResize, null)
useChart(el, getChartProps(props), { emit }, isLoaded, onResize)
// onMounted(() => {
// window.addEventListener('resize', onResize)
// onResize()
Expand All @@ -96,7 +98,7 @@ export default defineComponent({
// window.removeEventListener('resize', onResize)
// })
const sortedData = computed(() => {
const sortedData = computed(():[] => {
if (!loadedData.value) {
return []
}
Expand Down Expand Up @@ -137,13 +139,15 @@ export default defineComponent({
const scale = computed(() => {
const x = d3
.scaleLinear()
.domain([0, d3.max(sortedData.value, (d) => d.value)])
//@ts-expect-error D3 api
.domain([0, d3.max(sortedData.value, (d:Datum) => d.value)])
.range([0, padded.value.width - valueWidth.value])
return { x }
})
const bars = computed(() => {
return sortedData.value.map((d, i) => {
const bars = computed(():Bar[] => {
return sortedData.value.map((d:Datum, i) => {
return {
//@ts-expect-error D3 api
width: Math.abs(scale.value.x(d.value)),
height: Math.abs(props.barHeight),
value: d.value,
Expand All @@ -154,7 +158,7 @@ export default defineComponent({
})
})
const labels = computed(() => {
return sortedData.value.map((d, i) => {
return sortedData.value.map((d:Datum, i) => {
return {
label: d.label,
x: labelWidth.value,
Expand All @@ -166,15 +170,16 @@ export default defineComponent({
return (props.barHeight + props.barGap) * sortedData.value.length
})
function formatXDatum(d) {
function formatXDatum(d:number|number[]) {
return d3Formatter(d, props.xAxisTickFormat)
}
function onResize() {
if (el.value) {
width.value = el.value.offsetWidth
width.value = el.value?.offsetWidth
}
}
function initialize() {
// @ts-expect-error D3 api
d3.axisBottom().scale(scale.value.x)
}
Expand Down

0 comments on commit 1c005bd

Please sign in to comment.