Visual Studio,图表,Axis x点名称

时间:2013-01-06 21:51:07

标签: c# sql charts

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

enter image description here

代码:

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

1 个答案:

答案 0 :(得分: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");

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