ItemToolTip Problem with DateFormat

6 posts, 0 answers
  1. marcos
    marcos avatar
    11 posts
    Member since:
    Dec 2010

    Posted 11 Mar 2011 Link to this post

    Hey,

    I can't display the correct x-axis date value in the itemtooltip.
    Here are the interesting facts for you...

    I have a chart with thousands of points so i use:
    <telerik:AxisX AutoRange="True"...


    That's why in my ItemToolTip the first value works and the last not (because of this link this link):
    <telerik:LineSeriesDefinition ItemToolTipFormat="#X --> #DATAITEM.Date or #DATAITEM.Date{MM/dd/yyyy}"...


    But I can't use #X because it's from double type and the result is something like "42k" - but not a date!

    What kind of solution you can offer for my problem? Is there a posibility to convert the double to string inside xaml?
    (i can't use codebehind...)
  2. Evgenia
    Admin
    Evgenia avatar
    1437 posts

    Posted 16 Mar 2011 Link to this post

    Hi marcos,

    To convert the double values to DateTime so that you will be able to set custom formatting there are two approaches you may choose from:
    1. Convert the values during binding - to achieve this you must create a class that implements the IValueConverter interface, which includes the Convert and ConvertBack methods as shown in this article.

    2. Subscribe to ItemDataBound event of the Chart and convert the current value to DateTime like this: 
    DateTime MyDate = DateTime.FromOADate(MyOADate);
    Then apply custom format string for it.

    Best wishes,
    Evgenia
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. marcos
    marcos avatar
    11 posts
    Member since:
    Dec 2010

    Posted 16 Mar 2011 Link to this post

    Hey,
    please taka a look to my xaml which is here:
    http://www.telerik.com/community/forums/wpf/chart/itemtooltip-problem-with-dataformat.aspx
    (I realized to late that I've posted in WPF and not Silverlight...)

    Can you explain where exactly I have to change???

    P.S.  I attach a screenshot of the result
    ItemToolTipFormat="Values #Y - #X - #X{dd.MM.yyyy} "
  4. Evgenia
    Admin
    Evgenia avatar
    1437 posts

    Posted 18 Mar 2011 Link to this post

    Hi marcos,

    The problem appears because the value bound to XValue on the DataPoint is not DateTime.
    The chart cannot implicitly infer if the double is just double or OAData.

    I can offer you two solutions:

    1. Subscribe to ItemDataBound event of the Chart and set each DataPoint's IsDateTime property to true which ensures that the XValue of the DataPoint will be DateTime. 

    radChart1.ItemDataBound += new EventHandler<ChartItemDataBoundEventArgs>(radChart1_ItemDataBound);
       
    void radChart1_ItemDataBound(object sender, ChartItemDataBoundEventArgs e)
    {
       e.DataPoint.IsDateTime = true;
    }

    2. The second one is to subscribe to ItemTooltipOpening event of the ChartArea and set custom format string for the DateTime converted XValue:

    radChart1.DefaultView.ChartArea.ItemToolTipOpening += new ItemToolTipEventHandler(ChartArea_ItemToolTipOpening);
       
    void ChartArea_ItemToolTipOpening(ItemToolTip2D tooltip, ItemToolTipEventArgs e)
    {
    double value = (double)e.DataPoint.XValue;
        tooltip.Content = e.DataSeries.LegendLabel + 
    string.Format("{0:dd.MM.yyyy} \n {1}", DateTime.FromOADate(value), e.DataPoint.YValue);
    }

    Let me know how this works for you.

    All the best,
    Evgenia Milcheva
    the Telerik team

  5. marcos
    marcos avatar
    11 posts
    Member since:
    Dec 2010

    Posted 18 Mar 2011 Link to this post

    Hi,
    I tried the first solution and it works perfectly. So thanks a lot...

    But if I understand it correct, the problem ist the Double Format of my XAxisValue. Well, if I set the XValue directly to a DateTimeProperty I get an unhandled Error in my Silverlight Application (Code 4004, somethign with Average and Type System.Linq.Enumerable...)

    Is there a way to achieve a well formated output in the graph and the tooltip too only by modifying the xaml without codebehind???
    What about different properties for XCat and XValue?
    Available are these properties:
    1) DATE_DOUBLE (Double)
    2) DATE_STRING (String)
    3) DATE (DateTime)

    I am looking forward to hearing from you soon.

    marcos
  6. Evgenia
    Admin
    Evgenia avatar
    1437 posts

    Posted 23 Mar 2011 Link to this post

    Hello marcos,

      Before answering your questions let me explain you the way RadChart's XAxis values can be set. As XAxis expects it's items to be of type double you can't directly set DateTime values as XValue. The DateTime values should be converted to their OLE Automation equivalents using theToOADate() method provided by the Framework.
    If you want to plot not only values (e.g. number of sales, etc.), but also categories (e.g. months of the year, units, people, etc.) you should use XCategory as it treats the data as a sequence of non-numerical text labels.
    To your question:
      1) DATE_DOUBLE (Double) - ItemTooltipFormat can be set as I showed you in my previous post.
      2) DATE_STRING (String) - as if your Date values were represented by XCategory. In this case the ItemTooltipFormat can be set as my co-worker Evgeni suggested in this forum post. Happily to you this suggestion uses XAML only to format the XCategory.
      3) DATE (DateTime) - you should convert the DateTime to OADate() and then set the ItemTooltipFormat as written in 1).

    I hope this information helps!

    All the best,
    Evgenia
    the Telerik team
Back to Top