Как сделать график chart c# wpf по данным из таблицы бд?

Рейтинг: 0Ответов: 0Опубликовано: 24.03.2023

Я решил применить графики в моем проекте ,для аналитики продаж. Есть таблица продаж с данными:

public partial class ProductSale
    {
        public int Id { get; set; }
        public int ProductId { get; set; }
        public int Quantity { get; set; }
        public DateTime Datatime { get; set; }
        public int UsersId { get; set; }
        public int ClientId { get; set; }
        public string Paytment { get; set; } = null!;

        public virtual Client Client { get; set; } = null!;
        public virtual Product Product { get; set; } = null!;
        public virtual User Users { get; set; } = null!;
    }

Я вывел данные в DataGrid и теперь хочу сделать по этим данным график. В xaml прописал разметку для Chart и подключил библиотеки,также добавил комбобокс для выбора типа диаграм

<WindowsFormsHost>
            <charts:Chart x:Name="ChartSales">
                <charts:Chart.Legends>
                    <charts:Legend> 
                    </charts:Legend>
                </charts:Chart.Legends>
            </charts:Chart>
        </WindowsFormsHost>

В коде прописал

public partial class GraficSalesPage : Page
{
    public GraficSalesPage()
    {
        InitializeComponent();
        using(var db = new ipopovContext())
        {

            var currentSale = new Series("ProductSale")
            {
                IsValueShownAsLabel = true
            };
            ChartSales.Series.Add(currentSale);
            comboCharttypes.ItemsSource = Enum.GetValues(typeof(SeriesChartType));

            
        }
        ChartSales.ChartAreas.Add(new System.Windows.Forms.DataVisualization.Charting.ChartArea("Main"));
    }

    private void comboCharttypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        using (var db = new ipopovContext())
        {
            if (comboCharttypes.SelectedItem is SeriesChartType currenttype)
            {
                Series currentSale = ChartSales.Series.FirstOrDefault();
                currentSale.ChartType = currenttype;
                currentSale.Points.Clear();


                var ProductSaleList = db.ProductSales.ToList();
                foreach (var product in ProductSaleList)
                {
                    currentSale.Points.DataBindXY(product.Paytment , product.Seler, db.ProductSales.ToList());
                }
            }
        }
            

    }
}

Короче не получается построить график по данным из таблицы помогите пожалуйста

Ответы

Ответов пока нет.