Hosted By

Contact Me

Misc Links

OpenNTF BlogSphere LotusGeek CoComment Custom Button

Ads by Google

Welcome to keithstric.com!

I hope you find this site useful in some way or another. I strive to bring you all sorts of geeky information and solutions to your most frustrating of issues with the occasional rant on whatever topic, technical reviews and weblog. You'll also find many products that I've developed and make available for you to use however you like. So, grab a cup of coffee, sit down and visit for a while.

« Blogsphere, lists and images... Oh my! | Home Page| Funny Chat spam (This is not spam...) »

SnTT: Using the Google Charts class to put Charts in a Lotus Notes application

04/29/2009 4:11 PM By Keith Strickland

The other day I posted about a Google Charts class I created to make it easier to put a Google Chart in your Lotus Notes/Domino application. Well, this SnTT will show you how you can incorporate this class and display some pretty charts in the Lotus Notes client. I've done other articles about using charts in your Lotus Notes applications, but I think this is probably the easiest and most elegant way of showing a chart. But, let's get to it shall we...

OK, we'll keep this simple so this article will fit within the 65k limit of lookups and such. But we'll need the following:

  • A form to display the chart on inside a Microsoft Web Browser control. Actually this could probably be a page without any ill effects
  • A form to store our HTML in
  • Some views properly sorted to get our data from
  • A view to store our HTML form in sorted by UNID
  • About an hour to put all of this together
  • A server to place this db on, this does not work locally!

Now, we'll start with our first form and call it GoogleCharts. We need to put a Computed field called SaveOptions and give it a value of "0". We then place a Microsoft Web Browser control on the form with the properties displayed below:
google-chart-browser-properties.jpg

The "SizeBelowFields" property will make the browser take up the entire window. I do this because I usually open these types of things within a frame and I want it to take up the whole frame so it looks nice. So, this is all that's needed for this form.

We now need another form which we'll call HTML. This form will contain 1 field named GoogleHTML. This should be hidden from web browsers. Now add below that field (ensure no hide-when is enabled) a computed text item with a value of "GoogleHTML" and mark it as Pass-Thru HTML and that's it for this form.

OK, let's create our view that will hold the HTML documents. This view should have a selection criteria of "SELECT Form="HTML"" and have one column with a formula of "@Text(@DocumentUniqueID)" and sort it ascending.

So our framework is there, we just need a view that we will get the data from that we want to use to populate our chart(s). Since we want to keep this simple we'll just create 2 views, one for categories (luCategories) and one for data (luData). The categories view will contain 5 documents so our chart isn't too cluttered and then the (luData) view will contain the data that we will get the raw numbers from.

Keep reading for the rest of this...

Now the fun begins, we'll need to put some code in the PostOpen event of the GoogleCharts form. Be sure to change the URL to point to your server and wherever you placed the demo database.

Sub Postopen(Source As Notesuidocument) Dim w As New NotesUIWorkspace Dim tmpDoc As NotesDocument Dim HTML As String Dim browser As Variant Dim luUNID As String, url As String Set db = w.CurrentDatabase.Database HTML = |<center><h1>Google Dashboard</h1></center><table width="100%"><tr>| HTML = HTML + |<td><img src="| + getChart("Totals") + |" alt="Goolge Chart">| HTML = HTML + |</br><center>Totals By Company</center></td>| HTML = HTML + |</tr></table>| Set tmpDoc = New NotesDocument(db) tmpDoc.Form = "HTML" tmpDoc.googleHTML = HTML Call tmpDoc.Save(True,False) luUNID = tmpDoc.UniversalID Set browser = Source.GetObject("Browser") url = "http://www.yourdomain.com/thisdb.nsf/HTML/" + luUNID + "?OpenDocument" browser.Navigate(url) End Sub
This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at nsftools.com.

And here's the function that gets called:

Function getChart(dataSel As String) As String Dim luDataView As NotesView Dim luCatView As NotesView Dim doc As NotesDocument Dim luDocCol As NotesDocumentCollection Dim gc As GoogleChart Dim i As Integer Dim gcDataList List As String Set gc = New GoogleChart Set luCatView = db.GetView("(luGoogleCategoryName)") Set luDataView = db.GetView("(luGoogleTotals)") For i = 1 To 10 Set doc = luCatView.GetNthDocument(i) Set luDocCol = luDataView.GetAllDocumentsByKey(doc.coName(0),True) If luDocCol.Count > 0 Then gcDataList(doc.coName(0)) = Cstr(luDocCol.Count) End If Next gc.ChartData = gcDataList gc.ChartSize = "450x150" gc.ChartType = "p3" getChart = gc.ChartURL End Function
This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at nsftools.com.

