2

我正在创建一个小程序,它通过图表按月显示我们的总销售额,我需要通过我的 SQL 响应名称(2012'7、2012'8、2012 调用 X 轴点(1、2、3、4、5) '9),但找不到如何做到这一点。

在此处输入图像描述

代码:

        sqlQuery = "SELECT YEAR(`importdate`) as 'Year', MONTH(`importdate`) as 'Month', SUM(`price`) as 'Sum' FROM `wagon` 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);

        chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Gainsboro;
        chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Gainsboro;

        try
        {
            sqlConnection.Open();

            MySqlDataReader sqlReader = sqlCommand.ExecuteReader();

            if (sqlReader.HasRows)
            {
                while (sqlReader.Read())
                {
                    string Sum = sqlReader["Sum"].ToString();
                    if (Sum.Contains(",")) Sum = Sum.Replace(",", ".");

                    string serieName = "Series1";
                   // string serieName = sqlReader["Year"].ToString() + '\'' + sqlReader["Month"].ToString();
                   // chart1.Series.Add(sqlReader["Year"].ToString() + '\'' + sqlReader["Month"].ToString());

                   // chart1.Series[serieName].ChartArea = "ChartArea1";

                    chart1.Series[serieName].Points.AddY(Sum);


                    chart1.Series[serieName].ChartType = SeriesChartType.Line;
                    chart1.Series[serieName].IsValueShownAsLabel = true;
                    //sqlReader["Year"].ToString() + '"' + sqlReader["Month"].ToString()
                }
            }
        }
4

1 回答 1

6

如果我说对了,您可以使用:

Chart1.ChartAreas(0).AxisX.CustomLabels.Add(0.5, 1.5, "1");
            Chart1.ChartAreas(0).AxisX.CustomLabels.Add(1.5, 2.5, "2");
            Chart1.ChartAreas(0).AxisX.CustomLabels.Add(2.5, 3.5, "3");
            Chart1.ChartAreas(0).AxisX.CustomLabels.Add(3.5, 4.5, "4");
            Chart1.ChartAreas(0).AxisX.CustomLabels.Add(4.5, 5.5, "5");

前两个数字决定了这些标签的传播范围。第三是标签本身。

于 2013-01-06T21:53:29.160 回答