Skip to content

Instantly share code, notes, and snippets.

@josephg
Created September 11, 2011 04:28
Show Gist options
  • Save josephg/1209166 to your computer and use it in GitHub Desktop.
Save josephg/1209166 to your computer and use it in GitHub Desktop.
coffeescript: In site.coffee, Parse error on line NaN: Unexpected ')'
request = (url, options, callback) ->
if typeof options is 'function'
[options, callback] = [{}, options]
options = {}
options.url = url
options.success = (obj) ->
callback(null, obj)
options.error = (err) ->
if err then callback(err) else callback(true)
if options.data and typeof options.data is 'object'
options.data = JSON.stringify(options.data)
unless options.dataType
options.processData = false
options.contentType = 'application/json'
options.dataType = 'json'
$.ajax(options)
getCategories = do ->
categories = null
(callback) ->
if categories
callback categories
else
request "_view/categories", (error, data) ->
throw new Error if error
categories = new Array(data.total_rows + 1)
categories[row.key] = row.value for row in data.rows
console.log "read #{data.total_rows} categories"
callback categories
# Split an array ['a', 'b', 'c', 'd', 'e', 'f'] into groups of size size
# -> [['a', 'b'], ['c', 'd'], ['e', 'f']]
splitList = (array, size) ->
array[offset...offset + size] for offset in [0...array.length] by size
# Takes some lists of elements and associates them
# Eg,
# zip [[1,2,3], ['a','b', 'c']] -> [[1,'a'], [2,'b'], [3,'c']]
zip = (arrays) ->
i = 0
while i < arrays[0].length
list = (a[i] for a in arrays)
i++
list
convertToPercentage = (data) ->
sum = 0
sum += d for d in data
d/sum for d in data
makeChart = (categories, region, table, dest) ->
console.log table.values
values = table.values
# values = convertToPercentage table.values
options =
chart:
renderTo: dest
defaultSeriesType: 'bar'
title:
text: 'Age'
subtitle:
text: 'Source: ABS'
xAxis:
title:
text: categories[0].name
categories: categories[0].labels
yAxis:
min: 0
title:
text: null
tooltip:
formatter: -> "#{@series.name}: #{@y}"
plotOptions:
series:
stacking: 'percent'
# bar:
# dataLabels:
# enabled:true
series: if categories.length == 1
[data: values]
else if categories.length == 2
console.log 'table.values', values, categories[1].labels.length
split = splitList values, categories[1].labels.length
console.log 'split', split
split = zip split
console.log 'split zip', split
for label, i in categories[1].labels
{name: label, data: split[i]}
console.log options
new Highcharts.Chart options
$ ->
app = $.sammy '#content', ->
@use 'Mustache', 'ms'
# Index of all databases
# this.get('', app.index)
this.get "#/", ->
@swap 'asdfasdfasfd'
this.get "#/region/:regionid", ->
regionId = @params.regionid
throw new Error 'no regionid' unless regionId?
getCategories (categories) ->
catByName = {}
catByName[name] = id for id, {name} of categories
request "../../06_#{regionId}", (error, region) ->
throw new Error if error
visibleTables = [
['Sex']
['Sex', 'Age']
]
# Need to pick from region.categories where the labels match above.
# First, we'll rewrite visibleTables into [1, 6] form.
visibleTables = for t in visibleTables
catByName[name] for name in t
regionHas = {}
regionHas[v.join '|'] = true for v in region.categories[0]
@tables = (for t in visibleTables when regionHas[t.join '|']
name: (categories[c].name for c in t).join ' by '
divid: "chart #{t.join '|'}")
@partial 'chart.ms'
###
if
@tables.push
key = "06_#{regionId}_0_#{joinedCats}"
request "../../#{key}", (error, table) ->
tcats = (categories[c] for c in cats)
makeChart tcats, region, table, "chart #{cats.join '|'}"
###
# window.alert @params[regionId]
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment