简体   繁体   中英

How generate Graphs using modern UI Charts according to user inputs

I'm developing a program which involves graphical chart generation according to user inputs. Data for those graphs should be retrieved from the data base according to the inputs given by the user. I'm using modern UI charts library to generate charts. You can find the library here http://modernuicharts.codeplex.com/

Here is my mainWindow.xaml's screen shot : http://imgur.com/eQUBl9w

Here user selects the graph type and pattern that he wants and the desired graph should be generated accordingly with the data available in the database. Currently I have followed the modern ui charts tutorial on above link and able to generate chart for only one LINQ query which is not based on user inputs. How can I get user inputs at runtime and execute different queries based on them and bind data at run time.

here is my 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; }
    }
}

}

Not tested but should be something like that :

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

and in StackedBarChart cs :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM