Chart dynamically==================
<UserControl x:Class="Silverligchartdynamically_RK.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</UserControl>


.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;

namespace Silverligchartdynamically_RK
{
    public partial class MainPage : UserControl
    {
        List<Sale> addsale;
        public MainPage()
        {
            InitializeComponent();
            addsale = new List<Sale>();
            addsale.Add(new Sale { name="soap" ,sales=200});
            addsale.Add(new Sale {name="vegetables",sales=300 });
            addsale.Add(new Sale { name = "wheat", sales = 150 });

            Chart chartobj = new Chart();
            chartobj.Height =400;
            chartobj.Width = 400;
            //chartobj.VerticalAlignment = MouseLeftButtonDown;
            ColumnSeries colunseribj = new ColumnSeries();
            colunseribj.ItemsSource = addsale;
            colunseribj.IndependentValuePath = "name";
            colunseribj.DependentValuePath = "sales";
            chartobj.Series.Add(colunseribj);
            LayoutRoot.Children.Add(chartobj);


            //Chart chartobj2 = new Chart();
            //chartobj2.Height = 200;
            //chartobj2.Width = 200;
            //ColumnSeries colunseribj2 = new ColumnSeries();
            //PieSeries pie = new PieSeries();
            //pie.ItemsSource = addsale;
            //pie.IndependentValuePath = "name";
            //pie.DependentValuePath = "sales";
            //chartobj2.Series.Add(pie);
            //LayoutRoot.Children.Add(chartobj2);
            
        }
    }

    public class Sale
    {

        public string name { get; set; }
        public int sales { get; set; }
    }
}

No comments:

Post a Comment