20 December, 2011

Localization in Chart (Microsoft Chart Control), Localizing DataPoint

I had a scenario where need to localize the data points that are displayed on the chart, that the decimal needs to be localized as comma (,) in Russian and Spanish., Thanks to my friend Gagan for this
E.g.:

We can assign an EventHandler to FormatNumber Property as Below

private MemoryStream RenderGraph()
{
Chart barchart = new Chart();
//many number codes are removed
//Removed other formatting lines
barchart.FormatNumber += new EventHandler<FormatNumberEventArgs>(ConvertChartDecimalFormat);
            barchart.DataSource = dt;   
barchart.Series[0].XValueMember = "XProp";
            barchart.Series[0].YValueMembers = "YProp";
            barchart.DataBind();

//return the memory stream image
}

Followed the event method will look as below. 
void ConvertChartDecimalFormat(object sender, FormatNumberEventArgs e)
{
if (sender.GetType() == typeof(DataPoint))
       {
                try
                {
                    e.LocalizedValue = Double.Parse(e.LocalizedValue).ToString(currentCulture);
                }
                catch (Exception)
                {
                    //do if anything required.
                }
        }

}




Enjoy.!!! :-)

17 December, 2011

knockoutjs with Umbraco and Umbraco base call

I was thinking how we could use knockoutjs with Umbraco, so just thought blog it.

Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model. Any time you have sections of UI that update dynamically (e.g., changing depending on the user’s actions or when an external data source changes), KO can help you implement it more simply and maintainably.
  • Features
  • Elegant dependency tracking
  • Declarative bindings
  • Flexible and sophisticated templating
  • Trivially extensible

More information reference: http://knockoutjs.com.

Well, we start with Umbraco, To know more on umbraco, Please refer below links
http://our.umbraco.org/wiki/how-tos/a-complete-newbie's-guide-to-umbraco
http://umbraco.com/help-and-support/video-tutorials/getting-started?freeVideos=1
http://our.umbraco.org/wiki/how-tos/getting-started-with-umbraco-what-is-next-after-you-install
http://our.umbraco.org/wiki/install-and-setup

I have created a document with document type "Announcement" with below two properties
  • announcementTitle 
  • description
My Html block is as shown below
<p>
        <h1 data-bind="text: Title">
        </h1>
    </p>
    <p>
        <span data-bind="html: Description"></span>
    </p>
    <form id="frmAnnoumcent">
    <p>
        Title:
        <input data-bind="value: Title" name="Title" style="width: 400px" /></p>
    <p>
        Annoucement:
        <textarea data-bind="value: Description" name="Description" rows="5" cols="60"> </textarea></p>
    <button data-bind="click: save">
        Save Listings</button>
    </form>

Script Block

    <script type="text/javascript">

    var announcement = {
        "Title": ko.observable("") ,
        "Description": ko.observable(""),
    };
  
   var viewModel=ko.mapping.fromJS(announcement);
     $.ajax({
        url: '/base/baseCall/GetAnnouncement.aspx',
        type: "POST",
        data: $('#frmTest').serialize(),
        dataType: 'JSON',
        success: function(result)
        {
            ko.mapping.fromJS(result,{},viewModel);
          
        },
        error:
        function(result)
        {
        alert(result.message);
        }
       
    });
  
    viewModel.save = function() {
    $.ajax({
        url: '/base/baseCall/SaveAnnouncement.aspx',
        type: "POST",
        data: $('#frmTest').serialize(),
        dataType: 'JSON',
        success: function(result)
        {

            ko.mapping.fromJS(result,{},viewModel);
             ko.applyBindings(viewModel);
          
        },
        error:
        function(result)
        {
        alert(result.message);
        }
       
    });
},
$(function() {
    ko.applyBindings(viewModel);
});
     </script>

