Echart에 series total 기능이 기본으로 제공되지 않아서 구현해주어야 한다.

 

legend에서 시리즈를 숨길때 total value가 반영

<!DOCTYPE html>
<html lang="en">
    <head>
    <script src="./index.js"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.5.0/dist/echarts.min.js"></script>
 
</head>


<style>
    .flex-container{
        display:flex;
        /* background-color: cadetblue; */
        width: 100%;
        height: 200px;

    }
    .flex-container-item1 {
        width: 100%;
        /* background-color: teal; */
        flex: 1;
        border: 1px solid red;
    }
    .flex-container-item2 {
        width: 100%;
        /* background-color: teal; */
        flex: 2;
        border: 1px solid red;
    }
</style>
<body>
    <div>
        <h1>hellow world</h1>
    </div>
        <div class="flex-container">
            <div class="flex-container-item1" id="main1">1</div>
            <div class="flex-container-item1" id="main2">2</div>
            <div class="flex-container-item1" id="main3">3</div>
        </div>
        <div class="flex-container">
            <div class="flex-container-item1" id="main4">1</div>
            <div class="flex-container-item1" id="main5">2</div>
            <div class="flex-container-item1" id="main6">3</div>
        </div>
    <script>
    //for total value
    function creatChart(target){
        let result = 0;
        let array = [];
        // Create the echarts instance
        let chartDom = document.getElementById(target);
        let myChart = echarts.init(chartDom);

        let series = [
                {name: 'Email',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [120, 132, 101, 134, 90, 230, 210]},
                {name: 'Union Ads',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [220, 182, 191, 234, 290, 330, 310]},
                {name: 'Video Ads',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [150, 232, 201, 154, 190, 330, 410]},
                {name: 'Baidu',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [620, 732, 701, 734, 1090, 1130, 1120]},
                {name: 'Google',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [120, 132, 101, 134, 290, 230, 220]},
                {name: 'Bing',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [60, 72, 71, 74, 190, 130, 110]},
                {name: 'Others',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [62, 82, 91, 84, 109, 110, 120]}
            ];
            sum();
        let option;
        option = {
            tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                        // Use axis to trigger tooltip
                        type: 'shadow', // 'shtotalow' as default; can also be 'line' or 'shtotalow'
                        label:{
                            //for total value
                            formatter: function (params){
                            for ( let i = 0; i < params.seriesData.length; i++){
                                result += params.seriesData[i].value
                            } 
                            let resultFinal = "total: " + result
                            result = 0;
                            return resultFinal;
                            }
                        }
                        }
                    },
            legend: {
                //for total value
                data: series.map(item => item.name),
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
                }
            ],
            yAxis: [
                {
                type: 'value'
                }
            ],
            series: [
               ...series,
               {
                    name: ' A total of ',
                    type: 'bar',
                    stack: 'AD',
                    tooltip: {
                    show: false
                    },
                    stack: '',
                    color: '#FFFFFF',
                    label: {
                    normal: {
                        show: true,
                        position: 'top'
                    },
                    },
                    data: array,
                    z: -1,
                    barGap: '-100%',
                }
            ]
        };

        //for total value
        function sum(){
            for ( let z = 0; z < series[0].data.length; z++){
                for( let i = 0; i < series.length; i++){
                    result += series[i].data[z];
                }
                array.push(result)
                result = 0;
            }
        }

        myChart.on('legendselectchanged', function (params) {
        array.length = 0
        for ( let z = 0; z < series[0].data.length; z++){
            for( let i = 0; i < series.length; i++){
                if(params.selected[series[i].name])
                    result += series[i].data[z];
                }
            array.push(result)
            result = 0;
        }
            myChart.setOption(option);

        });

        //chart Resize code
        myChart.setOption(option);

        window.addEventListener('resize', function(){
            myChart.resize();
        });
    }
    creatChart("main1");
    // creatChart("main2");
    // creatChart("main3");
    // creatChart("main4");
    // creatChart("main5");
    // creatChart("main6");
    </script>
</body>
</html>

'jquery' 카테고리의 다른 글

Multiselet Change Event  (0) 2024.12.13
<tr>에서 get value() 벨류값 가져오기  (0) 2019.05.22
jQuery 선택자  (0) 2018.10.14
제이쿼리 소개  (0) 2018.10.14
jquery Json date read  (0) 2018.10.14

이차트 사용시에 css flex를 적용하여도 container size만 반응형으로 적용되고,

Echart 사이즈는 변하지 않아 아래 window.onload로 resize 되도록 적용


<!DOCTYPE html>
<html lang="en">
    <head>
    <script src="./index.js"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.5.0/dist/echarts.min.js"></script>
 
</head>


<style>
    .flex-container{
        display:flex;
        /* background-color: cadetblue; */
        width: 100%;
        height: 200px;

    }
    .flex-container-item1 {
        width: 100%;
        /* background-color: teal; */
        flex: 1;
        border: 1px solid red;
    }
    .flex-container-item2 {
        width: 100%;
        /* background-color: teal; */
        flex: 2;
        border: 1px solid red;
    }
</style>
<body>
    <div>
        <h1>hellow world</h1>
    </div>
        <div class="flex-container">
            <div class="flex-container-item1" id="main1">1</div>
            <div class="flex-container-item1" id="main2">2</div>
            <div class="flex-container-item1" id="main3">3</div>
        </div>
        <div class="flex-container">
            <div class="flex-container-item1" id="main4">1</div>
            <div class="flex-container-item1" id="main5">2</div>
            <div class="flex-container-item1" id="main6">3</div>
        </div>
        <div class="flex-container">
            <div class="flex-container-item1" id="main7">1</div>
            <div class="flex-container-item2" id="main8">3</div>
        </div>
    <script>
    
    function creatChart(target){
        // Create the echarts instance
        let chartDom = document.getElementById(target);
        let myChart = echarts.init(chartDom);
        let option;
        option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                type: 'shadow'
                }
            },
            legend: {},
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
                }
            ],
            yAxis: [
                {
                type: 'value'
                }
            ],
            series: [
                {name: 'Email',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [120, 132, 101, 134, 90, 230, 210]},
                {name: 'Union Ads',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [220, 182, 191, 234, 290, 330, 310]},
                {name: 'Video Ads',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [150, 232, 201, 154, 190, 330, 410]},
                {name: 'Baidu',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [620, 732, 701, 734, 1090, 1130, 1120]},
                {name: 'Google',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [120, 132, 101, 134, 290, 230, 220]},
                {name: 'Bing',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [60, 72, 71, 74, 190, 130, 110]},
                {name: 'Others',type: 'bar',stack: 'AD',emphasis: {focus: 'series'},data: [62, 82, 91, 84, 109, 110, 120]}
            ]
        };

        //chart Resize code
        myChart.setOption(option);
        // function resize(){
        //     myChart.resize();
        // }
        // $(window).on('resize', resize);

        window.addEventListener('resize', function(){
            myChart.resize();
        });
    }
    creatChart("main1");
    creatChart("main2");
    creatChart("main3");
    creatChart("main4");
    creatChart("main5");
    creatChart("main6");
    creatChart("main7");
    creatChart("main8");
  
    </script>
</body>
</html>

 

코드 참고:https://aloner-blog.tistory.com/78

'HTML' 카테고리의 다른 글

table colgroup 사용방법  (0) 2018.10.14
javascript 페이지 이동  (0) 2018.10.05
HTML 색상표현  (0) 2017.12.19
HTML이란?  (0) 2017.12.18
HTML 이미지 삽입  (0) 2017.12.18

+ Recent posts