Skip to content

New plugin: toggleLegend - #848

Closed
jamesinc wants to merge 146 commits into
flot:masterfrom
jamesinc:master
Closed

jamesinc wants to merge 146 commits into
flot:masterfrom
jamesinc:master

Conversation

@jamesinc

Copy link
Copy Markdown

This is a simple flot plugin to allow series to be toggled on or off by clicking on their colour boxes in the chart legend. A demo file has been added to examples/

It also adds a new hook, legendInserted, that the plugin uses to determine when to reinitialise itself.

@romabysen

Copy link
Copy Markdown

I have problems getting this to work with more than one graph on the page. The problem is that the toggling only works on one graph. The graphs are created dynamically and only the last graph has the toggle functionality.

@jamesinc

Copy link
Copy Markdown
Author

Thanks, I'll look at it and fix the issue.

Fixed: jamesinc@753bd2f

…gends.

Updated the example file to demonstrate multi-chart capability.
@romabysen

Copy link
Copy Markdown

Yep, it works now. Sweet, I've been looking for just something like this.
One more thing though; would it be possible to add an option to scale the graph like in the "Turning Series On/Off" example?

@jamesinc

Copy link
Copy Markdown
Author

At the moment no (I think), because the chart doesn't get completely torn down and re-drawn from scratch. I'll have a look though.

@Dieterbe

Copy link
Copy Markdown

awesome. just what I need! i had been implementing this based on flot/examples/turning-series.html but using a legend labelFormatter that created checkboxes with onclick handlers to modify the series and replot, but then i noticed of course that flot removes the entry from the legend too, making me unable to toggle it on again.

your solution is better, i hope it gets merged. but +1 on adjusting the scale.

@luney112

Copy link
Copy Markdown

There are 3 small problems that I noticed with the latest commit.

a) jQuery (at least on version jquery-1.7.1.min) messes up the iteration over entries (it finds a .each propery and calls setupSwatch() on that). Should user for(;;) loop instead.
b) When hiding a series, it does not scale the y-axis as it should. This can be solved by calling setupGrid(), but adding a parameter that will not redraw the legend.
c) This will only work for legends that are within the plot itself. If you move the legend out of the plot (by setting the legend.container option) then it will not work.

Other than those 3 things, this plugin worked perfectly.

@Dieterbe

Dieterbe commented Dec 3, 2012

Copy link
Copy Markdown

hey @jamesinc any updates? thanks!

@itdevelopr

Copy link
Copy Markdown

Thanks for the plugin, this is very useful and working perfectly. Thanks Jamesinc.

Could you please suggest what sI am making a small enhancement to the demo file togglelegend.html to make the graph as dynamic by calling the same code with setinterval() within a function drawgraph(). The flot is hiding the series only for that instance of call and then while redrawing next time the series was showing without hiding.
hould we do to hide the series while drawing dynamically. Do we need to make change in the code or in the plug in?

Here is the code with my changes:

<title>Flot Examples</title> <script language="javascript" type="text/javascript" src="../jquery.js"></script> <script language="javascript" type="text/javascript" src="../jquery.flot.js"></script> <script language="javascript" type="text/javascript" src="../jquery.flot.stack.js"></script> <script language="javascript" type="text/javascript" src="../jquery.flot.togglelegend.js"></script>
<body>    <h1>Flot Examples</h1>
    <div id="placeholder" style="width:600px;height:300px;"></div>
    <div id="placeholder2" style="width:600px;height:300px;"></div>    <p>With the toggleLegend plugin, you can click on legend colour boxes to show or hide those series on the chart.</p>
    <script id="source">$(function () {
    
    function drawgraph() {
        var d1 = { label: "Series 1", data: [ ]};
            for (var i = 0; i <= 10; i += 1)        
            d1.data.push([i, parseInt(Math.random() * 30)]);
        var d2 = { label: "Series 2", data: [ ]};
            for (var i = 0; i <= 10; i += 1)
            d2.data.push([i, parseInt(Math.random() * 30)]);
        var d3 = { label: "Series 3", data: [ ]};
            for (var i = 0; i <= 10; i += 1)
            d3.data.push([i, parseInt(Math.random() * 30)]);
        var stack = 0, bars = true, lines = false, steps = false;
        
        function plotWithOptions() {
                $.plot($("#placeholder"), [ d1, d2, d3 ], {legend: {toggle: true}});
                //$.plot($("#placeholder2"), [ d1, d2, d3 ], {legend: {toggle: true}});
                }
        plotWithOptions();
        }//end draw function
      setInterval(drawgraph, 2000);
     } // end main function
  );
