2 lines
5.1 KiB
JavaScript
2 lines
5.1 KiB
JavaScript
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var constants=require('./constants'),utils=require('./utils'),utilsLite=require("utils-lite");require("echarts/lib/chart/bar"),require("echarts/lib/chart/line"),require("echarts/lib/chart/candlestick"),require("echarts/lib/component/visualMap"),require("echarts/lib/component/dataZoom");var Core=_interopDefault(require('./core')),DEFAULT_MA=[5,10,20,30],DEFAULT_K_NAME="日K",DEFAULT_DOWN_COLOR="#ec0000",DEFAULT_UP_COLOR="#00da3c",DEFAULT_START=50,DEFAULT_END=100,SHOW_FALSE={show:!1};function getCandleLegend(e){var t=e.showMA,a=e.MA,i=e.legendName,n=e.labelMap,o=[DEFAULT_K_NAME];return t&&(o=o.concat(a.map(function(e){return"MA"+e}))),n&&(o=o.map(function(e){return null==n[e]?e:n[e]})),{data:o,formatter:function(e){return null!=i[e]?i[e]:e}}}function getCandleTooltip(e){var t=e.metrics,a=e.dataType,i=e.digit,n=e.labelMap;return{trigger:"axis",axisPointer:{type:"cross"},position:function(e,t,a,i,n){var o={top:10};return o[e[0]<n.viewSize[0]/2?"right":"left"]=60,o},formatter:function(e){var o=[];return o.push(e[0].axisValue+"<br>"),e.forEach(function(e){var r=e.data,l=e.seriesName,s=e.componentSubType,u=e.color,d=null==n[l]?l:n[l];if(o.push(constants.itemPoint(u)+" "+d+": "),"candlestick"===s)o.push("<br>"),t.slice(0,4).forEach(function(e,t){var l=null!=n[e]?n[e]:e,s=utils.getFormated(r[t+1],a,i);o.push("- "+l+": "+s+"<br>")});else if("line"===s){var c=utils.getFormated(r,a,i);o.push(c+"<br>")}else if("bar"===s){var p=utils.getFormated(r[1],a,i);o.push(p+"<br>")}}),o.join("")}}}function getCandleVisualMap(e){var t=e.downColor,a=e.upColor,i=e.MA;return{show:!1,seriesIndex:e.showMA?1+i.length:1,dimension:2,pieces:[{value:1,color:t},{value:-1,color:a}]}}function getCandleGrid(e){return[{left:"10%",right:"8%",top:"10%",height:e.showVol?"50%":"65%",containLabel:!1},{left:"10%",right:"8%",top:"65%",height:"16%",containLabel:!1}]}function getCandleXAxis(e){var t=e.dims,a={onZero:!1};return[{type:"category",data:t,scale:!0,boundaryGap:!1,axisLine:a,splitLine:SHOW_FALSE,min:"dataMin",max:"dataMax"},{type:"category",gridIndex:1,data:t,scale:!0,boundaryGap:!1,axisLine:a,axisTick:SHOW_FALSE,splitLine:SHOW_FALSE,axisLabel:SHOW_FALSE,min:"dataMin",max:"dataMax"}]}function getCandleYAxis(e){var t=e.dataType,a=e.digit;return[{scale:!0,axisTick:SHOW_FALSE,axisLabel:{formatter:function(e){return utils.getFormated(e,t,a)}}},{scale:!0,gridIndex:1,splitNumber:2,axisLine:SHOW_FALSE,axisTick:SHOW_FALSE,splitLine:SHOW_FALSE,axisLabel:SHOW_FALSE}]}function getCandleDataZoom(e){var t=e.start,a=e.end;return[{type:"inside",xAxisIndex:[0,1],start:t,end:a},{show:!0,xAxisIndex:[0,1],type:"slider",top:"85%",start:t,end:a}]}function getCandleSeries(e){var t=e.values,a=e.volumes,i=e.upColor,n=e.downColor,o=e.showMA,r=e.MA,l=e.showVol,s=e.labelMap,u=e.digit,d=e.itemStyle||{normal:{color:i,color0:n,borderColor:null,borderColor0:null}},c={normal:{opacity:.5}},p=[{name:null==s[DEFAULT_K_NAME]?DEFAULT_K_NAME:s[DEFAULT_K_NAME],type:"candlestick",data:t,itemStyle:d}];return o&&r.forEach(function(e){var a="MA"+e;p.push({name:null==s[a]?a:s[a],data:calculateMA(e,t,u),type:"line",lineStyle:c,smooth:!0})}),l&&p.push({name:"Volume",type:"bar",xAxisIndex:1,yAxisIndex:1,data:a}),p}function calculateMA(e,t,a){var i=[];return t.forEach(function(n,o){if(o<e)i.push("-");else{for(var r=0,l=0;l<e;l++)r+=t[o-l][1];i.push(+(r/e).toFixed(a))}}),i}var candle=function(e,t,a,i){var n=a.dimension,o=void 0===n?e[0]:n,r=a.metrics,l=void 0===r?e.slice(1,6):r,s=a.digit,u=void 0===s?2:s,d=a.itemStyle,c=a.labelMap,p=void 0===c?{}:c,A=a.legendName,h=void 0===A?{}:A,f=a.MA,x=void 0===f?DEFAULT_MA:f,g=a.showMA,m=void 0!==g&&g,v=a.showVol,L=void 0!==v&&v,E=a.showDataZoom,M=void 0!==E&&E,_=a.downColor,b=void 0===_?DEFAULT_DOWN_COLOR:_,C=a.upColor,S=void 0===C?DEFAULT_UP_COLOR:C,y=a.start,F=void 0===y?DEFAULT_START:y,T=a.end,O=void 0===T?DEFAULT_END:T,D=a.dataType,w=i.tooltipVisible,U=i.legendVisible,N=utilsLite.isArray(t[0]),V=[],W=[],H=[],q=l.slice(0,4),I=l[4];N?t.forEach(function(t){var a=[];V.push(t[e.indexOf(o)]),q.forEach(function(i){a.push(t[e.indexOf(i)])}),W.push(a),I&&H.push(t[e.indexOf(I)])}):t.forEach(function(e,t){var a=[];if(V.push(e[o]),q.forEach(function(t){a.push(e[t])}),W.push(a),I){var i=e[l[0]]>e[l[1]]?1:-1;H.push([t,e[I],i])}});var k=U&&getCandleLegend({showMA:m,MA:x,legendName:h,labelMap:p}),K=w&&getCandleTooltip({metrics:l,dataType:D,digit:u,labelMap:p}),P=L&&getCandleVisualMap({downColor:b,upColor:S,MA:x,showMA:m}),R=M&&getCandleDataZoom({start:F,end:O});return{legend:k,tooltip:K,visualMap:P,grid:getCandleGrid({showVol:L}),xAxis:getCandleXAxis({dims:V}),yAxis:getCandleYAxis({dataType:D,digit:u}),dataZoom:R,series:getCandleSeries({values:W,volumes:H,upColor:S,downColor:b,showMA:m,MA:x,showVol:L,labelMap:p,digit:u,itemStyle:d}),axisPointer:{link:{xAxisIndex:"all"}}}},_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var a=arguments[t];for(var i in a)Object.prototype.hasOwnProperty.call(a,i)&&(e[i]=a[i])}return e},index=_extends({},Core,{name:"VeCandle",data:function(){return this.chartHandler=candle,{}}});module.exports=index;
|