Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建时间线图表控件?_C#_Winforms_Charts_Filenames_Timeline - Fatal编程技术网

C# 如何创建时间线图表控件?

C# 如何创建时间线图表控件?,c#,winforms,charts,filenames,timeline,C#,Winforms,Charts,Filenames,Timeline,我正在尝试创建一个时间线,并且一直在尝试使用图表控件。但它不起作用,因为我只需要X值,图表系列就像,只有AddY或AddXY,没有AddX/AddXX2 我知道以前有类似这样的问题。有一个人问 如何创建时间轴控件 比如,3年前,但我不确定他们在回答和评论中到底说了什么 我目前的代码是: DirectoryInfo dInfo = new DirectoryInfo(tbSelectFolder.Text); FileInfo[] Files = dInfo.GetFiles("

我正在尝试创建一个时间线,并且一直在尝试使用图表控件。但它不起作用,因为我只需要X值,图表系列就像,只有AddY或AddXY,没有AddX/AddXX2

我知道以前有类似这样的问题。有一个人问

如何创建时间轴控件

比如,3年前,但我不确定他们在回答和评论中到底说了什么

我目前的代码是:

 DirectoryInfo dInfo = new DirectoryInfo(tbSelectFolder.Text);
        FileInfo[] Files = dInfo.GetFiles("*.ts");
        List<string> fileNames = new List<string>();

        List<DateTime> fileDates = new List<DateTime>();


        chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.White;
        chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
        chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Solid;
        chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
        chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
        chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
        chart1.ChartAreas[0].AxisX2.Enabled = AxisEnabled.True;

        foreach (FileInfo file in Files)
        {
            string filename = Path.GetFileNameWithoutExtension(file.Name);
            string[] fileNameSplit = filename.Split(' ');
            fileNames.Add(fileNameSplit[0]);

            DateTime date = DateTime.ParseExact(fileNameSplit[1], "yyMMdd",null);
            fileDates.Add(date);
        }


        foreach (var value in fileNames)
        {
            foreach (var value1 in fileDates)
            {
                chart1.Series["US"].Points.AddXY(value1, value); 
            }
        }
DirectoryInfo dInfo=newdirectoryinfo(tbSelectFolder.Text);
FileInfo[]Files=dInfo.GetFiles(“*.ts”);
列表文件名=新列表();
List fileDates=新列表();
chart1.ChartAreas[0]。AxisX.MajorGrid.LineColor=Color.White;
chart1.ChartAreas[0]。AxisY.MajorGrid.LineColor=Color.White;
chart1.ChartAreas[0]。AxisX.MajorGrid.LineDashStyle=ChartDashStyle.Solid;
chart1.ChartAreas[0]。AxisY.MajorGrid.LineDashStyle=ChartDashStyle.Dash;
chart1.ChartAreas[0]。AxisX.LabelStyle.ForeColor=Color.White;
chart1.ChartAreas[0]。AxisY.LabelStyle.ForeColor=Color.White;
chart1.ChartAreas[0].AxisX2.Enabled=AxisEnabled.True;
foreach(文件中的文件信息文件)
{
string filename=Path.GetFileNameWithoutExtension(file.Name);
字符串[]fileNameSplit=filename.Split(“”);
fileNames.Add(fileNameSplit[0]);
DateTime date=DateTime.ParseExact(fileNameSplit[1],“yyMMdd”,null);
添加(日期);
}
foreach(文件名中的var值)
{
foreach(fileDates中的var值1)
{
图表1.系列[“US”].点.添加XY(值1,值);
}
}
这基本上给了我这个

我试图创建的时间线基本上就像一个时间表。那么,有没有办法让它看起来像这样


这里有一个可能的解决方案:

