{ ChartType,
} = require 'kraken/chart/chart-type'
+root = do -> this
class exports.BarChartType extends ChartType
SPEC_URL : '/schema/d3/d3-bar.json'
# NOTE: ChartType.register() must come AFTER `typeName` declaration.
- typeName : 'd3-bar'
+ typeName : 'd3-bar'
ChartType.register this
legend : '.graph-legend'
-
-> super ...
+ getData: ->
+ @model.dataset.getColumns()
+
+
+ transform: ->
+ dataset = @model.dataset
+ options = @model.getOptions() import @determineSize()
+ options import do
+ colors : dataset.getColors()
+ labels : dataset.getLabels()
+ options
+ renderChart: (data, viewport, options, lastChart) ->
+ ### Starting with http://bost.ocks.org/mike/chart/
+
+ margin = {top: 20, right: 20, bottom: 20, left: 20}
+ width = 760
+ height = 320
+ xScale = d3.time.scale()
+ yScale = d3.scale.linear()
+
+ dates = data[0]
+ cols = data.slice(1)
+
+ # Calculate extents using all the data points (but not dates)
+ # allValues = d3.merge @model.dataset.getDataColumns()
+ allValues = d3.merge cols
+
+
+ # Update the x-scale with the extents of the dates.
+ xScale
+ .domain d3.extent dates
+ .range [ 0, width - margin.left - margin.right ]
+
+ # Update the y-scale with the extents of the data.
+ yScale
+ .domain d3.extent allValues
+ .range [ height - margin.top - margin.bottom, 0 ]
+
+ # Select the svg element, if it exists.
+ svg = d3.select viewport.0 .selectAll "svg"
+ .data [cols]
+
+ # ...Otherwise, create the skeletal chart.
+ enterFrame = svg.enter()
+ .append "svg" .append "g"
+ .attr "class", "frame"
+ enterFrame.append "g"
+ .attr "class", "x axis time"
+
+ # Update chart dimensions.
+ svg .attr "width", width
+ .attr "height", height
+ frame = svg.select "g.frame"
+ .attr "transform", "translate(#{margin.left},#{margin.top})"
+
+ # Update the x-axis.
+ xAxis = d3.svg.axis().scale(xScale).orient("bottom").tickSize(6, 0)
+ frame.select ".x.axis.time"
+ .attr "transform", "translate(0,#{yScale.range()[0]})"
+ .call xAxis
+
+ X = (d, i) -> xScale d[0]
+ Y = (d, i) -> yScale d[1]
+
+ ### Render Bars
+ barWidth = svg.attr('width')/dates.length
+ barHeight = (d) -> svg.attr('height')-Y(d)
+
+ bars = frame.selectAll "g.bars"
+ .data cols.map -> d3.zip dates, it
+ bars.enter().append "g"
+ .attr "class", (col, i) -> "metric bars #i"
+ bars.exit().remove()
+
+ bars.selectAll ".bar"
+ .data op.first
+ .enter().append "rect"
+ .attr "class", "bar"
+ .attr "x", X
+ .attr "y", Y
+ .attr "height", barHeight
+ .attr "width", -> barWidth
+ # TODO grab color from graph spec
+ .attr "fill", "red"
+ .attr "stroke", "white"
+
+
+ ### Mouse Lens
+ lens = root.lens = frame.selectAll "g.lens"
+ .data [[]]
+ gLens = lens.enter().append "g"
+ .attr "class", "lens"
+ .style "z-index", 1e9
+ gInner = gLens.append "g"
+ .attr "transform", "translate(1.5em,0)"
+ gInner.append "circle"
+ .attr "r", "1.5em"
+ # .style "opacity", "0.4"
+ # .style "fill", "white"
+ .style "fill", "rgba(255, 255, 255, 0.4)"
+ .style "stroke", "white"
+ .style "stroke-width", "3px"
+ gInner.append "text"
+ .attr "y", "0.5em"
+ .attr "text-anchor", "middle"
+ .style "fill", "white"
+ .style "font", "12px Helvetica"
+ .style "font-weight", "bold"
+
+
+ mf = frame.selectAll "g.mf"
+ .data ["mf"]
+ .enter().append "g"
+ .attr "class", "mf"
+ .append "text"
+ .attr "class", "yoyo"
+ .attr "dx", 50
+ .attr "dy", 100
+
+
+
+ bars.selectAll ".bar"
+ .on "mouseover", (d, i) ->
+ el = root.el = el # DOM element of event
+ # {r,g,b} = color = d3.rgb options.colors[i]
+ mf
+ .transition()
+ .duration(300)
+ .ease("exp")
+ .text "Uh boy, the target would be:"+d[1]
+ .style "font-size", "25px"
+
+
+ .on "mouseout", (d, i) ->
+ mf
+ .transition()
+ .duration(1000)
+ .text "BUMMER!!!"
+ .style "font-size", "0px"
+
+
+
+ # {x:lineX, y:lineY} = root.pt = line.indexToPoint idx
+ # lens = frame.select "g.lens"
+ # .attr "transform", "translate(#lineX, #lineY)"
+ # lens.select "circle" .style "fill", "rgba(#r, #g, #b, 0.4)"
+ # lens.select "text" .text Y
+
+
+
+ svg