Axis problem when switching from column to bar charttypes

Hi,

I am dynamically populating a chart via this code:

ChartDataBindAxisLabelModel xAxisLabelModel = null;

dataSeriesModel = new ChartDataBindModel(dgv.DataSource);

 

// If ChartDataBindModel.XName is empty or null, X value is index of point.

dataSeriesModel.YNames = new string[] { "Column3" };

series.ResetSeriesModel();

series.SeriesModel = dataSeriesModel;

series.Style.Border.Color = Color.Transparent;

series2.ResetSeriesModel();

series2.SeriesModel = dataSeriesModel;

series2.Style.Border.Color = Color.Transparent;

 

dataSeriesModel.XName = "Column1";

dataSeriesModel2 = new ChartDataBindModel(dgv.DataSource);

xAxisLabelModel = new ChartDataBindAxisLabelModel(dgv.DataSource);

xAxisLabelModel.LabelName = "Column2";

sXA1.LabelsImpl = xAxisLabelModel;

sXA1.ValueType = ChartValueType.Custom;

sXA1.TickLabelsDrawingMode = ChartAxisTickLabelDrawingMode.UserMode;

sXA1.Font = new Font("Calibri", 7);

// If ChartDataBindModel.XName is empty or null, X value is index of point.

dataSeriesModel.YNames = new string[] { "Column3" };

dataSeriesModel2.YNames = new string[] { "Column4" };

series.SeriesModel = dataSeriesModel;

series.Style.Border.Color = Color.Transparent;

series2.SeriesModel = dataSeriesModel2;

series2.Style.Border.Color = Color.Transparent;

//add axis

ChartAxis sYA = new ChartAxis(ChartOrientation.Vertical);

chart.Axes.Add(sYA);

series2.YAxis = sYA;

series2.XAxis = sXA1;


I am adding new axes each time because I have to clear the axes everytime due to the number of series changing periodically and I do not want to keep adding new axes everytime which happens if axes are not cleared. This works fine with column charts. However, if I switch to a bar chart the axis labels get messed up. I can still see the xaxis labels listed on the horizontal axes (all crunched together on the left side), and the vertical axis does not have any labels. I tried to simply switch the orientations of each axis but that does not help. Is there any way to have the x-axis automatically move to the vertical position and render the labels properly?


Thanks.


3 Replies

SK Sanjith Kesavan Syncfusion Team July 12, 2018 08:28 AM UTC

Hi Travis, 

Thanks for contacting Syncfusion support. we have analyzed your query and code example. In your code example, you are used “ChartDataBindModel” to bind data for chart. While doing this, you have changed “LabelsImpl” for the axis, you have created. Instead of doing this, kindly follow the below code. 

[C#] 
ChartAxis axis = new ChartAxis(ChartOrientation.Horizontal); 
series.XAxis = axis; 
series.ActualXAxis.LabelsImpl = dataLabelsModel; 
series.ActualXAxis.ValueType = ChartValueType.Custom; 
series.ActualXAxis.LabelsOffset = 1; 
chart.Axes.Add(axis); 

In the above code, we have created the new axis, assigned that for series and also added in the chart. After that we have set the “LabelsImpl”, “ValueType” for ActualXAxis not the axis we have created. This will changes the value type and labelsimpl for the series. Please find the below screenshot after setting series type as “Bar”. 
 

In the below link, we have attached sample for your reference.  
Sample link: chart-sample 

Kindly check the sample and let us know if you have any concern. 

Thanks, 
Sanjith.                                                                                                                                     
  



TC Travis Chambers July 13, 2018 03:28 AM UTC

Worked like a charm. I didn't realize "actualxAxis" existed.

Thank you!


SK Sanjith Kesavan Syncfusion Team July 13, 2018 08:55 AM UTC

Hi Travis, 

We are glad to hear it. Please let us know if you have any other queries.  

Thanks, 
Sanjith. 


Loader.
Up arrow icon