Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.   <div ref="container" class="chart-container">
  3.     <div class="canvas-container">
  4.       <div class="currency">
  5.         <img ref="iconCurrency" :src="'/img/currency-'+currency+'.png'">
  6.       </div>
  7.       <div class="canvas-charts">
  8.         <canvas class="canvas-hover" ref="canvasHover"></canvas>
  9.         <canvas class="canvas-liquid" ref="canvasSell"/>
  10.         <canvas class="canvas-liquid" ref="canvasBuy"/>
  11.       </div>
  12.       <div class="nav-zoom">
  13.         <button @click="zoom(false)" :class="{'disabled-zoom':disabledZoomM}" :disabled="disabledZoomM"><c-icon name="zoom+"></c-icon></button>
  14.         <button @click="zoom(true)" :class="{'disabled-zoom':disabledZoomP}" :disabled="disabledZoomP"><c-icon name="zoom-"></c-icon></button>
  15.       </div>
  16.     </div>
  17.   </div>
  18. </template>
  19.  
  20. <style scoped lang="scss">
  21.   @import "~assets/styles/base.scss";
  22.  
  23.   .bucketList {
  24.     position: absolute;
  25.     color: $color-base3;
  26.     background-color: $color-base0;
  27.     width: 100%;
  28.     height: 100%;
  29.     overflow-y: auto;
  30.     ul {
  31.       list-style: none;
  32.     }
  33.   }
  34.  
  35.   .chart-container {
  36.     flex: 1 0 auto;
  37.     display: flex;
  38.     flex-direction: column;
  39.     position: relative;
  40.     margin-right: 5px;
  41.     .currency{
  42.       color: white;
  43.       fill: white;
  44.       display: none;
  45.     }
  46.     .nav-zoom{
  47.       position: absolute;
  48.       top: 41%;
  49.       left: 70px;
  50.       width: 20px;
  51.       height: 70px;
  52.       button{
  53.         width: 20px;
  54.         height: 20px;
  55.         padding: 0;
  56.         background: none;
  57.         border: none;
  58.         color: $color-base3-dark;
  59.         &.disabled-zoom{
  60.           color: $color-base1;
  61.         }
  62.       }
  63.       :first-child{
  64.         margin-bottom: 10px;
  65.       }
  66.       :last-child{
  67.         margin-top: 10px;
  68.       }
  69.     }
  70.   }
  71.  
  72.   .canvas-container {
  73.     position: relative;
  74.     min-height: 500px;
  75.   }
  76.  
  77.   .chart-text {
  78.     width: 50%;
  79.     padding: 0;
  80.     margin: 0;
  81.     margin-left: 50%;
  82.     padding-left: 5px;
  83.     position: absolute;
  84.     color: $color-base0;
  85.     li {
  86.       list-style: none;
  87.       line-height: 19px;
  88.       font-size: 15px;
  89.       .item {
  90.  
  91.       }
  92.       .item--red {
  93.         color: $color-negative0;
  94.       }
  95.     }
  96.   }
  97.   .canvas-charts{
  98.     position: relative;
  99.     height: 500px;
  100.   }
  101.   canvas.canvas-hover{
  102.     position: absolute;
  103.     width: 100%;
  104.     min-height: 500px;
  105.     height: 100%;
  106.   }
  107.   canvas.canvas-liquid {
  108.     display: block;
  109.     width: 100%;
  110.     max-height: 250px;
  111.     height: 100%;
  112.   }
  113. </style>
  114.  
  115. <script>
  116.   import {mapState} from 'vuex'
  117.   import cloneL from 'lodash/clone'
  118.   import cloneLD from 'lodash/cloneDeep'
  119.   export default {
  120.     props:{
  121.       currency: {
  122.         type: String,
  123.         default: 'usd'
  124.       },
  125.       rawData: {
  126.         type: Array,
  127.         default: []
  128.       },
  129.     },
  130.     data: () => ({
  131.       sizePoint: 2,
  132.       groupingUnit: 0,
  133.       disabledZoomP: true,
  134.       disabledZoomM: true,
  135.       cxSell: undefined,
  136.       cxBuy: undefined,
  137.  
  138.       pointsBuy: undefined,
  139.       pointsSell: undefined,
  140.  
  141.       maxVolume: null,
  142.       minVolume: null,
  143.     }),
  144.     mounted () {
  145.       this.handleDepth(this.rawData);
  146.  
  147.       this.getWindowWidth();
  148.       this.$nextTick(function () {
  149.         window.addEventListener('resize', this.getWindowWidth);
  150.       });
  151.     },
  152.     beforeDestroy() {
  153.       window.removeEventListener('resize', this.getWindowWidth);
  154.     },
  155.     computed: {
  156.       ...mapState({
  157.         positive: state => state.views.positive,
  158.         negative: state => state.views.negative,
  159.         positiveOp: state => state.views.positiveOp4,
  160.         negativeOp: state => state.views.negativeOp4,
  161.         base3: state => state.views.base3,
  162.         base3Dark: state => state.views.base3Dark,
  163.       }),
  164.     },
  165.     watch:{
  166.       navBar: function(val) {
  167.         this.getWindowWidth();
  168.       },
  169.     },
  170.     methods: {
  171.       redraw() {
  172.         if (this.cxSell === undefined) {this.cxSell = this.$refs.canvasSell.getContext('2d');}
  173.         if (this.cxBuy === undefined) {this.cxBuy = this.$refs.canvasBuy.getContext('2d');}
  174.         let cxSell = this.cxSell;
  175.         let cxBuy = this.cxBuy;
  176.         cxSell.clearRect(-0.5, -0.5, this.canvasWidth, this.canvasHeight);
  177.         cxBuy.clearRect(-0.5, -0.5, this.canvasWidth, this.canvasHeight);
  178.  
  179.       },
  180.       getWindowWidth() {
  181.         this.updateSize();
  182.         this.redraw();
  183.       },
  184.       updateSize () {
  185.         let cSell = this.$refs.canvasSell;
  186.         let cBuy = this.$refs.canvasBuy;
  187.         let c2 = this.$refs.canvasHover;
  188.         cSell.width = cSell.offsetWidth;
  189.         cSell.height = cSell.offsetHeight;
  190.         cBuy.width = cSell.offsetWidth;
  191.         cBuy.height = cSell.offsetHeight;
  192.         c2.width = cSell.offsetWidth;
  193.         c2.height = cSell.offsetHeight*2;
  194.         this.canvasWidth = cSell.width;
  195.         this.canvasHeight = cSell.height;
  196.       },
  197.       drawLiquidChart(cx, points, revers = false){
  198.  
  199.  
  200.       },
  201.       handleDepth(data){
  202.         let newData = cloneL(data);
  203.         newData.sort(this.sortList);
  204.         let buy = this.clean(newData, 'buy');
  205.         let sell = this.clean(newData, 'sell');
  206.         if (this.groupingUnit > 0) {
  207.           buy = this.group(buy);
  208.           sell = this.group(sell);
  209.         }
  210.         this.pointsBuy = this.accumulate(buy);
  211.         this.pointsSell = this.accumulate(sell, true);
  212.         this.maxVolume = Math.max(this.pointsSell[0][1], this.last(this.pointsBuy)[1]);
  213.         this.minVolume = Math.min(this.last(this.pointsSell)[1], this.pointsBuy[0][1]);
  214.       },
  215.       clean(data, type){
  216.         let newData = [];
  217.         for(let i = 0; i < data.length; i++){
  218.           if(data[i].action === type){
  219.             newData.push([data[i].price, data[i].amount]);
  220.           }
  221.         }
  222.         return newData
  223.       },
  224.       group(from) {
  225.         let j = 0, newData = [];
  226.         for (let i = this.floor(from[0][0], this.groupingUnit); i >= this.floor(this.last(from)[0], this.groupingUnit); i -= this.groupingUnit) {
  227.           let volume = 0;
  228.           while (j < from.length && this.floor(from[j][0], this.groupingUnit) === parseFloat(i.toFixed(2))) {
  229.             volume += from[j][1];
  230.             j++;
  231.           }
  232.           if(volume !== 0){
  233.             newData.push([parseFloat(i.toFixed(2)), volume]);
  234.           }
  235.         }
  236.         return newData;
  237.       },
  238.       floor(number, unit) {
  239.         return parseFloat((Math.floor(number / unit) * unit).toFixed(2))
  240.       },
  241.       last(array) {
  242.         return array[array.length - 1]
  243.       },
  244.       accumulate(arrayL, reverse) {
  245.         let array = cloneLD(arrayL);
  246.         reverse = typeof reverse !== 'undefined' ? reverse : false;
  247.  
  248.         let start = reverse ? array.length - 2 : 1,
  249.           end = reverse ? -1 : array.length,
  250.           step = reverse ? -1 : 1;
  251.  
  252.         for (let i = start; reverse ? i > end : i < end; i += step) {
  253.           array[i][1] += array[i - step][1]
  254.         }
  255.         return array;
  256.       },
  257.       sortList(a, b) {
  258.         if (a.price < b.price) {
  259.           return 1
  260.         }
  261.         if (a.price > b.price) {
  262.           return -1
  263.         }
  264.         return 0;
  265.       },
  266.     },
  267.   }
  268. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement