Solved

jQuery Flot (stacked bar chart)  and Coldfusion cfquery

Posted on 2014-02-11
8
1,443 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now