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

import "../../../assets/style/IndividualActivities.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;
    function TrendChart(width, height, $canvas) {
        const data = (prop.list || []).map(v => {
            return {
                Data: DataFormmat(v.dt),
                sales: v.playCount
            }
        })
        const chart = new Chart({
            container: $canvas,
            autoFit: true,
            height: height - 20,
            width,
            padding: [
                20,
                60,
                30,
                70,
            ]
        });
        let max = 0, min = Infinity;
        (data || []).map(v => {
            if (v.sales > max) {
                max = v.sales;
            }
            if (v.sales < min) {
                min = v.sales;
            }
        })
        chart.data(data);
        chart.scale('Data', {
            range: [0, 1],
            tickCount: 10,
            type: 'timeCat',
            mask: "MM-DD"
        });
        chart.scale('sales', {
            nice: true,
        });
        // chart.annotation().dataMarker({
        //     position: [maxText, max],
        //     top: true,
        //     text: {
        //         content: '最高值：' + max,
        //         style: {
        //             fontSize: 13,
        //             fill: "#fff",
        //         }
        //     },
        //     line: {
        //         length: 30,
        //     },
        // });
        chart.axis('sales', {
            label: {
                formatter: n => {
                    return utils.formatNumber(n, 2);
                },
                style: {
                    fill: "#fff"
                },
            },
            grid: null
        });
        chart.axis('Data', {
            label: {
                style: {
                    fill: "#ffffff00"
                },
            },
            grid: null
        });
        chart.tooltip(false);

        chart.line().shape('smooth').color('#3ae8ef').position('Data*sales');
        chart.area().shape('smooth').color('l(90) 0:#3ae8ef 1:rgba(0,0,0,0)').position('Data*sales');
        chart.render();
    }
    onMount(() => {
        TrendChart(prop.width, prop.height, $canvas)
    })
    return (
        <div class="livRang" style={{
            width: `${prop.width}px`
        }}>
            <div class="head">{prop.title}</div>
            <div ref={$canvas}></div>
        </div>
    );
}

export default Usertrend;
