rookie1
rookie1

Reputation: 29

How do you display data points in percentage format using mschart in c#

I have been trying to display percentage for my data points in ms chart in this format 00.00%. I can not figure out how.

I used this for my series label

chart1.Series[1].Label = "#PERCENT";

and it turns out like this on the chart, the values are not displaying correctly.

enter image description here

The data is coming from a data table and this is how I am populating the chart

 //Chart data points
                foreach(DataRow r in dt.Rows)
                {
                    object wk = (r["week_num"]).ToString();
                    object fpy = (r["fpy"]); // First pass yield percentage
                    object thrput = (r["throughput"]).ToString();

                    chart1.Series[0].Points.AddXY(wk, thrput);
                    chart1.Series[1].Points.AddXY(wk, fpy);
                }

I am not sure how to correct format the data points in the line graph to display the correct value and in the percentage format.

Any suggestions?

EDIT:

if I remove this line

chart1.Series[1].Label = "#PERCENT";

The values are correct but not in percentage format

enter image description here

Upvotes: 1

Views: 1767

Answers (2)

rookie1
rookie1

Reputation: 29

I decided to convert the fpy to a decimal and remove the all decimal points. I now have the correct value displayed on my line graph data points. Thank your all for you help! :)

decimal fpy = Convert.ToDecimal(string.Format("{0:F0}", ((r["fpy"])))); // First pass yield percentage

Upvotes: 1

Rachel Gallen
Rachel Gallen

Reputation: 28583

You will have to format the Label style

Try chart.ChartAreas.AxisX.LabelStyle.Format = "{00:00}"

actually you may have to customise the percent specifer

i think its' set to just show one number (before the point) by default

e.g.

double value = .086;
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                "{0:#0.##%}", value));
// Displays 8.6%  

you will have to override it and put in an extra zero

Upvotes: 2

Related Questions