2

i'm using WPF chart controls downloaded from http://wpf.codeplex.com/releases/view/40535

Here i'm trying to plot a barchart and this is my sample code

public partial class Window1 : Window
{
    List<Institute> list = new List<Institute> {
        new Institute { Subject = "Computers", students = 122 },
        new Institute { Subject = "Physics", students = 170 },
        new Institute { Subject = "Maths", students = 210 },
        new Institute { Subject = "Chemistry", students = 1840 },
        new Institute { Subject = "Electronics", students = 140 },
        new Institute { Subject = "Economics", students = 20 },
        new Institute { Subject = "Science", students = 100 },
        new Institute { Subject = "Scocial", students = 110 },
        new Institute { Subject = "English", students = 120 },
        new Institute { Subject = "Biology", students = 130 },
        new Institute { Subject = "Zoology", students = 140 },
        new Institute { Subject = "Hindi", students = 150 }};

    public Window1()
    {
        InitializeComponent();
        ColumnSeries bs = mcChart.Series[0] as ColumnSeries;
        bs.ItemsSource = list;           
    }

}
public class Institute
{
    public string Subject
    {
        get;
        set;
    }
    public int students
    {
        get;
        set;
    }
}

XAML code is

<Window x:Class="net.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
Title="Window1" Height="800" Width="800" xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
<Grid>
    <DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart"
       Width="800" Height="450" FontSize="12"
       Background="DarkGray" Foreground="DarkRed">
        <DVC:Chart.Series>
            <DVC:ColumnSeries x:Name="Barchart" Title="Students of an institute"
        ItemsSource="{Binding}" 
        IndependentValueBinding="{Binding Path=Subject}"
        DependentValueBinding="{Binding Path=students}" >
                <DVC:ColumnSeries.DataPointStyle>
                    <Style TargetType="DVC:ColumnDataPoint">
                        <Setter Property="Background" Value="#001100"/>                          
                    </Style>
                </DVC:ColumnSeries.DataPointStyle>
            </DVC:ColumnSeries>               
        </DVC:Chart.Series>
    </DVC:Chart>
</Grid>

With this code i can plot the chart but i need to plot it dynamically.

When ever i'm running this code i need generate random number of students(using Random) for each and every subject continuously and graph should be plotted based on the new values. That means i want to see the dynamic changes in that graph on the GUI

Is it possible?

If it is possible please answer this.

Thanks in advance.

Sowmya
  • 345
  • 1
  • 5
  • 18

1 Answers1

0

You can use ObservableCollection to this. When ever your collection is modified, raise an event CollectionChanged and your chart will get rebind. Check this example from MSDN or this.

Hope this works for you.

Community
  • 1
  • 1
Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46