Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legend multi series #455

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions examples/multi2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<!doctype html>
<head>
<link type="text/css" rel="stylesheet" href="../rickshaw.css">
<link type="text/css" rel="stylesheet" href="css/lines.css">
<link type="text/css" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css">
<script src="../vendor/d3.v3.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.min.js"></script>

<script src="../rickshaw.js"></script>
<style>
.column {
display: inline-block
}

#legend {
background: white
}

#legend .line {
color: #333
}
</style>
</head>
<body>

<div class="column">
<div id="chart"></div>
<div id="slider"></div>
</div>
<div class="column" id="legend"></div>

<script>

var data =
{
"labels": ["06q1", "06q2", "06q3", "06q4", "07q1", "07q2", "07q3", "07q4", "08q1", "08q2", "08q3", "08q4", "09q1", "09q2", "09q3", "09q4", "10q1", "10q2", "10q3", "10q4", "11q1", "11q2", "11q3", "11q4", "12q1", "12q2", "12q3", "12q4", "13q1", "13q2", "13q3", "13q4", "14q1"],
"data": {
"RENT_JA3_2_2000": [570.0, 580.0, 600.0, 600.0, 650.0, 650.0, 650.0, 680.0, 700.0, 720.0, 740.0, 730.0, 750.0, 725.0, 725.0, 750.0, 730.0, 750.0, 750.0, 750.0, 765.0, 780.0, 780.0, 800.0, 800.0, 800.0, 800.0, 820.0, 805.0, 810.0, 830.0, 850.0, 822.5],
"RENT_JA3_2_2032": [347.5, 335.0, 330.0, 330.0, 342.5, 370.0, 372.5, 375.0, 400.0, 420.0, 420.0, 427.5, 435.0, 460.0, 440.0, 450.0, 440.0, 460.0, 480.0, 480.0, 500.0, 505.0, 525.0, 500.0, 520.0, 520.0, 550.0, 482.5, 520.0, 532.5, 530.0, 520.0, 550.0],
"RENT_JA3_2_2131": [255.0, 275.0, 280.0, 280.0, 285.0, 300.0, 320.0, 310.0, 325.0, 350.0, 350.0, 350.0, 350.5, 360.0, 360.0, 370.0, 375.0, 380.0, 380.0, 400.0, 390.0, 415.0, 405.0, 412.5, 410.0, 410.0, 420.0, 410.0, 425.0, 430.0, 430.0, 440.0, 450.0],
"RENT_JA3_2_2204": [250.0, 270.0, 270.0, 290.0, 280.0, 280.0, 320.0, 355.0, 310.0, 345.0, 350.0, 350.0, 360.0, 370.0, 360.0, 370.0, 360.0, 420.0, 400.0, 420.0, 400.0, 420.0, 427.5, 407.5, 420.0, 420.0, 430.0, 450.0, 450.0, 472.5, 460.0, 450.0, 470.0],
"RENT_JA3_2_2265": [115.0, null, 165.0, null, 170.0, null, 157.5, null, null, null, null, null, 152.0, null, 220.0, null, null, null, null, null, 180.0, null, null, null, null, null, null, null, null, null, null, null, 193.0]
},
"metadata": {
"RENT_JA3_2_2000": {"count": ["MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY"]},
"RENT_JA3_2_2032": {"count": ["MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY"]},
"RENT_JA3_2_2131": {"count": ["MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY"]},
"RENT_JA3_2_2204": {"count": ["MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY", "MANY"]},
"RENT_JA3_2_2265": {"count": ["FEW", "MISSING", "FEW", "MISSING", "FEW", "MISSING", "FEW", "MISSING", "NOT_ENOUGH", "MISSING", "MISSING", "MISSING", "FEW", "MISSING", "FEW", "NOT_ENOUGH", "MISSING", "MISSING", "MISSING", "NOT_ENOUGH", "FEW", "MISSING", "MISSING", "NOT_ENOUGH", "NOT_ENOUGH", "NOT_ENOUGH", "NOT_ENOUGH", "NOT_ENOUGH", "NOT_ENOUGH", "NOT_ENOUGH", "NOT_ENOUGH", "NOT_ENOUGH", "FEW"]}
}
};

// TODO: sort from highest price to lowest price (average) (for area)

var series = [];

var names = 0;
for (var name in data.data) {
++names;
}