I made a big assumption on how your views are formatted and how many category items you might want to get. I guess the best thing would be to use the EntryCount property of the category view, but you should get the idea of how all of this works, it's pretty straight forward.

  • First, we get the NotesDatabase and then start building our html.
  • The getChart function just loops through each category and then gets all the corresponding category names from the data view and records the count from a document collection gotten from the data view. This gets recorded in a list formatted like "gcDataList("categoryName") = 20". Of course you can get as complicated or as simple as you like, but we must spit out a list with the listTag being a lable which will show up on the chart and the number being the number that corresponds, in this case, to the piece of the pie for this category.
  • We then pass this list to the GoogleChart ChartData property, which gets used to build the URL.
  • We then use the GoogleChart ChartSize property to set the size of the chart. The width should be 2.5 times larger than the height so that the labels will fit properly.
  • Then, we define the chart type we want to return, in this case we're using a 3d pie chart which corresponds to "p3"
  • And then we get the url using the GoogleChart property ChartURL.
  • The url gets returned to the PostOpen event that then gets put into an img tag.
  • Once all of this is done, we populate the GoogleHTML field in the HTML form, save it and then navigate our browser to the HTML document we just created.

If all of this is clear as mud, which it just might be, I've included a sample database so you can see all of this in action. You can get the sample database over there on the left in the downloads section. The only thing you should need to change is the url to your server in the getChart function. Hopefully you'll get some mileage out of this handy little class and demo.

google-chart-sntt.jpg

EDIT - 4/30/2009 - Updated the requirements list at the top and gave more instruction where to change the URL, also added a screen shot of what this should look like.

Comments

ID: 1
Date: 04/30/2009 11:53:44 AM
Name: Bruce Currier
Website: http://

Keith,
I'm using an 8.5 client and when I try and open your demo DB, I get "Attemped encyrption operation is not supported by this version of Notes".

ID: 2
Date: 04/30/2009 12:11:26 PM
Name: Keith Strickland
Website: http://www.keithstric.com

OK, I thought I had unchecked the "Encrypt locally..." property before I copied it Emoticon But I guess it encrypts it by default as you're copying it. But, it should work now.

ID: 3
Date: 04/30/2009 01:10:19 PM
Name: John Head
Website: http://www.johndavidhead.com

Nice demo Keith! What I really want from google is the ability to do this with no connection to the internet :)

ID: 4
Date: 04/30/2009 01:36:20 PM
Name: John Head
Website: http://www.johndavidhead.com

Keith - this url is failing for me "{ Link }

something special we need to do?

ID: 5
Date: 04/30/2009 01:42:01 PM
Name: Keith Strickland
Website: http://www.keithstric.com

John, change that url to be the hostname of the server you've got the demo on and also the path to the db. So it should read something like:

http:/ /yourservername.com/path/to/googlecharts.nsf

of course, no space between the 2 slashes above, if I didn't put the space there it just got converted to {link} Emoticon

ID: 6
Date: 05/06/2009 06:37:57 AM
Name: Martin
Website: http://

Very nice SnTT demo. But I have to suggestions:

1) I think it would be better to build the dataset using a NotesViewNavigator, and catagorized views. Thats a far move intuitive approach for a Notes developer.

2) If the data isn't very dynamic I would consider downloading the chart image from Google and import it into the Notes document. Doing that it's possible to get rid of the embedded Microsoft Web Browser.

ID: 7
Date: 05/06/2009 09:05:26 AM
Name: Keith Strickland
Website: http://www.keithstric.com

Thanks Martin. I've never used the NotesViewNavigator, guess I need to check it out.

As for downloading the chart, depending on the application and the number of charts that are displayed each time, size could become an issue. Not to mention the possible performance hit of all that code to build the rich text for that. This was meant to be something that you could implement and make something nice in a very short amount of time. For me at least, anytime I have to build rich text on the fly, that usually becomes very time consuming frustrating event Emoticon

Post A Comment

:-D:-o:-p:-(:-):-\:-|:angry::cool::cry::dontknow::emb::hairout::laugh::rolleyes::whew:;-)

Subscribe to keithstric.com

OpenNTF

Disclaimer

The opinions and ideas posted on keithstric.com are not necessarily the opinions and ideas of my employer. The solutions, techniques and code provided here are not guaranteed or warranted in any way and are free for you to use at your own risk.