mirror of
https://github.com/morrisjs/morris.js.git
synced 2024-11-13 07:11:12 +01:00
feature/static labels
This commit is contained in:
parent
a738317adb
commit
eb5756101c
2 changed files with 41 additions and 0 deletions
|
@ -107,6 +107,8 @@ class Morris.Bar extends Morris.Grid
|
||||||
left += sidx * (barWidth + @options.barGap) unless @options.stacked
|
left += sidx * (barWidth + @options.barGap) unless @options.stacked
|
||||||
size = bottom - top
|
size = bottom - top
|
||||||
|
|
||||||
|
if opts = @options?.staticLabels
|
||||||
|
@drawXAxisLabel(left + barWidth / 2, top - (opts.margin or 0), @labelContentForRow(idx))
|
||||||
top -= lastTop if @options.stacked
|
top -= lastTop if @options.stacked
|
||||||
@drawBar(left, top, barWidth, size, @colorFor(row, sidx, 'bar'))
|
@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
|
x = @left + (index + 0.5) * @width / @data.length
|
||||||
[content, x]
|
[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) ->
|
drawXAxisLabel: (xPos, yPos, text) ->
|
||||||
label = @raphael.text(xPos, yPos, text)
|
label = @raphael.text(xPos, yPos, text)
|
||||||
.attr('font-size', @options.gridTextSize)
|
.attr('font-size', @options.gridTextSize)
|
||||||
|
|
|
@ -48,3 +48,33 @@ describe 'Morris.Bar', ->
|
||||||
it 'should have text with configured font size', ->
|
it 'should have text with configured font size', ->
|
||||||
chart = Morris.Bar $.extend {}, defaults
|
chart = Morris.Bar $.extend {}, defaults
|
||||||
$('#graph').find("text[font-size='12px']").size().should.equal 7
|
$('#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
|
||||||
|
|
Loading…
Reference in a new issue