epoch-charting
Version:
A general purpose real-time charting library for building beautiful, smooth, and high performance visualizations.
62 lines (48 loc) • 2.02 kB
text/coffeescript
class Epoch.Chart.Histogram extends Epoch.Chart.Bar
defaults =
type: 'histogram'
domain: [0, 100]
bucketRange: [0, 100]
buckets: 10
cutOutliers: false
optionListeners =
'option:bucketRange': 'bucketRangeChanged'
'option:buckets': 'bucketsChanged'
'option:cutOutliers': 'cutOutliersChanged'
constructor: (={}) ->
super( = Epoch.Util.defaults(, defaults))
optionListeners
# Prepares data by sorting it into histogram buckets as instructed by the chart options.
# [Array] data Data to prepare for rendering.
# [Array] The data prepared to be displayed as a histogram.
_prepareData: (data) ->
bucketSize = (.bucketRange[1] - .bucketRange[0]) / .buckets
prepared = []
for layer in data
buckets = (0 for i in [0....buckets])
for point in layer.values
index = parseInt((point.x - .bucketRange[0]) / bucketSize)
if .cutOutliers and ((index < 0) or (index >= .buckets))
continue
if index < 0
index = 0
else if index >= .buckets
index = .buckets - 1
buckets[index] += parseInt point.y
preparedLayer = { values: (buckets.map (d, i) -> {x: parseInt(i) * bucketSize, y: d}) }
for own k, v of layer
preparedLayer[k] = v unless k == 'values'
prepared.push preparedLayer
return prepared
# Called when options change, this prepares the raw data for the chart according to the new
# options, sets it, and renders the chart.
resetData: ->
# Updates the chart in response to an <code>option:bucketRange</code> event.
bucketRangeChanged: ->
# Updates the chart in response to an <code>option:buckets</code> event.
bucketsChanged: ->
# Updates the chart in response to an <code>option:cutOutliers</code> event.
cutOutliersChanged: ->