Files
myprojplanet_vite/src/components/CLineChart/CLineChart.ts

215 lines
5.6 KiB
TypeScript
Raw Normal View History

import Vue, { computed, defineComponent, onMounted, ref } from 'vue'
import { tools } from '../../store/Modules/tools'
import { useUserStore } from '@store/UserStore'
import { useRouter } from 'vue-router'
import { useGlobalStore } from '@store/globalStore'
import { useI18n } from '@/boot/i18n'
import { useQuasar } from 'quasar'
import { Chart, ChartItem, ChartConfiguration } from 'chart.js'
export default defineComponent({
2024-02-06 20:13:06 +01:00
name: 'CLineChart',
components: {},
props: {
mydata: { required: false, default: [] },
title: { required: false, default: false },
sum: { required: false, default: false },
color: { required: false, default: 'red' },
bordercolor: { required: false, default: 'red' },
mycolors: { required: false, default: null },
offset: { required: false, default: 0 },
2024-02-06 20:13:06 +01:00
showMedia: { required: false, default: false }
},
setup(props, { emit }) {
const userStore = useUserStore()
const globalStore = useGlobalStore()
const { t } = useI18n()
const q = useQuasar()
const myarrlabel = ref(<any>[])
const myarrdata = ref(<any>[])
2024-02-06 20:13:06 +01:00
const myarrdataLine = ref(<any>[])
2022-03-10 23:20:09 +01:00
const myarrbg = ref(<any>[])
const myarrsum = ref(<any>[])
2024-02-06 20:13:06 +01:00
const chartDataLine = ref({
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
2024-02-06 20:13:06 +01:00
label: 'Data One',
backgroundColor: '#f87979',
data: [40, 39, 10, 40, 39, 80, 40]
}
2024-02-06 20:13:06 +01:00
]
})
2024-02-06 20:13:06 +01:00
const chartOptionsLine = ref({
responsive: true,
maintainAspectRatio: false
})
2024-02-06 20:13:06 +01:00
function calcolaMedia(myarray: any, periodi: number) {
if (myarray.length === 0 || myarray.length < periodi) {
return 0; // Se l'array è vuoto o più corto del numero di periodi, la media è 0.
}
const somma = myarray.slice(-periodi).reduce((accumulator: number, currentValue: number) => accumulator + currentValue, 0);
return somma / periodi;
}
function getRecordByDate(mydata: any, currentDate: Date) {
const targetDate = currentDate.toISOString().split('T')[0];
return mydata.find((record: any) => record._id === targetDate);
}
function mounted() {
myarrdata.value = []
2024-02-06 20:13:06 +01:00
myarrdataLine.value = []
2022-03-10 23:20:09 +01:00
myarrbg.value = []
myarrlabel.value = []
myarrsum.value = []
let somma = 0
if (props.sum)
somma = props.offset
let rec: any
let ind = ''
2022-03-10 23:20:09 +01:00
let num = 1
2024-02-06 20:13:06 +01:00
let mostraggtutti = true
if (mostraggtutti) {
let num = 1;
let strstartDate: any = props.mydata[0]
let startDate = new Date(strstartDate._id); // Data di inizio da props
let endDate = new Date(); // Data di fine da props
let currentDate = new Date(startDate);
// console.log('startDate', startDate, 'endDate', endDate)
while (currentDate <= endDate) {
let dataPresente = getRecordByDate(props.mydata, currentDate)
let count = dataPresente ? dataPresente.count : 0;
let day = currentDate.toISOString().split('T')[0].split('-');
let mydate = day[2] + '/' + day[1];
myarrlabel.value.push(mydate);
myarrdata.value.push(count);
let media = calcolaMedia(myarrdata.value, 30);
myarrdataLine.value.push(media ? media : undefined);
if (currentDate === endDate) {
myarrbg.value.push(tools.colourNameToHex('green'));
} else {
myarrbg.value.push(tools.colourNameToHex(props.color));
}
myarrsum.value.push(somma);
num++;
currentDate.setDate(currentDate.getDate() + 1); // Passa al giorno successivo
}
2024-02-06 20:13:06 +01:00
} else {
for (rec of props.mydata) {
if (props.sum) {
somma += rec.count
} else {
somma = rec.count
}
let day = rec._id.split('-')
ind = day[2] + '/' + day[1]
//myarrlabel.value.push(rec._id)
myarrlabel.value.push(ind)
myarrdata.value.push(rec.count)
let media = calcolaMedia(myarrdata.value, 14)
myarrdataLine.value.push(media ? media : undefined)
if (num === props.mydata.length) {
myarrbg.value.push(tools.colourNameToHex('green'))
} else {
myarrbg.value.push(tools.colourNameToHex(props.color))
}
myarrsum.value.push(somma)
num++
// ind++
2022-03-10 23:20:09 +01:00
}
}
2024-02-06 20:13:06 +01:00
const chartData: any = {
labels: myarrlabel.value,
datasets: [
{
type: 'bar',
label: props.title,
data: myarrdata.value,
borderColor: tools.colourNameToHex('red'),
backgroundColor: myarrbg.value,
fill: true,
},
],
}
const media = {
type: 'line',
label: 'Media',
data: myarrdataLine.value,
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgb(255, 99, 132)',
fill: true,
}
if (props.showMedia)
chartData.datasets.push(media)
const configBar: ChartConfiguration = {
type: 'bar',
data: chartData,
options: {
scales: {
y: {
beginAtZero: true
}
},
interaction: {
intersect: false
},
},
};
const canvasTag = <ChartItem>document.getElementById('myChart')
const myChart = new Chart(
canvasTag,
configBar)
}
function getoffset() {
return props.offset
}
onMounted(mounted)
return {
tools,
getoffset,
q,
}
},
})