import { onMount } from "solid-js";
import { Chart } from '@antv/g2';
// import DataSet from '@antv/data-set';

import "../../../assets/style/BigScreenHome.css"
import utils from "../../../utils/index"

function DataFormmat(timeString) {
    if (!timeString || typeof timeString !== 'string') return "";
    const y = timeString[0] + timeString[1] + timeString[2] + timeString[3];
    const m = timeString[4] + timeString[5];
    const d = timeString[6] + timeString[7];
    return [y, m, d].join("-");
}

function Usertrend(prop) {
    let $canvas = undefined, origin = prop.list || [], textList = [], maxaxis = "", max = 0;
    function getlist() {
        const setli = [];
        for (let i = 0; i < origin.length; i++) {
            const item = origin[i];
            (item.data || []).map(v => {
                setli.push({
                    Data: DataFormmat(v.dt),
                    value: v.readCount,
                    type: item.name
                })
            })
        }
        return setli
    }
    function TrendChart(width, height, $canvas) {
        const data = getlist(), li = {};
        const chart = new Chart({
            container: $canvas,
            autoFit: true,
            height,
            width,
            padding: [
                30,
                40,
                30,
                70,
            ]
        });
        chart.data(data);
        chart.legend({
            position: "top",
            marker:{
                symbol:"hyphen"
            },
            itemName:{
                style:{
                    fill: "#ffffff",
                }
            }
        });
        chart.scale('Data', {
            range: [0, 1],
            tickCount: 10,
            type: 'timeCat'
        });
        chart.axis('Data', {
            label: {
                style: {
                    fill: "#fff"
                },
            },
            grid: null
        });
        chart.tooltip({
            showCrosshairs: true,
        });
        chart.axis("value", {
            label: {
                formatter: n => {
                    return utils.formatNumber(n, 2);
                },
                style: {
                    fill: "#fff"
                },
            },
            position: "left",
            grid: null
        });

        chart.line().shape('smooth').color('type').position('Data*value');
        chart.area().shape('smooth').color('type').position('Data*value');
        chart.scale(li);
        chart.render();
    }
    onMount(() => {
        TrendChart(prop.width, prop.height, $canvas)
    })
    return (
        <div className="livRang" style={`width:${prop.width}px;`}>
            <div className="head">{prop.title}</div>
            <div ref={$canvas}></div>
        </div>
    );
}

export default Usertrend;