//从干净的板岩中设置:
chart1.ChartAreas.Clear();
图1.Series.Clear();
ChartArea CA=chart1.ChartAreas.Add(“CA”);
S1系列=图表1.系列添加(“S1”);
S1.ChartType=系列ChartType.Column;//无论什么
//对我自己的文件有一些限制:
CA.axix.max=新日期时间(2014,12,31).ToOADate();
DirectoryInfo dInfo=新的DirectoryInfo(“D:\\”);
FileInfo[]Files=dInfo.GetFiles(“f*.png”);
//继续使用文件信息列表!
//列表文件名=新列表();
//List fileDates=新列表();
chart1.ChartAreas[0]。AxisX.MajorGrid.LineColor=Color.White;
chart1.ChartAreas[0]。AxisY.MajorGrid.LineColor=Color.White;
chart1.ChartAreas[0]。AxisX.MajorGrid.LineDashStyle=ChartDashStyle.Solid;
chart1.ChartAreas[0]。AxisY.MajorGrid.LineDashStyle=ChartDashStyle.Dash;
chart1.ChartAreas[0]。AxisX.LabelStyle.ForeColor=Color.White;
chart1.ChartAreas[0]。AxisY.LabelStyle.ForeColor=Color.White;
chart1.ChartAreas[0].AxisX2.Enabled=AxisEnabled.True;
S1.IsValueShownAsLabel=true;
S1.LabelFormat=“YYY.MM”;
//最多限制为20个文件:
for(inti=0;i
以下是一个可能的解决方案:

//从干净的板岩中设置:
chart1.ChartAreas.Clear();
图1.Series.Clear();
ChartArea CA=chart1.ChartAreas.Add(“CA”);
S1系列=图表1.系列添加(“S1”);
S1.ChartType=系列ChartType.Column;//无论什么
//对我自己的文件有一些限制:
CA.axix.max=新日期时间(2014,12,31).ToOADate();
DirectoryInfo dInfo=新的DirectoryInfo(“D:\\”);
FileInfo[]Files=dInfo.GetFiles(“f*.png”);
//继续使用文件信息列表!
//列表文件名=新列表();
//List fileDates=新列表();
chart1.ChartAreas[0]。AxisX.MajorGrid.LineColor=Color.White;
chart1.ChartAreas[0]。AxisY.MajorGrid.LineColor=Color.White;
chart1.ChartAreas[0]。AxisX.MajorGrid.LineDashStyle=ChartDashStyle.Solid;
chart1.ChartAreas[0]。AxisY.MajorGrid.LineDashStyle=ChartDashStyle.Dash;
chart1.ChartAreas[0]。AxisX.LabelStyle.ForeColor=Color.White;
chart1.ChartAreas[0]。AxisY.LabelStyle.ForeColor=Color.White;
chart1.ChartAreas[0].AxisX2.Enabled=AxisEnabled.True;
S1.IsValueShownAsLabel=true;
S1.LabelFormat=“YYY.MM”;
//最多限制为20个文件:
for(inti=0;i
我希望这符合您的需要:不幸的是,axis标签并不完美,这就是为什么您可以通过取消前三行代码的注释来完全删除它们

//Just pass your list of dates to this function
private void DrawTimeline(List<DateTime> dates)
{

    //chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Black;
    //chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
    //chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
    chart1.ChartAreas[0].AxisY.IsStartedFromZero = false;

    //initialize a legend with some settings
    chart1.Legends.Clear();
    chart1.Legends.Add("Timespans");
    chart1.Legends[0].LegendStyle = LegendStyle.Table;
    chart1.Legends[0].Docking = Docking.Bottom;
    chart1.Legends[0].Alignment = StringAlignment.Center;
    chart1.Legends[0].Title = "Timespans";
    chart1.Legends[0].BorderColor = Color.Black;

    chart1.Series.Clear();
    string seriesname;
    //adding the bars with some settings
    for (int i = 0; i < dates.Count-1; i++)
    {
        seriesname = Convert.ToString(dates[i].Date + " - " + dates[i + 1].Date);
        chart1.Series.Add(seriesname);
        chart1.Series[seriesname].ChartType = SeriesChartType.RangeBar;
        chart1.Series[seriesname].YValuesPerPoint = 2;
        chart1.Series[seriesname].Points.AddXY("Timeline", dates[i].Date, dates[i + 1].Date);
        chart1.Series[seriesname]["DrawSideBySide"] = "false";
        chart1.Series[seriesname].BorderColor = Color.Black;
        chart1.Series[seriesname].ToolTip = seriesname;
    }
}
//只需将日期列表传递给此函数
私有时间线(列出日期)
{
//chart1.ChartAreas[0]。AxisX.MajorGrid.LineColor=Color.Black;
//chart1.ChartAreas[0]。AxisY.MajorGrid.LineColor=Color.White;
//chart1.ChartAreas[0].AxisX.LabelStyle.Enabled=false;
chart1.ChartAreas[0].AxisY.IsStartedFromZero=false;
//使用一些设置初始化图例
图表1.图例.清除();
图表1.图例。添加(“时间跨度”);
图表1.图例[0]。LegendStyle=LegendStyle.Table;
图表1.图例[0]。停靠=停靠。底部;
图表1.图例[0]。对齐方式=StringAlignment.Center;
图表1.图例[0]。Title=“Timespans”;
图表1.图例[0]。边框颜色=颜色。黑色;
图1.Series.Clear();
字符串序列名;
//添加带有一些设置的条形图
对于(int i=0;i//Just pass your list of dates to this function
private void DrawTimeline(List<DateTime> dates)
{

    //chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Black;
    //chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
    //chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
    chart1.ChartAreas[0].AxisY.IsStartedFromZero = false;

    //initialize a legend with some settings
    chart1.Legends.Clear();
    chart1.Legends.Add("Timespans");
    chart1.Legends[0].LegendStyle = LegendStyle.Table;
    chart1.Legends[0].Docking = Docking.Bottom;
    chart1.Legends[0].Alignment = StringAlignment.Center;
    chart1.Legends[0].Title = "Timespans";
    chart1.Legends[0].BorderColor = Color.Black;

    chart1.Series.Clear();
    string seriesname;
    //adding the bars with some settings
    for (int i = 0; i < dates.Count-1; i++)
    {
        seriesname = Convert.ToString(dates[i].Date + " - " + dates[i + 1].Date);
        chart1.Series.Add(seriesname);
        chart1.Series[seriesname].ChartType = SeriesChartType.RangeBar;
        chart1.Series[seriesname].YValuesPerPoint = 2;
        chart1.Series[seriesname].Points.AddXY("Timeline", dates[i].Date, dates[i + 1].Date);
        chart1.Series[seriesname]["DrawSideBySide"] = "false";
        chart1.Series[seriesname].BorderColor = Color.Black;
        chart1.Series[seriesname].ToolTip = seriesname;
    }
}