根据用户输入使用现代UI图表如何生成图表(How generate Graphs using mod

2019-10-22 12:24发布

我正在开发一个程序根据用户输入其涉及图形图表的生成。 对于那些图的数据应该从数据库根据用户给定的输入进行检索。 我使用的是现代的UI图表库生成图表。 你可以在这里找到库http://modernuicharts.codeplex.com/

这里是我的mainWindow.xaml的屏幕截图: http://imgur.com/eQUBl9w

这里,用户选择他想要和期望的图形应该相应地与在数据库中可用的数据来生成图形类型和图案。 目前,我按照上面的链接,并能生成图表不基于用户输入只有一个LINQ查询,现代的UI图教程。 我怎样才能在运行时用户的输入并执行基于他们不同的查询并在运行时数据绑定。

这里是我的mainWindo.xaml.cs

public partial class MainWindow : UserControl
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ChartController();
    }
}

ChartController.CS

public class ChartController : INotifyPropertyChanged
{
    public ObservableCollection<string> ChartTypes { get; set; }

    public ChartController()
    {
        ChartTypes = new ObservableCollection<string>();
        ChartTypes.Add("Pie");
        ChartTypes.Add("Doughnut");
        ChartTypes.Add("Clustered Bar");
        ChartTypes.Add("Clustered Column");
        ChartTypes.Add("Stacked Bar");
        ChartTypes.Add("Stacked Column");
        ChartTypes.Add("Stacked Bar Percentage");
        ChartTypes.Add("Stacked Column Percentage");
    }

    private string _simpleStringProperty;
    public string SimpleStringProperty
    {
        get { return _simpleStringProperty; }
        set
        {
            _simpleStringProperty = value;
            if (value.Equals("Pie"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\PieChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Doughnut"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\DoughnutChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Clustered Column"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\ClusteredColumnChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Clustered Bar"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\ClusteredBarChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Bar"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedBarChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Column"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedColumnChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Bar Percentage"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedBarChart100Percent.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Column Percentage"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedColumnChart100Percent.xaml", UriKind.Relative);
            }
            if (value.Equals("Radial Gauge"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\RadialGaugeChart.xaml", UriKind.Relative);
            }
            OnPropertyChanged("SimpleStringProperty");

        }
    }

    private Uri _selectedPageChart;
    public Uri SelectedPageChart
    {
        get { return _selectedPageChart; }
        set
        {
            _selectedPageChart = value;
            OnPropertyChanged("SelectedPageChart");
        }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

}

PieChart.xaml

<Grid>

    <metroChart:PieChart
Style="{StaticResource MinimalChartStyle}"
ChartTitle="Minimal Pie Chart"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" >
        <metroChart:PieChart.Series>
            <metroChart:ChartSeries
        SeriesTitle="Errors"
        DisplayMember="Year"
        ValueMember="Cost"
        ItemsSource="{Binding Path=Errors}" />
        </metroChart:PieChart.Series>
    </metroChart:PieChart>

</Grid>

PieChart.xaml.cs

public Page1()
    {
        InitializeComponent();
        DataContext = new ChartViewModel();
    }

chartViewModel.cs

  namespace ModernUIForWPFSample.WithoutBackButton.Graphs.ViewModels
{
public class ChartViewModel
{
    public ObservableCollection<stockLotsCostByYear> Errors { get; private set; }
    public ChartViewModel()
    {
        adoraDBContext _c = new adoraDBContext();
        var result = from ps in _c.PurchasingShipments
                  group ps by ps.date.Value.Year into grp
                  select new
                  {
                      Year = grp.Key,
                      Cost = grp.Sum(x => x.NoOfPieces * x.PricePerPiece + x.Micelleneous + x.TransportCost + x.SupplierCommission)
                  }; 

        Errors = new ObservableCollection<stockLotsCostByYear>();

        foreach (var d in result)
            Errors.Add(new stockLotsCostByYear() { Year = d.Year, Cost = d.Cost });

    }

    private object selectedItem = null;
    public object SelectedItem
    {
        get
        {
            return selectedItem;
        }
        set
        {
            selectedItem = value;
        }
    }
    public class TestClass
    {
        public string Category { get; set; }

        public int Number { get; set; }
    }

    public class stockLotsCostByYear
    {
        public int Year { get; set; }

        public decimal? Cost { get; set; }
    }
}

}

Answer 1:

未经测试,但应该是这样的:

SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedBarChart.xaml?parameter=test", UriKind.Relative)

在StackedBarChart CS:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
       string parameter = string.Empty;
    if (NavigationContext.QueryString.TryGetValue("parameter", out parameter)) {
        this.label.Text = parameter;
    }
}


文章来源: How generate Graphs using modern UI Charts according to user inputs
标签: c# wpf xaml charts