COMP5347 - Tutorial w9 Jquery Charting
COMP5347 - Tutorial w9 Jquery Charting
Learning Objectives
• Get familiar with jQuery selectors and syntax
Download week9-src.zip from Canvas, Start Eclipse, select “open projects from
file system” under file menu, go to the directory you just extracted the content and
click open. In this case, all the necessary node modules are specified in
package.json. Right click it on the project explorer panel, select “Run as” then “npm
install".
You can run revserver.js as node.js application and you open this address with
your browser: http://localhost:3000/revisionajax. Open the Developer Tools
or Inspector from your browser and refresh the page. You should see information
similar to this figure:
1
COMP5347 Tutorial
You can check revision.server.routes.js and see that path /revisionajax will
load titleFormAjax.pug. If you check this pug file, it loads main.js file inside js/
directory and jQuery library. Beside loading main.js file, this file also has a button
with id “button” and a div with id “results”. You will need to check the main.js
file and try to understand what will this js file do with these two elements. When the
button is clicked, it will try to find the value of “ title” input and use it as a
parameter for requesting /getLatest page. The results of /getLatest request
then will be displayed inside the div with id result. This way, the result of the
requested title will be shown in the same page of the form.
While running the server, access a new page from browser with this address:
http://localhost:3000/gchart. This would display a simple page with two links. If
you click the first link “Pie Chart”, a pie chart would display. If you click the second
link, you will get an error message.
2
COMP5347 Tutorial
ExpressJS has predefined functions to configure the response in various ways, for a full
list see reference document https://expressjs.com/en/api.html#res.
Now open graphs.pug and inspect its content in Eclipse. The body element of
graphs.pug contains two anchor links. Both links are meant to be processed by
client side javascript code. The head element of graphs.pug contains links to three
scripts. The first two are third party libraries: jQuery and Google Charts loader. The
last one is our own script: graphs.js
Majority of the processing in this graph page happens on client side and is implemented
in graphs.js. Charting related functions and statements in this script are adapted from
Google Charts Tutorials.
(https://developers.google.com/chart/interactive/docs/quick_start)
The first statement in this script calls the load() function to load the corechart
package. This statement is required for any web page using Google Charts. Two
global variables: options and data are declared in the next two statements. A
global function drawPie() is declared to draw a pie chart based on the chart data
stored in data variable. The chart data is stored as javascript object, the next few
statements follow google Charts tutorial to prepare the DataTable. jQuery’s
$.each() function is used to iterate all key value pairs in the javascript object. After
DataTable is prepared, a pie chart is created on the designed.
(https://developers.google.com/chart/interactive/docs/basic_load_libs)
$(document).ready(function() {
...
}
3
COMP5347 Tutorial
Now write your own code to display a bar charts when a user clicks the “Bar Chart”
link. You can find tutorial on bar chart below. Note that Google Charts uses
different terminologies for vertical and horizontal bar charts.
https://developers.google.com/chart/interactive/docs/gallery/columnchart