Sencha chart series 동적 처리

2024. 11. 23. 23:42Javascript/Sencha

function getSeriesByData2() {
    let data = [
        { DATE: 1732167001000, A: 13, B: 'aks-userpool-260158-vmss000000' },
        { DATE: 1732167001000, A: 13, B: 'aks-userpool-260158-vmss000001' },
        { DATE: 1732167001000, A: 14, B: 'aks-userpool-260158-vmss000002' },
        { DATE: 1732167061000, A: 7, B: 'aks-userpool-260158-vmss000003' },
        { DATE: 1732167061000, A: 17, B: 'aks-userpool-260158-vmss000006' },
        { DATE: 1732167121000, A: 18, B: 'aks-userpool-260158-vmss000006' },
    ];

    // Extract unique B values manually
    let uniqueKeys = [];
    for (let i = 0; i < data.length; i++) {
        let key = data[i].B;
        if (uniqueKeys.indexOf(key) === -1) {
            uniqueKeys.push(key); // Add key if it's not already in the array
        }
    }

    // Generate series for each unique B
    let series = [];
    for (let j = 0; j < uniqueKeys.length; j++) {
        let key = uniqueKeys[j];
        series.push({
            type: 'line',
            title: key, // Use B as the series title
            xField: 'DATE',
            yField: 'A',
            style: {
                stroke: getRandomColor(), // Random color
                lineWidth: 2,
            },
            marker: {
                type: 'circle',
                size: 4,
                fillStyle: getRandomColor(),
            },
        });
    }

    return series;
}

function getRandomColor() {
    // Generate a random hex color code
    return '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
}