基本 WPF LiveCharts DateTime 示例不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43985282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic WPF LiveCharts DateTime example not working
提问by Mr J
I've followed the Live Charts TimeDate basic example as closely as I can but can't seem to get the X axis to display properly.
我尽可能地遵循了实时图表 TimeDate 基本示例,但似乎无法正确显示 X 轴。
https://lvcharts.net/App/examples/v1/wpf/Date%20Time
https://lvcharts.net/App/examples/v1/wpf/Date%20Time
My MainWindow code
我的主窗口代码
public partial class MainWindow : Window
{
public Func<double, string> Formatter { get; set; }
public MainWindow()
{
InitializeComponent();
var dayConfig = Mappers.Xy<DateModel>()
.X(dateModel => dateModel.DateTime.Ticks / TimeSpan.FromDays(1).Ticks)
.Y(dateModel => dateModel.Value);
SeriesCollection Series = new SeriesCollection(dayConfig)
{
new LineSeries
{
Title = "Google Rank",
Values = new ChartValues<DateModel>
{
new Wpf.CartesianChart.Using_DateTime.DateModel
{
DateTime = System.DateTime.UtcNow,
Value = 5
},
new Wpf.CartesianChart.Using_DateTime.DateModel
{
DateTime = System.DateTime.UtcNow.AddDays(1),
Value = 9
},
new Wpf.CartesianChart.Using_DateTime.DateModel
{
DateTime = System.DateTime.UtcNow.AddDays(2),
Value = 4
}
},
Fill = Brushes.Transparent,
},
};
Formatter = value => new System.DateTime((long)(value * TimeSpan.FromDays(1).Ticks)).ToString("t");
RankGraph.Series = Series;
}
}
My XAML on my MainWindow
我的主窗口上的 XAML
<Grid>
<lvc:CartesianChart x:Name="RankGraph" Series="{Binding Series}">
<lvc:CartesianChart.AxisX>
<lvc:Axis LabelFormatter="{Binding Formatter}"></lvc:Axis>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
</Grid>
Date Model Object
日期模型对象
namespace Wpf.CartesianChart.Using_DateTime
{
public class DateModel
{
public DateTime DateTime { get; set; }
public double Value { get; set; }
}
}
This produces the following with the dates messed up...
这会产生以下日期混乱...
回答by Daulet Kshibekov
For some reason, the LiveCharts bindings in XAML don't work sometimes. You need to name your LiveCharts controls in XAML (to anythin):
出于某种原因,XAML 中的 LiveCharts 绑定有时不起作用。您需要在 XAML 中命名您的 LiveCharts 控件(任何类型):
<lvc:CartesianChart x:Name="RankGraph" Series="{Binding Series}">
<lvc:Axis x:Name="RankGraphAxisX" LabelFormatter="{Binding Formatter}"></lvc:Axis>
and then bind both Series and LabelFormatter in your code behind:
然后在后面的代码中绑定 Series 和 LabelFormatter :
RankGraph.Series = Series;
RankGraphAxisX.LabelFormatter = Formatter;
回答by PeteH
Dunno if this might be any use to anyone but I read this example code too (which is also published on their site). I kept on reading more about LiveCharts and came across their DateTimePoint class (LiveCharts.Defaults.DateTimePoint). I'm just starting to use these controls, but at first glance it is charting pretty much what I'm expecting to see.
不知道这是否对任何人都有用,但我也阅读了这个示例代码(也在他们的网站上发布)。我继续阅读有关 LiveCharts 的更多信息,并遇到了他们的 DateTimePoint 类 (LiveCharts.Defaults.DateTimePoint)。我刚刚开始使用这些控件,但乍一看,它绘制的图表几乎是我期望看到的。
My problem is that I have a bunch of Cartesian points which are of type <DateTime, double>, but the DateTimes are not spread out regularly - so I have data points like ("1 Jan 2015 00:00", 9.5). ("20 Jan 2015 04:00", 10), ("4 Jan 2016 06:46", 5.2), etc. I figured that irregular time intervals are best covered by their Scatter chart, and I'm using WPF to develop my app, so I ended up with the XAML
我的问题是我有一堆类型为 的笛卡尔点<DateTime, double>,但是DateTimes 没有定期分布 - 所以我有像 ("1 Jan 2015 00:00", 9.5) 这样的数据点。("20 Jan 2015 04:00", 10), ("4 Jan 2016 06:46", 5.2) 等等。我认为他们的散点图最好涵盖不规则的时间间隔,我正在使用 WPF 开发我的应用程序,所以我最终得到了 XAML
....
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
...
<lvc:CartesianChart DataTooltip="{x:Null}">
<lvc:CartesianChart.Series>
<lvc:ScatterSeries Title="Raw Data" Values="{Binding RawDataSeries}" />
</lvc:CartesianChart.Series>
....
Now, I happen to be taking an MVVM approach (note my binding) so in my corresponding ViewModel, I've written the method
现在,我碰巧采用了 MVVM 方法(注意我的绑定),因此在我相应的 ViewModel 中,我编写了该方法
public ChartValues<DateTimePoint> RawDataSeries
{
get
{
ChartValues<DateTimePoint> Values = new ChartValues<DateTimePoint>();
foreach (DatabaseEntry dbe in _Readings)
{
Values.Add(new DateTimePoint(dbe.Timestamp, dbe.Value));
}
return Values;
}
}
Obviously this is a lot less code than on their web page. DatabaseEntryis one of mine - it's just a container for 7 or 8 properties, including Timestamp (DateTime) and Value (double).
显然,这比他们的网页上的代码少得多。DatabaseEntry是我的一个——它只是一个包含 7 或 8 个属性的容器,包括时间戳 (DateTime) 和值 (double)。
I'm still writing this code so I'm sure I have more to do, but just in terms of starting off I'm seeing pretty much what I'm expecting to see so far.
我仍在编写这段代码,所以我确信我还有更多工作要做,但就开始而言,我目前看到的几乎是我期望看到的。


