J-P
J-P

Reputation: 403

Adding x axis string value in a chart instead of number

I'm trying to design a chart where x represents the exception message and y represents how many time it occurred.

I'm not able to show x value (exception message) under each bar, I also tried labelling x and having range for each label but the bars are not generated over this label but away from them.

Here an example of my code

 reportChart.Series.Add(exception);
 reportChart.Series[exception].SetDefault(true);
 reportChart.Series[exception].Enabled = true;
 reportChart.Series[exception].Points.AddXY(exception,ExceptionMessages[exception]);

ExceptionMessages[exception] is a dictionary that contains the value of how many times the current exception occurred.

Upvotes: 4

Views: 7980

Answers (1)

jsanalytics
jsanalytics

Reputation: 13188

You can do this:

    private void Form1_Load(object sender, EventArgs e)
    {
        Dictionary<string, int> ExceptionMessages = new Dictionary<string, int>();

        ExceptionMessages.Add("Exception 1", 20);
        ExceptionMessages.Add("Exception 2", 50);
        ExceptionMessages.Add("Exception 3", 30);
        ExceptionMessages.Add("Exception 4", 60);
        ExceptionMessages.Add("Exception 5", 10);

        foreach (KeyValuePair<string, int> exception in ExceptionMessages)
            chart1.Series[0].Points.AddXY(exception.Key, exception.Value);
    }

Chart: enter image description here

EDIT: add some logic to assign colors like this:

        foreach (KeyValuePair<string, int> exception in ExceptionMessages)
        {
            chart1.Series[0].Points.AddXY(exception.Key, exception.Value);

            //add business logic for your color here
            if (exception.Key == "Exception 4")
                chart1.Series[0].Points[chart1.Series[0].Points.Count - 1].Color = Color.Red;
        }

Chart:

enter image description here

Upvotes: 7

Related Questions