【问题标题】:How to create a timeline chart control thing?如何创建时间线图表控件?
【发布时间】:2015-11-26 13:24:01
【问题描述】:

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

我知道之前有类似的问题等等。有人问了

如何创建时间线控件?

就像 3 年前一样,但我不确定他们在答案和 cmets 中到底在说什么..

我当前的代码是:

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

这基本上给了我这个

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

【问题讨论】:

  • 您的图表事物看起来像一个柱形图。您使用的是什么图表类型?
  • 我使用的是折线图或条形图。我正在考虑堆叠条形图,但我不知道该怎么做,因为我似乎无法自动添加系列。虽然很可能通过系列添加 series = new Series();我想?
  • 您可能希望将您想要的图像稍微改善一下以使其更清晰。在我看来,它当然根本不像一个时间表。显示的零件是否必须具有不同的尺寸?这些数字是多少?日期??
  • 对不起,因为我没有我想要的实际图像,所以我使用了 Word。这张图是不是更清晰了?
  • 所以你想列出文件名和日期?创建日期或您可以从 FileInfo 类获得的其他日期?还是从文件名?好吧,这当然是可行的,但是您是否考虑过这样的时间表将占用的空间?您可以显示的内容是有限的(32k 像素),用户甚至不会喜欢它..也许垂直列表会更好..?或者预计会显示多少个文件名?

标签: c# winforms charts filenames timeline


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多