Checkbox problem on Radhart

1 posts, 0 answers
  1. Muhammad
    Muhammad avatar
    1 posts
    Member since:
    Aug 2013

    Posted 22 Aug 2013 Link to this post

    I have used RadChart for displaying graph, On legend i have used chekboxes for each item. upon which the respective series will be show/hide.On opening the form the data is loaded and legend items are checked by default. The problem is that when i unchecked the items and then scroll the chart or zoom in the checboxes are automatically checked, although the series are hidden as i have already uncheck the items.Why the checkboxes are automatically got checked on zooming or scrolling.I want the checkboxes to remain unchecked

    User control class

    public partial class UserControl1 : UserControl
        {
            DataSeries lineSeries;
            SeriesMapping seriesMapping;
            SeriesDefinition seriesDefinition;
            vm objvm;
            List<Data> vitalSign;
            List<CheckBox> VSChk;
            public Dictionary<object, bool?> chkbxState;
            
            public UserControl1()
            {
                InitializeComponent();
                radChart1.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Between;
                radChart1.DefaultView.ChartArea.AxisX.AxisLabelsVisibility = Visibility.Hidden;
                //radChart1.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;
                radChart1.DefaultView.ChartArea.ZoomScrollSettingsX.ScrollMode = ScrollMode.ScrollAndZoom;
                radChart1.DefaultView.ChartArea.ZoomScrollSettingsX.MinZoomRange = 0.2;
                radChart1.DefaultView.ChartLegend.UseAutoGeneratedItems = true;
               
                 
            }
     
            public void initViewModel()
            {       
                objvm = ((vm)this.FindResource("ViewModel"));
                vitalSign = new List<Data>();     
            }
            public void addData(string XValue, string YValue, string VSCode, string EncounterDate)
            {
                vitalSign.Add(new Data(DateTime.Parse(XValue), double.Parse(YValue), VSCode, DateTime.Parse(EncounterDate)));
            }
            public void setSeriesMapping()
            {
               // List<Data> vs;
                try
                {
                    var v1 = (from c1 in vitalSign
                              select new { itemCode = c1.vsCode }).Distinct().ToList();
     
                    foreach (var item in v1)
                    {
                       // vs = new List<Data>();
                        seriesMapping = new SeriesMapping();
                        seriesDefinition = new LineSeriesDefinition();
                        seriesDefinition.ShowItemToolTips = true;
                        seriesMapping.LegendLabel = item.itemCode;
                        seriesMapping.SeriesDefinition = seriesDefinition;
                        seriesMapping.SeriesDefinition.Visibility = SeriesVisibility.Visible;
     
                        var v2 = from c2 in vitalSign
                                 where c2.vsCode == item.itemCode
                                 select c2;
     
                        seriesMapping.ItemMappings.Add(new ItemMapping("encounterDate", DataPointMember.XCategory));
                        seriesMapping.ItemMappings.Add(new ItemMapping("Y", DataPointMember.YValue));
                        seriesMapping.ItemMappings.Add(new ItemMapping("toolTip", DataPointMember.Tooltip));
     
                        /*foreach (var vitalsign in v2)
                        {
                            vs.Add(new Data(vitalsign.X, vitalsign.Y, vitalsign.vsCode, vitalsign.encounterDate));
                        }
                         */
                        objvm._mapping.Add(seriesMapping.LegendLabel, seriesMapping);
                       // seriesMapping.ItemsSource = vs;
                        seriesMapping.ItemsSource = v2.ToList();
                        radChart1.SeriesMappings.Add(seriesMapping);
                    }
     
                }
                catch (Exception ex)
                {
     
                }
            }     
            public void setData(string controlname)
            {
                // not use now
                try
                {
                    seriesMapping = new SeriesMapping();
                    seriesDefinition = new LineSeriesDefinition();
                    seriesDefinition.ShowItemToolTips = true;
                    seriesMapping.LegendLabel = controlname;
                    seriesMapping.SeriesDefinition = seriesDefinition;
                    seriesMapping.SeriesDefinition.Visibility = SeriesVisibility.Visible;              
                   // seriesMapping.ItemMappings.Add(new ItemMapping("X", DataPointMember.XValue));
                    seriesMapping.ItemMappings.Add(new ItemMapping("X", DataPointMember.XCategory));
                    seriesMapping.ItemMappings.Add(new ItemMapping("Y", DataPointMember.YValue));
                    seriesMapping.ItemMappings.Add(new ItemMapping("toolTip", DataPointMember.Tooltip));
     
                    seriesMapping.ItemsSource = this.vitalSign;
                     
                    objvm._mapping.Add(seriesMapping.LegendLabel, seriesMapping);             
                    radChart1.SeriesMappings.Add(seriesMapping);
                }
                catch  (Exception ex)
                {
                 
                }
            }   
            public void setDataLineSeries()
            {
                try
                {
                    var v7 = (from c7 in vitalSign
                              select new { itemCode = c7.vsCode }).Distinct().ToList();
                     
                    foreach (var item in v7)
                    {
                        lineSeries = new DataSeries();
                        lineSeries.Definition = new LineSeriesDefinition();
                        lineSeries.Definition.ShowItemToolTips = true;
                        lineSeries.LegendLabel = item.itemCode;
     
                        var v8 = from c8 in vitalSign
                                 where c8.vsCode == item.itemCode
                                 select c8;
     
                        foreach (var vitalsign in v8)
                        {
                            lineSeries.Add(new DataPoint() { XCategory = vitalsign.encounterDate.ToString("g"), YValue = vitalsign.Y, Tooltip = vitalsign.toolTip });
                        }
                       // objvm._series.Add(lineSeries.LegendLabel, lineSeries);
                        radChart1.DefaultView.ChartArea.DataSeries.Add(lineSeries);
                       
                    }
                }
                catch (Exception ex)
                {
     
                }
            }
            private void RadChart1_Loaded(object sender, RoutedEventArgs e)
            {
               /* List<CheckBox> checkBoxes = radChart1.DefaultView.ChartLegend.ChildrenOfType<CheckBox>().ToList<CheckBox>();
                if (checkBoxes.Count > 0)
                {
                    checkBoxes[0].IsEnabled = false;
                }
                //       SetSeriesItemStyle();
                */
            }
            public void getchkbxControl()
            {
                VSChk = new List<CheckBox>();
                var mapping = radChart1.SeriesMappings;
                try
                {
                    for (int i = 0; i < mapping.Count; i++)
                    {
                        ChartLegendItem legendItem = (radChart1.DefaultView.ChartLegend as System.Windows.Controls.ItemsControl).Items[i] as ChartLegendItem;
                        CheckBox checkBox = legendItem.ChildrenOfType<CheckBox>().FirstOrDefault();
                        VSChk.Add(checkBox);
                    }
                }
                catch (Exception ex)
                {
     
                }
            }
     
            private void AxisX_RangeChanged(object sender, EventArgs e)
            {
                try
                {
                    if (VSChk != null)
                    {
                        foreach (var item in VSChk)
                        {
                            item.IsChecked = chkbxState[item.Content].Value;
                        }
                    }            
                }
                catch (Exception ex)
                { }
            }
     
            private void CheckBox_Checked(object sender, RoutedEventArgs e)
            
                chkbxState = new Dictionary<object, bool?>();
                foreach (var item in VSChk)
                {          
                    chkbxState.Add(item.Content, item.IsChecked);
                }
            }
     
            private void ChartArea_Loaded(object sender, RoutedEventArgs e)
            {
                
            }
            
            private void post_Unchecked(object sender, RoutedEventArgs e)
            {
                chkbxState = new Dictionary<object, bool?>();
                foreach (var item in VSChk)
                {
                    chkbxState.Add(item.Content, item.IsChecked);
                }
            }
        }
       

    here is the XAML code

    <UserControl x:Class="HMVitalSignGraph.UserControl1"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:example="clr-namespace:HMVitalSignGraph"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                 mc:Ignorable="d"
                 d:DesignHeight="300" d:DesignWidth="500">
     
        <UserControl.Resources>
            <example:vm x:Key="ViewModel" />
            <Style x:Key="SeriesItemLabelStyle"
            TargetType="telerik:SeriesItemLabel">
                <Setter Property="HorizontalContentAlignment"
                Value="Center" />
                <Setter Property="Padding"
                Value="2,0" />
                <Setter Property="Foreground"
                Value="Black" />
                <Setter Property="FontWeight"
                Value="Bold" />
                <Setter Property="FontFamily"
                Value="Trebuchet MS" />
                <Setter Property="FontSize"
                Value="12" />
            </Style>
             
            <Style x:Key="CustomLegendItemStyle" TargetType="telerik:ChartLegendItem">
                <Setter Property="Template" >
                    <Setter.Value>
                         
                        <ControlTemplate TargetType="telerik:ChartLegendItem">
                            <Grid x:Name="PART_MainContainer" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,0,5,0">
                                <Path x:Name="PART_LegendItemMarker"
                                      Height="20"
                                      Width="150"
                                      Style="{TemplateBinding ItemStyle}"
                                      Stretch="UniformToFill">
                                    <Path.Data>
                                        <PathGeometry x:Name="PART_ItemMarkerGeometry" />
                                    </Path.Data>
                                </Path>
                                 
                                <CheckBox x:Name="post" IsChecked="True"
                                          Foreground="Black"
                                          VerticalAlignment="Center"
                                          Margin="2,0"
                                          Content="{TemplateBinding Label}"                                 
                                          Background="{TemplateBinding Background}"
                                          Command="{Binding Path=ChangeSeriesVisibilityCommand, Source={StaticResource ViewModel}}"
                                          CommandParameter="{TemplateBinding Label}" Checked="CheckBox_Checked" Unchecked="post_Unchecked" />
                            </Grid>
     
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </UserControl.Resources>
        <Canvas x:Name="canv" Background="#edf7ff">
            <Grid Width="{Binding ActualWidth, ElementName=canv}"  Height="{Binding ActualHeight, ElementName=canv}" MinWidth="1">
                <telerik:RadChart Name="radChart1" DataContext="{Binding}"  HorizontalAlignment="Left" telerik:StyleManager.Theme="Windows7" Loaded="RadChart1_Loaded"  VerticalAlignment="Top" >
                    <telerik:RadChart.DefaultView>
                        <telerik:ChartDefaultView  ChartLegendPosition="Right">
                            <!--<telerik:ChartDefaultView.ChartTitle>
                            <telerik:ChartTitle>CDC growth chart</telerik:ChartTitle>
                        </telerik:ChartDefaultView.ChartTitle>-->
                            <telerik:ChartDefaultView.ChartLegend>
                                <telerik:ChartLegend x:Name="PrimaryLegend" Header="Click on vital signs to hide/show:" LegendItemMarkerShape="Square" LegendItemStyle="{StaticResource CustomLegendItemStyle}"  />
                            </telerik:ChartDefaultView.ChartLegend>
                            <telerik:ChartDefaultView.ChartArea >
                                <telerik:ChartArea  SmartLabelsEnabled="True" EnableAnimations="False" Padding="5,10,20,5" LabelFormatBehavior="HumanReadable"    LegendName="PrimaryLegend" FontSize="10" Loaded="ChartArea_Loaded" >
                                    <telerik:ChartArea.AxisX>
                                        <telerik:AxisX  RangeChanged="AxisX_RangeChanged"  />
                                    </telerik:ChartArea.AxisX>
                                    <!--<telerik:ChartArea.AxisY>
                                        <telerik:AxisY ExtendDirection="None"  />
                                    </telerik:ChartArea.AxisY>-->
                                </telerik:ChartArea>
                            </telerik:ChartDefaultView.ChartArea>
                        </telerik:ChartDefaultView>
                    </telerik:RadChart.DefaultView>
                </telerik:RadChart>
            </Grid>
        </Canvas>
    </UserControl>

    View model class

    class vm : ViewModelBase
        {
            public Dictionary<string, SeriesMapping> _mapping = new Dictionary<string, SeriesMapping>();
            //public Dictionary<string, DataSeries> _series = new Dictionary<string, DataSeries>();
            private ICommand _changeSeriesVisibilityCommand;
         
            public SeriesMapping this[string name]
            {
                get
                {
                    return _mapping[name];
                }
                set
                {
                    _mapping[name] = value;
                }
            }    
           /* public DataSeries this[string name]
            {
                get
                {
                    return _series[name];
                }
                set
                {
                    _series[name] = value;
                }
            }
            */
            public vm()
            {
                this.WireCommands();
            }
            public void WireCommands()
            {
                this.ChangeSeriesVisibilityCommand = new DelegateCommand(this.ChangeSeriesVisibility);
            }
            public ICommand ChangeSeriesVisibilityCommand
            {
                get
                {
                    return this._changeSeriesVisibilityCommand;
                }
                private set
                {
                    this._changeSeriesVisibilityCommand = value;
                }
            }
            public void ChangeSeriesVisibility(object parameter)
            {
                try
                {
                    string parm = parameter.ToString();
     
                    if (this._mapping[parm].SeriesDefinition.Visibility == SeriesVisibility.Collapsed || this._mapping[parm].SeriesDefinition.Visibility == SeriesVisibility.Hidden)
                        this._mapping[parm].SeriesDefinition.Visibility = SeriesVisibility.Visible;
                    else
                        this._mapping[parm].SeriesDefinition.Visibility = SeriesVisibility.Hidden;
                     
                   /* if (this._series[parm].Definition.Visibility == SeriesVisibility.Collapsed || this._series[parm].Definition.Visibility == SeriesVisibility.Hidden)
                        this._series[parm].Definition.Visibility = SeriesVisibility.Visible;
                    else
                        this._series[parm].Definition.Visibility = SeriesVisibility.Hidden;
                    */
                }
                catch (Exception ex)
                {
     
                    throw;
                }
            }
        }

    WPF application class

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                userControl11.initViewModel();
                userControl11.addData(DateTime.Now.ToString("g"), "10", "Diasystolic blood pressure", DateTime.Now.ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(2).ToString("g"), "20", "Diasystolic blood pressure", DateTime.Now.ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(4).ToString("g"), "30", "Diasystolic blood pressure", DateTime.Now.AddDays(2).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(6).ToString(), "40", "Diasystolic blood pressure", DateTime.Now.AddDays(2).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(8).ToString(), "50", "Diasystolic blood pressure", DateTime.Now.AddDays(2).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(10).ToString(), "60", "Diasystolic blood pressure", DateTime.Now.AddDays(4).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(11).ToString(), "70", "Diasystolic blood pressure", DateTime.Now.AddDays(4).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(12).ToString(), "80", "Diasystolic blood pressure", DateTime.Now.AddDays(6).ToString("g"));
           
                userControl11.addData(DateTime.Now.AddDays(1).ToString(), "10", "Pulse rate", DateTime.Now.AddDays(1).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(3).ToString(), "60", "Pulse rate", DateTime.Now.AddDays(1).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(5).ToString(), "90", "Pulse rate", DateTime.Now.AddDays(3).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(7).ToString(), "20", "Pulse rate", DateTime.Now.AddDays(3).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(9).ToString(), "110", "Pulse rate", DateTime.Now.AddDays(5).ToString("g"));
     
                userControl11.addData(DateTime.Now.AddDays(1).ToString(), "5", "rate", DateTime.Now.ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(3).ToString(), "15", "rate", DateTime.Now.AddDays(1).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(5).ToString(), "25", "rate", DateTime.Now.AddDays(5).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(7).ToString(), "35", "rate", DateTime.Now.AddDays(5).ToString("g"));
                userControl11.addData(DateTime.Now.AddDays(9).ToString(), "45", "rate", DateTime.Now.AddDays(7).ToString("g"));
     
                userControl11.setSeriesMapping();
                   
            }
     
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                userControl11.getchkbxControl();
            }
Back to Top