3

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.

Renaissance
  • 798
  • 5
  • 15
Mark
  • 81
  • 8
  • similar post to this which I think has the solution you're looking for http://stackoverflow.com/questions/4230945/flot-graph-use-legend-to-turn-on-off-series – Stephen James Sep 28 '13 at 21:04
  • Stephen, it is similar, I am trying to see if it solves my issue currently. Thanks for the pointer! – Mark Sep 29 '13 at 01:39

1 Answers1

1

Okay.. so it took some time, but below is the code I settled upon. This adds the swatches to a table below the flot plot. The plot still has the legend on the right side, which appear and disappear as I toggle sets on and off with the checkboxes. The swatches in the table persist. Maybe this will help someone!

var options = {
legend: {
    show: true
},
series: {
    points: {
        show: false
    },
    lines: {
        show: true
    }
},
grid: {
    hoverable: true
},
xaxis: {

},
yaxis: {


}
};

 var i = 0;
 var track = 0;
choiceContainer = $("#labeler<?php echo $i ?>");
 var table = $('<table border=1/>');    
var row = $('<tr/>');
var cell = $('<td width=\"100\"/>');
var temp = $(table);

$.each(results, function(key, val) {
 track = track + 1;
val.color = i;
++i;
l = val.label; 


 if (track == 1){
    temp.append(row);
    row.append(cell);
    cell.append('Left Channel');
 } else if(track == <?php echo $tracks ?>){
    row = $('<tr/>');
    temp.append(row);
    cell = $('<td width=\"100\">');
    row.append(cell);
    cell.append('Right Channel');
 }

    cell = $('<td width=\"75\"/>');
    row.append(cell);
    var bar = $('<div style=\"width:18px\"><bar />');
    cell.append(bar);
   var inp = $('<input name="' + l + '" id="' + l + '" type="checkbox" checked="checked">');
    cell.append(inp);
    var bits = $('<label>', {
            text: l,
            'for': l
            });
    cell.append(bits); 

choiceContainer.append(temp);

});

function plotAccordingToChoices() {
var data = [];

choiceContainer.find("input:checked").each(function() {
    var key = this.name;

    for (var i = 0; i < results.length; i++) {
        if (results[i].label === key) {
            data.push(results[i]);
            return true;
        }
    }
});

$.plot($("#placeholder<?php echo $i ?>"), data, options);
}

var previousPoint = null;

$("#placeholder").bind("plothover", function(event, pos, item) {
$("#x").text(pos.x.toFixed(2));
$("#y").text(pos.y.toFixed(2));

if (item) {
    if (previousPoint != item.datapoint) {
        previousPoint = item.datapoint;

        $("#tooltip").remove();
        var x = item.datapoint[0].toFixed(2),
            y = item.datapoint[1].toFixed(2);

        showTooltip(item.pageX, item.pageY, item.series.label + " $" + y);
    }
} else {
    $("#tooltip").remove();
    previousPoint = null;
}
});

function showTooltip(x, y, contents) {
$('<div id="tooltip">' + contents + '</div>').css({
    position: 'absolute',
    display: 'none',
    top: y + 5,
    left: x + 15,
    border: '1px solid #fdd',
    padding: '2px',
    backgroundColor: '#fee',
    opacity: 0.80
}).appendTo("body").fadeIn(200);
}

plotAccordingToChoices();
choiceContainer.find("input").change(plotAccordingToChoices);

$('.legendColorBox > div').each(function(i){
$(this).clone().prependTo(choiceContainer.find("bar").eq(i));
});
Mark
  • 81
  • 8