Google Visualization API and classic ASP.NET and MVC becomes a breeze

238 views
Skip to first unread message

Jens

unread,
Oct 17, 2010, 7:29:25 AM10/17/10
to Google Visualization API
I thought I would share that I today released the Google Visualization
API C# class library that I had to develop for a solution a while
back. It can be used for classical ASP.NET and MVC. Source can be
found on github, http://github.com/panterlo/v1s.GoogleVisualization/wiki.
Released under GPL v3, share, contribute and continue to build a .NET
library to use Google Visualization easily.

Basically this class library offers .NET users to easily work with
Google Visualization Requests in the controllers to support
constructing proper visualization responses together with the
datatable. This example just shows one way of handling the request,
another ways are available. The new GoogleVisualizationAttribute can
be used to "push" the request into a controller object of
GoogleVisualizationRequest type.

Short example of a controller method in MVC 2.

[GoogleVisualizationAttribute]
public GoogleVisualizationResult
GetSomeGoogleVisualizationData(GoogleVisualizationRequest
googleVisualizationRequest, int numRecords, string someParam)
{
// Let make the response, construct the response by
passing the request
GoogleVisualizationResponse googleVisualizationResponse =
new GoogleVisualizationResponse(googleVisualizationRequest);

// We need a Google Visualization DataTable to store the
data
GoogleVisualizationDataTable dataTable = new
GoogleVisualizationDataTable();

// Add a column representing date time
GoogleVisualizationDataTableColumn column1 =
dataTable.AddColumn(GoogleVisualizationDataTableColumnType.DateTime,
"date");

// Add a column representing a number
GoogleVisualizationDataTableColumn column2 =
dataTable.AddColumn(GoogleVisualizationDataTableColumnType.Number,
"Serie1");


// Now we need to add the data
Random random = new Random();

// Just use the now date plus 7 days for each iteration
DateTime dateTime = DateTime.Now;

// Fake some data records
for (int i = 0; i < numRecords; i++)
{
// Add new row to the datatable
GoogleVisualizationDataTableRow row =
dataTable.AddRow();

// Add cell by column parameter
List<GoogleVisualizationDataTableRowCell> cells =
dataTable.AddCellForColumn(row, column2,random.Next(10,100), null);

// But you can enter the column index instead when
inserting cell values
cells[0].CellValue = dateTime;

// Add 7 days for next iteration
dateTime = dateTime.AddDays(7);
}

// Set the datatable in the response
googleVisualizationResponse.GoogleVisualizationDataTable =
dataTable;

// Send back response
return new
GoogleVisualizationResult(googleVisualizationResponse);
}


Regards,

Jens
Reply all
Reply to author
Forward
0 new messages