| 1 |
Rickshaw.namespace('Rickshaw.Graph.Legend'); |
| 2 |
|
| 3 |
Rickshaw.Graph.Legend = Rickshaw.Class.create( { |
| 4 |
|
| 5 |
className: 'rickshaw_legend', |
| 6 |
|
| 7 |
initialize: function(args) { |
| 8 |
this.element = args.element; |
| 9 |
this.graph = args.graph; |
| 10 |
this.naturalOrder = args.naturalOrder; |
| 11 |
|
| 12 |
this.element.classList.add(this.className); |
| 13 |
|
| 14 |
this.list = document.createElement('ul'); |
| 15 |
this.element.appendChild(this.list); |
| 16 |
|
| 17 |
this.render(); |
| 18 |
|
| 19 |
// we could bind this.render.bind(this) here |
| 20 |
// but triggering the re-render would lose the added |
| 21 |
// behavior of the series toggle |
| 22 |
this.graph.onUpdate( function() {} ); |
| 23 |
}, |
| 24 |
|
| 25 |
render: function() { |
| 26 |
var self = this; |
| 27 |
|
| 28 |
while ( this.list.firstChild ) { |
| 29 |
this.list.removeChild( this.list.firstChild ); |
| 30 |
} |
| 31 |
this.lines = []; |
| 32 |
|
| 33 |
var series = this.graph.series |
| 34 |
.map( function(s) { return s } ); |
| 35 |
|
| 36 |
if (!this.naturalOrder) { |
| 37 |
series = series.reverse(); |
| 38 |
} |
| 39 |
|
| 40 |
series.forEach( function(s) { |
| 41 |
self.addLine(s); |
| 42 |
} ); |
| 43 |
|
| 44 |
|
| 45 |
}, |
| 46 |
|
| 47 |
addLine: function (series) { |
| 48 |
var line = document.createElement('li'); |
| 49 |
line.className = 'line'; |
| 50 |
if (series.disabled) { |
| 51 |
line.className += ' disabled'; |
| 52 |
} |
| 53 |
if (series.className) { |
| 54 |
d3.select(line).classed(series.className, true); |
| 55 |
} |
| 56 |
var swatch = document.createElement('div'); |
| 57 |
swatch.className = 'swatch'; |
| 58 |
swatch.style.backgroundColor = series.color; |
| 59 |
|
| 60 |
line.appendChild(swatch); |
| 61 |
|
| 62 |
var label = document.createElement('span'); |
| 63 |
label.className = 'label'; |
| 64 |
label.innerHTML = series.name; |
| 65 |
|
| 66 |
line.appendChild(label); |
| 67 |
this.list.appendChild(line); |
| 68 |
|
| 69 |
line.series = series; |
| 70 |
|
| 71 |
if (series.noLegend) { |
| 72 |
line.style.display = 'none'; |
| 73 |
} |
| 74 |
|
| 75 |
var _line = { element: line, series: series }; |
| 76 |
if (this.shelving) { |
| 77 |
this.shelving.addAnchor(_line); |
| 78 |
this.shelving.updateBehaviour(); |
| 79 |
} |
| 80 |
if (this.highlighter) { |
| 81 |
this.highlighter.addHighlightEvents(_line); |
| 82 |
} |
| 83 |
this.lines.push(_line); |
| 84 |
return line; |
| 85 |
} |
| 86 |
} ); |
| 87 |
|