gpt4 book ai didi

c# - 设置图表以从类列表中动态绘制点

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:18 25 4
gpt4 key购买 nike

所以我有一个类 List <Entry>看起来像这样:

namespace WindowsFormsApplication4.Models
{
public class Entry
{
public string Segment { get; set; }
public int Time { get; set; }
public double Speed { get; set; }
}
}

填充的列表有大约 70 个列表项。我在将列表作为图表的来源时遇到了问题。我希望时间值是 X 轴上的标记,速度值是所述 X 标记上的点。图片说明:

enter image description here

我已经尝试过发布 Here 的解决方案,没有错误,但我的图表只显示一个空白区域。 Form1中的代码是:

        chart1 = new Chart();

chart1.DataSource = list;

chart1.Series.Add("Speed").YValueMembers = "Speed";
chart1.Series["Speed"].ChartType = SeriesChartType.Bar;
chart1.Series["Speed"].XValueType = ChartValueType.Double;
chart1.Series["Speed"].YValueType = ChartValueType.Double;

任何建议如何将列表绑定(bind)为图表的数据源?关于该主题的论坛相当稀少。

最佳答案

试试这个:

    private void Form1_Load(object sender, EventArgs e)
{
list = new List<Entry>
{
new Entry {Time = 1, Speed = 80, Segment = "Seg 1" },
new Entry {Time = 2, Speed = 40, Segment = "Seg 2" },
new Entry {Time = 3, Speed = 100, Segment = "Seg 3" },
new Entry {Time = 4, Speed = 20, Segment = "Seg 4" },
new Entry {Time = 5, Speed = 60, Segment = "Seg 5" },
};

chart1 = new Chart();
chart1.Dock = DockStyle.Fill;
chart1.ChartAreas.Add("ChartArea1");

chart1.Series.Add("Speed");
chart1.Series["Speed"].ChartType = SeriesChartType.Column;
chart1.Series["Speed"].XValueMember = "Time";
chart1.Series["Speed"].YValueMembers = "Speed";

chart1.DataSource = list;
chart1.DataBind();

Controls.Add(chart1);
}

enter image description here

编辑: 添加按钮 Click 事件:

    private void Form1_Load(object sender, EventArgs e)
{
chart1 = new Chart();
chart1.Dock = DockStyle.Fill;
chart1.ChartAreas.Add("ChartArea1");

chart1.Series.Add("Speed");
chart1.Series["Speed"].ChartType = SeriesChartType.Column;
chart1.Series["Speed"].XValueMember = "Time";
chart1.Series["Speed"].YValueMembers = "Speed";

list = new List<Entry>();

chart1.DataSource = list;

panel1.Controls.Add(chart1);
}

private void button1_Click(object sender, EventArgs e)
{
list.Add(new Entry { Time = i, Speed = i * 10, Segment = i.ToString() });
chart1.DataBind();

i++;
}

关于c# - 设置图表以从类列表中动态绘制点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37534215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com