echarts圖表k線行情

微信截圖_20240524110716.png
 <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="#B4E6FB">
    </head>
    <style type="text/css">
        body { margin: 0; padding: 0; }
        .Kline-div {
            position: fixed;
            width: 100%;
            bottom: 0;
            top: 0;
        }
        .Kline-div .K-line {
            height: 75%;
            width: 100%;
        }
        .loading {
            display: none; 
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }
        .loading .spinner {
            border: 4px solid rgba(255, 255, 255, 0.3);
            border-top: 4px solid #13B777;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
    <body>
        <div id="Kline-div" class="Kline-div">
            <div id="m-line" class="m-line"></div>
            <div id="k-content" class="K-line"></div>
        </div>
        <div class="loading">
            <div class="spinner"></div>
        </div>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js"></script>
        <script src="https://cdn.bootcss.com/echarts/3.7.1/echarts.min.js"></script>
        <script>
            var bgColor = "#121420";
            var upColor = "#F9293E";
            var downColor = "#00aa3b";

            var ma5Color = "#39afe6";
            var ma10Color = "#da6ee8";
            var ma20Color = "#ffab42";
            var ma30Color = "#00940b";

            function splitData(rawData) {
                var datas = [];
                var times = [];
                var vols = [];
                for (var i = 0; i < rawData.length; i++) {
                    datas.push(rawData[i]);
                    times.push(rawData[i].splice(0, 1)[0]);
                    vols.push(rawData[i][4]);
                }
                return { datas: datas, times: times, vols: vols };
            }

            function calculateMA(dayCount, data) {
                var result = [];
                for (var i = 0, len = data.times.length; i < len; i++) {
                    if (i < dayCount) {
                        result.push('-');
                        continue;
                    }
                    var sum = 0;
                    for (var j = 0; j < dayCount; j++) {
                        sum += data.datas[i - j][1];
                    }
                    result.push((sum / dayCount).toFixed(2));
                }
                return result;
            }

            var calcEMA = function (n, data, field) {
                var i, l, ema, a;
                a = 2 / (n + 1);
                if (field) {
                    ema = [data[0][field]];
                    for (i = 1, l = data.length; i < l; i++) {
                        ema.push((a * data[i][field] + (1 - a) * ema[i - 1]).toFixed(2));
                    }
                } else {
                    ema = [data[0]];
                    for (i = 1, l = data.length; i < l; i++) {
                        ema.push((a * data[i] + (1 - a) * ema[i - 1]).toFixed(3));
                    }
                }
                return ema;
            };

            var calcDIF = function (short, long, data, field) {
                var i, l, dif, emaShort, emaLong;
                dif = [];
                emaShort = calcEMA(short, data, field);
                emaLong = calcEMA(long, data, field);
                for (i = 0, l = data.length; i < l; i++) {
                    dif.push((emaShort[i] - emaLong[i]).toFixed(3));
                }
                return dif;
            };

            var calcDEA = function (mid, dif) {
                return calcEMA(mid, dif);
            };

            var calcMACD = function (short, long, mid, data, field) {
                var i, l, dif, dea, macd, result;
                result = {};
                macd = [];
                dif = calcDIF(short, long, data, field);
                dea = calcDEA(mid, dif);
                for (i = 0, l = data.length; i < l; i++) {
                    macd.push(((dif[i] - dea[i]) * 2).toFixed(3));
                }
                result.dif = dif;
                result.dea = dea;
                result.macd = macd;
                return result;
            };

            function initKOption(cdata) {
                var data = splitData(cdata);
                var macd = calcMACD(12, 26, 9, data.datas, 1);

                var lastDataIndex = data.datas.length - 1;
                var lastData = data.datas[lastDataIndex];

                return {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross'
                        }
                    },
                    legend: [
                        {
                            icon: 'rect',
                            type: 'scroll',
                            itemWidth: 14,
                            itemHeight: 2,
                            animation: true,
                            textStyle: {
                                fontSize: 12,
                                color: '#0e99e2'
                            },
                            pageIconColor: '#0e99e2',
                            data: [
                           
                                { name: 'MA5', textStyle: { color: '#39afe6' } },
                                { name: 'MA10', textStyle: { color: '#da6ee8' } },
                                { name: 'MA20', textStyle: { color: '#ffab42' } },
                                { name: 'MA30', textStyle: { color: '#00940b' } },
                                { name: 'DIF', textStyle: { color: '#39afe6' } },
                                { name: 'DEA', textStyle: { color: '#da6ee8' } }
                            ],
                            top: '-1%',
                            left: 'center'
                        },
                        // {
                        //     icon: 'rect',
                        //     type: 'scroll',
                        //     itemWidth: 14,
                        //     itemHeight: 2,
                        //     animation: true,
                        //     textStyle: {
                        //         fontSize: 12,
                        //         color: '#0e99e2'
                        //     },
                        //     pageIconColor: '#0e99e2',
                        //     data: [
                        //         { name: 'Volumn', textStyle: { color: '#ffab42' } }
                        //     ],
                        //     top: '2%',
                        //     left: 'center'
                        // },
                        {
                            icon: 'rect',
                            type: 'scroll',
                            itemWidth: 14,
                            itemHeight: 2,
                            animation: true,
                            textStyle: {
                                fontSize: 12,
                                color: '#0e99e2'
                            },
                            pageIconColor: '#0e99e2',
                            data: [
                                // { name: 'Volumn', textStyle: { color: '#ffab42' } },
                                // { name: 'MACD', textStyle: { color: '#ffab42' } },
                                // { name: 'DIF', textStyle: { color: '#39afe6' } },
                                // { name: 'DEA', textStyle: { color: '#da6ee8' } }
                            ],
                            top: '4%',
                            left: 'center'
                        }
                    ],
                    axisPointer: {
                        show: true
                    },
                    color: [ma5Color, ma10Color, ma20Color, ma30Color],
                    grid: [{
                        id: 'gd1',
                        left: '1%',
                        right: '60',
                        height: '50%',
                        top: '5%'
                    }, {
                        left: '1%',
                        right: '60',
                        top: '60%',
                        height: '15%'
                    }, {
                        left: '1%',
                        right: '60',
                        top: '80%',
                        height: '14%'
                    }],
                    xAxis: [{
                        type: 'category',
                        data: data.times,
                        scale: true,
                        boundaryGap: false,
                        axisLine: {
                            onZero: false
                        },
                        axisLabel: {
                            show: true,
                            color: '#9b9da9',
                            fontSize: 10
                        },
                        splitLine: {
                            show: true,
                            lineStyle: {
                                color: '#181a23'
                            }
                        },
                        splitNumber: 20,
                        min: 'dataMin',
                        max: 'dataMax'
                    }, {
                        type: 'category',
                        gridIndex: 1,
                        data: data.times,
                        axisLabel: {
                            color: '#9b9da9',
                            fontSize: 10
                        },
                        splitLine: {
                            show: true,
                            lineStyle: {
                                color: '#181a23'
                            }
                        },
                    }, {
                        type: 'category',
                        gridIndex: 2,
                        data: data.times,
                        axisLabel: {
                            // 顯示第三張圖表的x軸時間
                            show: true,
                            color: '#9b9da9',
                            fontSize: 10
                        },
                        axisLine: {
                            show: true,
                            lineStyle: {
                                color: '#181a23'
                            }
                        },
                        splitLine: {
                            // 添加背景網(wǎng)格
                            show: true,
                            lineStyle: {
                                color: '#181a23'
                            }
                        }
                    }],
                    yAxis: [{
                        position: 'right', // 從左到右顯示刻度
                        scale: true,
                        z: 4,
                        axisLabel: {
                            color: '#c7c7c7',
                            inside: false,
                            position: 'right' 
                        },
                        splitLine: {
                            show: true,
                            lineStyle: {
                                color: '#181a23'
                            }
                        },
                    }, {
                        position: 'right', // 從左到右顯示刻度
                        gridIndex: 1,
                        splitNumber: 3,
                        z: 4,
                        axisLine: {
                            onZero: false
                        },
                        axisTick: {
                            show: false
                        },
                        splitLine: {
                            show: true,
                            lineStyle: {
                                color: '#181a23'
                            }
                        },
                        axisLabel: {
                            color: '#c7c7c7',
                            inside: false,
                            fontSize: 8
                        },
                    }, {
                        position: 'right', // 從左到右顯示刻度
                        z: 4,
                        gridIndex: 2,
                        splitNumber: 4,
                        axisLine: {
                            onZero: false
                        },
                        axisTick: {
                            show: false
                        },
                        splitLine: {
                            // 添加背景網(wǎng)格
                            show: true,
                            lineStyle: {
                                color: '#181a23'
                            }
                        },
                        axisLabel: {
                            color: '#c7c7c7',
                            inside: false,
                            fontSize: 8
                        },
                    }],
                    dataZoom: [{
                        type: 'inside',
                        xAxisIndex: [0, 1, 2],
                        // start: 100,
                        // end: 10,
                        throttle: 10,
                        top: '94%',
                        height: '6%',
                        left: '10%',
                        right: '10%',
                        borderColor: '#696969',
                        textStyle: {
                            color: '#dcdcdc'
                        },
                        handleSize: '90%',
                        handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
                        dataBackground: {
                            lineStyle: {
                                color: '#fff'
                            },
                            areaStyle: {
                                color: '#696969'
                            }
                        }
                    }],
                    animation: true,
                    backgroundColor: bgColor,
                    blendMode: 'source-over',
                    series: [{
                        name: 'Kline',
                        type: 'candlestick',
                        data: data.datas,
                        barWidth: '55%',
                        large: true,
                        largeThreshold: 100,
                        itemStyle: {
                            normal: {
                                color: upColor,
                                color0: downColor,
                                borderColor: upColor,
                                borderColor0: downColor,
                            }
                        },
                        markLine: {
                            symbol: 'none',
                            data: [
                                {
                                    name: 'Last Close',
                                    xAxis: data.times[lastDataIndex],
                                    yAxis: lastData[1],
                                    lineStyle: {
                                        type: 'dotted',
                                        color: '#ffab42'
                                    },
                                    label: {
                                        show: true,
                                        // 將數(shù)值標(biāo)簽位置改為左邊的y軸
                                        formatter: function (params) {
                                            return 'Last Close: ' + params.value.toFixed(2);
                                        },
                                        position: 'insideEndTop',
                                        color: '#ffab42',
                                        fontSize: 12
                                    }
                                }
                            ]
                        }
                    }, {
                        name: 'MA5',
                        type: 'line',
                        data: calculateMA(5, data),
                        smooth: true,
                        symbol: "none",
                        lineStyle: {
                            normal: {
                                opacity: 0.8,
                                color: '#39afe6',
                                width: 1
                            }
                        },
                    }, {
                        name: 'MA10',
                        type: 'line',
                        data: calculateMA(10, data),
                        smooth: true,
                        symbol: "none",
                        lineStyle: {
                            normal: {
                                opacity: 0.8,
                                color: '#da6ee8',
                                width: 1
                            }
                        }
                    }, {
                        name: 'MA20',
                        type: 'line',
                        data: calculateMA(20, data),
                        smooth: true,
                        symbol: "none",
                        lineStyle: {
                            opacity: 0.8,
                            width: 1,
                            color: ma20Color
                        }
                    }, {
                        name: 'MA30',
                        type: 'line',
                        data: calculateMA(30, data),
                        smooth: true,
                        symbol: "none",
                        lineStyle: {
                            normal: {
                                opacity: 0.8,
                                width: 1,
                                color: ma30Color
                            }
                        }
                    }, {
                        name: 'Volumn',
                        type: 'bar',
                        xAxisIndex: 1,
                        yAxisIndex: 1,
                        data: data.vols,
                        barWidth: '60%',
                        itemStyle: {
                            normal: {
                                color: function (params) {
                                    var colorList;
                                    if (data.datas[params.dataIndex][1] > data.datas[params.dataIndex][0]) {
                                        colorList = upColor;
                                    } else {
                                        colorList = downColor;
                                    }
                                    return colorList;
                                },
                            }
                        }
                    }, {
                        name: 'MACD',
                        type: 'bar',
                        xAxisIndex: 2,
                        yAxisIndex: 2,
                        data: macd.macd,
                        barWidth: '40%',
                        itemStyle: {
                            normal: {
                                color: function (params) {
                                    var colorList;
                                    if (params.data >= 0) {
                                        colorList = upColor;
                                    } else {
                                        colorList = downColor;
                                    }
                                    return colorList;
                                },
                            }
                        }
                    }, {
                        name: 'DIF',
                        type: 'line',
                        symbol: "none",
                        xAxisIndex: 2,
                        yAxisIndex: 2,
                        data: macd.dif,
                        lineStyle: {
                            normal: {
                                color: '#39afe6',
                                width: 1
                            }
                        }
                    }, {
                        name: 'DEA',
                        type: 'line',
                        symbol: "none",
                        xAxisIndex: 2,
                        yAxisIndex: 2,
                        data: macd.dea,
                        lineStyle: {
                            normal: {
                                opacity: 0.8,
                                color: '#da6ee8',
                                width: 1
                            }
                        }
                    }]
                };
                
                
            }

            $('.loading').fadeIn();
            $('#chart').hide()


            // 更新圖表數(shù)據(jù)
            function tabChange(tab) {
                
                let newKlineData = []
           
                $.ajax({
                    url: 'https://api-aws.huobi.pro/market/history/kline',
                    type: 'GET',
                    data: {
                        symbol: 'btcusdt',
                        period: tab,
                        size: 70
                    },
                    success: function (data) {
                        // 加載完成后隱藏加載動畫
                        $('.loading').fadeOut();
                        $('#chart').show()
                        // 請求成功 data 包含響應(yīng)數(shù)據(jù)
                        if (data.status == 'ok') {
                            // console.log(data.data)
                            const res = data.data
                    
                            for (i = 0; i < res.length; i++) {
            
                                const timestamp = res[i].id * 1000; // 轉(zhuǎn)換為毫秒級時間戳
                                const date = new Date(timestamp);
                                const year = date.getFullYear();
                                const month = date.getMonth() + 1;
                                const day = date.getDate();
                                const hours = date.getHours();
                                const minutes = date.getMinutes();
                                const seconds = date.getSeconds();

                                const formattedTime =   String(month).padStart(2, '0') + '/' + String(day).padStart(2, '0') + '-' + String(hours).padStart(2, '0') + ':' + String(minutes).padStart(2, '0') ;

                                //newKlineData.push([formattedTime, res[i].open, res[i].close, '-', '-', res[i].low, res[i].high, res[i].vol, res[i].count,'-'])
                                newKlineData.push([formattedTime, res[i].open, res[i].close, res[i].low, res[i].high, res[i].vol, res[i].count])

                            }
           
                            // initChart(newKlineData.reverse(),false,line,res[0].close);
                            fetchDataAndUpdateChart(newKlineData.reverse());
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        // $('.loading').fadeOut();
                        // $('#chart').show()
                        console.log('Error: ' + textStatus);
                    }
                });

            }


            function fetchDataAndUpdateChart(kdata) {
                        
                //[{
                //  "id": 1716521040,
                //  "open": 67870.48,
                //  "close": 67870.49,
                //  "low": 67870.48,
                //  "high": 67870.49,
                //  "amount": 0.005206,
                //  "vol": 353.33376507,
                //  "count": 2
                //},{
                //  "id": 1716520980,
                //  "open": 67850.34,
                //  "close": 67876.6,
                //  "low": 67850.34,
                //  "high": 67876.61,
                //  "amount": 0.18876069628094272,
                //  "vol": 12808.80971393,
                //  "count": 28
                //}]
                
                kChart.setOption(initKOption(kdata));
            }

           
            var kChart = echarts.init(document.getElementById('k-content'));
            window.addEventListener('resize', handleResize);
            function handleResize() {
                kChart.resize();
            }


            tabChange('1min');  // 1hou  1day 
            
        </script>
    </body>
    </html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末茄唐,一起剝皮案震驚了整個濱河市落塑,隨后出現(xiàn)的幾起案子参歹,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡闲昭,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門靡挥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來序矩,“玉大人,你說我怎么就攤上這事跋破◆さ恚” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵毒返,是天一觀的道長租幕。 經(jīng)常有香客問我,道長拧簸,這世上最難降的妖魔是什么劲绪? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上贾富,老公的妹妹穿的比我還像新娘歉眷。我一直安慰自己,他們只是感情好颤枪,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布汗捡。 她就那樣靜靜地躺著,像睡著了一般畏纲。 火紅的嫁衣襯著肌膚如雪扇住。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天霍骄,我揣著相機與錄音台囱,去河邊找鬼。 笑死读整,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的咱娶。 我是一名探鬼主播米间,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼膘侮!你這毒婦竟也來了屈糊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤琼了,失蹤者是張志新(化名)和其女友劉穎逻锐,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雕薪,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡昧诱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了所袁。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盏档。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖燥爷,靈堂內(nèi)的尸體忽然破棺而出蜈亩,到底是詐尸還是另有隱情,我是刑警寧澤前翎,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布稚配,位于F島的核電站,受9級特大地震影響港华,放射性物質(zhì)發(fā)生泄漏道川。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望愤惰。 院中可真熱鬧苇经,春花似錦、人聲如沸宦言。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽奠旺。三九已至蜘澜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間响疚,已是汗流浹背鄙信。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留忿晕,地道東北人装诡。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像践盼,于是被迫代替她去往敵國和親鸦采。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容