Jan 092013
 
Share...Tweet about this on Twitter2Share on Facebook0Share on Google+1Share on StumbleUpon0Share on LinkedIn2Flattr the authorPin on Pinterest0Share on Reddit0Share on TumblrDigg this

Google DataTable .NET Wrapper library got its third version (v3.0.0). In this post I will highlight the changes.

For basic information about the library check a previous post of how to use the Google DataTable .Net Wrapper Library

To download or consult the source code please visit the project’s CodePlex page or install directly from Visual Studio by using Nuget

PM> Install-Package Google.DataTable.Net.Wrapper

Custom object properties

Version 3.0.0 mainly brings a unified way of managing custom properties that could be attached to the Column, Row or Cell objects. Of course, depending of the chart type you are using, those will be supported or not.

The (breaking) change here is that instead of having a simple String property (previously known as Properties), now a List type have been introduced for all the above three types of objects.

Let’s see what would be a very common usage:

DataTable dt = new DataTable();

var row = dt.NewRow();

//Previously you had to manually format the output for the JSON string....
row.Properties = "\"style\": \"border:1\", \"cssStyle\": \"mystyle\"";

//Now you can use a much more user friendly....
row.AddProperty(new Property("style", "border:1"));
row.AddProperty(new Property("cssStyle", "mystyle");

As the same kind of logic has been used for Cell, Column and Row, the output is exactly the same in all the cases (the “p:” part). This is an example of the JSON returned for the above Row example…

"rows" : 
[
	{
		"c" : [{"v": new Date(2013, 0, 7), "f": "Year"}, {"v": 0}], 
		"p" : {"style" : "border:1", "cssStyle" : "myStyle"}
	}
]

PropertyMap

In order to achieve this, I’ve introduced a very simple class called Property that would hold the values for a single entry.

public class Property
{
    public Property(string name, string value)
    {
        Name = name;
        Value = value;
    }

    public string Name { get; set; }

    public string Value { get; set; }
}

//and the signature for the objects is now...
public class Cell //Column and Row
{
//....
    public IEnumerable<Property> PropertyMap
    {
        get { return _propertyMap; }
    }

    public Property AddProperty(Property p)
    {
        _propertyMap.Add(p);
        return p;
    }

    public void RemoveProperty(Property p)
    {
        _propertyMap.Remove(p);
    }
//.... omitted for brevity
}

SystemDataTableConverter

This version introduced a new functionality implemented as a new type called SystemDataTableConverter which enables the conversion from the widely known Ado.NET System.Data.DataTable into our DataTable. This obviously gives a possibility to return a JSON string and it is very handy as a large amount of projects stilly use the System.Data.DataTable as the mean of transferring data.

How to use the converter

The following code is an example of how to use the converter:

System.Data.DataTable sysDt = new System.Data.DataTable();
sysDt.Columns.Add("firstcolumn", typeof(string));
sysDt.Columns.Add("secondcolumn", typeof(int));
sysDt.Columns.Add("thirdcolumn", typeof(decimal));

var row1 = sysDt.NewRow();
row1[0] = "Ciao";
row1[1] = 10;
row1[2] = 2.2;
sysDt.Rows.Add(row1);

var dataTable = SystemDataTableConverter.Convert(sysDt);
string jsonString = dataTable.GetJson();

this would be the output for the above code:

{
"cols": 
    [
	{"type": "string" ,"id": "firstcolumn" ,"label": "firstcolumn" }, 
	{"type": "number" ,"id": "secondcolumn","label": "secondcolumn"},
	{"type": "number" ,"id": "thirdcolumn" ,"label": "thirdcolumn" }
    ], 
"rows" : 
    [
	{
            "c" : [ {"v": "Ciao"}, {"v": 10}, {"v": 2.2} ]
        }
    ]
}

so, without any effort your existing DataTable will get serialized into JSON needed by the Google Javascript library.

Column Role attribute

As it is stated in the google datatable documentation at the following link Google DataTable Roles, Column object should support the “Role”.

This version implements a new attribute for the Column, called indeed, Role.

Let’s see an example:

var column = new Column(ColumnType.String);

//please not the ColumnRole.Annotation 
column.Role = ColumnRole.Annotation;

Role is a string, and I’ve simply created a helper class ColumnRole that implements as attributes the currently available values that DataTable Column supports.
this is absolutely a valid code instead:

var column = new Column(ColumnType.String);
column.Role = "annotation";

Serialization of the Date() values

The way of how the DateTime and Date type columns are serialized is changed slightly. In the earlier version indeed columns of Date and Datetime were not properly serialized, so this is a bug fix.

While this would work when you are directly passing the literal string to the Javascript dataTable

 {c:[{v: 'c'}, {v: 3.0, f: 'Three'}, {v: new Date(2010, 3, 30, 0, 31, 26)}]}

it won’t work if you return a JSON in such a format, as JSON doesn’t support such a notation, so you should use instead

 {c:[{v: 'c'}, {v: 3.0, f: 'Three'}, {v: "Date(2010, 3, 30, 0, 31, 26)"}]}

the difference is the new keyword and the quotes around the value.

Final Thoughts

Please test out the new version and let me know if there are issues or if there are things that you would like to see in the new version by contacting me or leaving a comment either here or on the CodePlex web site.

Share...Tweet about this on Twitter2Share on Facebook0Share on Google+1Share on StumbleUpon0Share on LinkedIn2Flattr the authorPin on Pinterest0Share on Reddit0Share on TumblrDigg this

My name is Zoran Maksimovic a Software Developer and Solution Architect. I'm interested in Software Development, Object-Oriented Design and Software Architecture all this especially bound to the Microsoft.NET platform. Feel free to contact me or know more in the about section

Leave a Reply

nakailizabeth@mailxu.com