69
Bohyun Kim LITA Forum 2013 Louisville, KY Nov. 8, 2013 http://bohyu nkim.net @bohyunkim Slides: http:// slideshare.n et/bohyunkim TAKE BETTER CARE OF LIBRARY DATA AND SPREADSHEETS WITH GOOGLE VISUALIZATION API QUERY LANGUAGE

Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

Embed Size (px)

DESCRIPTION

Presentation given at 2013 LITA Forum on Nov 8, 2013. http://www.ala.org/lita/conferences/forum/2013 ; Example files are at http://github.com/bohyunkim/examples

Citation preview

Page 1: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

Bohyun KimLITA Forum 2013Louisville, KYNov. 8, 2013

http://bohyunkim.net@bohyunkim

Slides:http://slideshare.net/bohyunkim

TAKE BETTER CARE OF LIBRARY DATA

AND SPREADSHEETS WITH

GOOGLE VISUALIZATION API QUERY LANGUAGE

Page 2: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

BECAUSE WE ALL LOVE TO KEEP TRACK OF THINGS …

CIRC

Newspaper

holdings

Textbook lists

ACQ

Newspaper

holdings

Textbook lists

REFNewspap

er holdings

Textbook lists

WEB

Newspaper

holdings

Textbook lists

Page 3: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

Creating a web application with a database is a good but not always available, convenient, or effi cient solution. Image from Flickr CreativeCommons http://www.flickr.com/photos/88575173@N00/2445955296/

Page 4: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

A LOW-BARRIER LOW-COST SOLUTION

CIRC

ACQ REF WE

B

1. Data Source<- Google Spreadsheet

2. Query data <-- Google Visualization API Query Language

3. Display on the web<-- Google Chart Libraries

Page 5: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

Allows you perform various data manipulations with the query to the data source, such as Google spreadsheet.

Enables you to query the data source (e.g. Google spreadsheet & fusion table) and visualize data with Google Chart Libraries

Google Visualization API Reference: https://developers.google.com/chart/interactive/docs/reference

I. GOOGLE VISUALIZATION API QUERY LANGUAGE

Page 6: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

GOOGLE SPREADSHEET: LOW BARRIER FOR USE AND

ADOPTION

Page 7: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

In Google Spreadsheet – Sheet3https://docs.google.com/spreadsheet/ccc?key=0AqAPbBT_k2VUdDc3aC1xS2o0c2ZmaVpOQWkyY0l1eVE&usp=drive_web#gid=2

Raw Data Response: includes a DataTable that you can retrieve by calling getDataTable() https://spreadsheets.google.com/tq?&tq=&key=0AqAPbBT_k2VUdDc3aC1xS2o0c2ZmaVpOQWkyY0l1eVE&gid=2

HTML Tablehttps://spreadsheets.google.com/tq?tqx=out:html&tq=&key=0AqAPbBT_k2VUdDc3aC1xS2o0c2ZmaVpOQWkyY0l1eVE&gid=2

URLS FOR SHEET3

Page 10: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

DATATABLE FROM G SPREADSHEET

Page 11: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language
Page 13: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

OUTPUT AS A STANDALONE WEBPAGE WITH DATA IN A TABLE

Page 14: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

The query string can be added to the data source URL using the tq parameter.

The query string must be properly encoded as a URL parameter.

You can encode a URL using the JavaScript encodeURIComponent() function, or you can encode it by hand.

URL encoder/decoder: http://meyerweb.com/eric/tools/dencoder/

“Query Language Reference (Version 0.7)”https://developers.google.com/chart/interactive/docs/querylanguage#Setting_the_Query_in_the_Data_Source_URL

II. SET THE QUERY (A) IN THE DATA SOURCE URL

Page 15: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

ENCODING URL

Page 16: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

Step 1. Form a query e.g Show column B, C, D, F where C contains ‘Florida’

Step 2. Output the query result As a web page with a table

ORInto your web page as a table (or a chart)

LET’S QUERY GOOGLE SPREADSHEET

Page 18: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

https://spreadsheets.google.com/

tq?

tqx=out:html

