【问题标题】:Chart doesn't show Array Values图表不显示数组值
【发布时间】:2014-04-08 13:23:01
【问题描述】:

我有一个保存用户统计信息的文件。该文件有 2 行。我有一个示例文件,其中包含以下内容“23\n48”。 我将其加载到字符串数组中并将其返回到我的图表的数据源。 一旦我让它工作,但现在在我更改一些属性并按 ctrl+z 取消更改后它不显示任何内容,但我确信源代码没有任何改变。 现在它保持空白,就像它的 DataSource 为 NULL 一样。 我删除了图表并再次对其进行了初始化,除了将 chartType 设置为“pie”外,没有更改其属性上的任何内容,但没有帮助。我用调试器检查了图表的源代码,它有文件的值。但我不明白为什么在搜索了几个小时后它不再显示我了。我知道它适用于数组,因为我做过一次,所以请不要告诉我我也可以尝试使用其他类型,如 DataTable。我现在有点好奇:P

这是我输入的内容:

chart1.DataSource = File.ReadAllLines("Stats/" + sender.toString());

方法 ReadAllLines 返回带有值的字符串数组。我也尝试过将其转换并放入一个 int 数组,但仍然没有结果:(

我希望我就我的问题提供了足够的信息。 对不起,如果我的英语不好。这不是我的主要语言。

哦,我使用 Visual Studio 2012 和 C# 作为语言

最好的问候(新手)ExSynth :)

编辑(已解决): 这是我的源代码的更大的 sn-p:

private void Form1_Load(object sender, EventArgs e)
{
    if (!Directory.Exists("Languages")) { Directory.CreateDirectory("Languages"); }
    if (!Directory.Exists("Stats")) { Directory.CreateDirectory("Stats"); }
    RefreshLanguages();
}

private void spracheHinzufügenToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (!Directory.Exists("Languages")) { Directory.CreateDirectory("Languages"); }
    Add_Language Add_Language = new Add_Language();
    Add_Language.ShowDialog();
    RefreshLanguages();
}

private void RefreshLanguages()
{
    int j = spracheAuswählenToolStripMenuItem.DropDownItems.Count;
    for (int i = 0; i < j; i++) { spracheAuswählenToolStripMenuItem.DropDownItems.RemoveAt(0); }

    string[] fileListWithPath = Directory.GetFiles("Languages");
    for (int i = 0; i < fileListWithPath.Length; i++)
    {
        ToolStripMenuItem item = new ToolStripMenuItem(System.IO.Path.GetFileName(fileListWithPath[i]));
        item.Click += new System.EventHandler(this.Abfragen);
        spracheAuswählenToolStripMenuItem.DropDownItems.Add(item);
    }
}

private void Abfragen(object sender, EventArgs e)
{
    lbl_Language.Text = sender.ToString();
    chart1.DataSource = File.ReadAllLines("Stats/" + sender.ToString());
    chart1.DataBind();
}

编辑:

我解决了我的问题。我在互联网上找不到任何有用的东西,所以我查看了我的旧源代码并找到了这个:

    chart_History.DataSource = table; // sets the datasource of the chart to the datatable which was filled above
    chart_History.Series[0].XValueMember = table.Columns["index"].ToString();  // sets the datasource of the X-Axis of the chart
    chart_History.Series[0].YValueMembers = table.Columns[Convert.ToString(cmbBox_List.SelectedIndex + 1)].ToString(); // sets the datasource of the Y-Axis of the chart
    chart_History.Series[0].LegendText = cmbBox_List.SelectedItem.ToString(); // Sets the string of the legend to the selected item of the combobox
    chart_History.DataBind(); // Finally binds the Information to the chart

显然我必须声明 X 和 Y 轴... 所以我按CTRL + Z错误地删除了一些东西...... 但我想我会改用 DataTable。数组太乱了。 谢谢你的帮助。 我希望这会在未来对其他人有所帮助:)

【问题讨论】:

  • 这是 Microsoft 图表控件吗?举例说明 File.ReadAllLines("Stats/" + sender.toString()) 返回的内容。
  • @sarin 调试器显示如下: chart1.DataSource[0] = 23 (string) chart1.DataSource[1] = 48 (string) 这些值是正确的。它们位于文件中,并已正确返回到 DataSource。是的,我认为这是 Microsoft 图表控件,因为我使用来自 Microsoft 的 Visual Studio 2012。我不导入任何外部控件或类。

标签: c# arrays charts datasource output


【解决方案1】:

尝试在后面添加以下内容?:

chart1.DataBind();

这是一个旧帖子,但它可能会有所帮助:Data Binding Microsoft Chart Control

【讨论】:

  • 我已经尝试过 DataBind()。还是不行。我去看看那个链接。
  • 我编辑了我的第一篇文章。在那里观看 :) 感谢您的帮助。抱歉,我用了德语单词,没有发表任何评论。这只是我自己的一个项目,所以我不太在意......
  • 我解决了。有关解决方案,请参阅我的第一篇文章。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
相关资源
最近更新 更多