视觉化KBARS(5)-1分k展示

前面把java跟python部分完成後,
接下来要继续写js和html的步骤来完成1分k视觉化。

(1)引用的js套件

    <script type="text/javascript" th:src="@{/js/jquery-3.3.1.min.js}" ></script>
	<script src="https://code.highcharts.com/stock/highstock.js"></script>
	<script src="https://code.highcharts.com/stock/modules/data.js"></script>
	<script src="https://code.highcharts.com/stock/modules/drag-panes.js"></script>
	<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>

(2)html的部分,
kbar_data为java传过来的资料

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <input type="hidden" id="kbar_data" th:value="${kbar_data}"/>
    <div id="container" style="height: 800px; min-width: 620px"></div>
</body>
</html>

(3)highcharts js撰写,
将时间戳记、开、收、最低、最高价、量的资料取出,
并带入套件中

<script type="text/javascript">
const kbar_data = $("#kbar_data").val();
const myArr = JSON.parse(kbar_data);

const ts = myArr.map.ts.myArrayList;
const Open = myArr.map.Open.myArrayList;
const High = myArr.map.High.myArrayList;
const Low = myArr.map.Low.myArrayList;
const Close = myArr.map.Close.myArrayList;
const Volume = myArr.map.Volume.myArrayList;

    // split the data set into ohlc and volume
    var ohlc = [],
        volume = [],
        dataLength = myArr.map.Volume.myArrayList.length,
        i = 0;

    for (i; i < dataLength; i += 1) {
        ohlc.push([
            parseInt(String(ts[i]).substring(0, 13)), // the date
            Open[i], // open
            High[i], // high
            Low[i], // low
            Close[i] // close
        ]);

        volume.push([
            parseInt(String(ts[i]).substring(0, 13)), // the date
            Volume[i] // the volume
        ]);
    }


    // create the chart
    Highcharts.stockChart('container', {

        rangeSelector: {
            selected: 1
        },

        title: {
            text: '2603长荣1分K'
        },

        yAxis: [{
            labels: {
                align: 'right',
                x: -3
            },
            title: {
                text: 'OHLC'
            },
            height: '60%',
            lineWidth: 2,
            resize: {
                enabled: true
            }
        }, {
            labels: {
                align: 'right',
                x: -3
            },
            title: {
                text: 'Volume'
            },
            top: '65%',
            height: '35%',
            offset: 0,
            lineWidth: 2
        }],

        tooltip: {
            split: true
        },

        series: [{
            type: 'candlestick',
            name: 'AAPL1',
            data: ohlc,
            dataGrouping: {
                enabled: false
            }
        }, {
            type: 'column',
            name: 'Volume',
            data: volume,
            yAxis: 1,
            dataGrouping: {
                enabled: false
            }
        }]
    });

</script>

(4)结果如图
https://ithelp.ithome.com.tw/upload/images/20211012/20107848YMILMLn2RO.jpg

上方是k棒、而下方是量的柱状图,
这样就完成第二个视觉化的功能。


<<:  [Day 27] Final Project (3/5) — 让 App 在本机端运行

>>:  【Day27】线性收敛除法器实作

Sklearn读取自定的CSV k-means范例修改

数据集的使用,常常令人一头雾水,举例来说,iris dataset这个最常用的资料集。 用一行代码就...

[第26天]30天搞懂Python-直方图

前言 使用numpy及matplotlib函式库实作直方图。 程序 import numpy as ...

Day21 Let's ODOO: 流水号

当我们在建立单据与发票的时候,若想要自定义流水号该怎麽在ODOO里面设定呢? 我们以invoice单...

开始写CRUD的 C 吧! - day04

在前一篇文里提到,VoK希望开发者专注於 Kotlin code 的开发,所以Karibu-DSL ...

数据分析的好夥伴 - Python基础:流程控制与回圈

今天是重头戏。 通常从这边开始就会不太懂程序在干嘛,因此理解其中逻辑非常重要!废话不多说,我们赶快开...