4

我正在创建一个小程序,通过图表按月显示我们的总销售额,但我的图表不够详细,它显示了例如 2012'07 的总和在 20 到 40 之间,但我需要确切的值。(32,4)

在此处输入图像描述

需要帮助。

代码

 private void formGraph()
    {
        string database, host, user, pass, sqlParams, sqlQuery;
        string resultPassword = String.Empty;

        database = "";
        host = "localhost";
        user = "";
        pass = "";

        sqlParams = "Database=" + database + ";Data Source=" + host + ";User Id=" + user + ";Password=" + pass;
        sqlQuery = "SELECT YEAR(`importdate`) as 'Year', MONTH(`importdate`) as 'Month', SUM(`price`) as 'Sum' FROM `data` GROUP BY MONTH(`importdate`), YEAR(`importdate`) ORDER BY YEAR(`importdate`), MONTH(`importdate`) ASC LIMIT 12";

        MySqlConnection sqlConnection = new MySqlConnection(sqlParams);
        MySqlCommand sqlCommand = new MySqlCommand(sqlQuery, sqlConnection);

        try
        {
            sqlConnection.Open();

            MySqlDataReader sqlReader = sqlCommand.ExecuteReader();

            if (sqlReader.HasRows)
            {
                while (sqlReader.Read())
                {
                    string Sum = sqlReader["Sum"].ToString();
                    if (Sum.Contains(",")) Sum = Sum.Replace(",", ".");
                    if (Sum == "0") Sum = "1";
                    chart1.Series.Add(sqlReader["Year"].ToString() + '\'' + sqlReader["Month"].ToString());
                    chart1.Series[sqlReader["Year"].ToString() + '\'' + sqlReader["Month"].ToString()].Points.AddY(Sum);
                    //sqlReader["Year"].ToString() + '"' + sqlReader["Month"].ToString()
                }
            }
        }
        catch (MySqlException sqlError)
        {
            MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            sqlConnection.Close();
        }
    }
4

2 回答 2

3

您需要浏览所有系列并将该IsValueShownAsLabel属性设置为 true。像这样,做第一个:

Chart1.Series[0].IsValueShownAsLabel = true;

希望这对你有帮助!

于 2013-01-06T21:24:51.787 回答
3
Chart1.Series(Series1.Name).Font = New System.Drawing.Font("Arial", 10.0F, System.Drawing.FontStyle.Italic)

如果您将 Series1.Name 替换为您的,它应该可以工作 = 在列顶部显示值

Chart1.Series(Series1.Name).IsValueShownAsLabel = True;
于 2013-01-06T21:27:55.257 回答