</script>

@itdevelopr

Copy link
Copy Markdown

Sorry, the format of the code file got messed up. Here attaching the file.

@itdevelopr

Copy link
Copy Markdown

hey @jamesinc any suggestions on this? thanks!

@itdevelopr

Copy link
Copy Markdown

Hi,
I try to hide the series using togglelegend.js. I am able to hide it until the next data point arrives then the series is showing up again. I tried with another plugin hiddengraphs.js and that too doing the same. Please let me know your comments.
Thanks

@jamesinc

Copy link
Copy Markdown
Author

Hey guys, sorry I have been away for a couple of weeks! I will go through all your feedback now. Thanks for taking the time!

@itdevelopr

Copy link
Copy Markdown

Hi James welcome back.
I am making a small enhancement to the demo file togglelegend.html to make the graph as dynamic by calling the same code with setinterval() within a function drawgraph(). The flot is hiding the series only for that instance of call and then while redrawing next time the series was showing without hiding.

I sent the code in previous messages, if you want I can send it back. Really appreicate your input on this.

@jamesinc

Copy link
Copy Markdown
Author

@itdevelopr I didn't get your code. Could you possibly put it up as a jsFiddle, or even just e-mail it to me? [email protected]

@jamesinc

Copy link
Copy Markdown
Author

@JeremySimpson could you give me a little more info on point (a), above?

jQuery (at least on version jquery-1.7.1.min) messes up the iteration over entries (it finds a .each propery and calls setupSwatch() on that). Should user for(;;) loop instead.

Edit: Never mind, I figured out what you are referring to. Derp.

@jamesinc

Copy link
Copy Markdown
Author

@itdevelopr The latest has a draft of this functionality. Implementation aside, I should probably add an option to turn the feature on/off.

dnschnur and others added 20 commits January 25, 2014 11:29
Lowercase name in package.json
- Fix JSDoc headers where applicable
- Add spacing before/after comments per jQuery style guide
jQuery removed $.support.leadingWhitespace from jquery greater 2.0
because support was dropped for IE 7/8. So if property is undefined,
we are pretty shure its ie > 8 or jQuery does not support the browser
anyway.
Fix browser detection for jQuery > 2.0
 Pan trigger extended to contain information if the dragging has ended or not
Old version had a hard peerDependency that caused npm install to fail during CI. The lastest uses a more flexible version mapping.
Add spacing to comments in jquery.flot.js
Conflicts:
	NEWS.md
	component.json
	flot.jquery.json
	jquery.flot.canvas.js
	jquery.flot.fillbetween.js
	jquery.flot.pie.js
	jquery.flot.time.js
	package.json
	src/jquery.flot.js
	src/plugins/jquery.flot.crosshair.js
	src/plugins/jquery.flot.navigate.js
	src/plugins/jquery.flot.resize.js
Removing appending warning to dom
@codeBelt

Copy link
Copy Markdown

This is an awesome plugin. Can it get added to the official project? Looks like it failed.

James Ducker added 2 commits November 25, 2014 17:31
The plugin example for toggleLegend has been updated to align better with
Flot's examples structure.
@jamesinc

Copy link
Copy Markdown
Author

@codeBelt the plugin is not the cause of the Travis CI build errors. If you check Travis you'll see the master branch of flot is also failing. The passing branch is their 0.9-work branch.

In any case, the build failures are due to a bad build config. If you want to use the plugin it will work just fine.

@dotnetwise

Copy link
Copy Markdown

What about resizing the axes to match only the checked/visible items?

Currently it only hides them, but it's useless since it won't rescale to the remaining items.

@xource-tech

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.