如何创建时间表图表控件?

时间:2015-11-26 13:24:02

标签: c# winforms charts filenames timeline

我正在尝试创建时间轴,并一直在尝试使用图表控件。但是它没有用,因为我只需要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); 
            }
        }

这基本上给了我这个

Lame Looking Chart

我正在尝试创建的时间轴基本上就像一张时间表。那么,有没有办法让它看起来像这样

What i Want

2 个答案:

答案 0 :(得分:0)

这是一个可能的解决方案:

enter image description here

    // set up from clean slate:
    chart1.ChartAreas.Clear();
    chart1.Series.Clear();
    ChartArea CA = chart1.ChartAreas.Add("CA");
    Series S1 = chart1.Series.Add("S1");
    S1.ChartType = SeriesChartType.Column;  // whatever..

    // a few restriction for my own files:
    CA.AxisX.Maximum = new DateTime(2014, 12, 31).ToOADate();
    DirectoryInfo dInfo = new DirectoryInfo("D:\\");
    FileInfo[] Files = dInfo.GetFiles("f*.png");

    // staying with the file info list!
    //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;

    S1.IsValueShownAsLabel = true;
    S1.LabelFormat = "YYY.MM";

    // restrict to 20 files max:
    for (int i = 0; i < Math.Min(20, Files.Length); i++)
    {
        FileInfo FI = Files[i];
        int p = chart1.Series[0].Points.AddXY(FI.CreationTime, 1);
        S1.Points[p].Label = Path.GetFileNameWithoutExtension(FI.FullName);
    }

答案 1 :(得分:0)

我希望这符合您的需求:遗憾的是,轴标签并不完美,这就是为什么您可以通过取消注释前三行代码来完全删除它们。

//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;
    }
}