我需要显示折线图的帮助

时间:2018-08-20 03:29:45

标签: c# winforms list datatable linechart

我创建了一个简单的代码,该代码从csv文件中读取数据,然后将该数据传输到数据表中。数据表显示在datagridview上。我想显示从我的datable到折线图的两列。该应用程序正常运行,但没有图表出现。我在做什么错了。

namespace WindowsFormsApp6
{
 public partial class Form1 : Form
 {
    public static class Construct
    {
        public static DataTable MainDataTable = new DataTable();
        public static List<string> fileDirectories = new List<string>();
        public static List<string> pathList = new List<string>();
    }
    public class datalist
    {
        public decimal DataLimit { get; set; }

        public DateTime Date1 { get; set; }

    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Construct.MainDataTable.Dispose();
        Construct.MainDataTable.Columns.Clear();
        Construct.MainDataTable.Rows.Clear();
        Construct.fileDirectories.Clear();
        chart1.Series.Clear();
        chart1.ChartAreas.Clear();

        GC.Collect();
        string earliestdate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
        string latestdate = dateTimePicker2.Value.ToString("yyyy-MM-dd");
        DateTime EarlyTime = dateTimePicker1.Value.Date;
        DateTime LateTime = dateTimePicker2.Value.Date;

        List<string> fileDirectories = new List<string>();
        if (1 == 1)
        {
            fileDirectories.Add(@"C:\Users\999\Downloads\x");
        }
        foreach (string selectedPath in fileDirectories)
        {
            string[] level1 = Directory.GetFiles(@selectedPath, "*.csv", SearchOption.AllDirectories);
            foreach (string level2 in level1)
            {
                DateTime lastModDate = File.GetLastWriteTime(@level2);
                if (lastModDate >= Convert.ToDateTime(earliestdate) && lastModDate < Convert.ToDateTime(latestdate).AddDays(1))
                {

                    Construct.pathList.Add(level2);

                }
            }
        }
        chart1.Visible = false;
        DateTime beginDate = dateTimePicker1.Value;
        DateTime endDate = dateTimePicker2.Value;
        Construct.MainDataTable.Columns.Add("Date",typeof(DateTime));
        Construct.MainDataTable.Columns.Add("Time");
        Construct.MainDataTable.Columns.Add("TestNo");
        Construct.MainDataTable.Columns.Add("Lower Limit");
        Construct.MainDataTable.Columns.Add("Upper Limit");
        Construct.MainDataTable.Columns.Add("Data", typeof(decimal));
        foreach (string x in Construct.pathList)
        {


            using (FileStream stream = File.Open(x, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string LowLimit = "";
                    string Upplimit = "";
                    string testNo = "";
                    string date = "";
                    string time = "";
                    string Data = "";

                    while (!reader.EndOfStream)
                    {
                        try
                        {
                            var line = reader.ReadLine();
                            string[] values = line.Split(',');

                            if (line.Split(',')[0].Equals("Date"))
                            {
                                date = line.Split(',')[1];
                                time = line.Split(',')[3];

                            }
                            if (line.IndexOf("7u883", StringComparison.CurrentCultureIgnoreCase) >= 0)
                            {
                                DataRow allShiftRow = Construct.MainDataTable.NewRow();
                                Data = line.Split(',')[12];
                                testNo = line.Split(',')[3];
                                Upplimit = line.Split(',')[8];
                                LowLimit = line.Split(',')[10];
                                allShiftRow["Date"] = date;
                                allShiftRow["Time"] = time;
                                allShiftRow["TestNo"] = testNo;
                                allShiftRow["Upper Limit"] = Upplimit;
                                allShiftRow["Lower Limit"] = LowLimit;
                                allShiftRow["Data"] = Data;
                                Construct.MainDataTable.Rows.Add(allShiftRow);
                                dataGridView1.DataSource = Construct.MainDataTable;

                                List<datalist> DAList = new List<datalist>();
                                DAList = (from DataRow dr in Construct.MainDataTable.Rows
                                          select new datalist()
                                          {
                                              DataLimit = Convert.ToDecimal(dr["Data"]),
                                          }).ToList();
                                List<datalist> DATEList = new List<datalist>();
                                DATEList = (from DataRow dr in Construct.MainDataTable.Rows
                                            select new datalist()
                                            {
                                                Date1 = Convert.ToDateTime(dr["Date"]),
                                          }).ToList();
                                var series1 = new Series
                                {
                                    Name = "Series1",
                                    Color = System.Drawing.Color.Green,
                                    IsVisibleInLegend = false,
                                    IsXValueIndexed = true,
                                    ChartType = SeriesChartType.Line

                                };
                                //chart1.Series[0].XValueType = ChartValueType.DateTime;
                                chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd";
                                chart1.ChartAreas[0].AxisX.Interval = 1;
                                chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Weeks;
                                chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
                                chart1.Series[0].XValueType = ChartValueType.DateTime;
                                chart1.ChartAreas[0].AxisX.Minimum = EarlyTime.ToOADate();
                                chart1.ChartAreas[0].AxisX.Maximum = LateTime.ToOADate();
                                chart1.ChartAreas[0].AxisY.Interval = .25;
                                chart1.ChartAreas[0].AxisY.Minimum = 0;
                                chart1.ChartAreas[0].AxisX.Maximum = 4;
                                chart1.Series.Add(series1);

                                series1.Points.AddXY(DATEList, DAList);

                            }
                        }
                        catch
                        {
                            Console.WriteLine("over.");
                        }
                        chart1.Visible = true;
                    }
                }
            }
        }
    }
}

}

谢谢你!

0 个答案:

没有答案