&tq=select B,C,D,F where (C contains 'Florida’)

&key=0AqAPbBT_k2VUdEtXYXdLdjM0TXY1YUVhMk9jeUQ0NkE

&gid=0 (optional for Sheet1)

QUERY IN A URL

Page 19: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

https://spreadsheets.google.com/tq?tqx=out:html&tq=select B,C,D,F where (C contains 'Florida')&key=0AqAPbBT_k2VUdEtXYXdLdjM0TXY1YUVhMk9jeUQ0NkE

https%3A%2F%2Fspreadsheets.google.com%2Ftq%3Ftqx%3Dout%3Ahtml%26tq%3Dselect%20B%2CC%2CD%2CF%20where%20(C%20contains%20%27Florida%27)%26key%3D0AqAPbBT_k2VUdEtXYXdLdjM0TXY1YUVhMk9jeUQ0NkE

ENCODE URL

encode

decode

Page 21: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

To set the query string from within JavaScript code, call the setQuery method of the google.visualization.Query class.

More flexible and you can add control options for users.

II. (B) WITHIN JAVASCRIPT

Page 22: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

CAN BE DROPPED INTO ANY WEB PAGE

Query: SELECT B, C, D, F WHERE C contains ‘Florida’

Page 23: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

example1._florida.html

Page 24: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

headers=N - Specifi es how many rows are header rows, where N is an integer zero or greater. These wil l be excluded from the data and assigned as column labels in the data table. If you don't specify this parameter, the spreadsheet wil l guess how many rows are header rows. Note that if al l your columns are string data, the spreadsheet might have diffi culty determining which rows are header rows without this parameter.

Range=NN:NN – e.g. range=A1:C6 gid=N - Specifi es which sheet in a multi-sheet document to l ink

to, if you are not l inking to the fi rst sheet. N is the sheet's ID number, an integer zero or greater. It is one less than the number in the sheet name when it is created: for example, gid=0 for Sheet1You can use the sheet parameter instead of this parameter.

Example: gid=5. sheet=sheet_name - Specifi es which sheet in a multi-sheet

document you are l inking to, if you are not l inking to the fi rst sheet. sheet_name is the display name of the sheet. You can use the gid parameter instead of this parameter. Example: sheet=Sheet5.

PARAMETERS

Page 25: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

III. HOW TO FORM A QUERY

Image from Flickr Creative Commons http://www.flickr.com/photos/96301288@N00/117322774/

Page 26: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

Google Query Language ReferenceSimilar to SQLhttps://developers.google.com/chart/interactive/docs/

querylanguage

GQL REFERENCE

Page 27: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

Show columns, B, C, D, F where B contains ‘Agoulnik.’https://spreadsheets.google.com/tq?

tqx=out:html&tq=select+B,C,D,F+where+%28B+contains+%27Agoulnik%27%29&key=0AqAPbBT_k2VUdEtXYXdLdjM0TXY1YUVhMk9jeUQ0NkE

SELECT __ WHERE __

Page 28: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language
Page 29: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

SELECT B,C,D,F WHERE (B CONTAINS 'AGOULNIK') ORDER BY

DESChttps://spreadsheets.google.com/tq?

tqx=out:html&tq=select+F, B,C,D+where+%28B+contains+%27Agoulnik%27%29+order+by+F+DESC&key=0AqAPbBT_k2VUdEtXYXdLdjM0TXY1YUVhMk9jeUQ0NkE

Page 30: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language
Page 31: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

https://spreadsheets.google.com/tq?tqx=out:html&tq=select+F, B,C,D+where+%28B+contains+%27Agoulnik%27%29+order+by+F+DESC+limit+5&key=0AqAPbBT_k2VUdEtXYXdLdjM0TXY1YUVhMk9jeUQ0NkE

LIMIT

Page 32: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

LIMIT 5: THE MOST RECENT FIVE ARTICLES

Page 33: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

https://spreadsheets.google.com/tq?tqx=out:html&tq=select+F, B,C,D+where+%28B+contains+%27Agoulnik%27%29+order+by+F+DESC+off set+3&key=0AqAPbBT_k2VUdEtXYXdLdjM0TXY1YUVhMk9jeUQ0NkE

OFFSET

Page 34: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

OFFSET 3

Page 35: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

SELECT C, COUNT(I) WHERE (G CONTAINS '2011')

GROUP BY CShows the number of literature search requests in

2011 by values in C - either Faculty or Staff.

Shows the number of articles published in a particular year.

select F, count(C) where (B contains 'Agoulnik') group by F

Page 36: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

SELECT G, C, COUNT(I) WHERE (G CONTAINS '2011') GROUP BY G,

CShows the number of literature search requests in

2011 by values in C – Faculty or Staff - and G – date.

Page 38: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

SELECT D,F COUNT(C) WHERE (B CONTAINS ‘AUTHOR’) GROUP BY

D, F

Page 39: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

PIVOT

Page 44: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

select C, count(I) where (G contains '2011') group by C label C ‘Type’, count(I) ‘Number of Requests’

https://spreadsheets.google.com/tq?tqx=out:html&tq=select+C,count%28I%29+where+%28G+contains+%272011%27%29+group+by+C+label+C+%27Type%27+,+count%28I%29+%27Number%20of%20Requests%27&key=0AqAPbBT_k2VUdFVnUlpkS3dHRVotWGtUeDNibUxWWlE

LABEL

Page 45: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

LABEL

Page 46: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

The format clause is used to specify a formatted value for cells in one or more columns. The returned data should include both an actual value and a formatted value for each cell in a formatted column. Many visualizations use the unformatted value for calculations, but the formatted value for display. The patterns that you specify in this clause are usually returned in the pattern property of the corresponding columns.

Pattern Syntax:number, date, timeofday, datetime The date and number

patterns defi ned by the ICU. boolean Pattern is a string in the format 'value-if-true:value-if-false '. Example:

format salary '#,##0.00', hireDate 'dd-MMM-yyyy', isSenior 'Yes!:Not yet'

FORMAT G

Page 47: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

SELECT G,H,I WHERE G CONTAINS 2011 LIMIT 3

Page 48: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

SELECT G,H,I WHERE G CONTAINS 2011 LIMIT 3 FORMAT G 'DD-MMM-

YYYY'

Page 50: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

avg() max() min() count() sum() year(), month(), date(), hour() minute(), second(), millisecond() quarter() dayOfWeek() now() dateDiff () toDate() upper() lower()

OTHER FUNCTIONS

https://developers.google.com/chart/interactive/docs/querylanguage#Options

Page 51: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

The most common way to use Google Charts is with simple JavaScript that you embed in your web page.

1. You load some Google Chart libraries,2. list the data to be charted,3. select options to customize your chart, 4. and finally create a chart object with an id that you

choose. Then, later in the web page, you create a <div> with that id to display the Google Chart.

https://developers.google.com/chart/interactive/docs/

IV. VISUALIZE YOUR SPREADSHEET DATA

USING GOOGLE CHART LIBRARIES

Page 52: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

DATA AS A TABLE OR ANY CHART

To set the query string from within JavaScript code, call the setQuery method of the google.visualization.Query class

Chart drawing techniques:

a) chart.draw()b) ChartWrapperc) DrawChart()

