我正在使用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 part无法正常工作。我被困在这一点上。

最佳答案

看起来您在错误地创建了绑定(bind)(尝试从Visual Studio输出窗口中确认它-报告有关错误绑定(bind)的消息)。

例如:

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[],并且您有一个binding,它尝试使用“Labels”属性-该属性不存在。

您需要一个有效的绑定(bind)源,很可能是DataContext:
setBinding("LabelFormatter", DataContext, xAxis, Axis.LabelFormatterProperty);
setBinding("Labels", DataContext, yAxis, Axis.LabelsProperty);
setBinding("Series", DataContext, chart, CartesianChart.SeriesProperty);

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

关于c# - C#WPF实时图表-一般创建图表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58876562/

10-12 12:42
查看更多