Collection of PrimaryXAxis.Labels is empty

Hello,

when the user clicks on a label on the X-axis of my chart, I like to make the label text bold for example. In the ChartRegionClick event I can get the index of the clicked label with e.Region.AxisLabelIndex. Now when I loop through the collection of chart.PrimaryXAxis.Labels, the collection is always empty and I cannot acces the labels.

What am I doing wrong? Thanks for any help.

Michael

7 Replies 1 reply marked as answer

SJ Suyamburaja Jayakumar Syncfusion Team November 17, 2020 04:50 PM UTC

Hi Michael Witzik, 
 
Greetings from Syncfusion.   
 
Currently we are validating the reported query and we will update the complete details on or before November 18, 2020. 
 
Regards, 
Suyamburaja J. 



SJ Suyamburaja Jayakumar Syncfusion Team November 19, 2020 05:23 PM UTC

Hi Michael Witzik, 
 
Sorry for the delay. 
 
We are facing some difficulties in achieving your requirement and due to complexity, we need some more time to provide a feasible solution for your requirement and we will update you on November 20, 2020. 
 
Regards, 
Suyamburaja J. 



MW Michael Witzik November 27, 2020 07:05 AM UTC

Hello Suyamburaja,

do you have any updates on this topic?

Thank you.
Michael


YP Yuvaraj Palanisamy Syncfusion Team November 30, 2020 12:27 PM UTC

Hi Michael Witzik, 

We regret for the delay.  
   
We have reproduced your reported problem at our end. And we have forwarded this to our development team and currently, they are validating this, and we will update you with the complete details on or before December 2, 2020.  
  
Regards, 
Yuvaraj. 



YP Yuvaraj Palanisamy Syncfusion Team December 3, 2020 05:14 AM UTC

Hi Michael Witzik, 

We regret for the delay. 

We have achieved your requirement “clicks on a label on the X-axis, to make the label text bold” by using chart axis CustomLabels support. Please find the below code snippet for your reference. 

this.chartControl1.PrimaryXAxis.TickLabelsDrawingMode = ChartAxisTickLabelDrawingMode.UserMode; 

this.chartControl1.ChartRegionClick += ChartControl1_ChartRegionClick; 

this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("1999", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 0, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2000", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 1, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2001", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 2, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2002", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 3, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2003", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 4, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2004", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 5, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2005", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 6, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2006", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 7, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2007", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 8, "", ChartValueType.Custom)); 
this.chartControl1.PrimaryXAxis.Labels.Add(new ChartAxisLabel("2008", Color.Black, new Font("Arial", 8F, System.Drawing.FontStyle.Regular), 9, "", ChartValueType.Custom)); 

 

int previousIndex = -1; 

private void ChartControl1_ChartRegionClick(object sender, ChartRegionMouseEventArgs e) 
    ChartControl series = sender as ChartControl; 
    int axisIndex = e.Region.AxisLabelIndex; 
    if (previousIndex != -1) 
   
        ChartAxisLabel previousChartAxisLabel = (chartControl1.PrimaryXAxis.Labels[previousIndex]) as ChartAxisLabel; 
        previousChartAxisLabel.Font = new Font(FontFamily.GenericMonospace, 8f, FontStyle.Regular); 
        chartControl1.PrimaryXAxis.Labels.RemoveAt(previousIndex); 
        chartControl1.PrimaryXAxis.Labels.Insert(previousIndex, previousChartAxisLabel); 
   
    ChartAxisLabel chartAxisLabel = (chartControl1.PrimaryXAxis.Labels[axisIndex]) as ChartAxisLabel; 
    chartAxisLabel.Font = new Font(FontFamily.GenericMonospace, 12f, FontStyle.Bold); 
    chartControl1.PrimaryXAxis.Labels.RemoveAt(axisIndex); 
    chartControl1.PrimaryXAxis.Labels.Insert(axisIndex, chartAxisLabel); 
    previousIndex = axisIndex; 

 

Also, we have attached the sample for your reference, please find the sample from the below link. 

  
For more information, please refer the below link 

Output: 
 

Regards, 
Yuvaraj. 
  


Marked as answer

MW Michael Witzik December 4, 2020 10:45 AM UTC

Hello Yuvaraj,

thank you! When I use the "ChartAxisTickLabelDrawingMode.UserMode" as shown, I can now access the labels collections and change the font as described.

Kind regards
Michael


YP Yuvaraj Palanisamy Syncfusion Team December 7, 2020 10:45 AM UTC

Hi Michael Witzik,  
  
Thanks for your update.   
  
Please let us know if you need any further assistance.  
 
Regards, 
Yuvaraj. 


Loader.
Up arrow icon