'use strict';
define(['app', 'jquery', 'highstock'], function (app, $, Highcharts) {
app.directive('highStock', function () {
/**
* @return {number}
*/
//算移动均线
var MovingAverage = function (data, i, n) {
if (i >= n - 1) {
var sum = 0;
for (var j = 0; j <= n - 1; j++) {
sum = sum + parseFloat(data[i - j].ClosingPrice);
}
return sum / n;
} else {
return null;
}
};
// 处理转换数据
var handleSeries = function (data) {
var dataArray = {ohlcArray: [], volumeArray: [], MA5Array: [], MA10Array: [], MA30Array: []};
for (var i = 0; i < data.length; i++) {
dataArray.ohlcArray.push([
parseInt(data[i].Date * 1000), // the date
parseFloat(data[i].OpeningPrice), // open
parseFloat(data[i].HighestPrice), // high
parseFloat(data[i].LowestPrice), // low
parseFloat(data[i].ClosingPrice) // close
]);
dataArray.MA5Array.push([
parseInt(data[i].Date * 1000), // the date
MovingAverage(data, i, 5)
]);
dataArray.MA10Array.push([
parseInt(data[i].Date * 1000),
MovingAverage(data, i, 10)
]);
dataArray.MA30Array.push([
parseInt(data[i].Date * 1000),
MovingAverage(data, i, 30)
]);
dataArray.volumeArray.push([
parseInt(data[i].Date * 1000), // the date
parseInt(data[i].Volume) // 成交量
]);
}
var last = data.length - 1;
if (last + 1 <= 90 && $(window).width() > 768) {
for (i = 1; i <= 90 - last - 1; i++) {
var date = data[last].Date * 1000 + i * 24 * 60 * 60 * 1000;
dataArray.ohlcArray.push([
date, // the date
null, // open
null, // high
null, // low
null // close
]);
}
}
return dataArray;
};
//画图表
var getMergedOptions = function (element, options, series) {
var defaultOptions = {
chart: {
renderTo: element[0],
margin: [10, 10, 15, 10],
plotBorderColor: '#E7F2F8',
plotBorderWidth: 0
},
title: {
enabled: false
},
subtitle: {
enabled: false
},
exporting: {
enabled: false
},
credits: {
enabled: false
},
rangeSelector: {
enabled: false
},
plotOptions: {
//修改蜡烛颜色
candlestick: {
color: '#33AA11',
upColor: '#DD2200',
lineColor: '#33AA11',
upLineColor: '#DD2200',
maker: {
states: {
hover: {
enabled: false
}
}
}
},
//去掉曲线和蜡烛上的hover事件
series: {
states: {
hover: {
enabled: false
}
},
line: {
marker: {
enabled: false
}
}
}
},
tooltip: {
formatter: function () {
if (this.y == undefined) {
return;
}
for (var i = 0; i < series.length; i++) {
if (this.x == series[i].Date * 1000) {
var zde = parseFloat(series[i].ClosingPrice - series[i].OpeningPrice).toFixed(intFormatFloat);
//zdf = parseFloat((data[i].ClosingPrice - data[i].OpeningPrice) / data[i].OpeningPrice).toFixed(intFormatFloat);
var zdf = (series[i].ClosingPrice - series[i].OpeningPrice) / series[i].OpeningPrice;
}
}
var open = this.points[0].point.open.toFixed(4);
var high = this.points[0].point.high.toFixed(4);
var low = this.points[0].point.low.toFixed(4);
var close = this.points[0].point.close.toFixed(4);
var y = this.points[1].point.y;
var tip = '' + Highcharts.dateFormat('%Y-%m-%d %A', this.x) + '
';
tip += '开盘价:' + open + '
';
tip += '收盘价:' + close + '
';
tip += '最高价:' + high + '
';
tip += '最低价:' + low + '
';
if (open < close) {
tip += '涨跌额:' + zde + '
';
tip += '涨跌幅:' + (zdf * 100).toFixed(2) + '%
';
} else
if (open > close) {
tip += '涨跌额:' + zde + '
';
tip += '涨跌幅:' + (zdf * 100).toFixed(2) + '%
';
} else {
tip += '涨跌额:' + zde + '
';
tip += '涨跌幅:' + (zdf * 100).toFixed(2) + '%
';
}
if (y > 10000 * 10000) {
tip += "成交量:" + (y * 0.00000001).toFixed(4) + "亿
";
} else if (y > 10000) {
tip += "成交量:" + (y * 0.0001).toFixed(4) + "万
";
} else if(isNaN(y)){
tip += "成交量:" + 0 + "
";
}else {
tip += "成交量:" + y + "
";
}
return tip;
},
borderColor: 'white',
shadow: true
},
navigator: {
//enabled: false,
adaptToUpdatedData: false,
xAxis: {
labels: {
formatter: function () {
return Highcharts.dateFormat('%m-%d', this.value);
}
}
},
handles: {
backgroundColor: '#808080'
}
// margin: 0
},
xAxis: {
type: 'datetime',
tickLength: 0,
events: {
afterSetExtremes: function (e) {
var minTime = Highcharts.dateFormat("%Y-%m-%d", e.min);
var maxTime = Highcharts.dateFormat("%Y-%m-%d", e.max);
var chart = this.chart;
}
},
labels: {
formatter: function () {
return Highcharts.dateFormat('%m-%d', this.value);
}
}
},
yAxis: [{
title: {
enable: false
},
height: '74%',
lineWidth: 1,
gridLineColor: '#346691',
gridLineWidth: 0.1,
opposite: true
}, {
title: {
enable: false
},
top: '80%',
height: '20%',
offset: 0,
gridLineColor: '#346691',
gridLineWidth: 0.1,
lineWidth: 1
}],
series: []
};
var mergedOptions = {};
if (options) {
mergedOptions = $.extend(true, {}, defaultOptions, options);
} else {
mergedOptions = defaultOptions;
}
if (series) {
mergedOptions.series = series
}
return mergedOptions
};
return {
restrict: 'EC',
scope: {
series: '=',
options: '=',
title: '='
},
replace: true,
template: '