Created
April 24, 2017 14:12
-
-
Save webuti/0039b910cf7275c8e5f79682bb066cf7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
agappname.factory('gChart', function() { | |
var gChart; | |
var func = {}; | |
func.destroy = function(){ | |
gChart = $('#chartContainer').StockChartX(); | |
gChart.destroy(); | |
} | |
func.parseBars = function(data, isIntraday) { | |
var dataSeries = gChart.barDataSeries(); | |
var lines = data.split("\n"); | |
lines.reverse(); | |
$.each(lines, function (index, line) { | |
var values = line.split(","), | |
date = moment(values[0], "D-MMM-YY").toDate(); | |
dataSeries.date.add(date); | |
dataSeries.open.add(parseFloat(values[1])); | |
dataSeries.high.add(parseFloat(values[2])); | |
dataSeries.low.add(parseFloat(values[3])); | |
dataSeries.close.add(parseFloat(values[4])); | |
dataSeries.volume.add(parseInt(values[5], 10)); | |
}); | |
gChart.updateComputedDataSeries(); | |
}, | |
func.loadChart = function() { | |
var symbolsFilePath = StockChartX.Environment.isMobile ? "assets/chart/data/symbols.mobile.json" : "assets/chart/data/symbols.json"; | |
$.get(symbolsFilePath, function (symbols) { | |
StockChartX.getAllInstruments = function () { | |
return symbols; | |
} | |
}); | |
gChart = $('#chartContainer').StockChartX({ | |
width: 768, | |
height: 460, | |
theme: StockChartX.Theme.Light, | |
onToolbarLoaded: function () { | |
if (window.innerHeight < gChart.size.height) { | |
gChart.size = { | |
width: gChart.size.width, | |
height: window.innerHeight | |
}; | |
gChart.update(); | |
} | |
if ((StockChartX.Environment.isMobile && $(window).width() < 768) || StockChartX.Environment.isPhone) { | |
var jToolbar = gChart.container.find('.scxToolbar'); | |
jToolbar.find('.scxToolbarViewMode').remove(); | |
var jLastChild = jToolbar.children().last(); | |
jLastChild.hasClass('scxToolbar-delimiter') && jLastChild.remove(); | |
} | |
} | |
}); | |
if (!StockChartX.Environment.isPhone) { | |
var myIndicator = new MyCustomMACD(); | |
gChart.addIndicators([myIndicator, TASdk.RelativeStrengthIndex, TASdk.BollingerBands]); | |
} | |
var ind = gChart.addIndicators(StockChartX.VolumeIndicator); | |
ind.setParameterValue(StockChartX.IndicatorParam.LINE_WIDTH, 5); | |
gChart.on(StockChartX.ChartEvent.SYMBOL_ENTERED, function (event) { | |
// TODO: Load data for the new symbol | |
gChart.showWaitingBar(); | |
gChart.instrument = event.value; | |
setTimeout(function () { | |
gChart.update(); | |
gChart.hideWaitingBar(); | |
}, 2000); | |
}); | |
gChart.on(StockChartX.ChartEvent.TIME_FRAME_CHANGED, function (event) { | |
// TODO: Process time frame change | |
console.log(event.value.interval + ' ' + event.value.periodicity); | |
}); | |
gChart.on(StockChartX.ChartEvent.MORE_HISTORY_REQUESTED, function () { | |
console.log("TODO: Load more history!"); | |
}); | |
gChart.showWaitingBar(); | |
var dataFilePath = StockChartX.Environment.isMobile ? "assets/chart/data/aapl-1day.mobile.csv" : "assets/chart/data/aapl-1day.csv"; | |
$.get(dataFilePath, function (data) { | |
func.parseBars(data, false); | |
gChart.instrument = { | |
symbol: "GOOG", | |
company: "Google Inc.", | |
exchange: "NASDAQ" | |
}; | |
gChart.timeInterval = StockChartX.TimeSpan.MILLISECONDS_IN_DAY; | |
gChart.updateIndicators(); | |
!StockChartX.Environment.isMobile && gChart.recordRange(1000); | |
gChart.setNeedsUpdate(true); | |
gChart.hideWaitingBar(); | |
}).fail(function () { | |
gChart.hideWaitingBar(); | |
alert('Unable to load data file.'); | |
}); | |
} | |
return func; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment