Search code examples
c#datetimemschart

Microsoft Data Visualization (Chart) shows date as 1899-30-01


Ok I've resolved already the problem. Must be double[] xvaluesTmp, and values are assigned: xvaluesTmp[0] = DateTime.Now.ToOADate() (as double), Now works perfect


I downloaded Microsoft Charts control (System.Windows.Forms.DataVisualization.dll). To display hour, minute and seconds i have format:

chartArea.AxisX.LabelStyle.Format = "{HH}:{mm}:{ss}";

And it show rigth. But how looks format to display year-month-day ? I try from documentation: "D", "G", "M", "d", "y". It shows me date but always shows 1899-30-01 or 30 January 1899 etc. I pass to DataBinding DateTime.Now:

List<DateTime> xvaluesTmp = new List<DateTime>();
            xvaluesTmp.Add(DateTime.Now);
            xvaluesTmp.Add(new DateTime(2011, 3, 28));
...
this.chart.Series[0].Points.DataBindXY(xvaluesTmp, yvaluesTmp);

What is wrong ?

Thanks


Solution

  • I have had this problem for some time and have finally resolved it by:

    Attaching to the customise event of the chart:

    _chart.Customize += Remove_1899_Label;
    

    And in the Remove_1899_Label event handler:

    private void Remove_1899_Label(object sender, EventArgs e)
    {
        Chart msChart = sender as Chart;
    
        foreach (var chartArea in msChart.ChartAreas)
        {
            foreach (var label in chartArea.AxisX.CustomLabels)
            {
                if (!string.IsNullOrEmpty(label.Text) && label.Text == "1899")
                {
                    label.SetFieldValue("customLabel", true);
                    label.Text = string.Empty;
                }
            }
        }
    }
    

    N.B For some reason, changing the text of the label doesn't work unless you also set the private bool member variable named 'customLabel' of the Label to true. The implementation of the extension method 'SetFieldValue(string, object)' I got from here: http://dotnetfollower.com/wordpress/2013/06/c-how-to-set-or-get-value-of-a-private-or-internal-field-through-the-reflection/

    However, I've included it below for completeness.

    public static class ReflectionHelper
    {
            //...
            // here are methods described in the post
            // http://dotnetfollower.com/wordpress/2012/12/c-how-to-set-or-get-value-of-a-private-or-internal-property-through-the-reflection/
            //...
    
            private static FieldInfo GetFieldInfo(Type type, string fieldName)
            {
                    FieldInfo fieldInfo;
                    do
                    {
                            fieldInfo = type.GetField(fieldName,
                                         BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                            type = type.BaseType;
                    }
                    while (fieldInfo == null && type != null);
                    return fieldInfo;
            }
    
            public static object GetFieldValue(this object obj, string fieldName)
            {
                    if (obj == null)
                            throw new ArgumentNullException("obj");
                    Type objType = obj.GetType();
                    FieldInfo fieldInfo = GetFieldInfo(objType, fieldName);
                    if (fieldInfo == null)
                            throw new ArgumentOutOfRangeException("fieldName",
                                string.Format("Couldn't find field {0} in type {1}", fieldName, objType.FullName));
                    return fieldInfo.GetValue(obj);
            }
    
            public static void SetFieldValue(this object obj, string fieldName, object val)
            {
                    if (obj == null)
                            throw new ArgumentNullException("obj");
                    Type objType = obj.GetType();
                    FieldInfo fieldInfo = GetFieldInfo(objType, fieldName);
                    if (fieldInfo == null)
                            throw new ArgumentOutOfRangeException("fieldName",
                                string.Format("Couldn't find field {0} in type {1}", fieldName, objType.FullName));
                    fieldInfo.SetValue(obj, val);
            }
    }