Scroll to top
Sponsored Content

This sponsored post features a product relevant to our readers while meeting our editorial guidelines for being objective and educational.

Web charts built with JavaScript are a great way to add interactivity to your apps and sites, but if you prefer working in jQuery, your options can be limited. Developers are often left to choose between convenience or features. All the bells and whistles in 100 lines of code or a simpler version in 30? In order to address this, the team at ZingChart have developed a wrapper to use their API with jQuery syntax, allowing devs to quickly build charts with the rich interactivity they want.

Common Use Cases

There is a jQuery call for every function in the ZingChart API–all 169 of them. In this tutorial we’ll be covering a handful of them in three of the most common use cases:

  1. DOM manipulation
  2. Chart manipulation
  3. Using AJAX data

You can view the full reference on the ZingChart jQuery wrapper Github page.

Scripts and Files

If you don’t have a copy of the ZingChart library or jQuery wrapper, there are a few options:

Setting Up

Set up your HTML file by including the ZingChart library and any additional modules you need. You’ll also need to include jQuery and, finally, the ZingChart jQuery wrapper. The wrapper is compatible with jQuery versions 1.x and 2.x.

1
<!DOCTYPE html>
2
3
<html>
4
5
    <head>
6
7
		<meta charset="utf-8">
8
9
		<title>ZingChart jQuery Wrapper Demo</title>
10
11
		<script src="http://cdn.zingchart.com/zingchart.min.js"></script>
12
13
		<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
14
15
		<script src="http://cdn.zingchart.com/zingchart.jquery.min.js"></script>

Initializing charts is now straight forward with the .zingchart() call. This method (and all others which take an object as a parameter) can take chart data directly or by reference (in this case where the data is stored in a variable data1).

Init by reference

1
$("#demo-1").zingchart({
2
3
 data: data1
4
5
});

Init with data

1
//Init chart with data directly

2
3
    		$("#demo-2").zingchart({
4
5
			    data: {
6
7
				    type: "line",
8
9
	                "background-color":"#eff0f0",
10
11
	                "tooltip":{
12
13
	                    "padding":"20 20 20 20",
14
15
	                    "font-family":"arial",
16
17
	                    "font-color":"#666666",
18
19
	                    "border-radius":5,
20
21
	                    "shadow":0
22
23
	                },
24
25
	                "scale-x":{
26
27
	                    "line-color":"#666666",
28
29
	                    "tick":{
30
31
	                        "line-color":"#666666"
32
33
	                    },
34
35
	                    "item":{
36
37
	                        "font-color":"#666666",
38
39
	                        "font-family":"arial"
40
41
	                    }
42
43
	                },
44
45
	                "scale-y":{
46
47
	                    "line-color":"#666666",
48
49
	                    "tick":{
50
51
	                        "line-color":"#666666"
52
53
	                    },
54
55
	                    "item":{
56
57
	                        "font-color":"#666666",
58
59
	                        "font-family":"arial"
60
61
	                    }
62
63
	                },
64
65
				    plot:{
66
67
				        aspect:"spline",
68
69
	                    "hover-state":{
70
71
	                        "shadow":0
72
73
	                    },
74
75
	                    "marker":{
76
77
	                        "size":8,
78
79
	                        "border-width":0,
80
81
	                        "background-color":"#00ccff",
82
83
	                        "shadow":0
84
85
	                    }
86
87
				    },
88
89
				    series: [
90
91
				        {
92
93
				            values: [3,4,10,2,6,5],
94
95
	                        "line-color":"#00ccff",
96
97
	                        "shadow":0
98
99
				        }
100
101
				    ]
102
103
				}
104
105
			});

1. DOM Manipulation

The first demo is an example of DOM manipulation using one of the listeners from the wrapper, .nodeHover(). Hovering over a node updates the table below it–particularly useful in situations where you need to provide additional information about chart data outside of the chart itself. There are listeners for all chart objects as well as certain events, such as .feedStart(), .historyBack(), and many more.

1
    		// Bind an event listener to node hover

2
3
			$("#demo-1").nodeHover(
4
5
			    
6
7
			    // plotMouseOver function

8
9
			    function(){
10
11
			        // Get all values for the hovered plot

12
13
			        var plotVals = $(this).getPlotValues({
14
15
			            plotindex:this.event.plotindex
16
17
			        });
18
19
			        
20
21
			        // Get hover node index

22
23
			        var idx = this.event.nodeindex;
24
25
			        
26
27
			        for (var i = 0; i<plotVals.length; i++) {
28
29
			            // Set each table elem equal to the cooresponding node

30
31
			            $("#table-1 td").eq(i).text(plotVals[i])
32
33
			            // Highlight the corresponding hovered node in the table

34
35
			            $("#table-1 td").eq(idx).addClass("hover");
36
37
			        }
38
39
			    },
40
41
			    
42
43
			    // plotMouseOut function

44
45
			    function(){
46
47
			        // Reset the table text

48
49
			        $("#table-1 td").each(function(){
50
51
			            $(this).text("--").removeClass("hover");
52
53
			    });
54
55
			});

Take a look at the demo to see what that gives us.

2. Chart Manipulation

The second chart demonstrates the reverse–chart manipulation via the DOM. Using normal jQuery, we place input listeners on a set of sliders. Slider input is cast to an int and passed to a .setNodeValue() call for the corresponding node.

1
    		$("input[type='range']").each(function(idx){
2
3
			    
4
5
			    // Bind input events to each slider

6
7
			    $(this).on("input",function(){
8
9
			        // Get the value of each slider on input events

10
11
			        var newVal = parseInt($(this).val());
12
13
			        // Set the value of the corresponding node to the slider's new value

14
15
			        $("#demo-2").setNodeValue({
16
17
			            plotindex:0,
18
19
			            nodeindex:idx,
20
21
			            value: newVal
22
23
			        })
24
25
			    });
26
27
			});

Take a look at the demo on Codepen to see what that gives us.

3. Loading AJAX Data

Loading new data is a snap. Upon a successful .get request, pass your results in with one of the many setter methods such as .appendSeriesData(), .setSeriesValues(), .modify(), .setData(), etc. In the example below, we use .setSeriesValues() to pass in a new array of values returned from our AJAX call.

1
    		// Bind a click event to the button

2
3
			$("button").click(function(){
4
5
			    
6
7
			    // Issue a get request

8
9
			    $.get( 'https://api.myjson.com/bins/530az', function() {})
10
11
			    
12
13
			    // Upon a successful get request...

14
15
			    // (notice we haven't even touched the ZingChart API yet)

16
17
			    .done(function(res){
18
19
			        
20
21
			        // Store the new data in a variable (totally optional)

22
23
			        var newData = res.data;
24
25
			        
26
27
			        // Set the series values equal to the newData

28
29
			        $("#demo-3").setSeriesValues({
30
31
			            "values": [newData]
32
33
			        });
34
35
			        
36
37
			        // Tada! Your chart just used AJAX data. Begin the disco.

38
39
			    });
40
41
			});

Again, take a look at the demo on Codepen to see what we now have.

Chaining

Method chaining is one of the most popular features of jQuery. This wrapper supports chaining for any methods or events that return a jQuery object. Instead of calling chart manipulation functions separately, you can now chain your calls in one line:

1
$("#myChart").set3dView({"y-angle":10}).resizeChart({"width":600,"height":400});

The full demo file is available to download.

Conclusion

That was a very quick run through, demonstrating how to use jQuery for ZingChart. With these basics under your belt you should be able to take your own charts much further! Show us your examples and feel free to ask for feedback in the comments.

Resources

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.