Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery Flot (stacked bar chart)  and Coldfusion cfquery

Posted on 2014-02-11
8
Medium Priority
?
1,532 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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…

916 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