I am specifically using the flot plotting code. My intent is to plot audio power spectrum data, including both channels for each track on a plot. So I can expect to have between 2 and say 40 lines for a given concert.
I hope to have a method to toggle each line on and off, while keeping the numbers and colors static on the plot. I have tried a variety of methods and have two that nearly get to where I want to be:
The first is based on the code from http://people.iola.dk/olau/flot/examples/turning-series.html, shown below:
var track = 0;
// hard-code color indices to prevent them from shifting as
// countries are turned on/off
var i = 0;
$.each(datasets, function(key, val) {
val.color = i;
++i;
});
// insert checkboxes
var choiceContainer = $("#choices<?php echo $i ?>");
choiceContainer.append('<table><tr>');
$.each(datasets, function(key, val) {
track = track + 1;
if (track == 1){
choiceContainer.append('<td width=\"100\">Left Channel</td>');
} else if(track == <?php echo $tracks ?>){
choiceContainer.append('</tr><tr>');
choiceContainer.append('<td>Right Channel</td>');
}
choiceContainer.append('<td width=\"35\"><input type="checkbox" name="' + key +
'" checked="checked" id="id' + key + '">' +
'<label for="id' + key + '">'
+ val.label + '</label></td>');
});
choiceContainer.append('</tr></table>');
choiceContainer.find("input").click(plotAccordingToChoices);
function plotAccordingToChoices() {
var data = [];
choiceContainer.find("input:checked").each(function () {
var key = $(this).attr("name");
if (key && datasets[key])
data.push(datasets[key]);
});
var options = {
yaxis: { min: 0 },
xaxis: { tickDecimals: 0 },
xaxes: [{
axisLabel: 'Frequency (Hz)',
}],
yaxes: [{
position: 'left',
axisLabel: 'Power (dB)',
}],
series: {
lines: {
lineWidth: 2
}
},
legend: {
noColumns:7,
container: $("#labeler<?php echo $i ?>"),
labelFormatter: function(label, series){
return '<a href="#" onClick="togglePlot('+series.idx+'); return false;">'+label+'</a>';
}
}
};
if (data.length > 0){
$.plot($("#placeholder<?php echo $i ?>"), data, options);
}
}
plotAccordingToChoices();
This works great for what I am trying to accomplish, with the exception that the checkboxes
do not show the color swatch next to them...
The example here: http://jsfiddle.net/mpazaryna/Zvqqn/ shows a code which quite simply creates the on off functionality with the color swatches. but does not lend itself (IMHO) to formatting out the legend in a manner that is coherent as to which channel and which track is being selected.
So, my goal would be find a way to add the color swatches to my existing code, above, as this is currently structured in a manner I find beneficial to the layout of the page.