0
votes

I am having a problem labeling a chart axis in C# with the Microsoft Chart control. My label is the following string: "Elevation (m)". It works perfectly fine when labeling the x-axis. However, when I put the label on the y-axis, it displays as follows: "Elevation (m|". For some reason the ) displays as a |. Is this a bug or am I doing something wrong?

EDIT:

Here is my code:

DataTable dt1 = curve.GetCurveDataTableFromDatabase(); //Gets a datatable with columns Elevation and Storage
string seriesName = "Raw Data";
string xaxislabel = "Storage (m3)";
string yaxislabel = "Elevation (m)";
chartPlotArea.Series.Clear();
chartPlotArea.DataSource = dt1;
chartPlotArea.Series.Add(seriesName);
chartPlotArea.Series[seriesName].XValueMember = "Storage";
chartPlotArea.Series[seriesName].YValueMembers = "Elevation";
chartPlotArea.Series[seriesName].ChartType = SeriesChartType.Line;
chartPlotArea.ChartAreas[0].AxisX.Title = xaxislabel;
chartPlotArea.ChartAreas[0].AxisY.Title = yaxislabel;
chartPlotArea.Titles.Clear();
chartPlotArea.Titles.Add(new Title("Storage vs Elevation Plot"));

And here is what the chart looks like: enter image description here

2
Sounds weird. Show us the code you use! - TaW
I can't imagine this to be a chart bug and I see nothing wrong with the posted code. So either there is something else going on somwwhere or the posted code is not quite the real one. I have seen length attempts to find an error when all that was wrong was a funny charcter that had sneaked into the code. Delete the line (string yaxislabel = "Elevation (m)";) and retype it. (Don't copy and paste it.) - TaW

2 Answers

1
votes

It has something to do with the font. I can duplicate the issue when I use the default font for the chart axis titles. Setting the font to size 10 fixed the issue for me.

Through the gui: enter image description here

Programmatically:

chart1.ChartAreas[0].AxisX.TitleFont = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
chart1.ChartAreas[0].AxisY.TitleFont = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
0
votes

I was having this same issue. I changed one of my chart axis title font sizes to 9 and I don't see the problem. It seems that some font sizes cause this and some don't. I believe it has to do with the fact that this text is printed vertically.

Here is how I changed the font size:

MeasurementResultsChart.ChartAreas[0].Axes[0].TitleFont = new Font("Microsoft San Serif", 9, FontStyle.Regular);
MeasurementResultsChart.ChartAreas[0].Axes[0].Title = "Reference Concentration (%)";
MeasurementResultsChart.ChartAreas[0].Axes[1].TitleFont = new Font("Microsoft San Serif", 9, FontStyle.Regular);
MeasurementResultsChart.ChartAreas[0].Axes[1].Title = "Measured Concentration (%)";