比TradingView 更轻量级 K线图 KLineChart vue dome

klinechart 是一个轻量级的K线图插件(基于html5 canvas构建,图表支持20种技术指标,支持自定义指标参数,支持绘制标记图形,支持自定义样式) https://github.com/liihuu/KLineChart

相比tradingview 更适合小白

 

<template>
  <div class="chart" v-loading="chartLoading">
    <div class="tools">
      <ul class="times">
        <li v-for="item in timeList" :key="item.value" :class="timeValue === item.value ? 'is_li':''"
            @click="choiceTime(item)">
          {{item.label}}
        </li>
      </ul>
      <ul class="types">
        <li @click="choiceType('line')" :class="typeValue === 'line' ? 'is_li':''">Line</li>
        <li @click="choiceType('kline')" :class="typeValue === 'kline' ? 'is_li':''">KLine</li>
      </ul>
    </div>
    <div id="Kline" class="kling cb">
    </div>
  </div>
</template>
<script>
  import {init, dispose} from 'klinecharts'
  import {config} from './kLineConfig/config'

  export default {
    data() {
      return {
        dealInfo: {},//交易对信息
        timeList: [
          {value: 1, label: '1M'},
          {value: 5, label: '5M'},
          {value: 10, label: '10M'},
          {value: 30, label: '30M'},
          {value: 60, label: '1H'},
          {value: 360, label: '6H'},
          {value: 720, label: '12H'},
          {value: 1440, label: '1D'},
          {value: 7200, label: '5D'},
        ],//时间列表
        timeValue: 1,//时间选择
        typeValue: 'kline',//图形类型
        klinecharts: null,//K线图实例
        klineData: [
          {
            timestamp: 1587009743000,
            open: 159.06,
            high: 159.65,
            low: 158.94,
            close: 159.45,
            volume: 143.66688,
            turnover: 0
          }
        ],  //初始数据
        resolution: '1',//分辨率
        lang: 'en', //语言
        chartLoading: true, //加载动画
        chartInterval: null,//定时器
      }
    },
    created() {

    },
    mounted() {
      this.klinecharts = init(document.getElementById('Kline'));
      //成交量指标
      this.klinecharts.addTechnicalIndicator('VOL', 100);
      this.initData(this.dealInfo.hash, this.timeValue);

      setInterval(() => {
        let newData = {
          timestamp: (new Date()).getTime(),
          open: Math.floor(Math.random() * 100),
          high: Math.floor(Math.random() * 60),
          low: Math.floor(Math.random() * 80),
          close: Math.floor(Math.random() * 50),
          volume: Math.floor(Math.random() * 1000),
          turnover: 0
        };
        this.klinecharts.updateData(newData);
      }, this.timeValue * 6000)
    },
    destroyed() {
      dispose(document.getElementById('Kline'))
    },
    methods: {

      /**
       * @disc: 初始数据
       * @date: 2020-04-14 12:44
       * @author: Wave
       */
      async initData() {
        // 设置样式配置
        this.klinecharts.setStyleOptions(config);
        //隐藏三根辅助线
        this.klinecharts.setTechnicalIndicatorParams("MA", [5, 10, 30]);
        // 设置精度
        this.klinecharts.setPrecision(4, 4);
        //筛入数据
        this.klinecharts.applyNewData(this.klineData, 0);
      },

      /**
       * @disc: 选择时间
       * @params: timeInfo
       * @date: 2020-04-14 15:48
       * @author: Wave
       */
      choiceTime(timeInfo) {
        this.timeValue = timeInfo.value;
        this.klinecharts.clearData();
        this.initData(this.dealInfo.hash, this.timeValue);
      },

      /**
       * @disc: 图标类型选择
       * @params: type
       * @date: 2020-04-14 15:52
       * @author: Wave
       */
      choiceType(type) {
        if (type === 'line') {
          this.klinecharts.setCandleStickChartType('real_time');
        } else {
          this.klinecharts.setCandleStickChartType('candle_stick');
        }
        this.typeValue = type
      },

    },

  }
</script>

 

vue dome:https://github.com/waveTan/vue-KLineChart

react-dome:https://github.com/liihuu/KLineChartSample/tree/master/react-sample

 

QQ开发交流群:677936660

### 关于 KLineChart 动态配置 KLineChart 是一个专注于 HTML5 Canvas 的轻量级 K 线图库,具备高可定制性和零依赖特性,并且支持移动设备[^1]。为了实现 KLineChart 图表的动态配置,可以采用如下方法: #### 初始化图表并设置基础配置 首先,在页面中创建用于容纳图表的 DOM 元素,并通过 JavaScript 或 TypeScript 进行初始化操作。 ```javascript // 创建容器节点 const container = document.createElement('div'); container.style.width = '800px'; container.style.height = '600px'; document.body.appendChild(container); // 配置项对象 const config = { width: 800, height: 600, grid: { left: '10%', right: '10%' }, }; // 实例化 KLineChart 并挂载至指定 DOM 节点 const chartInstance = new KLineChart(container, config); ``` #### 动态新数据源 当需要实时刷新或改显示的数据时,可以通过 `setData` 方法来传递新的时间序列数据集给图表实例。 ```javascript function updateChartData(newData) { const seriesOption = [ { type: 'candlestick', name: 'Stock Price', data: newData.map(item => [item.time, item.open, item.close, item.low, item.high]), } ]; // 新系列选项以及整个图表的数据源 chartInstance.setSeries(seriesOption); } ``` #### 修改样式和其他属性 除了基本结构外,还可以随时调整其他视觉效果相关的参数,比如颜色主题、坐标轴标签文字大小等。 ```javascript chartInstance.update({ xAxis: [{ axisLabel: { fontSize: 14 } }], yAxis: [{ splitLine: { show: false }, // 不显示分割线 axisTick: { inside: true } // 刻度朝内侧绘制 }] }); ``` 以上就是有关如何利用 KLineChart 库来进行灵活多变的图形渲染的一些指导说明[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值