Files
boursecu_user/src/views/base/home/composables/usePriceChart.ts
T
2026-06-11 21:21:15 +03:30

34 lines
1.1 KiB
TypeScript

import { ref } from 'vue'
import { getAllPrices } from '@/api/home'
export function usePriceChart() {
const priceChartSeries = ref([{ name: 'قیمت', data: [] }])
const selection = ref('DAILY')
const chartLoading = ref(false)
const toPersianNumber = (num: number) => {
return num.toString().replace(/\d/g, (d) => '۰۱۲۳۴۵۶۷۸۹'[d])
}
const fetchPrices = async () => {
try {
chartLoading.value = true
const response = await getAllPrices()
const data = response.data?.prices
const selectedData = data[selection.value] || []
priceChartSeries.value[0].data = selectedData.map((item: any) => [item.timestamp, item.value - 1])
} catch (error) {
console.error('Error fetching prices:', error)
} finally {
chartLoading.value = false
}
}
const updateChartData = (timeline: string) => {
selection.value = timeline
fetchPrices()
}
return { priceChartSeries, chartLoading, updateChartData, fetchPrices }
}