var n = 0;
for (name in data.data) {
var seriesData = [];
//noinspection JSUnfilteredForInLoop
for (var x = 0; x < data.data[name].length; x++) {
//noinspection JSUnfilteredForInLoop
var price = data.data[name][x];
seriesData.push({x: x, y: price});
}

// TODO: hue based on rank
//noinspection JSUnfilteredForInLoop
series.push({
name: name,
data: seriesData,
color: 'hsla(' + (120 - n * (120 / (names - 1))) + ',100%,33%,.5)',
renderer: 'line'
});
//noinspection JSUnfilteredForInLoop
series.push({
name: name,
data: seriesData,
color: 'hsla(' + (120 - n * (120 / (names - 1))) + ',100%,33%,1)',
renderer: 'scatterplot'
});
++n;
}

// seriesData[0][10].y = null;//gap in stack
// seriesData[1][20].y = null;//gap in stack
// seriesData[2][30].y = null;//gap in scatter
// seriesData[3][40].y = null;//gap in bar
// seriesData[4][50].y = null;//0 in line

var graph = new Rickshaw.Graph({
element: document.getElementById("chart"),
renderer: 'multi',
stack: false,
width: 900,
height: 500,
dotSize: 5,
series: series
// [
// {
// name: 'temperature',
// data: seriesData.shift(),
// color: 'rgba(255, 0, 0, 0.4)',
// renderer: 'stack'
// },
// {
// name: 'heat index',
// data: seriesData.shift(),
// color: 'rgba(255, 127, 0, 0.4)',
// renderer: 'stack'
// },
// {
// name: 'dewpoint',
// data: seriesData.shift(),
// color: 'rgba(127, 0, 0, 0.3)',
// renderer: 'scatterplot'
// },
// {
// name: 'pop',
// data: seriesData.shift().map(function (d) {
// return {x: d.x, y: d.y / 4}
// }),
// color: 'rgba(0, 0, 127, 0.4)',
// renderer: 'bar'
// },
// {
// name: 'humidity',
// data: seriesData.shift().map(function (d) {
// return {x: d.x, y: d.y * 1.5}
// }),
// renderer: 'line',
// color: 'rgba(0, 0, 127, 0.25)'
// }
// ]
});

var slider = new Rickshaw.Graph.RangeSlider.Preview({
graph: graph,
element: document.querySelector('#slider')
});

graph.window.xMin = x - 12;
graph.window.xMax = x - 1;
graph.render();

var detail = new Rickshaw.Graph.HoverDetail({
graph: graph
});

var legend = new Rickshaw.Graph.Legend({
graph: graph,
element: document.querySelector('#legend')
});

var highlighter = new Rickshaw.Graph.Behavior.Series.Highlight({
graph: graph,
legend: legend,
disabledColor: function () {
return 'rgba(0, 0, 0, 0.2)'
}
});

var shelving = new Rickshaw.Graph.Behavior.Series.Toggle({
graph: graph,
legend: legend
});

//no order but can still drag and drop?


</script>
</body>
33 changes: 22 additions & 11 deletions src/js/Rickshaw.Graph.Behavior.Series.Highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,27 @@ Rickshaw.Graph.Behavior.Series.Highlight = function(args) {
if (l === line) {

// if we're not in a stacked renderer bring active line to the top
if (self.graph.renderer.unstack && (line.series.renderer ? line.series.renderer.unstack : true)) {
if (self.graph.renderer.unstack)
line.series.forEach(function (series) {
if (series.renderer ? series.renderer.unstack : true) {

var seriesIndex = self.graph.series.indexOf(line.series);
line.originalIndex = seriesIndex;
var seriesIndex = self.graph.series.indexOf(series);
line.originalIndex = seriesIndex;

var series = self.graph.series.splice(seriesIndex, 1)[0];
self.graph.series.push(series);
}
series = self.graph.series.splice(seriesIndex, 1)[0];
self.graph.series.push(series);
}
});
return;
}

colorSafe[line.series.name] = colorSafe[line.series.name] || line.series.color;
line.series.color = disabledColor(line.series.color);
// backup and change the series' colors
line.series.forEach(function (series, s) {
if (!(series.name in colorSafe))
colorSafe[series.name] = [];
colorSafe[series.name][s] = colorSafe[series.name][s] || series.color;
series.color = disabledColor(series.color);
});

} );

Expand All @@ -61,9 +69,12 @@ Rickshaw.Graph.Behavior.Series.Highlight = function(args) {
delete line.originalIndex;
}

if (colorSafe[line.series.name]) {
line.series.color = colorSafe[line.series.name];
}
// restore the series' colors
line.series.forEach(function (series, s) {
if (colorSafe[series.name] && colorSafe[series.name][s]) {
series.color = colorSafe[series.name][s];
}
});
} );

