基本WPF LiveCharts DateTime示例不起作用

问题描述 投票:4回答:3

我尽可能地遵循Live Charts TimeDate基本示例,但似乎无法正确显示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; }
    }
}

这产生以下内容,日期搞砸了......

enter image description here

c# wpf xaml data-visualization livecharts
3个回答
5
投票

您忘了设置数据上下文:

DataContext = this;

enter image description here


2
投票

出于某种原因,XAML中的LiveCharts绑定有时不起作用。您需要在XAML中命名您的LiveCharts控件(到任何地方):

<lvc:CartesianChart x:Name="RankGraph" Series="{Binding Series}">
<lvc:Axis x:Name="RankGraphAxisX" LabelFormatter="{Binding Formatter}"></lvc:Axis>

然后在你的代码中绑定Series和LabelFormatter:

RankGraph.Series = Series;
RankGraphAxisX.LabelFormatter = Formatter;

0
投票

Dunno,如果这对任何人都有用,但我也读过这个示例代码(也在他们的网站上发布)。我继续阅读有关LiveCharts的更多信息,并遇到了他们的DateTimePoint类(LiveCharts.Defaults.DateTimePoint)。我刚刚开始使用这些控件,但乍一看它几乎是我期待看到的图表。

我的问题是我有一堆类型为<DateTime, double>的笛卡尔点,但是DateTimes没有经常展开 - 所以我有数据点(“2015年1月1日00:00”,9.5)。 (“2015年1月20日04:00”,10),(“2016年1月4日06:46”,5.2)等我认为不规则的时间间隔最好用Scatter图表覆盖,我正在使用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个属性的容器,包括Timestamp(DateTime)和Value(double)。

我仍然在编写这段代码,所以我确信我还有更多工作要做,但就起步而言,我几乎看到了我期待看到的东西。

© www.soinside.com 2019 - 2024. All rights reserved.