Base Service Call

       [RestExtension("baseCall")]
    public class TestService
    {
        [RestExtensionMethod(returnXml = false)]
        public static string SaveAnnouncement()
        {
            DocumentType dt = DocumentType.GetByAlias("Announcement");
            User author = User.GetUser(0);

            string title = HttpContext.Current.Request["Title"];
            string Description = HttpContext.Current.Request["Description"];

            Document doc = Document.MakeNew(title, dt, author, 1048);
            doc.getProperty("announcementTitle").Value = title;
            doc.getProperty("description").Value = Description;
            //after creating the document, prepare it for publishing

            doc.Publish(author);

            //Tell umbraco to publish the document
            umbraco.library.UpdateDocumentCache(doc.Id);

            return GetAnnouncement();
        }
        [RestExtensionMethod(returnXml=false)]
        public static string GetAnnouncement()
        {

            List<Announcement> announcement = new List<Announcement>();
            Document root = new Document(1048);
            string sJSON="";
            if (root.HasChildren)
            {
                foreach (Document tempDocument in root.Children.OrderByDescending(res => res.CreateDateTime))
                {
                    Announcement a = new Announcement();
                    Property prop1 = tempDocument.getProperty("announcementTitle");

                    a.Title = (string)prop1.Value;
                    prop1 = tempDocument.getProperty("description");
                    a.Description = (string)prop1.Value;
                    announcement.Add(a);
                    System.Web.Script.Serialization.JavaScriptSerializer oSerializer =
        new System.Web.Script.Serialization.JavaScriptSerializer();
                    sJSON = oSerializer.Serialize(a);

                    sJSON=sJSON.Insert(sJSON.Length - 2, ",");
                    break;
                }
            }
          
            return sJSON;
        }



    }
    public class Announcement
    {
        public string Title { get; set; }
        public string Description { get; set; }
    }

Of-course the knockoutjs can be used well, and it gives an live UI. We could create an amazing user experience with it.

Error: ko.mapping.updateFromJS, use ko.mapping.fromJS instead

I had the same issue, (Error: ko.mapping.updateFromJS, use ko.mapping.fromJS instead) I bugged for long time, then i tried the another alternative.fromJS

 the functionality is the same, but be aware that updateFromJS's parameter list is just the other away around, so instead of doing:
   ko.mapping.updateFromJS(viewModel, jsData);

You need to replace this with:
   ko.mapping.fromJS(jsData, {}, viewModel);

16 December, 2011

Localization in Umbraco, Item Page field localization




Umbraco is one of the most deployed Web Content Management Systems on the Microsoft stack. It's in the top five most popular server applications and among the ten most popular open source tools in general.

You can directly download and start using it from http://www.umbraco.com

If you have not used the application yet, its always worth trying it once. I am sure you will love it.

Well, for the first time users, Umbraco provide step by step instruction for initial set up, once you are done you can use some sample template or create a blank website.


To know more on umbraco, Please refer below links
http://our.umbraco.org/wiki/how-tos/a-complete-newbie's-guide-to-umbraco
http://umbraco.com/help-and-support/video-tutorials/getting-started?freeVideos=1
http://our.umbraco.org/wiki/how-tos/getting-started-with-umbraco-what-is-next-after-you-install
http://our.umbraco.org/wiki/install-and-setup


I have created a blank website, Now we will go step by step.

  • First create the home page template.

  • I created the document type with same name, and some properties, like, Page title, and page content, with two tabs, one for English and one for Malayalam
  • Now you are Good to go for localization, but with few changes in your properties, You can insert the page field of your property using umbraco page field utility.
                      <umbraco:Item field="pageTitle" runat="server" />

  • To apply the localization on fields, we have to customize our on item field as below, 
    • Write a class Item by implementing umbraco.presentation.templateControls.Item
  public class Item : umbraco.presentation.templateControls.Item
    {
        [Bindable(true)]
        [Category("Umbraco")]
        [DefaultValue("")]
        [Localizable(true)]
        public new string Field
        {
            get
            {
                return base.Field;
            }
            set
            {
                umbraco.cms.businesslogic.web.Document objCurrentDocument = new umbraco.cms.businesslogic.web.Document(umbraco.NodeFactory.Node.GetCurrent().Id);
                string locale = Utils.GetLanguageCode(HttpContext.Current.Request["locale"]);
                if (locale == "ENG")
                {
                    locale = "";
                }
                else
                {
                    locale = "_" + locale;
                }
                string sNewField = string.Format("{0}{1}", value, locale);
                if (objCurrentDocument.getProperty(sNewField) != null && objCurrentDocument.getProperty(sNewField).Value != null && !string.IsNullOrEmpty(objCurrentDocument.getProperty(sNewField).Value.ToString()))
                {
                    value = sNewField;
                }
                base.Field = value;
            }
        }
    }
  • Register this class on the template as below 
    <%@ Register Assembly="site" Namespace="site.UmbracoUsercontrols"
    TagPrefix="Cumbraco" %>
  •  Well, Now you can directly use the utility  as below.
<title><Cumbraco:Item field="pageTitle" runat="server" /></title>
  •  Now we can create  home page as below content 
 
The localized page can be seen as below
Umbraco one of the fastest growing platforms for building websites. I suggest to just use it, I am sure you will love it.