From eb5756101ce841c5e572f775e08bf5989862ad6b Mon Sep 17 00:00:00 2001 From: sudodoki Date: Mon, 21 Oct 2013 01:02:13 +0300 Subject: [PATCH] feature/static labels --- lib/morris.bar.coffee | 11 +++++++++++ spec/lib/bar/bar_spec.coffee | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/morris.bar.coffee b/lib/morris.bar.coffee index 5e6c37a..534abab 100644 --- a/lib/morris.bar.coffee +++ b/lib/morris.bar.coffee @@ -107,6 +107,8 @@ class Morris.Bar extends Morris.Grid left += sidx * (barWidth + @options.barGap) unless @options.stacked size = bottom - top + if opts = @options?.staticLabels + @drawXAxisLabel(left + barWidth / 2, top - (opts.margin or 0), @labelContentForRow(idx)) top -= lastTop if @options.stacked @drawBar(left, top, barWidth, size, @colorFor(row, sidx, 'bar')) @@ -174,6 +176,15 @@ class Morris.Bar extends Morris.Grid x = @left + (index + 0.5) * @width / @data.length [content, x] + labelContentForRow: (index) -> + row = @data[index] + content = '' + for y, j in row.y + content += "#{@options.labels[j]}:#{@yLabelFormat(y)} " + if typeof @options.staticLabels.labelCallback is 'function' + content = @options.staticLabels.labelCallback(index, @options, content) + content + drawXAxisLabel: (xPos, yPos, text) -> label = @raphael.text(xPos, yPos, text) .attr('font-size', @options.gridTextSize) diff --git a/spec/lib/bar/bar_spec.coffee b/spec/lib/bar/bar_spec.coffee index cfa2936..10f6545 100644 --- a/spec/lib/bar/bar_spec.coffee +++ b/spec/lib/bar/bar_spec.coffee @@ -48,3 +48,33 @@ describe 'Morris.Bar', -> it 'should have text with configured font size', -> chart = Morris.Bar $.extend {}, defaults $('#graph').find("text[font-size='12px']").size().should.equal 7 + + describe 'when enabling static labels', -> + describe 'svg structure', -> + defaults = + element: 'graph' + data: [{x: 'foo', y: 2, z: 3}, {x: 'bar', y: 4, z: 6}] + xkey: 'x' + ykeys: ['y', 'z'] + labels: ['Y', 'Z'] + staticLabels: + margin: 20 + labelCallback: (index, options, label) -> + label + ' km' + + it 'should have extra text nodes', -> + chart = Morris.Bar $.extend {}, defaults + $('#graph').find("text").size().should.equal 11 + + describe 'should have needed text', -> + it 'given custom labelCallback', -> + chart = Morris.Bar $.extend {}, defaults + $('#graph').find("text").filter($("#graph").find("text").filter (index, el) -> $(el).text().match /km$/).size().should.equal 4 + + it 'without labelCallback', -> + delete defaults.staticLabels + chart = Morris.Bar $.extend { + staticLabels: + margin: 20 + }, defaults + $('#graph').find("text").filter($("#graph").find("text").filter (index, el) -> $(el).text().match /Y\:\d\sZ\:\d/).size().should.equal 4