我一直试图从另一个类中填充图形。我已经公开了图,因此子类可以访问它。当我尝试在Visual Studio 2019上运行整个程序时。它没有显示任何错误,但该图完全没有填充。
我尝试从基类中实现相同的代码,但是它可以工作,但是从子类中运行它时却不起作用。
这是另一个名为“ ClassStatistics”的类的方法,基类称为“ home”。 “ linechart”是我正在使用的图形变量名称,并在基类-home中声明。
我在这里做错什么了吗?
DataTable dt = new DataTable();
dt.Columns.Add("X_Value", typeof(DateTime)); //X axis of the type date.
dt.Columns.Add("Y_Value", typeof(double)); //Y axis of type double.
Console.WriteLine("Hello M");
StreamReader sr = new StreamReader(Path.GetFullPath("UserData/Logs") + "\\" + System.DateTime.Today.ToString("MM-yyyy") + "_Logs.txt"); //Path to the log file.
string line;
DayOfWeek day = DateTime.Now.DayOfWeek; //Current day of the week
int days = day - DayOfWeek.Monday; //Keeps track of the number of days left before the next week.
DateTime Start = DateTime.ParseExact(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("dd-MM-yyyy"), "dd-MM-yyyy", CultureInfo.InvariantCulture); //First day of the month
DateTime End = DateTime.ParseExact(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1).ToString("dd-MM-yyyy"), "dd-MM-yyyy", CultureInfo.InvariantCulture); //Last day of the month
DateTime temp; //stores the date extracted from the log file.
int max = 0;
while ((line = sr.ReadLine()) != null)
{
string[] strarr = line.Split(':'); //Splits the data using ':' for '0' to store the date and '1' to store the number of errors
temp = DateTime.ParseExact(strarr[0], "dd-MM-yyyy", CultureInfo.InvariantCulture); //Extracts the date present in the array
if (temp >= Start && temp <= End) //checks if the given date lies in the same month
{
if (Convert.ToInt32(strarr[1]) > max)
max = Convert.ToInt32(strarr[1]); //Storing the maximum value of Y in max
Console.WriteLine(strarr[1]);
dt.Rows.Add(temp, strarr[1]); //Add data to the data table to show in the graph.
}
}
linechart.DataSource = dt;
linechart.Series["Error-Date"].XValueMember = "X_Value";
linechart.Series["Error-Date"].YValueMembers = "Y_Value";
linechart.Series["Error-Date"].ChartType = SeriesChartType.Line; //Defining the type of chart
linechart.ChartAreas[0].AxisY.Maximum = max; //setting the maximum value of the Y axis
//Change the line colors and grid colors of the chart of both the axes.
linechart.ChartAreas[0].AxisX.LineColor = Color.Black;
linechart.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Black;
linechart.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
linechart.ChartAreas[0].AxisX.Interval = 1;
linechart.ChartAreas[0].AxisY.LineColor = Color.Black;
linechart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Black;
linechart.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
linechart.Series["Error-Date"].BorderWidth = 5;
if (sr != null) sr.Close();