2016-11-29 2 views
0

ライブチャートライブラリの探索を開始したばかりですが、時間の経過とともにx軸が更新されないようです。私はこの時間をもう一度過ごし、問題を見つけるように見えない。データポイントが正しく更新されていますが、これは私が非常に混乱していることを発見しています!"ライブチャート" x軸は時間とともに更新されないWPF c#

私は後日、テストが実行されている時間を表示するタイマーでDateTimeを置き換えます。これによりソリューションがより簡単になりますか?

私の一部は、これが何か単純なものでなければならないと思っています!

私はこのお手伝いをします。 XAMLで

public partial class MainWindow : Window 
{ 

    private double _axisMax; 
    private double _axisMin; 
    Stopwatch stopwatch = new Stopwatch(); 


    public MainWindow() 
    { 
     InitializeComponent(); 
     //To handle live data easily, in this case we built a specialized type 
     //the MeasureModel class, it only contains 2 properties 
     //DateTime and Value 
     //We need to configure LiveCharts to handle MeasureModel class 
     //The next code configures MEasureModel globally, this means 
     //that livecharts learns to plot MeasureModel and will use this config every time 
     //a ChartValues instance uses this type. 
     //this code ideally should only run once, when application starts is reccomended. 
     //you can configure series in many ways, learn more at http://lvcharts.net/App/examples/v1/wpf/Types%20and%20Configuration 


     var mapper = Mappers.Xy<MeasureModel>() 
      .X(model => model.DateTime.Ticks) //use DateTime.Ticks as X 
      .Y(model => model.Value);   //use the value property as Y 

     //lets save the mapper globally. 
     Charting.For<MeasureModel>(mapper); 


     //the values property will store our values array 
     ChartValues = new ChartValues<MeasureModel>(); 

     //lets set how to display the X Labels 
     DateTimeFormatter = value => new DateTime((long)value).ToString("hh:mm:ss"); 

     AxisStep = TimeSpan.FromSeconds(1).Ticks; 
     SetAxisLimits(DateTime.Now); 

     //The next code simulates data changes every 300 ms 
     Timer = new DispatcherTimer 
     { 
      Interval = TimeSpan.FromMilliseconds(300) 
     }; 
     Timer.Tick += TimerOnTick; 
     IsDataInjectionRunning = false; 
     R = new Random(); 
     DataContext = this; 
    } 

    public ChartValues<MeasureModel> ChartValues { get; set; } 
    public Func<double, string> DateTimeFormatter { get; set; } 

    public double AxisStep { get; set; } 

    public double AxisMax 
    { 
     get { return _axisMax; } 
     set 
     { 
      _axisMax = value; 
      OnPropertyChanged("AxisMax"); 
     } 
    } 
    public double AxisMin 
    { 
     get { return _axisMin; } 
     set 
     { 
      _axisMin = value; 
      OnPropertyChanged("AxisMin"); 
     } 
    } 

    public DispatcherTimer Timer { get; set; } 
    public bool IsDataInjectionRunning { get; set; } 
    public Random R { get; set; } 

    private void RunDataOnClick(object sender, RoutedEventArgs e) 
    { 
     if (IsDataInjectionRunning) 
     { 
      stopwatch.Stop(); 
      Timer.Stop(); 
      IsDataInjectionRunning = false; 
     } 
     else 
     { 
      stopwatch.Start(); 
      Timer.Start(); 
      IsDataInjectionRunning = true; 
     } 
    } 

    private void TimerOnTick(object sender, EventArgs eventArgs) // Class is referencing from here! 
    { 
     var now = DateTime.Now; 

     ChartValues.Add(new MeasureModel 
     { 
      DateTime = DateTime.Now, 
      Value = R.Next(0, 10) 
     }); 

     SetAxisLimits(DateTime.Now); 

     //lets only use the last 30 values 
     if (ChartValues.Count > 30) ChartValues.RemoveAt(0); 
    } 

    private void SetAxisLimits(DateTime now) 
    { 
     AxisMax = now.Ticks + TimeSpan.FromSeconds(20).Ticks; // lets force the axis to be 100ms ahead 
     AxisMin = now.Ticks - TimeSpan.FromSeconds(8).Ticks; //we only care about the last 8 seconds 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName = null) 
    { 
     if (PropertyChanged != null) // if subrscribed to event 
      PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

public class MeasureModel 
{ 
    public DateTime DateTime { get; set; } 
    public double Value { get; set; } 
} 

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:graph_test_6" 
    xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf" 
    xmlns:chart="http://mindfusion.eu/charting/wpf" x:Class="graph_test_6.MainWindow" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
    </Grid.RowDefinitions> 
    <Button Grid.Row="0" Height="30" Click="RunDataOnClick"> 
     Inject/Stop Data 
    </Button> 
    <lvc:CartesianChart Grid.Row="1"> 
     <lvc:CartesianChart.Series> 
      <lvc:LineSeries Values="{Binding ChartValues}" PointGeometrySize="18" StrokeThickness="4" /> 
     </lvc:CartesianChart.Series> 
     <lvc:CartesianChart.AxisX> 
      <lvc:Axis LabelFormatter="{Binding DateTimeFormatter}" 
         MaxValue="{Binding AxisMax}" 
         MinValue="{Binding AxisMin}" 
         DisableAnimations="True"> 
       <lvc:Axis.Separator> 
        <lvc:Separator Step="{Binding AxisStep}"></lvc:Separator> 
       </lvc:Axis.Separator> 
      </lvc:Axis> 
     </lvc:CartesianChart.AxisX> 
    </lvc:CartesianChart> 
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="323,-71,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> 
</Grid> 

Screen shot of problem

+0

にあなたの問題を変更

public partial class MainWindow : Window 

を修正する

。私はここからそれをダウンロードしました... https://lvcharts.net/App/examples/v1/wpf/Install ありがとう – charley

+0

[OK]をクリックしてください[https://lvcharts.net/App/例/ v1/wpf /定数%20Changes)?投稿したコードは、ウェブサイトの例と同じであるようです。私は現在githubのソースから最新バージョンを実行していますが、うまくいくようです。 – 3615

+0

また、サンプルのソースとの相違点がすべて考慮されていることを確認するために、コード付きの新しいwpfアプリケーションを作成しました:[動作しています](http://screencast.com/t/fLers3IbW)。あるいは、私はあなたの問題を理解していないかもしれません。 – 3615

答えて

2

あなたはAxisXプロパティMaxValueをとMinValueプロパティのために結合されています

MaxValue="{Binding AxisMax}" 
MinValue="{Binding AxisMin}" 

ウィンドウがロードされると、WPFメカニズムはバインドされた値を読み取ります。これがX軸の初期値の取得方法です。その後のコードで、あなたはこれらの値を変更している。問題は、それがサポートしていることを、ウィンドウ制御自体は宣言しないということである

OnPropertyChanged("AxisMin"); 
OnPropertyChanged("AxisMax"); 

AxisMax = now.Ticks + TimeSpan.FromSeconds(20).Ticks; // lets force the axis to be 100ms ahead 
AxisMin = now.Ticks - TimeSpan.FromSeconds(8).Ticks; //we only care about the last 8 seconds 

XAMLは、経由して、これらの変更について通知しなければならないし、それが行われています変更通知:それはないですINotifyPropertyChanged。だから私は最新のものを信じている3615 @

public partial class MainWindow : Window, INotifyPropertyChanged 
+0

完璧!本当にありがとう!今すぐ試して、時間よりも経過時間をプロットする:) – charley

関連する問題