2016-12-16 9 views
1

私は初めてのユニバーサルウィンドウアプリケーションを作成しています。現在、加速度計を試しています。 現在のところ、数値は画面上に表示されていますが、これらの値を視覚的に示すグラフも表示したいと考えています。 私はいくつかのチャートの例をオンラインで見たことがありますが、データのライブストリームを受け入れて表示するものはありません。Windows普遍的なアプリの加速度計データのライブチャート

基本的には、値を表す線を描く時間領域のグラフです加速度計出力。

これは私のWindowsプログラミングでの最初の試みで、私はC#で作業しています。

このようなことを行うための「適切な」方法はありますか?一般的に受け入れられている方法ですか?

+1

OxyPlotも同様に機能します。 –

答えて

3

使用WinRTXamlToolkit

enter image description here

XAML:

<Page 
    x:Class="App3.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App3" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:Charting="using:WinRTXamlToolkit.Controls.DataVisualization.Charting" 
    mc:Ignorable="d" Loaded="Page_Loaded"> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="10*"></RowDefinition> 
      <RowDefinition Height="*"></RowDefinition> 
      <RowDefinition Height="*"></RowDefinition> 
     </Grid.RowDefinitions> 
     <Charting:Chart x:Name="chart1" Grid.Row="0"> 
      <Charting:LineSeries ItemsSource="{Binding Data}" 
           DependentValuePath ="Accel" 
           IndependentValuePath ="Timestamp" Margin="0"/> 
     </Charting:Chart> 
     <Button x:Name="btnStart" Content="START" Grid.Row="1" Click="btnStart_Click" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Button> 
     <Button x:Name="btnStop" Content="STOP" Grid.Row="2" Click="btnStop_Click" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Button> 
    </Grid> 
</Page> 

メインページ:

public sealed partial class MainPage : Page 
{ 
    MyViewModel vm; 

    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 

    private void Page_Loaded(object sender, RoutedEventArgs e) 
    { 
     vm = new MyViewModel(); 
     DataContext = vm; 
    } 

    private void btnStart_Click(object sender, RoutedEventArgs e) 
    { 
     vm.Start(); 
    } 

    private void btnStop_Click(object sender, RoutedEventArgs e) 
    { 
     vm.Stop(); 
    } 
} 

ViewModel:

public class MyViewModel 
{ 
    private Timer accelerometer; 
    private Random r; 

    private ObservableCollection<MyAccelModel> data; 
    public ObservableCollection<MyAccelModel> Data { get { return data; } } 

    public MyViewModel() 
    { 
     data = new ObservableCollection<MyAccelModel>(); 
     r = new Random(DateTime.Now.Millisecond); 
    } 

    public void Start() 
    { 
     accelerometer = new Timer(AccelDataCallback, null, 100, 500); 
    } 

    public void Stop() 
    { 
     accelerometer.Dispose(); 
    } 

    private async void AccelDataCallback(object state) 
    { 
     await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
      () => 
      { 
       data.Add(new MyAccelModel { Timestamp = DateTime.Now, Accel = r.NextDouble() }); 
      }); 
    } 
} 
関連する問題