KeiStory

ECharts 에 바인딩 된 Dataset Source 내용 DataView 에 표시하기

 

ECharts 에는 DataView 기능이 있어 화면에 표시된 포인터 들의 정보를 확인할 수 있습니다

toolbox.feature.dataView 를 show:true 해주면 toolbox 에 view 버튼이 생겨 x,y 축 값을 확인할 수 있습니다.

<!DOCTYPE html>
<html style="height: 100%">
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.3.0/dist/echarts.min.js"></script>
</head>
<body style="height: 100%; margin: 0">
<div id="main" style="height: 100%"></div>
<script type="text/javascript">
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    var option;

    // 1만 개의 데이터를 생성
    var data = [['Time', 'Y', 'A', 'B', 'C', 'D']];
    var startTime = new Date(2020, 0, 1).getTime();
    for (var i = 0; i < 100; i++) {
        var time = new Date(startTime + i * 1000 * 60 * 60 * 24); // 하루 단위로 증가
        data.push([
            time.toISOString().split('T')[0],
            (Math.random() * 100).toFixed(2),
            (Math.random() * 100).toFixed(2),
            (Math.random() * 100).toFixed(2),
            (Math.random() * 100).toFixed(2),
            (Math.random() * 100).toFixed(2)
        ]);
    }

    option = {
        title: {
            text: 'Dataset DataView Test'
        },
        tooltip: {},
        toolbox: {
            feature: {
                dataView: {
                    show: true,
                    readOnly: true
                }
            }
        },
        dataset: {
            source: data
        },
        xAxis: { type: 'category' },
        yAxis: { type: 'value' },
        series: [
            { type: 'line', encode: { x: 'Time', y: 'Y' } }
        ]
    };

    myChart.setOption(option);
</script>
</body>
</html>

Dataview

 

optionToContent 를 이용하면 function 에 표시할 데이터를 커스텀하게 처리할 수 있습니다.

<!DOCTYPE html>
<html style="height: 100%">
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.3.0/dist/echarts.min.js"></script>
</head>
<body style="height: 100%; margin: 0">
<div id="main" style="height: 100%"></div>
<script type="text/javascript">
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    var option;

    // 1만 개의 데이터를 생성
    var data = [['Time', 'Y', 'A', 'B', 'C', 'D']];
    var startTime = new Date(2020, 0, 1).getTime();
    for (var i = 0; i < 100; i++) {
        var time = new Date(startTime + i * 1000 * 60 * 60 * 24); // 하루 단위로 증가
        data.push([
            time.toISOString().split('T')[0],
            (Math.random() * 100).toFixed(2),
            (Math.random() * 100).toFixed(2),
            (Math.random() * 100).toFixed(2),
            (Math.random() * 100).toFixed(2),
            (Math.random() * 100).toFixed(2)
        ]);
    }

    option = {
        title: {
            text: 'Dataset DataView Test'
        },
        tooltip: {},
        toolbox: {
            feature: {
                dataView: {
                    show: true,
                    readOnly: true,
                    optionToContent: function(opt) {
                        var table = '<table style="width:100%;text-align:center"><tbody>';
                        var source = opt.dataset[0].source;
                        for (var i = 0; i < source.length; i++) {
                            table += '<tr>';
                            for (var j = 0; j < source[i].length; j++) {
                                if (i === 0) {
                                    table += '<th>' + source[i][j] + '</th>';
                                } else {
                                    table += '<td>' + source[i][j] + '</td>';
                                }
                            }
                            table += '</tr>';
                        }
                        table += '</tbody></table>';
                        return table;
                    }
                }
            }
        },
        dataset: {
            source: data
        },
        xAxis: { type: 'category' },
        yAxis: { type: 'value' },
        series: [
            { type: 'line', encode: { x: 'Time', y: 'Y' } }
        ]
    };

    myChart.setOption(option);
</script>
</body>
</html>

DataView 

간단하게 표시 하는데는 문제없을 것 같은데 데이터가 많은 경우라면 가상화 처리도 필요해 보입니다.

예시링크

https://echarts.apache.org/examples/en/editor.html?code=PTAEEZBnO1BwawNcdIB2bAu44BkXA-naQuwOEZBgsAFCjGgBuAhgE6gAm5ALuaALygDabA5ACoCWAtgFNOAGlCcAmqPEBBaZwBC8gMLyAIpwC6mgNyESZKqADOjSvT5CWoAHaCA7qDUNBACgBMABi9jPY8ACUAHQA5oIWAm4BekQkAGYA9tSuFNS81p46oOkAPBCemdkA1EUBoADe-gYkqaD0kdZ2js70bqZUEVZF2aAAVPkFfaAAbJ5Do0PuACzR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