比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

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue中使用TradingView制作K线图,可以按照以下步骤进行: 1. 首先,需要在Vue项目中安装TradingView相关的npm包。可以使用以下命令进行安装: ``` npm install --save trading-vue-js tradingview-widget ``` 2. 在Vue组件中引入TradingViewTradingVue组件。在组件中可以设置TradingView的配置参数,如下所示: ```html <template> <div> <TradingVue :config="config"></TradingVue> </div> </template> <script> import TradingVue from 'trading-vue-js'; import 'trading-vue-js/dist/trading-vue.css'; export default { name: 'KlineChart', components: { TradingVue }, data() { return { config: { symbol: 'AAPL', interval: 'D', timezone: 'Asia/Shanghai', theme: 'dark', studies: ['MA@tv-basicstudies'], container_id: 'tv_chart_container', width: '100%', height: '100%', fullscreen: true } } }, mounted() { const script = document.createElement('script'); script.src = 'https://s3.tradingview.com/tv.js'; script.async = true; script.onload = () => { new TradingView.widget(this.config); }; document.head.appendChild(script); } } </script> ``` 3. 在组件中使用TradingVue组件并传入配置参数即可显示K线图。注意,还需要在mounted生命周期钩子中动态引入TradingView的js文件。 4. 可以根据自己的需求,调整TradingView的配置参数,如symbol(股票代码)、interval(K线图的时间间隔)、timezone(时区)、theme(主题)、studies(技术指标)等。 以上就是在Vue中引入TradingView制作K线图的简单步骤。希望可以帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值