2

I have a pie chart in .asp.net called chrt. I would like the values instead of the labels to show up in the chart with currency formatting. I want the labels to show up in the legend. I do the following and get Values shown on my chart instead of the labels, and the labels still show up in the legend.

chrt.Series[0].IsValueShownAsLabel = true;

Example:

Chart Shows "12345.678" Legend Shows "Sales"

All I need now is to get the chart to show with dollars.

If I then do the following, it changes the formatting to currency on the chart but unfortunately replaces the labels in the legend with the currency formatted values.

foreach (Series b in chrt.Series)
        {                            
            foreach (DataPoint c in b.Points)
            {                 
                //Sets both legend and chart value
                c.Label = c.YValues[0].ToString("C");                                   
            }
        }

Example:

Chart Shows "$12345.68" Legend Shows "$12345.68"

I've also tried the code below, but it sets the legend to the values and formats it to currency, leaving the values on the chart as is.

foreach (Series b in chrt.Series)
        {                            
            foreach (DataPoint c in b.Points)
            {            
               //Sets just the legend to the dollar values
               c.AxisLabel = c.YValues[0].ToString("C");
            }
         }

Example:

Chart Shows "12345.678" Legend Shows "$12345.68"

All I want it to show is

Chart Shows "$12345.68" Legend Shows "Sales"

John Wesley Gordon
  • 910
  • 2
  • 17
  • 39

1 Answers1

6

The only way I could figure this out is to not set

chrt.Series[0].IsValueShownAsLabel = true;

and then manually set the label and legendtext to what I want at the series level. The keywords #VALY and #VALX can be used so it will replace with the values at runtime and by placing the format string inside the curly braces after it, it applies the formatting. So in this case a dollar sign with , at every thousands separator and replacing to the left of the decimal with 0 if there is nothing there and replacing to the right with 0 if there is nothing there but only up to 2 decimal places.

foreach (Series b in chrt.Series)
        {

            b.Label = "#VALY{$#,##0.00}";           
            b.LegendText = "#VALX";
        }

Example:

12345.678 becomes $12,345.67

.1234 becomes $0.12

1234 becomes $1,234.00

John Wesley Gordon
  • 910
  • 2
  • 17
  • 39