我正在使用sample code from live charts
private double _axisMax;
private double _axisMin;
public Plotter()
{
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("ss");
//AxisStep forces the distance between each separator in the X axis
AxisStep = TimeSpan.FromSeconds(5).Ticks;
//AxisUnit forces lets the axis know that we are plotting seconds
//this is not always necessary, but it can prevent wrong labeling
AxisUnit = TimeSpan.TicksPerSecond;
SetAxisLimits(DateTime.Now);
//The next code simulates data changes every 300 ms
IsReading = false;
DataContext = this;
}
public ChartValues<MeasureModel> ChartValues { get; set; }
public Func<double, string> DateTimeFormatter { get; set; }
public double AxisStep { get; set; }
public double AxisUnit { get; set; }
public double AxisMax
{
get => _axisMax;
set
{
_axisMax = value;
OnPropertyChanged("AxisMax");
}
}
public double AxisMin
{
get => _axisMin;
set
{
_axisMin = value;
OnPropertyChanged("AxisMin");
}
}
public bool IsReading { get; set; }
private void SetAxisLimits(DateTime now)
{
AxisMax = now.Ticks + TimeSpan.FromSeconds(1).Ticks; // lets force the axis to be 1 second ahead
AxisMin = now.Ticks - TimeSpan.FromSeconds(20).Ticks; // and 20 seconds behind
}
实际上插入坐标的inside方法。我有:
所以
var now = DateTime.Now;
ChartValues.Add(new MeasureModel
{
DateTime = now,
Value = SomeFunction()
});
SetAxisLimits(now);
<wpf:CartesianChart Grid.Row="0"
AnimationsSpeed="0:0:0.9"
Hoverable="False"
DataTooltip="{x:Null}">
<wpf:CartesianChart.Series>
<wpf:LineSeries
Name="MyChart" Values="{Binding ChartValues}"
PointGeometry="{x:Null}"
LineSmoothness="2"
StrokeThickness="3"
Stroke="#F34336"
Fill="Transparent"/>
</wpf:CartesianChart.Series>
<wpf:CartesianChart.AxisX>
<wpf:Axis LabelFormatter="{Binding DateTimeFormatter}"
MaxValue="{Binding AxisMax}"
MinValue="{Binding AxisMin}"
Unit="{Binding AxisUnit}">
<wpf:Axis.Separator>
<wpf:Separator Step="{Binding AxisStep}" />
</wpf:Axis.Separator>
</wpf:Axis>
</wpf:CartesianChart.AxisX>
<wpf:CartesianChart.AxisY>
<wpf:Axis MinValue="-20"
MaxValue="20">
</wpf:Axis>
</wpf:CartesianChart.AxisY>
</wpf:CartesianChart>
但是如何更改代码,以便让我以图形显示x Axis label
从0
开始,然后转到0,1,2,...100,...300
等?而不是从第二个程序开始启动?
我想完成类似的事情:
也许我可以使用oxyplot之类的其他库,对此有何建议?
答案 0 :(得分:1)
我认为最直接的方法是保存第一个数据点DateTime
值。然后,您可以计算每个其他数据点的相对时间。使用这些相对值,您可以“使”图表从0
开始。
要使数据从图表中移出,如果时间继续流逝,则应将相对时间值输入到SetAxisLimits(TimeSpan)
函数中。数据类型必须更改为TimeSpan
。
类似这样的东西:
var now = DateTime.Now;
if(ChartValues.Count == 0)
start = now; // save first timestamp to reference the following datapoints
var time = now.Subtract(start)
ChartValues.Add(new MeasureModel
{
Time = time,
Value = SomeFunction()
});
SetAxisLimits(time);