C# WPF实时图表 - 创建通用图表

7
我正在使用WPF Live Charts库。我试图生成一个通用的柱状图。
以下是我的代码。无法在XAML中执行,因为它不支持泛型。
public abstract class AbstractGenericBarChart<T> : UserControl, INotifyPropertyChanged
{
    SeriesCollection _SeriesCollection;
    public SeriesCollection SeriesCollection { get { return _SeriesCollection; } set { _SeriesCollection = value; notifyPropertyChanged("SeriesCollection"); } }
    string[] _Labels;
    public string[] Labels { get { return _Labels; } set { _Labels = value; notifyPropertyChanged("Labels"); } }
    Func<int, string> _Formatter;
    public Func<int, string> Formatter { get { return _Formatter; } set { _Formatter = value; notifyPropertyChanged("Formatter"); } }

    public abstract void constructChart(List<T> chartItems);

    public void init(string xLabel, string yLabel)
    {
        renderChart(xLabel, yLabel);
    }

    public void renderChart(string xLabel, string yLabel)
    {
        CartesianChart chart = new CartesianChart { Margin = new Thickness(10, 10, 10, 10), LegendLocation = LegendLocation.Bottom, DataTooltip = new DefaultTooltip { SelectionMode = TooltipSelectionMode.SharedYValues } };
        Axis xAxis = new Axis { Foreground = Brushes.Black, FontSize = 14d, Title = xLabel };
        Axis yAxis = new Axis { Foreground = Brushes.Black, FontSize = 14d, Title = yLabel };
        chart.AxisX.Add(xAxis);
        chart.AxisY.Add(yAxis);
        setBinding("LabelFormatter", Formatter, xAxis, Axis.LabelFormatterProperty);
        setBinding("Labels", Labels, yAxis, Axis.LabelsProperty);
        setBinding("Series", SeriesCollection, chart, CartesianChart.SeriesProperty);
        Content = chart;
    }

    public void setBinding(string propertyName, object source, FrameworkElement control, DependencyProperty dependencyProperty)
    {
        Binding binding = new Binding(propertyName)
        {
            Source = source
        };
        control.SetBinding(dependencyProperty, binding);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void notifyPropertyChanged(string prop)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
    }
}


internal class BarChart : AbstractGenericBarChart<TopTransactingCount>
{
    public override void constructChart(List<TopTransactingCount> chartItems)
    {
        SeriesCollection = new SeriesCollection
        {
            new RowSeries
            {
                Title = "Transaction Count",
                Values = new ChartValues<long>(chartItems.Select(x=>x.TransCount))
            }
        };
        Labels = chartItems.Select(x => x.Date.ToShortDateString()).ToArray();
        Formatter = value => value.ToString();
        DataContext = this;
    }
}

启动时,我看到一个预期的空图表控件。

当我点击提交按钮时,我调用constructChart方法。

public partial class TotalTransCountsChart : UserControl, IChart
{
    private BarChart chart = new BarChart();
    List<object> chartData;

    public TotalTransCountsChart()
    {
        InitializeComponent();
    }

    public void init(List<object> chartData)
    {
        this.chartData = chartData;
        chart.init("Transaction Count", "Date");
        chart.constructChart(chartData.Cast<TopTransactingCount>().ToList());
        grid.Children.Add(chart);
        Grid.SetRow(chart, 3);
    }

    private void CmdSubmit_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        chart.constructChart(chartData.Cast<TopTransactingCount>().ToList());
    }
}

然而,图表仍然是空的。 我认为代码中的 binding部分 没有按预期工作。 我卡在了这个地方。


1
如果您选择使用图表工厂,那么您的GenericBarChart控件实际上不需要是通用的,因为T的唯一用途是在constructChart方法中。我建议您重构图表控件以使其非通用,并使用XAML。但是,如果您的WPF技能允许,我宁愿创建自定义Control而不是UserControl。对于视图控件,请优先使用DependencyProperty而不是INotifiyPropertyChanged(INPC)。INPC通常在ViewModel中更有用。 - grek40
1
请分享完整的日志。 - mfkl
1个回答

5

看起来你正在不正确地创建绑定(尝试从Visual Studio输出窗口确认 - 它会报告关于不正确绑定的消息)。

例如:

setBinding("Labels", Labels, yAxis, Axis.LabelsProperty);

public void setBinding(string propertyName, object source, FrameworkElement control, DependencyProperty dependencyProperty)
{
    Binding binding = new Binding(propertyName)
    {
        Source = source
    };
    control.SetBinding(dependencyProperty, binding);
}

Labels是一个string[],并且您有一个绑定,尝试使用不存在的“Labels”属性。

您需要一个有效的绑定源,最可能是DataContext:

setBinding("LabelFormatter", DataContext, xAxis, Axis.LabelFormatterProperty);
setBinding("Labels", DataContext, yAxis, Axis.LabelsProperty);
setBinding("Series", DataContext, chart, CartesianChart.SeriesProperty);

或者更好的方法是-不指定源,所有绑定都将连接到当前DataContext,即使它更新:
public void setBinding(string propertyName, object source, FrameworkElement control, DependencyProperty dependencyProperty)
{
    Binding binding = new Binding(propertyName);
    control.SetBinding(dependencyProperty, binding);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接