簡體   English   中英

在圖表中添加x軸字符串值而不是數字

[英]Adding x axis string value in a chart instead of number

我正在嘗試設計一個圖表,其中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]);

ExceptionMessages[exception]是一個字典,包含當前異常發生次數的值。

你可以這樣做:

    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);
    }

圖表: 在此輸入圖像描述

編輯 :添加一些邏輯來分配這樣的顏色:

        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;
        }

圖表:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM