How to format values with custom string formats?

Asked

Viewed 332 times

3

I am developing a graphic and would like to put the values of the Y axis as the image below:

modelo do gráfico

I have verified that it is possible to do this through custom string formats in that way ChartArea.AxisY.LabelStyle.Format = "formato customizado", however I could not create a format for output I need, someone has idea of which format to use to reach the expected output?

Search items:
Formatting Chart Axis Labels
Format value with Brazilian currency mask

1 answer

2


The question has been changed and I will try to save the answer as far as I can.

Use this:

private void chart1_FormatNumber(object sender, System.Windows.Forms.DataVisualization.Charting.FormatNumberEventArgs e) {
    if(e.ElementType == System.Windows.Forms.DataVisualization.Charting.ChartElementType.AxisLabels) {
        switch(e.Format) {
            case "formato customizado":
                e.LocalizedValue = string.Format(CultureInfo.CreateSpecificCulture("pt-BR"), "{0:F1}", e.Value / 1000);
                break;
            default:
                break;
        }
    }
}

See the result of formatting in ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Based on that response in the OS.

  • In this case, the table in the question is only to demonstrate the input in the Y axis and which output I expect, but the graph is filled automatically and the axis ranges are also generated automatically

  • Okay, and what does that mean? You just want to format the number?

  • Yes, for example ChartArea.AxisY.LabelStyle.Format = "{0:0,}";

  • ChartArea.AxisY.LabelStyle.Format is not a String.Format. This is something else from some library you’re using. You have to explain your question better. What kind of numbers?

  • I set the question, if you think you misread the first one, tell me I’ll rule that one out and create a new one

Browser other questions tagged

You are not signed in. Login or sign up in order to post.