Solved

jQuery Flot (stacked bar chart)  and Coldfusion cfquery

Posted on 2014-02-11
8
1,476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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 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…

751 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