self.graph.update();
Expand Down
2 changes: 1 addition & 1 deletion src/js/Rickshaw.Graph.Behavior.Series.Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Rickshaw.Graph.Behavior.Series.Order = function(args) {
var series = [];
jQuery(self.legend.list).find('li').each( function(index, item) {
if (!item.series) return;
series.push(item.series);
series = series.concat(item.series);
} );

for (var i = self.graph.series.length - 1; i >= 0; i--) {
Expand Down
12 changes: 7 additions & 5 deletions src/js/Rickshaw.Graph.Behavior.Series.Toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ Rickshaw.Graph.Behavior.Series.Toggle = function(args) {
line.element.insertBefore(anchor, line.element.firstChild);

anchor.onclick = function(e) {
if (line.series.disabled) {
if (line.series.disabled()) {
line.series.enable();
line.element.classList.remove('disabled');
} else {
if (this.graph.series.filter(function(s) { return !s.disabled }).length <= 1) return;
} else {
// prevent disabling the last series
if (this.graph.series.filter(function(s) { return !s.disabled }).length <= line.series.length) return;
line.series.disable();
line.element.classList.add('disabled');
}
Expand All @@ -31,13 +32,14 @@ Rickshaw.Graph.Behavior.Series.Toggle = function(args) {
var label = line.element.getElementsByTagName('span')[0];
label.onclick = function(e){

var disableAllOtherLines = line.series.disabled;
var disableAllOtherLines = line.series.disabled();
if ( ! disableAllOtherLines ) {
// if any other series is enabled, disableAllOtherLines
for ( var i = 0; i < self.legend.lines.length; i++ ) {
var l = self.legend.lines[i];
if ( line.series === l.series ) {
// noop
} else if ( l.series.disabled ) {
} else if ( l.series.disabled() ) {
// noop
} else {
disableAllOtherLines = true;
Expand Down
88 changes: 58 additions & 30 deletions src/js/Rickshaw.Graph.Legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,70 @@ Rickshaw.Graph.Legend = Rickshaw.Class.create( {
},

addLine: function (series) {
var line = document.createElement('li');
line.className = 'line';
if (series.disabled) {
line.className += ' disabled';
var line;
for (var l = 0; l < this.list.children.length; l++) {
line = this.list.children[l];
// don't need to check for line.series[0].name's existence. shouldn't have a line without it.
if (line.series[0].name == series.name) {
line.series.push(series);
break;
}
}
if (series.className) {
d3.select(line).classed(series.className, true);
}
var swatch = document.createElement('div');
swatch.className = 'swatch';
swatch.style.backgroundColor = series.color;

line.appendChild(swatch);

var label = document.createElement('span');
label.className = 'label';
label.innerHTML = series.name;

line.appendChild(label);
this.list.appendChild(line);

line.series = series;

if (series.noLegend) {
line.style.display = 'none';
if (l == this.list.children.length) {
line = document.createElement('li');
line.className = 'line';
if (series.disabled) {
line.className += ' disabled';
}
if (series.className) {
d3.select(line).classed(series.className, true);
}
var swatch = document.createElement('div');
swatch.className = 'swatch';
swatch.style.backgroundColor = series.color;

line.appendChild(swatch);

var label = document.createElement('span');
label.className = 'label';
label.innerHTML = series.name;

line.appendChild(label);
this.list.appendChild(line);

line.series = [series];

if (series.noLegend) {
line.style.display = 'none';
}
}

var _line = { element: line, series: series };
if (this.shelving) {
this.shelving.addAnchor(_line);
this.shelving.updateBehaviour();
var _line;
for (l = 0; l < this.lines.length; l++) {
_line = this.lines[l];
// don't need to check for _line.series[0].name's existence. shouldn't have a _line without it.
if (_line.series[0].name == series.name) {
_line.series.push(series);
break;
}
}
if (this.highlighter) {
this.highlighter.addHighlightEvents(_line);

if (l == this.lines.length) {
series = [series];
series.disabled = function () {return this[0].disabled};
series.enable = function () {this.forEach(function (s) {s.enable();})};
series.disable = function () {this.forEach(function (s) {s.disable();})};
_line = {element: line, series: series};
if (this.shelving) {
this.shelving.addAnchor(_line);
this.shelving.updateBehaviour();
}
if (this.highlighter) {
this.highlighter.addHighlightEvents(_line);
}
this.lines.push(_line);
}
this.lines.push(_line);
return line;
}
} );
Expand Down
Loading