app/assets/javascripts/plasticine/line.coffee in plasticine-1.2.0 vs app/assets/javascripts/plasticine/line.coffee in plasticine-1.2.1

- old
+ new

@@ -6,35 +6,39 @@ #@drawTooltip() @xScale = d3.scaleBand() @setXScaleRange() - @yScale = d3.scaleLinear().range([@holder.height(),0]) + @yScale = d3.scaleLinear().range([@holder.height(),50]) @setXAxis() @setYAxis() + @legend = d3.select("##{@holder.attr('id')}").append('ul').attr('class', 'legend') @svg = d3.select("##{@holder.attr('id')}").append('svg').style('width', @holderWidth()).style('height', @holder.height()).append('g') + d3.json(@holder.data('url')).then (data) => @build(data) build: (data) -> @xAxisFormat = data['axis_x_format'] @yAxisFormat = data['axis_y_format'] @yAxisTickCount = data['axis_y_tick_count'] @chartLayout = data['chart_layout'] @quarterStartMonth = Number(data.quarter_start_month) + @holder.attr('class', @holder.attr('class') + ' ' + data['chart_style']) + maxY = if @chartLayout == 'area' then data['max_y_stack'] * 1.1 else data['max_y'] - + data['lines'].forEach (line) => line.dots.forEach (d) => d.x = PlasticineHelpers.parseValue(d.x, @xAxisFormat) @xScale.domain data['lines'][0].dots.map( (d) -> d.x ) @yScale.domain [0, maxY] - + @drawYAxis() if @chartLayout == 'area' stackedDataset = [] stackedKeys = [] @@ -56,25 +60,30 @@ series = data['lines'] @chartPath = d3.line().curve(d3.curveCardinal) .x( (d) => @xScale(d.x)) .y( (d) => @yScale(d.y)) - @lines = @svg.append('g').attr('class', "elements #{@chartLayout}").selectAll('.element').data(series) + @lines = @svg.append('g').attr('class', "elements #{@chartLayout}").selectAll('.element').data(series) @refreshLines() - - @drawXAxis() + @drawXAxis() + @drawLegend(data) d3.select(window).on("resize." + @holder.attr("id"), () => @resizeX()) refreshLines: () -> $('g.element').remove() element = @lines.enter().append('g').attr('class', "element") element.append("path") .attr('class', 'line') .attr('d', (d) => if @chartLayout == 'area' then @chartPath(d) else @chartPath(d.dots)) - + + + drawLegend: (data) -> + data['lines'].forEach (line) => + @legend.append('li').html('<span></span>' + line.label) + drawXAxis: () -> height = @holder.height() @svg.append('g').attr('class', 'x axis') .attr('transform', 'translate(0,' + height + ')')