Skip to content

Instantly share code, notes, and snippets.

@sarupbanskota
Last active February 24, 2016 12:16
Show Gist options
  • Save sarupbanskota/8bdd6ca9d38193d06746 to your computer and use it in GitHub Desktop.
Save sarupbanskota/8bdd6ca9d38193d06746 to your computer and use it in GitHub Desktop.
NAV chart normalizing
@generate_nav_growth_chart = (report=false, dataProvider=APP.portfolios)->
nav_chart = new AmCharts.AmStockChart()
nav_chart.pathToImages = "/amchart_images/"
nav_chart.dataDateFormat = "YYYY-MM-DD"
dataSet = new AmCharts.DataSet()
dataSet.title = "NAV"
dataSet.fieldMappings = [
{
fromField: "nav_today"
toField: "close"
}
]
dataSet.color = "#87939F"
dataSet.dataProvider = dataProvider
dataSet.categoryField = "traded_on"
dataSet.showInSelect = false
nav_chart.dataSets.push(dataSet)
stockPanel = new AmCharts.StockPanel()
stockPanel.title = "NAV"
stockPanel.showCategoryAxis = true
stockPanel.categoryAxis.dashLength = 0
nav_chart.addPanel(stockPanel)
if !report
legend = new AmCharts.AmLegend()
legend.equalWidths = false
legend.periodValueText = "[[value.close]]"
legend.position = "bottom"
legend.valueAlign = "left"
legend.valueWidth = 100
stockPanel.addLegend(legend)
graph = new AmCharts.StockGraph()
graph.id = "g1"
graph.fillAlphas = 0
graph.lineAlpha = 1
graph.lineThickness = 3
graph.comparable = true
graph.compareGraphLineThickness = 3
graph.compareField = "close"
graph.title = "Portfolio NAV"
graph.valueField = "close"
graph.periodValue = "Open"
stockPanel.addStockGraph(graph)
chartCursor = new AmCharts.ChartCursor()
chartCursor.cursorAlpha = 0
nav_chart.chartCursor = chartCursor
panelsSettings = new AmCharts.PanelsSettings()
panelsSettings.panEventsEnabled = true
panelsSettings.marginLeft = if report then 10 else 35
panelsSettings.marginRight = if report then 53 else 10
panelsSettings.marginBottom = 5
panelsSettings.plotAreaBorderAlpha = 0
panelsSettings.plotAreaBorderColor = "#CACACA"
panelsSettings.fontFamily = "Roboto"
nav_chart.panelsSettings = panelsSettings
value_axis = new AmCharts.ValueAxis()
value_axis.position = if report then "right" else "left"
value_axis.usePrefixes= true
stockPanel.addValueAxis(value_axis)
cursorSettings = new AmCharts.ChartCursorSettings()
cursorSettings.pan = !$("#frm-search").is(":visible")
cursorSettings.valueBalloonsEnabled = true
nav_chart.chartCursorSettings = cursorSettings
nav_chart.valueAxesSettings.inside = false
nav_chart.valueAxesSettings.gridAlpha = 0
periodSelector = new AmCharts.PeriodSelector()
periodSelector.position = "top"
periodSelector.periodsText = ""
periodSelector.periods = [
{
period: "DD"
count: 10
label: "10d"
}
{
period: "MM"
count: 1
label: "1m"
}
{
period: "YYYY"
selected: !report
count: 1
label: "1y"
}
{
period: "YTD"
selected: report
label: "YTD"
}
{
period: "MAX"
label: "MAX"
}
]
nav_chart.chartScrollbarSettings.graph = "g1"
nav_chart.chartScrollbarSettings.graphType = "line"
nav_chart.chartScrollbarSettings.usePeriod = "WW"
nav_chart.chartScrollbarSettings.enabled = false if report
nav_chart.categoryAxesSettings.maxSeries = 365
nav_chart.categoryAxesSettings.gridAlpha = 0
nav_chart.categoryAxesSettings.axisAlpha = 0.3
nav_chart.periodSelector = periodSelector
nav_chart.extendToFullPeriod = false
nav_chart.addListener("zoomed", updateNavForPeriod)
nav_chart.addListener("init", add_explainer_button)
nav_chart.write("nav-growth-chart")
nav_chart.setUTC = true
add_datepicker_to_chart()
@chart = nav_chart
if report
$(".amChartsPeriodSelector").remove()
return nav_chart
updateNavForPeriod = (event) ->
i = 0
console.log event.startDate
while i < APP.portfolios.length
if Date.parse(APP.portfolios[i].traded_on) >= event.startDate
startDate = APP.portfolios[i].traded_on
break
i++
console.log(APP.portfolios[i]) # portfolio data point that meets startDate
console.log(startDate)
console.log "---"
if startDate == APP.portfolios[0].traded_on
data = APP.portfolios
else
data = []
matches = $.grep(APP.portfolios, (e) -> return e.traded_on == startDate);
console.log("nav today for #{startDate}: #{matches[0].nav_today}")
reference = matches[0]
$.each APP.portfolios, ->
new_object = jQuery.extend({}, this);
new_object.nav_today = (Number(this.nav_today) * 100.0/Number(reference.nav_today))
data.push(new_object)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment