var bgColor = "#ffffff" var labelColor = "#666"; //鏂囧瓧鑹 var upColor = '#ff0000';//娑ㄨ壊 var downColor = '#008b00';//璺岃壊 //MA棰滆壊 var ma5Color = "#39afe6"; var ma10Color = "#da6ee8"; var ma20Color = "#ffab42"; var ma30Color = "#00940b"; //杈规鑹 var borderColor = "#bebebe"; //=================================================MADC璁$畻鍏紡 var calcEMA, calcDIF, calcDEA, calcMACD; /* * 璁$畻EMA鎸囨暟骞虫粦绉诲姩骞冲潎绾匡紝鐢ㄤ簬MACD * @param {number} n 鏃堕棿绐楀彛 * @param {array} data 杈撳叆鏁版嵁 * @param {string} field 璁$畻瀛楁閰嶇疆 */ calcEMA = function (n, data, field) { var i, l, ema, a; a = 2 / (n + 1); if (field) { //浜岀淮鏁扮粍 ema = [data[0][field]]; for (i = 1, l = data.length; i < l; i++) { ema.push((a * data[i][field] + (1 - a) * ema[i - 1]).toFixed(2)); } } else { //鏅€氫竴缁存暟缁 ema = [data[0]]; for (i = 1, l = data.length; i < l; i++) { ema.push((a * data[i] + (1 - a) * ema[i - 1]).toFixed(3)); } } return ema; }; /* * 璁$畻DIF蹇嚎锛岀敤浜嶮ACD * @param {number} short 蹇€烢MA鏃堕棿绐楀彛 * @param {number} long 鎱㈤€烢MA鏃堕棿绐楀彛 * @param {array} data 杈撳叆鏁版嵁 * @param {string} field 璁$畻瀛楁閰嶇疆 */ calcDIF = function (short, long, data, field) { var i, l, dif, emaShort, emaLong; dif = []; emaShort = calcEMA(short, data, field); emaLong = calcEMA(long, data, field); for (i = 0, l = data.length; i < l; i++) { dif.push((emaShort[i] - emaLong[i]).toFixed(3)); } return dif; }; /* * 璁$畻DEA鎱㈢嚎锛岀敤浜嶮ACD * @param {number} mid 瀵筪if鐨勬椂闂寸獥鍙 * @param {array} dif 杈撳叆鏁版嵁 */ calcDEA = function (mid, dif) { return calcEMA(mid, dif); }; /* * 璁$畻MACD * @param {number} short 蹇€烢MA鏃堕棿绐楀彛 * @param {number} long 鎱㈤€烢MA鏃堕棿绐楀彛 * @param {number} mid dea鏃堕棿绐楀彛 * @param {array} data 杈撳叆鏁版嵁 * @param {string} field 璁$畻瀛楁閰嶇疆 */ calcMACD = function (short, long, mid, data, field) { var i, l, dif, dea, macd, result; result = {}; macd = []; dif = calcDIF(short, long, data, field); dea = calcDEA(mid, dif); for (i = 0, l = data.length; i < l; i++) { macd.push(((dif[i] - dea[i]) * 2).toFixed(3)); } result.dif = dif; result.dea = dea; result.macd = macd; return result; }; function calculateMA(dayCount, data) { var result = []; for (var i = 0, len = data.values.length; i < len; i++) { if (i < dayCount) { result.push('-'); continue; } var sum = 0; for (var j = 0; j < dayCount; j++) { var v = parseFloat(data.values[i - j][1]); sum += v; } result.push((sum / dayCount).toFixed(3)); } return result; } /** * K绾垮浘 * @param {*} data */ function createOptionKLine(data) { var macd = calcMACD(12, 26, 9, data.values, 1); var left = '5%'; var right = '5%' if ($(window).width() < 750) { left = '15%'; right = '0' } return { tooltip: { //寮规鎸囩ず鍣 trigger: 'axis', backgroundColor: "#f1f1f1", borderColor: borderColor, borderWidth: 1, textStyle: { color: '#333' }, axisPointer: { type: 'cross', label: { show: true, backgroundColor: '#333' } }, }, legend: { //鍥句緥鎺т欢,鐐瑰嚮鍥句緥鎺у埗鍝簺绯诲垪涓嶆樉绀 icon: 'rect', type: 'scroll', itemWidth: 14, itemHeight: 2, left: 0, top: '1%', selectedMode: false, textStyle: { fontSize: 12, color: labelColor } }, axisPointer: { show: true }, color: [ma5Color, ma10Color, ma20Color, ma30Color], grid: [{ show: true, borderColor: borderColor, id: 'gd1', left: left, right: right, height: '40%', //涓籏绾跨殑楂樺害, top: '9%' }, { show: true, borderColor: borderColor, left: left, right: right, top: '52%', height: '10%' //浜ゆ槗閲忓浘鐨勯珮搴 }, { show: true, borderColor: borderColor, left: left, right: right, top: '68%', //MACD 鎸囨爣 height: '10%' }], xAxis: [ //==== x杞 { //涓诲浘 type: 'category', data: data.categoryData, scale: true, boundaryGap: false, axisLine: { onZero: false }, axisLabel: { //label鏂囧瓧璁剧疆 show: false }, splitLine: { show: false, lineStyle: { color: '#3a3a3e' } }, splitNumber: 20, min: 'dataMin', max: 'dataMax' }, { //浜ゆ槗閲忓浘 type: 'category', gridIndex: 1, data: data.categoryData, axisLabel: { //label鏂囧瓧璁剧疆 color: '#9b9da9', fontSize: 10 }, }, { //骞呭浘 type: 'category', gridIndex: 2, data: data.categoryData, axisLabel: { show: false } } ], yAxis: [ //y杞 { //==涓诲浘 scale: true, z: 4, axisLabel: { //label鏂囧瓧璁剧疆 color: '#c7c7c7', inside: false, //label鏂囧瓧鏈濆唴瀵归綈 }, axisLine: { lineStyle: { color: borderColor, } }, splitLine: { //鍒嗗壊绾胯缃 show: true, lineStyle: { type: 'dashed' } } }, { //浜ゆ槗鍥 gridIndex: 1, splitNumber: 3, z: 4, axisLine: { onZero: false }, axisTick: { show: false }, splitLine: { show: false }, axisLabel: { //label鏂囧瓧璁剧疆 color: '#c7c7c7', inside: false, //label鏂囧瓧鏈濆唴瀵归綈 fontSize: 8 }, }, { //骞呭浘 z: 4, gridIndex: 2, splitNumber: 4, axisLine: { onZero: false }, axisTick: { show: false }, splitLine: { show: false }, axisLabel: { //label鏂囧瓧璁剧疆 color: '#c7c7c7', inside: false, //label鏂囧瓧鏈濆唴瀵归綈 fontSize: 8 }, } ], dataZoom: [{ type: 'slider', xAxisIndex: [0, 1, 2], //鎺т欢鑱斿姩 start: 100, end: 60, throttle: 10, top: '80%', height: '5%', borderColor: borderColor, textStyle: { color: '#dcdcdc' }, handleSize: '80%', //婊戝潡鍥炬爣 handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z', dataBackground: { lineStyle: { color: bgColor }, //鏁版嵁杈圭晫绾挎牱寮 areaStyle: { color: borderColor } //鏁版嵁鍩熷~鍏呮牱寮 } }], animation: false, //绂佹鍔ㄧ敾鏁堟灉 backgroundColor: bgColor, blendMode: 'source-over', series: [{ name: 'K绾垮懆鏈熷浘琛?, type: 'candlestick', data: data.values, barWidth: '55%', large: true, largeThreshold: 100, itemStyle: { normal: { color: upColor, //fd2e2e ff4242 color0: downColor, borderColor: upColor, borderColor0: downColor, } }, }, { name: 'MA5', type: 'line', data: calculateMA(5, data), smooth: true, symbol: "none", //闅愯棌閫変腑鏃舵湁灏忓渾鐐 lineStyle: { normal: { opacity: 0.8, color: '#39afe6', width: 1 } }, }, { name: 'MA10', type: 'line', data: calculateMA(10, data), smooth: true, symbol: "none", lineStyle: { //鏍囩嚎鐨勬牱寮 normal: { opacity: 0.8, color: '#da6ee8', width: 1 } } }, { name: 'MA20', type: 'line', data: calculateMA(20, data), smooth: true, symbol: "none", lineStyle: { opacity: 0.8, width: 1, color: ma20Color } }, { name: 'MA30', type: 'line', data: calculateMA(30, data), smooth: true, symbol: "none", lineStyle: { normal: { opacity: 0.8, width: 1, color: ma30Color } } }, { name: 'Volumn', type: 'bar', xAxisIndex: 1, yAxisIndex: 1, data: data.volumes, barWidth: '60%', itemStyle: { normal: { color: function (params) { var colorList; var values = data.values; var index = params.dataIndex; if (values[index][1] > values[index][0]) { colorList = upColor; } else { colorList = downColor; } return colorList; }, } } }, { name: 'MACD', type: 'bar', xAxisIndex: 2, yAxisIndex: 2, data: macd.macd, barWidth: '40%', itemStyle: { normal: { color: function (params) { var colorList; if (params.data >= 0) { colorList = upColor; } else { colorList = downColor; } return colorList; }, } } }, { name: 'DIF', type: 'line', symbol: "none", xAxisIndex: 2, yAxisIndex: 2, data: macd.dif, lineStyle: { normal: { color: '#da6ee8', width: 1 } } }, { name: 'DEA', type: 'line', symbol: "none", xAxisIndex: 2, yAxisIndex: 2, data: macd.dea, lineStyle: { normal: { opacity: 0.8, color: '#39afe6', width: 1 } } }] }; } /** * 15:20 鏃碟鍒 鏍煎紡鏃堕棿澧炲姞num鍒嗛挓 * @param {Object} time 璧峰鏃堕棿 * @param {Object} num */ function addTimeStr(time, num) { var hour = time.split(':')[0]; var mins = Number(time.split(':')[1]); var mins_un = parseInt((mins + num) / 60); var hour_un = parseInt((Number(hour) + mins_un) / 24); if (mins_un > 0) { if (hour_un > 0) { var tmpVal = ((Number(hour) + mins_un) % 24) + ""; hour = tmpVal.length > 1 ? tmpVal : '0' + tmpVal;//鍒ゆ柇鏄惁鏄竴浣 } else { var tmpVal = Number(hour) + mins_un + ""; hour = tmpVal.length > 1 ? tmpVal : '0' + tmpVal; } var tmpMinsVal = ((mins + num) % 60) + ""; mins = tmpMinsVal.length > 1 ? tmpMinsVal : 0 + tmpMinsVal;//鍒嗛挓鏁颁负 鍙栦綑60鐨勬暟 } else { var tmpMinsVal = mins + num + ""; mins = tmpMinsVal.length > 1 ? tmpMinsVal : '0' + tmpMinsVal;//涓嶅ぇ浜庢暣闄?0 } return hour + ":" + mins; } //鑾峰彇澧炲姞鎸囧畾鍒嗛挓鏁扮殑 鏁扮粍 濡 09:30澧炲姞2鍒嗛挓 缁撴灉涓 ['09:31','09:32'] function getNextTime(startTime, endTIme, offset, resultArr) { var result = addTimeStr(startTime, offset); resultArr.push(result); if (result == endTIme) { return resultArr; } else { return getNextTime(result, endTIme, offset, resultArr); } } /** * 涓嶅悓绫诲瀷鐨勮偂绁ㄧ殑浜ゆ槗鏃堕棿浼氫笉鍚 * @param {Object} type hs=娌繁 us=缇庤偂 hk=娓偂 */ var time_arr = function (type) { if (type.indexOf('us') != -1) {//鐢熸垚缇庤偂鏃堕棿娈 var timeArr = new Array(); timeArr.push('09:30') return getNextTime('09:30', '16:00', 1, timeArr); } if (type.indexOf('hs') != -1) {//鐢熸垚娌繁鏃堕棿娈 var timeArr = new Array(); timeArr.push('09:30'); timeArr.concat(getNextTime('09:30', '11:30', 1, timeArr)); timeArr.push('13:00'); timeArr.concat(getNextTime('13:00', '15:00', 1, timeArr)); return timeArr; } if (type.indexOf('hk') != -1) {//鐢熸垚娓偂鏃堕棿娈 var timeArr = new Array(); timeArr.push('09:30'); timeArr.concat(getNextTime('09:30', '11:59', 1, timeArr)); timeArr.push('13:00'); timeArr.concat(getNextTime('13:00', '16:00', 1, timeArr)); return timeArr; } } var get_m_data = function (m_data, type) { var priceArr = new Array(); var vol = new Array(); var times = time_arr(type); $.each(m_data.data, function (i, v) { priceArr.push(v[1]); vol.push(v[2]); //鐩墠鏁版嵁娌℃湁鍧囦环锛屽彇鍊兼彁鍓嶄竴浣 }) return { priceArr: priceArr, vol: vol, times: times } } /** * 鐢熸垚鍒嗘椂option * @param {Object} m_data 鍒嗘椂鏁版嵁 * @param {Object} type 鑲$エ绫诲瀷 us-缇庤偂 hs-娌繁 hk-娓偂 */ function initMOption(m_data, type) { var m_datas = get_m_data(m_data, type); var baseNumber = Number(m_data.yestclose).toFixed(2) var _minVal = Number(baseNumber - baseNumber * handle_num()).toFixed(2); var _maxVal = (Number(baseNumber) + baseNumber * handle_num()).toFixed(2); var _interval = Math.abs(Number((baseNumber - _minVal) / 5)); var left = '5%'; var right = '5%' if ($(window).width() < 750) { left = '15%'; right = '15%' } function handle_num() { var _aa = Math.abs((Math.max.apply(null, m_datas.priceArr) - baseNumber) / baseNumber).toFixed(2); var _bb = Math.abs((baseNumber - Math.min.apply(null, m_datas.priceArr)) / baseNumber).toFixed(2); return _aa > _bb ? _aa : _bb; } function format_y(v) { v = v.toFixed(2) if (v > m_data.yestclose) { return '{red|' + v + '}'; } else if (v == baseNumber) { return v; } else { return '{green|' + v + '}'; } } return { tooltip: { //寮规鎸囩ず鍣 trigger: 'axis', backgroundColor: "#f1f1f1", borderColor: "#ccc", borderWidth: 1, textStyle: { color: '#333' }, axisPointer: { type: 'cross', label: { show: true, backgroundColor: '#333' } }, formatter: function (params, ticket, callback) { var i = params[0].dataIndex; var color; if (m_datas.priceArr[i] > m_data.yestclose) { color = 'style="color:' + upColor + '"'; } else { color = 'style="color:' + downColor + '"'; } var html = '
\
褰撳墠浠 ' + m_datas.priceArr[i] + '
\
娑ㄥ箙 ' + ratioCalculate(m_datas.priceArr[i], m_data.yestclose) + '%
\
鎴愪氦閲 ' + m_datas.vol[i] + '
'; return html; } }, legend: { //鍥句緥鎺т欢,鐐瑰嚮鍥句緥鎺у埗鍝簺绯诲垪涓嶆樉绀 icon: 'rect', type: 'scroll', itemWidth: 14, itemHeight: 2, selectedMode: false, left: 0, top: '1%', textStyle: { fontSize: 12, color: labelColor } }, color: [ma5Color, ma10Color], grid: [{ show: true, borderColor: borderColor, id: 'gd1', height: '63%', //涓籏绾跨殑楂樺害, left: left, right: right, top: '9%' }, { show: true, borderColor: borderColor, id: 'gd2', height: '63%', //涓籏绾跨殑楂樺害, left: left, right: right, top: '9%' }, { show: true, borderColor: borderColor, id: 'gd3', left: left, right: right, top: '76%', height: '19%' //浜ゆ槗閲忓浘鐨勯珮搴 }], xAxis: [ //==== x杞 { //涓诲浘 gridIndex: 0, boundaryGap: false, axisTick: { show: false }, axisLine: { lineStyle: { color: borderColor, } }, data: m_datas.times, axisLabel: { //label鏂囧瓧璁剧疆 show: false }, splitLine: { //鍒嗗壊绾胯缃 show: true, lineStyle: { type: 'dashed' } }, }, { show: false, gridIndex: 1, boundaryGap: false, data: m_datas.times, axisLabel: { //label鏂囧瓧璁剧疆 show: false }, axisLine: { lineStyle: { color: borderColor, } }, }, { //浜ゆ槗閲忓浘 splitNumber: 2, type: 'category', gridIndex: 2, boundaryGap: false, data: m_datas.times, axisLabel: { //label鏂囧瓧璁剧疆 color: labelColor, fontSize: 10 }, axisTick: { show: false }, splitLine: { //鍒嗗壊绾胯缃 show: true, lineStyle: { type: 'dashed' } }, axisLine: { lineStyle: { color: borderColor, } } } ], yAxis: [ //y杞 { type: 'value', min: _minVal, max: _maxVal, interval: _interval, gridIndex: 0, scale: true, axisTick: { // 鍒嗗壊绾 鐭 show: false }, axisLine: { lineStyle: { color: borderColor, } }, axisPointer: { show: true, label: { formatter: function (params) { return (params.value).toFixed(2); } } }, axisLabel: { color: '#333', formatter: format_y, rich: { red: { color: 'red', lineHeight: 10 }, green: { color: 'green', lineHeight: 10 } } }, z: 4, splitLine: { //鍒嗗壊绾胯缃 show: true, lineStyle: { type: 'dashed' } }, }, { scale: true, gridIndex: 1, min: _minVal, max: _maxVal, interval: _interval, position: 'right', z: 4, axisTick: { show: false }, axisLine: { lineStyle: { color: borderColor, } }, axisLabel: { //label鏂囧瓧璁剧疆 color: function (val) { val = Number(val).toFixed(2) if (val == baseNumber) { return '#333' } return val > baseNumber ? upColor : downColor; }, inside: false, //label鏂囧瓧鏈濆唴瀵归綈 formatter: function (val) { var resul = ratioCalculate(val, baseNumber); return Number(resul).toFixed(2) + '%' } }, splitLine: { //鍒嗗壊绾胯缃 show: false, lineStyle: { color: '#181a23' } }, axisPointer: { show: true, label: { formatter: function (params) { //璁$畻鍙宠竟Y杞村搴旂殑褰撳墠浠风殑娑ㄥ箙姣斾緥 return ratioCalculate(params.value, baseNumber) + '%'; } } } }, { //浜ゆ槗鍥 // name: '涓?, nameGap: '0', nameTextStyle: { color: labelColor }, gridIndex: 2, z: 4, splitNumber: 3, axisLine: { onZero: false, lineStyle: { color: borderColor, } }, axisTick: { show: false }, axisPointer: { show: false, label: { formatter: function (params) { //璁$畻鍙宠竟Y杞村搴旂殑褰撳墠浠风殑娑ㄥ箙姣斾緥 var _p = ((params.value) / 10000).toFixed(1) + '涓?; return _p } } }, splitLine: { //鍒嗗壊绾胯缃 show: false, }, axisLabel: { //label鏂囧瓧璁剧疆 color: labelColor, inside: false, //label鏂囧瓧鏈濆唴瀵归綈 fontSize: 10, onZero: false, formatter: function (params) { //璁$畻鍙宠竟Y杞村搴旂殑褰撳墠浠风殑娑ㄥ箙姣斾緥 var _p = (params / 10000).toFixed(1); if (params == 0) { _p = '(涓?' } return _p } }, } ], backgroundColor: bgColor, blendMode: 'source-over', series: [ { name: '褰撳墠浠?, type: 'line', data: m_datas.priceArr, smooth: true, symbol: "circle", //涓椂鏈夊皬鍦嗙偣 lineStyle: { normal: { opacity: 0.8, color: '#39afe6', width: 1 } }, areaStyle: { normal: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(0, 136, 212, 0.7)' }, { offset: 0.8, color: 'rgba(0, 136, 212, 0.02)' }], false), shadowColor: 'rgba(0, 0, 0, 0.1)', shadowBlur: 10 } }, markLine: { name: '鏄ㄦ棩鏀剁洏浠?, symbol: ['none', 'none'], label: { show: false, formatter: Number(m_data.yestclose).toFixed(2), position: 'start', }, lineStyle: { color: '#4289c5', type: 'solid' }, data: [{ yAxis: m_data.yestclose, }] } }, { type: 'line', data: m_datas.priceArr, smooth: true, symbol: "none", gridIndex: 1, xAxisIndex: 1, yAxisIndex: 1, lineStyle: { //鏍囩嚎鐨勬牱寮 normal: { width: 0 } } }, { name: '鎴愪氦閲?, type: 'bar', gridIndex: 2, xAxisIndex: 2, yAxisIndex: 2, data: m_datas.vol, barWidth: '60%', itemStyle: { normal: { color: function (params) { var colorList; if (m_datas.priceArr[params.dataIndex] > m_datas.priceArr[params.dataIndex - 1]) { colorList = upColor; } else { colorList = downColor; } return colorList; }, } } } ] }; } /** * 璁$畻浠锋牸娑ㄨ穼骞呯櫨鍒嗘瘮 * @param {Object} price 褰撳墠浠 * @param {Object} yclose 鏄ㄦ敹浠 */ function ratioCalculate(price, yclose) { return ((price - yclose) / yclose * 100).toFixed(2); }