This question is locked. New answers and comments are not allowed.
Had a method that would load data and setup the chart. I was making this call in code. To my surprise it was causing the event to get handled multiple times causing all sorts of issues. Just an FYI for anyone that
is running into an issue with Charts CLick handler getting called multiple times.
StructureChart.DefaultView.ChartArea.ItemClick += new EventHandler<ChartItemClickEventArgs>(Structure_ItemClick);
3 Answers, 1 is accepted
0
Hello mike,
Yavor Ivanov
the Telerik team
Unfortunately we couldn't reproduce the problem you are describing in our tests. Can you please share the steps needed to reproduce it and also some sample code?
You can find the code we used for testing in our environment in the attached file.
Yavor Ivanov
the Telerik team
0
mike
Top achievements
Rank 1
answered on 18 Feb 2011, 06:15 PM
Sure.
I reconstructed some pieces of this code so it may not work as written, but you get the jist. As many times as its loaded is as many times the event hanlder will get called. At least that is what i was experiencing.
///Called from button click private void GetData(long s, long b, int d) { PageBusyIndicator.IsBusy = true; //get the business unit and structure id's, and days back to pass to the server Int64 _b= buid; Int64 _s= structId; int _d= daysBack; string _link = string.Format(Charting.BaseUrl + "/chartData?s={0}&b={1}&d={2}", _s, _b, _d); WebClient _buClient = new WebClient(); _buClient.OpenReadCompleted += new OpenReadCompletedEventHandler(InitalBuClient_OpenReadCompleted); _buClient.OpenReadAsync(new Uri(_link), _buId); } ///Called after webclient returns void InitalBuClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { //Read the Json getting returned DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(List<Classes.BusinessUnitChart>)); //Get the data from the results List<Classes.BusinessUnitChart> _buChartInfo = (List<Classes.BusinessUnitChart>)json.ReadObject(e.Result); if (_buChartInfo.Count > 0) { //Load the Chart LoadBusinessChart(_buChartInfo); } } ///called to reload the chart, Assign the click event handler here, this will cause mulitple calls to click event handler. private void LoadBusinessChart(List<Classes.BusinessUnitChart> businessUnitChartData) { //filter the count so we dont get 0 values businessUnitChartData = businessUnitChartData.Where(c => c.Count != 0).ToList<Classes.BusinessUnitChart>(); //hide legend BusinessUnitChart.DefaultView.ChartLegend.Visibility = System.Windows.Visibility.Collapsed; //reset zoom settings BusinessUnitChart.DefaultView.ChartArea.ZoomScrollSettingsX = new ZoomScrollSettings() { ScrollMode = ScrollMode.None }; //clear datasource and seriesmappings BusinessUnitChart.SeriesMappings.Clear(); BusinessUnitChart.ItemsSource = null;
BusinessUnitChart.DefaultView.ChartArea.ItemClick += new EventHandler<ChartItemClickEventArgs>(BU_ItemClick); //format the bar chart FormatBarCharts(BusinessUnitChart); //add the new seriesdefinition SeriesMapping seriesMapping = new SeriesMapping(); seriesMapping.SeriesDefinition = new HorizontalBarSeriesDefinition(); //set the Series colors seriesMapping.SeriesDefinition.Appearance.Fill = new SolidColorBrush(Charting.OpenColor); seriesMapping.SeriesDefinition.Appearance.Foreground = new SolidColorBrush(Colors.White); seriesMapping.SeriesDefinition.Appearance.Stroke = new SolidColorBrush(Charting.OpenColor); //add the item mappings ItemMapping yMapping = new ItemMapping("Count", DataPointMember.YValue); ItemMapping catMapping = new ItemMapping("BusinessUnitName", DataPointMember.XCategory); ItemMapping xMapping = new ItemMapping("BusinessUnitId", DataPointMember.XValue); seriesMapping.ItemMappings.Add(yMapping); seriesMapping.ItemMappings.Add(catMapping); seriesMapping.ItemMappings.Add(xMapping); //setup the item label format seriesMapping.SeriesDefinition.ItemLabelFormat = "#Y{0}"; //add the seriesmapping to the chart BusinessUnitChart.SeriesMappings.Add(seriesMapping); //see if we need zoom/scrolling if (businessUnitChartData.Count > 10) { BusinessUnitChart.DefaultView.ChartArea.ZoomScrollSettingsX = new ZoomScrollSettings() { ScrollMode = Telerik.Windows.Controls.Charting.ScrollMode.ScrollAndZoom}; } //add the datasource BusinessUnitChart.ItemsSource = businessUnitChartData; } void Bu_ItemClick(object sender, ChartItemClickEventArgs e)
{
//get the business unit and structure id's, and days back to pass to the server
Int64 _s= ((Classes.BusinessUnitChart)e.DataPoint.DataItem).BusinessUnitId;
Int64 _b= ((Classes.BusinessUnitChart)e.DataPoint.DataItem).StructureId;
int? _days = GetDays();
GetData(_structId, _buId, _days.Value);
}
I reconstructed some pieces of this code so it may not work as written, but you get the jist. As many times as its loaded is as many times the event hanlder will get called. At least that is what i was experiencing.
0
Hi Mike,
The ItemClick event is wired within LoadBusinessChart method. And this method is called within InitalBuClient_OpenReadCompleted callback, which in turn is executed when the webclient returns. So for each time webclient returns data you attach another handler to the ItemClick event of the ChartArea, hence the behavior you observe. Just for test purpose -- you can add a regular button and attach its Click event in the same manner -- you will get the same result.
Please, move the code for attaching the event handler to the user control's constructor or Loaded event handler. You can also move it to XAML.
Regards,
Ves
the Telerik team
The ItemClick event is wired within LoadBusinessChart method. And this method is called within InitalBuClient_OpenReadCompleted callback, which in turn is executed when the webclient returns. So for each time webclient returns data you attach another handler to the ItemClick event of the ChartArea, hence the behavior you observe. Just for test purpose -- you can add a regular button and attach its Click event in the same manner -- you will get the same result.
Please, move the code for attaching the event handler to the user control's constructor or Loaded event handler. You can also move it to XAML.
Regards,
Ves
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!