All files / src/components/UI/aboutportal line-chart.vue

0% Statements 0/60
0% Branches 0/10
0% Functions 0/25
0% Lines 0/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
<template>
    <v-card class="h-100 ">
        <Line
            v-if="data!==null"
            :data="data"
            ref ='linear'
            :options="options"
        />
    </v-card>
</template>
 
<script>
 
import moment from 'moment'
import {computed,ref} from 'vue'
import { usePortalFilterStore } from '@/store/AboutPortalFilter'
import {formatComa, formatDash} from '@/utils/universal_functions'
import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend,
  TimeScale
} from 'chart.js'
import { Line } from 'vue-chartjs'
import ChartDataLabels from 'chartjs-plugin-datalabels'
import 'chartjs-adapter-moment'
ChartJS.register(
  ChartDataLabels,
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend,
  TimeScale
)
 
export default {
 
    components: {
        Line,
    },

    props: {
 
        dataLineChart: {
            type: Object,
            default: () => {}
        },
 
        period: {
            type: Object,
            default: () => {}
        },
 
        filter: {
            type: String,
            default: 'd'
        }
    }, 
 
    setup(props, {emit}) {
        const filterStore = usePortalFilterStore()
        const between = ref(0)
        const arrlines = {
            datasets: [
                {
                    label: 'Открыто страниц',
                    backgroundColor: '#C62828',
                    borderColor: '#C62828',
                    borderWidth: 3,
                    cubicInterpolationMode: 'monotone',
                    data: [], 
                    func: (val)=>{return val.count},
                    datakey:  "count",                     
                    tooltips: false   ,
                    hidden: true,
                    yAxisID: 'y'             
                },
                {
                    label: 'Активные пользователи',
                    backgroundColor: 'teal',
                    borderColor: 'teal',
                    borderWidth: 3,
                    cubicInterpolationMode: 'monotone',
                    data: [],
                    func: (val)=>{return val.unic.length},
                    datakey:  "unic", 
                    customlabel: [],
                    tooltips: true,
                    yAxisID: 'y'
                }
                ,
                {
                    label: 'CRR',
                    backgroundColor: '#673AB7',
                    borderColor: '#673AB7',
                    borderWidth: 3,
                    cubicInterpolationMode: 'monotone',
                    data: [],
                    func: (val, key)=>{ return  parseFloat(crrfunc(key).toFixed(2))},
                    hidden: true,
                    datakey:  "crr", 
                    customlabel: [],
                    tooltips: true,
                    yAxisID: 'y1'
                }
                ,
                {
                    label: 'BR',
                    backgroundColor: '#283593',
                    borderColor: '#283593',
                    borderWidth: 3,
                    cubicInterpolationMode: 'monotone',
                    data: [],
                    func: (val)=>{return parseFloat((val.br/val.unic.length*100).toFixed(2))},
                    datakey:  "br", 
                    hidden: true,
                    customlabel: [],
                    tooltips: true,
                    yAxisID: 'y1'
                }
                ,
                {
                    label: 'PPV',
                    backgroundColor: '#EF9A9A',
                    borderColor: '#EF9A9A',
                    borderWidth: 3,
                    cubicInterpolationMode: 'monotone',
                    data: [],
                    func: (val)=>{return parseFloat((val.pages/val.unic.length).toFixed(2))},
                    datakey:  "ppv", 
                    hidden: true,
                    customlabel: [],
                    tooltips: true,
                    yAxisID: 'y'
                }
            ]
        }
 
    const dataf=computed({get() {
        fullpercreate()        
        let data1 = moment(filterStore.date_period.enddate)
        let data2 = moment(filterStore.date_period.startdate)
        between.value = data1.diff(data2, 'days') + 1
        let res = null
 
        if (Object.keys(props.dataLineChart).length>0){
            res = arrlines
            res.labels=[]
            res.datasets.forEach((e) => {e.data =[]})
                for(let [key,val] of Object.entries(props.dataLineChart)) {
                    res.datasets.forEach((e) => {
                        e.data.push(e.func(val,key))          
                    });
                    res.labels.push(formatComa(key))
                }
            }
        return  res
    }})
       
    const  fullper = ref([])

    const fullpercreate = () => {
        let arr = []
        props.period.period.forEach((e) => {
            let ind = arr.findIndex(el => el.visitdate == e.visitdate)

            if (ind ==-1) {
                arr.push({visitdate: e.visitdate, user: Array.from(new Set([e.user.id]))})
            }
            else {
                arr[ind].user = Array.from(new Set(arr[ind].user).add(e.user.id))
            }
        })    
 
        fullper.value= [...props.period.before, ...arr]
    }
  
    const crrfunc = (date)=>{
     /**
     * Расчет значения crr, и передача в родительский компонент значения, на последнюю дату в периоде для компонента card-total
     * @constructor
     * @param {object} date - по каждому дню, в указанном периоде (пример - 2024-03-22)
     * @description Для расчетного периода с 04.03.2024 по 20.03.2024 выбирается три периода:
     * 30.01.2024 - 16.02.2024 - baseunic
     * 17.02.2024 - 03.03.2024 - crrunic
     * 04.03.2024 - 20.03.2024 - отсюда даты для расчета
     * bothuser - Пользователи которые были в baseunic и crrunic
     */
        
        let baseunic = unicperson(fullper.value.filter(e => new Date(e.visitdate) >moment(new Date(date)).subtract(between.value*2, 'd') &&  new Date(e.visitdate) <= moment(new Date(date)).subtract(between.value, 'd')))
        let crrunic = unicperson(fullper.value.filter(e => new Date(e.visitdate) >moment(new Date(date)).subtract(between.value, 'd') &&  new Date(e.visitdate) <= new Date(date)))
        let bothuser = new Set([...baseunic].filter(element => crrunic.has(element)))
      
        if (date ==formatDash(filterStore.date_period.enddate)) {
            editCRR(bothuser.size*100/baseunic.size)
        }
    
        return baseunic.size ==0?0:bothuser.size*100/baseunic.size 
    }  
 
    const editCRR  = (val) => {
      
        emit('getCRR', val)
    }
    
    const unicperson = (arr) => {
        
        let arrnew =[]
        arr.forEach(e => arrnew.push(...e.user) )
        return new Set(arrnew)
    }

    const data=computed({get() {
   
        return JSON.parse(JSON.stringify(dataf.value))
    }
        })
    const linear=ref(null)

 

    const options =  {

        plugins: {  
    
            datalabels: {
                color: 'grey',
                align: 'top',
                display: 'auto'
            },
            legend: {
                onHover: (event, chartElement) => {
                    event.native.target.style.cursor ='pointer' ;
                },
                onLeave: (event, chartElement) => {
                    event.native.target.style.cursor ='default' ;
                },
                position: 'bottom',
                labels: {
                    color: 'grey',
                    align: 'right',
              
                 
 
                    
                    
                 
                 
                }
            },
 
 
        },
        layout: {
            padding: {
                top: 35,
                bottom: 5,
                left: 20,
                right: 15
            }
        },
        scales: {
            x: {
                grid: {
                    color: 'white',
                }
            },
 
            y: {
                ticks: {
                    stepSize: 10,
                },
                offset: true,
                grid: {
                    color: 'white',
                }
            },
            y1: {
               
                type: 'linear',
                display: true,
                position: 'right',
            },
        },

        responsive: true,
        maintainAspectRatio: false
 
        }
 
       
    
 
            return {
                data,
                options,
                linear
            }
        }
}
</script>

<style>
 
canvas {
    flex: 1!important
}
 
.chartjs-legend {
    cursor: pointer
}
</style>