在图表中添加 x 轴字符串值而不是数字

J-P*_*J-P 3 .net c# charts mschart winforms

我正在尝试设计一个图表,其中 x 表示异常消息,y 表示它发生的次数。

我无法在每个条形下显示 x 值(异常消息),我还尝试标记 x 并为每个标签设置范围,但条形不是在此标签上生成的,而是远离它们生成的。

这是我的代码示例

 reportChart.Series.Add(exception);
 reportChart.Series[exception].SetDefault(true);
 reportChart.Series[exception].Enabled = true;
 reportChart.Series[exception].Points.AddXY(exception,ExceptionMessages[exception]);
Run Code Online (Sandbox Code Playgroud)

ExceptionMessages[exception] 是一个包含当前异常发生次数的值的字典。

jsa*_*ics 5

你可以这样做:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

图表: 在此处输入图片说明

编辑:添加一些逻辑来分配这样的颜色:

        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;
        }
Run Code Online (Sandbox Code Playgroud)

图表:

在此处输入图片说明