Realtime Grafik pada WPF dengan c#

Posted: February 12, 2013 by Nila Novita Sari in C Sharp, WPF
Tags:
  1. Buat project baru menggunakan WPF Aplication

1

2. Masukan dll yang dibutuhkan ke dalam References

2

3. Ketikan program berikut pada xaml nya

<Window

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;

xmlns:rg=”clr-namespace:Righthand.RealtimeGraph;assembly=Righthand.RealtimeGraph”

xmlns:d=”http://schemas.microsoft.com/expression/blend/2008&#8243;

xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006&#8243;

mc:Ignorable=”d” x:Class=”Righthand.RealtimeGraph.Example.MainWindow”

Title=”MainWindow” Height=”350″ Width=”525″>

<rg:RealtimeGraphControl x:Name=”Graph” AxisColor=”Blue” AxisWidth=”1″

VerticalLinesInterval=”5″ VerticalLabelsStep=”2″ SpanX=”100″

MaxY=”250″ MinY=”50″ HorizontalLinesInterval=”10″/>

</Window>

4. Buat sebuah class baru dan ketikan code berikut

using Righthand.RealtimeGraph;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace RealTimeGraph.Contoh

{

    publicclassRealTimeGraph : IGraphItem

    {

        publicint Time { get; set; }

        publicdouble Value { get; set; }

    }

 

}

5. Ketikan pada program utama code berikut

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Windows.Threading;

using System.ComponentModel;

 

namespace Righthand.RealtimeGraph.Example

{

    ///<summary>

    /// Interaction logic for MainWindow.xaml

    ///</summary>

    publicpartialclassMainWindow : Window

    {

        BindingList<RealtimeGraphItem> items = newBindingList<RealtimeGraphItem>();

        privateDispatcherTimer timer;

        privateDateTime start;

        privateDateTime last;

 

        public MainWindow()

        {

            InitializeComponent();

 

            //float angle = 135;

            // bind items

            Graph.SeriesSource = items;

 

            timer = newDispatcherTimer

            {

                Interval = TimeSpan.FromMilliseconds(5)

            };

            timer.Tick += timer_Tick;

            last = start = DateTime.Now;

            timer.IsEnabled = true;

        }

 

        void timer_Tick(object sender, EventArgs e)

        {

            // add new items each tick

            TimeSpan span = DateTime.Now – last;

            TimeSpan totalSpan = DateTime.Now – start;

            int previousTime = items.Count > 0 ? items[items.Count – 1].Time: 0;

            RealtimeGraphItem newItem = newRealtimeGraphItem

            {

                Time = (int)(previousTime + span.TotalMilliseconds),

                Value = Math.Sin(totalSpan.TotalSeconds / 10f) * 90 + 150

            };

 

            items.Add(newItem);

            last = DateTime.Now;

        }

    }

}

3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s