This is a migrated thread and some comments may be shown as answers.

DataTable don't display null values as 0

1 Answer 155 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 18 Mar 2011, 08:59 PM
Hello,

I have a small problem.
As the data source of the chart I am using a DataTable because the DataSeries can be added dynamically.
Therefore it was the easiest way for me.

The DataTable has the following structure
   - Timestamp : DateTime
   - Value1 : double
   - Value2 : double

The problem is that in my case it is possible that a few rows have Value1 or Value2 = null because there was no value at this timestamp.
The chart displays this null value as 0. But for me it would be nice if it just ignores the null value

I created a small example which shows the problem:
public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
 
      var dt = new DataTable();
      dt.Columns.Add("Timestamp", typeof(DateTime));
      dt.Columns.Add("Value1", typeof(double));
      dt.Columns.Add("Value2", typeof(double));
 
      for (int i = 0; i < 20; i++)
      {
        var obj = new object[3];
        obj[0] = DateTime.Now.AddHours(i);
        obj[1] = i;
        if (i % 2 != 0)
          obj[2] = i + 1;
        else
          obj[2] = null;
        dt.Rows.Add(obj);
      }
 
      chart.ItemsSource = dt;
 
      SeriesMapping seriesMapping = new SeriesMapping();
      seriesMapping.SeriesDefinition = new LineSeriesDefinition();
      seriesMapping.ItemMappings.Add(new ItemMapping
      {
        DataPointMember = DataPointMember.XValue,
        FieldName = "Timestamp",
        FieldType = typeof(DateTime)
      });
      seriesMapping.ItemMappings.Add(new ItemMapping
      {
        DataPointMember = DataPointMember.YValue,
        FieldName = "Value1",
        FieldType = typeof(double)
      });
      chart.SeriesMappings.Add(seriesMapping);
       
      SeriesMapping seriesMapping1 = new SeriesMapping();
      seriesMapping1.SeriesDefinition = new LineSeriesDefinition();
      seriesMapping1.ItemMappings.Add(new ItemMapping
      {
        DataPointMember = DataPointMember.XValue,
        FieldName = "Timestamp",
        FieldType = typeof(DateTime)
      });
      seriesMapping1.ItemMappings.Add(new ItemMapping
      {
        DataPointMember = DataPointMember.YValue,
        FieldName = "Value2",
        FieldType = typeof(double)
      });
      chart.SeriesMappings.Add(seriesMapping1);
    }
  }


When you try this you will see that the second Series looks a bit strange because it added 0 instead of ignoring the null value.

Is there a workaround for that? 
How can I fix this problem?

I am using Version: 2010.3.1110.35

Kind Regards
Michael

1 Answer, 1 is accepted

Sort by
0
Michael
Top achievements
Rank 1
answered on 19 Mar 2011, 09:05 PM
Hey,

I found a solution for my problem. With the new Version (Q1 2011) there is the new Empty-Values property.
I just set it to Drop and everything works fine

Regards
Michael
Tags
Chart
Asked by
Michael
Top achievements
Rank 1
Answers by
Michael
Top achievements
Rank 1
Share this question
or