Autor Beitrag
funcry
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 110
Erhaltene Danke: 1

Win7 64, XP 32
C# (VS 2010 EE), Delphi (TD 2006 Win32)
BeitragVerfasst: Mo 02.11.09 14:39 
Aus dem WpfToolkit nutze ich derzeit die Chart Komponente. Diese ersetzt versuchsweise ein ZedGraphControl. Bei einer Sache finde ich meine Lösung unschön - insbesondere habe ich die Hoffnung auf eine bereits integrierte Lösung, die ich nur noch nicht gefunden habe.

Anforderung:
Ich habe ein Diagramm welches die letzten 10 Y-Werte darstellen soll. Diesem wird alle 10 Sekunden ein neuer Y-Wert übergeben, welcher in Form eines Liniendiagrammes dargestellt wird.

Meine Lösung:
Leider muss ich sowohl den X als auch den Y Wert übergeben. Daher habe ich eine Klasse erstellt welche eine ObservableCollection enthält. Dabei mache ich zwei Dinge. Zum einen kümmere ich mich darum, dass ähnlich wie bei einem Queue immer nur die letzzten 10 Elemente --> Point <--- gespeichert werden.
Sobal ein neuer y-Wert angefügt wird fliegt der erste Y-Wert heraus. Alle X-Werte werden angepasst. Diese Collection dient als Datacontext zu meiner Line-Series.

Mein Problem:
Die Manuelle Implementierung der Funkion einer RollingPointSeries finde ich unschön.

Quellcode:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
namespace MView
{
    public class RollingPoints
    {
        public ObservableCollection<Point> Points { get; private set; }
        private int capacity;

        public RollingPoints(int capacity)
        {
            this.capacity = capacity;
            this.Points = new ObservableCollection<Point>();
        }

        public void Add(double value)
        {
            if (this.Points.Count == 0)
                this.Points.Add(new Point(1, value));
            else
                if (this.Points.Count < this.capacity)
                    this.Points.Add(new Point(this.Points.Count + 1, value));
                else
                {
                    for (int i = 0; i < this.Points.Count; i++)
                        this.Points[i] = new Point(i, this.Points[i].Y);

                    this.Points.RemoveAt(0);
                    this.Points.Add(new Point(this.Points.Count + 1, value));
                }
        }
    }
}



ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
private void applyChartSettings(Chart chart, string backText)
        {            
            RollingPoints test = new RollingPoints(Properties.Settings.Default.RollingPoints);

            test.Add(2.5);
            test.Add(3.4);

            chart.Series[0].DataContext = test.Points;



ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
<chartingToolkit:Chart Name="chartL1" Grid.Row="0" Grid.Column="1" LegendStyle="{StaticResource LegendStyle}">
                    <chartingToolkit:Chart.Axes>
                        <chartingToolkit:LinearAxis                            
                            Orientation="X"
                            ShowGridLines="True" 
                            Foreground="White" />
                        <chartingToolkit:LinearAxis
                            Orientation="Y"
                            ShowGridLines="True"
                            Foreground="White" />
                    </chartingToolkit:Chart.Axes>
                    
                    <chartingToolkit:Chart.Series>
                        <chartingToolkit:LineSeries
                            ItemsSource="{Binding}" 
                            IndependentValuePath="X"
                            DependentValuePath="Y" 
                            Title="Meßwerte" 
                            IsSelectionEnabled="True"
                            Foreground="White">
                        </chartingToolkit:LineSeries>
                    </chartingToolkit:Chart.Series>
                    
                </chartingToolkit:Chart>
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mo 02.11.09 15:26 
Integriert wird es das wohl nicht geben, aber es sollte auch ohne ObservableCollection funktionieren:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
public class RollingPoints : INotifyPropertyChanged
{
  int capacity;
  Queue<int> queue = new Queue<int>();

  public RollingsPoints(int capacity) { this.capacity = capacity; }

  public IEnumerable<Point> Data {
    get {
      return queue.Select((y, x) => new Point(x, y));
    }
  }

  public void Add(double y)
  {
    if (queue.Count >= capacity)
      queue.Dequeue();
    queue.Enqueue(y);
    // PropertyChanged für "Data" werfen
  }
}

_________________
>λ=