chartingToolkit:LineSeries Memory Problem

Dec 5, 2009 at 2:23 PM

Hi all,

I'm using the chartingToolkit:LineSeries to view a dynamic list of trend values. The list has a maximum size of 100 and is refreshed two times second. That means the trend grows up to fit the whole view and than it moves from the right to left. That works fine, but only for a short time because memory and cpu usage is increasing rapidly. Did I something wrong? Here's the code:

WPF:

<chartingToolkit:Chart Name="chart" Title="Deviation" BorderThickness="0" >
	<chartingToolkit:LineSeries Name="trend_x"
			ItemsSource="{Binding UpdateSourceTrigger=Explicit}"
			DependentValueBinding="{Binding X}"
			IndependentValueBinding="{Binding Y}"
			IndependentValuePath="Running Meter"
			DependentValuePath="Deviation X"
			Title="Deviation X"
			Loaded="chart_Loaded"
			Height="Auto" 
			AnimationSequence="FirstToLast" >
	  
		<chartingToolkit:LineSeries.DataPointStyle>
			<Style TargetType="chartingToolkit:LineDataPoint">
				<Setter Property="Visibility" Value="Collapsed"/>
				<Setter Property="Background" Value="Green"/>
			</Style>
		</chartingToolkit:LineSeries.DataPointStyle>
		
	</chartingToolkit:LineSeries>
	<!-- Shared axis -->
	<chartingToolkit:Chart.Axes>
		<chartingToolkit:LinearAxis
					Orientation="X"
					FlowDirection="LeftToRight"
					Title="Running Meter"/>
		<chartingToolkit:LinearAxis
					Orientation="Y"
					Maximum="4"
					Minimum="-4"
					ShowGridLines="True"
					Interval="1.0" 
					Title="Deviation"/>
	</chartingToolkit:Chart.Axes> 
</chartingToolkit:Chart>				

Code behind:

private void chart_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    if (trend_x.ItemsSource == null)
    {
        trend_x.ItemsSource = TrendSimulation.List_X;
        TrendSimulation.Trend_x = trend_x;
        TrendSimulation.startThread();
    }
}

Value simulation thread including the refresh call:

private static void ThreadFunc()
{
    while (true)
    {
        sign = random.NextDouble() > 0.5 ? 4 : -4;
        DataItem d_x = new DataItem(r, Math.Round(random.NextDouble() * sign, 2));
        list_x.add(d_x);
        r += 0.4;

        if (trend_x != null)
        {
            trend_x.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal, new Action(
                delegate()
                {
                    trend_x.Refresh();
                }
            ));
        }

        
        Thread.Sleep(500);
    }
}

And the List class:

class DataList : List<DataItem>
{
    private int size = 100;

    public DataList()
    {
        this.Capacity = size;
    }

    public int Size
    {
        get { return size; }
        set { size = value; this.Capacity = size;}
    }

    public void add(DataItem item)
    {
        if (Count >= Size)
        {
            RemoveAt(0);
        }
        Add(item);
    }

}

public class DataItem
{
    public double Y { get; private set; }
    public double X { get; private set; }
    public DataItem(double y, double x)
    {
        Y = y;
        X = x;
    }
}

Any help is highly appreciated.

 

Thanks

Manni