15

I'm making some charts in C# Visual Studio 2010 and I had a quick (hopefully simple) question about formatting the labels on the X-Axis. I searched SO and Google for an answer and I'm pretty sure it has to do with simply editing the LabelStyle.Format for a chart area... But I can't figure it out! Okay, too much information, let me get on with the question

In short, my chart needs to have mileage on the x-axis and it ranges from 0 to 240,000. I would really like to spruce it up a bit and have a 'K' instead of the trailing 3 zeroes. Example: 60K, 120k, 180K, 240K.

SlimPDX
  • 713
  • 1
  • 6
  • 21

1 Answers1

35

Set the LabelStyle.Format property accordingly:

chart1.ChartAreas[0].AxisX.LabelStyle.Format = "{0:0,}K";

Documentation is here: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx (Section: "The ',' Custom Specifier")

DasKrümelmonster
  • 5,816
  • 1
  • 24
  • 45
  • Thank you, I was about to comment my own edition that worked which was "{0,}K" but yours is better. Might I ask what the ":" Colon specifier does? It's not in the documentation for some odd reason :) +1! – SlimPDX Aug 02 '13 at 22:59
  • 2
    The first thing is always the number of the argument. (That's the 0) after that you can specify the format, separated with a colon. The zero there is just the standard format, so you can go ahead and omit that. If you wanted to display a decimal place *if needed*, that would look like this: "{0:0,.#}K" The comma is the important bit, since it divides the value by 1000. – DasKrümelmonster Aug 03 '13 at 08:29
  • @DasKrümelmonster: I've a similar problem with my chart. I'm not able to set the format to display X Axis labels as Month names. Can you please take a look at http://stackoverflow.com/questions/22400364/asp-net-chart-control-set-month-names-from-int-value-on-x-axis-labels? Thanks! – Cheshire Cat Mar 18 '14 at 10:12