https://developers.google.com/chart/interactive/docs/drawing_charts

Page 53: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

1. Load the JSAPI, Google Visualization, and chart l ibraries2. Prepare your data3. Prepare any chart options4. Instantiate the chart class, passing in a handle to the page container

element.5. Optionally register to receive any chart events. I f you intend to cal l

methods on the chart, you should l isten for the "ready" event.6. Call chart.draw(), passing in the data and options.

Advantages: You have complete control over every step of the process. You can register to l isten for al l events thrown by the chart.

Disadvantages: Verbose You must explic it ly load al l required chart l ibrar ies I f you send queries, you must manually implement for the cal lback, check

for success, extract the returned DataTable, and pass it to the chart.

(A) CHART.DRAW()

Page 54: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language
Page 55: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language
Page 56: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

SEPARATE THE QUERY FROM URL

Page 57: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

MULTIPLE CHARTS

Page 58: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

example2_chartdraw.html

Adapted from Traci L. Ruthkoski, Google Visualization API Essentials, Packt, 2013.

Page 59: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

ChartWrapper is a convenience class that handles loading all the appropriate chart l ibraries for you and also simplifi es sending queries to Chart Tools Datasources.

Advantages: Much less code Loads all the required chart l ibraries for you Makes querying Datasources much easier by creating the Query

object and handling the callback for you Pass in the container element ID, and it wil l call getElementByID

for you. Data can be submitted in a variety of formats: as an array of

values, as a JSON literal string, or as a DataTable handle

Disadvantages: ChartWrapper currently propagates only the select, ready, and

error events. To get other events, you must get a handle to the wrapped chart and subscribe to events there. See the ChartWrapper documentation for examples.

(B) CHARTWRAPPER

Page 62: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

DrawChart is a global static method that wraps a ChartWrapper.

Advantages:Same as ChartWrapper,

but slightly shorter to use.

Disadvantages:Does not return a handle

to the wrapper, so you cannot handle any events.

(C) DRAWCHART()

Page 63: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

example3_piechart.html

Page 65: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

1. Create An HTML Skeleton For Your Dashboard2. Load Your Libraries3. Prepare Your Data4. Create A Dashboard Instance5. Create Control And Chart Instances6. Establish Dependencies7. Draw Your Dashboard8. Programmatic Changes After Draw

google.load('visualization', '1.0',{'packages':['controls']});….dashboard.bind(donutRangeSlider, pieChart);dashboard.draw(data);

STEPS

https://developers.google.com/chart/interactive/docs/gallery/controls

Page 66: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

http://code.google.com/apis/ajax/playground/

MORE EXAMPLES IN GOOGLE CODE PLAYGROUND

Page 67: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

DASHBOARD WITH MULTIPLE CONTROLS AND A REMOTE DATA

SOURCE

new google.visualization.Dashboard(document.getElementById('dashboard')).bind([slider, categoryPicker], [pie, table]).draw(data);

example5_dashboard.html

Page 68: Take Better Care of Library Data and Spreadsheets with Google Visualization API Query Language

http://www.google.com/publicdata/directory

MORE IDEAS & DATAAT GOOGLE PUBLIC DATA EXPLORER