Solved

jQuery Flot (stacked bar chart)  and Coldfusion cfquery

Posted on 2014-02-11
8
1,462 Views
Last Modified: 2014-03-06
Hello,

I'm trying to implement a stacked bar chart using jQuery Flot. The problem is, I'm having a hard time trying to pass the results of a cfquery to the chart. Any help and sample code is appreciated!

The below is the code I'm using for the chart. Need to pass the query results to d1 and d2.  Thanks!


if($("#stackchart").length)
	{
		var d1 = [
            [0, 20],
            [1, 35],
            [2, 20],
            [3, 35],
            [4, 35],
        ];
		

		var d2 = [
			[0, 100],
            [1, 150],
            [2, 125],
            [3, 160],
            [4, 95]	  
			];
		
		var stack = 0, bars = true, lines = false, steps = false;

		function plotWithOptions() {
			$.plot($("#stackchart"), [ { label:"Dispositioned", data: d1}, { label:"Qualifying", data:d2}], {				  
				series: {
					stack: stack,
					lines: { show: lines, fill: true, steps: steps },
					bars: { show: bars, barWidth: 0.6 },
				},
				colors: ["#2FABE9","#FA5833"],
			}
			);
			
		  }

Open in new window

0
Comment
Question by:HumanScaleDev
  • 4
  • 4
8 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39852896
I have created a page with your code, just adding a call to plotWithOptions and it seems to work fine.
<script type="text/javascript">

$(function() {


if($("#stackchart").length)
	{
		var d1 = [
            [0, 20],
            [1, 35],
            [2, 20],
            [3, 35],
            [4, 35],
        ];
		

		var d2 = [
			[0, 100],
            [1, 150],
            [2, 125],
            [3, 160],
            [4, 95]	  
			];
		
		var stack = 0, bars = true, lines = false, steps = false;

		function plotWithOptions() {
			$.plot($("#stackchart"), [ { label:"Dispositioned", data: d1}, { label:"Qualifying", data:d2}], {				  
				series: {
					stack: stack,
					lines: { show: lines, fill: true, steps: steps },
					bars: { show: bars, barWidth: 0.6 },
				},
				colors: ["#2FABE9","#FA5833"],
			}
			);
			
		  }

		  plotWithOptions();
	}

});

</script>

Open in new window

test page here: http://schutt.nl/ee/Q_28362467
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39853006
PS: I was assuming you have no problem generating the arrays. If that assumption is wrong, I can't test cf code but some basic examples "from the source" can be found here: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=arrayStruct_05.html#1123267
0
 

Author Comment

by:HumanScaleDev
ID: 39867780
I apologize some other urgent priority came up. I will try to look into this this week and I will close this question once  I have a chance. But from what I read, looks like all I needed was another set of eyes looking into this. :). I will get back to you as soon as I have a chance. Thank you!
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:HumanScaleDev
ID: 39907782
It's working great thanks!  Do you happen to know how to add the tooltip displaying the values?
Thank you!
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39907976
I've searched around a bit, it seems you need custom code for this, even some plugins exist. I tried a few but didn't get it to work with the above code yet...
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39908027
Ah, found something.
<script type="text/javascript">

$(function() {


if($("#stackchart").length)
    {
        var d1 = [
            [0, 20],
            [1, 35],
            [2, 20],
            [3, 35],
            [4, 35],
        ];
        

        var d2 = [
            [0, 100],
            [1, 150],
            [2, 125],
            [3, 160],
            [4, 95]	  
            ];
        
        var stack = 0, bars = true, lines = false, steps = false;

        function plotWithOptions() {
            $.plot($("#stackchart"), [ { label:"Dispositioned", data: d1}, { label:"Qualifying", data:d2}], {
                series: {
                    stack: stack,
                    lines: { show: lines, fill: true, steps: steps },
                    bars: { show: bars, barWidth: 0.6 },
                },
                colors: ["#2FABE9","#FA5833"],
                grid: { // this one is important
                    hoverable: true
                }
            });

        }

        plotWithOptions();

        function showTooltip(x, y, contents, z) {
            $('<div id="ftt">' + contents + '</div>').css({
                top: y - 20,
                left: x - 90,
                'border-color': z,
            }).appendTo("body").show();
        }

        $("#stackchart").bind("plothover", function (event, pos, item) {
            if (item) {
                if (previousPoint != item.datapoint) {
                    previousPoint = item.datapoint;
                    $("#ftt").remove();
     
                    x = item.datapoint[0];
                    y = item.datapoint[1];
                    z = item.series.color;
     
                    showTooltip(item.pageX, item.pageY,
                        "<b>" + item.series.label + "</b><br /> " + x + " = " + y,
                        z);
                }
            } else {
                $("#ftt").remove();
                previousPoint = null;
            }
        });

    }

});

</script>

<style type="text/css">
#ftt {
    position: absolute;
    border: 3px solid black;
    background-color: rgba(255,255,223,0.7);
}
</style>

Open in new window

Note the addition in the options when calling plot(). Also there's a lot of customisation you can do in the new function and CSS.
capture
0
 

Author Comment

by:HumanScaleDev
ID: 39910712
thank you! this is awesome! you just need to declare "previousPoint" otherwise you'll get a js error, that's all! thanks again! :D
0
 

Author Closing Comment

by:HumanScaleDev
ID: 39910718
I appreciate your help and your time as the tooltip question was not included here. Thank you so much!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question