我已经尽可能地使用了Live Charts TimeDate basic示例,但似乎无法正确显示X轴。
https://lvcharts.net/App/examples/v1/wpf/Date%20Time
我的MainWindow代码
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;
}
}我的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>日期模型对象
namespace Wpf.CartesianChart.Using_DateTime
{
public class DateModel
{
public DateTime DateTime { get; set; }
public double Value { get; set; }
}
}这会产生以下日期混乱的结果...

发布于 2018-12-24 17:52:00
不知道这对任何人是否有用,但我也读到了这个示例代码(也发布在他们的网站上)。我继续阅读更多关于LiveCharts的文章,偶然发现了他们的DateTimePoint类(LiveCharts.Defaults.DateTimePoint)。我刚刚开始使用这些控件,但乍一看,它所绘制的图表几乎就是我期望看到的。
我的问题是我有一堆笛卡尔类型的点,但DateTime不是定期分布的-所以我有这样的数据点("1 Jan 2015 00:00",9.5)。(“20Jan 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>
....现在,我碰巧采用了一种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;
}
}显然,这比他们的网页上的代码要少得多。DatabaseEntry是我的一个--它只是一个包含7或8个属性的容器,包括时间戳(DateTime)和值(double)。
我仍然在写这段代码,所以我确信我还有更多的工作要做,但就开始而言,我已经看到了到目前为止我所期望看到的内容。
https://stackoverflow.com/questions/43985282
复制相似问题