C# 使用Livecharts常量更改示例根据时间和串行端口的读数绘制点?
好的,基本上我想用这个图来代替DateTime,并根据秒表画出X轴。然后,我需要来自串行端口的数据在Y轴上绘制数据。这一切都应该在现场发生,只需按下一个按钮 以下是我遵循的示例: 问题是,它使用的是系统时间,而不是某种形式的秒表。我正在使用WPF,并希望扩展此图的功能 如果有人能帮我,通过skype或其他方式。请让我知道 最终,我希望能够从图表中获取所有数据并将其保存到某个地方。因此,我可以在以后与前面的几点进行比较 谢谢 Mainwindow.cs.xamlC# 使用Livecharts常量更改示例根据时间和串行端口的读数绘制点?,c#,wpf,xaml,linegraph,livecharts,C#,Wpf,Xaml,Linegraph,Livecharts,好的,基本上我想用这个图来代替DateTime,并根据秒表画出X轴。然后,我需要来自串行端口的数据在Y轴上绘制数据。这一切都应该在现场发生,只需按下一个按钮 以下是我遵循的示例: 问题是,它使用的是系统时间,而不是某种形式的秒表。我正在使用WPF,并希望扩展此图的功能 如果有人能帮我,通过skype或其他方式。请让我知道 最终,我希望能够从图表中获取所有数据并将其保存到某个地方。因此,我可以在以后与前面的几点进行比较 谢谢 Mainwindow.cs.xaml <Window x:Cla
<Window x:Class="TestChartProject.MainWindow"
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:TestChartProject"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
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" Click="InjectStopOnClick">
Inject/Stop Data
</Button>
<!--Here we disable tooltips and hovering to get a better performance-->
<lvc:CartesianChart Grid.Row="1" AnimationsSpeed="0:0:1" Hoverable="True" DataTooltip="{x:Null}">
<lvc:CartesianChart.Series>
<lvc:LineSeries Values="{Binding ChartValues}"
PointGeometry="{x:Null}"
LineSmoothness="1"
StrokeThickness="6"
Stroke="#F34336"
Fill="Transparent"/>
</lvc:CartesianChart.Series>
<lvc:CartesianChart.AxisX>
<lvc:Axis LabelFormatter="{Binding DateTimeFormatter}"
MaxValue="{Binding AxisMax}"
MinValue="{Binding AxisMin}"
Unit="{Binding AxisUnit}">
<lvc:Axis.Separator>
<lvc:Separator Step="{Binding AxisStep}" />
</lvc:Axis.Separator>
</lvc:Axis>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
</Grid>
根据我认为您需要的,也许这有助于您:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//used to generate random values
var r = new Random();
var t = 0d;
//lets instead plot elapsed milliseconds and value
var mapper = Mappers.Xy<MeasureModel>()
.X(x => x.ElapsedMilliseconds)
.Y(x => x.Value);
//save the mapper globally
Charting.For<MeasureModel>(mapper);
Values = new ChartValues<MeasureModel>();
var sw = new Stopwatch();
sw.Start();
Task.Run(() =>
{
while (true)
{
Thread.Sleep(500);
//we add the lecture based on our StopWatch instance
Values.Add(new MeasureModel
{
ElapsedMilliseconds = sw.ElapsedMilliseconds,
Value = t += r.Next(0,10)
});
}
});
DataContext = this;
}
public ChartValues<MeasureModel> Values { get; set; }
}
public class MeasureModel
{
public double ElapsedMilliseconds { get; set; }
public double Value { get; set; }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
//用于生成随机值
var r=新的随机变量();
var t=0d;
//而是让我们绘制经过的毫秒和值
var mapper=Mappers.Xy()
.X(X=>X.elapsedmillyses)
.Y(x=>x.Value);
//全局保存映射器
制图(制图员);
值=新的图表值();
var sw=新秒表();
sw.Start();
Task.Run(()=>
{
while(true)
{
睡眠(500);
//我们根据秒表实例添加讲座
添加(新的度量模型)
{
ElapsedMilliseconds=sw.ElapsedMilliseconds,
值=t+=r.Next(0,10)
});
}
});
DataContext=this;
}
公共图表值{get;set;}
}
公共类度量模型
{
公共双ElapsedMilliseconds{get;set;}
公共双值{get;set;}
}
XAML:
基于我认为您需要的,也许这有助于您:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//used to generate random values
var r = new Random();
var t = 0d;
//lets instead plot elapsed milliseconds and value
var mapper = Mappers.Xy<MeasureModel>()
.X(x => x.ElapsedMilliseconds)
.Y(x => x.Value);
//save the mapper globally
Charting.For<MeasureModel>(mapper);
Values = new ChartValues<MeasureModel>();
var sw = new Stopwatch();
sw.Start();
Task.Run(() =>
{
while (true)
{
Thread.Sleep(500);
//we add the lecture based on our StopWatch instance
Values.Add(new MeasureModel
{
ElapsedMilliseconds = sw.ElapsedMilliseconds,
Value = t += r.Next(0,10)
});
}
});
DataContext = this;
}
public ChartValues<MeasureModel> Values { get; set; }
}
public class MeasureModel
{
public double ElapsedMilliseconds { get; set; }
public double Value { get; set; }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
//用于生成随机值
var r=新的随机变量();
var t=0d;
//而是让我们绘制经过的毫秒和值
var mapper=Mappers.Xy()
.X(X=>X.elapsedmillyses)
.Y(x=>x.Value);
//全局保存映射器
制图(制图员);
值=新的图表值();
var sw=新秒表();
sw.Start();
Task.Run(()=>
{
while(true)
{
睡眠(500);
//我们根据秒表实例添加讲座
添加(新的度量模型)
{
ElapsedMilliseconds=sw.ElapsedMilliseconds,
值=t+=r.Next(0,10)
});
}
});
DataContext=this;
}
公共图表值{get;set;}
}
公共类度量模型
{
公共双ElapsedMilliseconds{get;set;}
公共双值{get;set;}
}
XAML:
太棒了!我唯一的问题是在X轴上,它给了我一堆随机数,而不是一种格式。我更喜欢这样:“分:秒”。谢谢令人惊叹的!我唯一的问题是在X轴上,它给了我一堆随机数,而不是一种格式。我更喜欢这样:“分:秒”。谢谢
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//used to generate random values
var r = new Random();
var t = 0d;
//lets instead plot elapsed milliseconds and value
var mapper = Mappers.Xy<MeasureModel>()
.X(x => x.ElapsedMilliseconds)
.Y(x => x.Value);
//save the mapper globally
Charting.For<MeasureModel>(mapper);
Values = new ChartValues<MeasureModel>();
var sw = new Stopwatch();
sw.Start();
Task.Run(() =>
{
while (true)
{
Thread.Sleep(500);
//we add the lecture based on our StopWatch instance
Values.Add(new MeasureModel
{
ElapsedMilliseconds = sw.ElapsedMilliseconds,
Value = t += r.Next(0,10)
});
}
});
DataContext = this;
}
public ChartValues<MeasureModel> Values { get; set; }
}
public class MeasureModel
{
public double ElapsedMilliseconds { get; set; }
public double Value { get; set; }
}
<lvc:CartesianChart>
<lvc:CartesianChart.Series>
<lvc:LineSeries Values="{Binding Values}" />
</lvc:CartesianChart.Series>
</lvc:CartesianChart>