Skip to content
panterlo edited this page Oct 17, 2010 · 3 revisions

The MVC sample is commited into the source and can be downloaded and tried out. It’s for .NET 4.0 and MVC 2. Basically there is only one controller method that is intresting which is, GetSomeGoogleVisualizationData. This controller method get’s the asynchronous call from the Google Visualization API Query method.

Here is the code

 [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);
        }
Clone this wiki locally