将列表的列表绑定到图表控件 [英] Binding list of lists to chart control

查看:186
本文介绍了将列表的列表绑定到图表控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在工作的窗体应用程序生成数据点的热图。我使用一个名为Pipes的自定义结构列表,其中包含自己的数据点列表。然后我遍历我的列表列表,如下所示:

  foreach(NewPipe pipe in wall)
{
foreach(DataPoint x in pipe.returnPoints(xNum))
{
chart1.Series [Series1]。Points.Add(x);
}
xNum ++;
}

虽然这样工作,似乎效率低下。如果我只是更新列表列表和图表更新,而不是必须清除它和重新图表(或手动删除删除的数据等),这将是很好。我熟悉BindingList结构,这是正确的工具使用,如果是这样,你如何将列表中的列表绑定到图表? (因为我真的在数据存储在下层列表中的列表之后)。也许我使用错误的结构完全? (虽然列表的列表实际上与数据的生成方式相当匹配)。

解决方案

假设您想创建< =https://en.wikipedia.org/wiki/Heat_map =nofollow noreferrer>热图,看起来像我的例子



这是令人失望的;毕竟 DataPoint.Color 是一个 bindable 属性。但它被忽略。



此处是支持的属性列表:


这些属性的列表如下:AxisLabel,Tooltip,Label,
LegendText,LegendTooltip和CustomPropertyName(
自定义属性的名称)。


结论:Afaik DataBinding 允许您设置彩色 DataPoints 。要使用图表控制使代码更有效,您只需尝试使用 chart1.SuspendLayout chart1.ResumeLayout ,以使所有设置一体化。





我在第一段给出的两个示例的链接显示了两种替代方法:




  • 第一个是关于在GDI +中绘制热图。这是非常简单和非常高效。对于简单的缩放,我建议绘制到一个位图,它分配给一个面板 PictureBox ;在 ClientSize 中创建它并设置 Panel.ImageLayout (或 PictureBox.SizeMode


  • 第二个示例使用 c> c> c>作为大图像的




查看第二个链接,了解如何创建一个漂亮的列表< Color>


I'm working a windows form application that generates a heat map of data points. I'm using a list of custom structures called "Pipes" that contain their own lists of data points. I then iterate over my list of lists as shown here:

foreach (NewPipe pipe in wall)
{
    foreach (DataPoint x in pipe.returnPoints(xNum))
    {
       chart1.Series["Series1"].Points.Add(x);
    }
    xNum++;
}

While this works, it seems horribly inefficient. Also it would be nice if I could just update my list of lists and have the graph update, instead of having to clear it and re-graph it (or manually delete the removed data and etc). I'm familiar with the BindingList structure, is this the right tool to use and if so, how do you bind a list within a list to a chart? (Since I'm really after the data stored in a list in the lower tier list). Maybe I'm using the wrong structures entirely? (Though lists of lists actually matches how the data is generated fairly well).

解决方案

Assuming you want to create a Heatmap which looks something like my examples here or here I'm afraid that using a Chart Control for this purpose may not be the best choice.

While it is not impossible to use a Charttype Point to create it, it will have several problems..

  • Most notable is the fact that you will have to create individual DataPoints for each point in the map.

  • These are rather expensive

  • They will not resize with the chart. You can set their size by setting the Series.MarkerSize to a suitable number of pixels but you will have to adapt when you resize.
  • One further limitation of this is that the markers all are squares, so it will be hard to create a gapless chart..

You have asked about DataBinding to make the whole thing more efficient.

You are using lists of list of DataPoints but:

  • DataBinding only binds values to the chart, not ready-made DataPoints
  • Even lists of values have their limitations:

When using non-tabular data sources such as lists, or arrays, you can bind only Y values, regardless of the type of data-binding method used. This is because columns cannot be specified for X values and other chart properties, such as Tooltip.

This may not be a big problem if your X-Values are not important.

Well, there are many ways to use DataBinding with a Chart, both at the Chart and at the Series levels.

And there is even one Points.DataBind overload that looks as if it would be suitable to bind Colors as is supports extended properties:

Points.DataBind

Same as the above, plus:

Supports binding for extended chart properties like tooltips.

So the binding to a DataView

DataTable DT = new DataTable("data");

DT.Columns.Add("xField", typeof(int));
DT.Columns.Add("yFields", typeof(int));
DT.Columns.Add("tipp", typeof(string));
DT.Columns.Add("kolor", typeof(Color));

DataRow row = DT.NewRow();
row["xField"] = 1; row["yFields"] = 1; row["tipp"] = "red"; row["kolor"] = Color.Red;
DT.Rows.Add(row); // ...etc...

DataView DV = new DataView(DT);
chart1.DataSource = DV;

should work like this:

someSeries.Points.DataBind(DV, "xField", "yFields",
                           "MarkerColor=kolor,Color=kolor,Tooltip=tipp,Label=tipp");

However, while the Labels and ToolTips do indeed get bound, the DataPoint.Color does not:

This is disappointing; after all the DataPoint.Color is a bindable attribute. But it gets ignored.

Here is a list of supported properties:

A list of these properties are as follows: AxisLabel, Tooltip, Label, LegendText, LegendTooltip and CustomPropertyName (the name of a custom property).

Conclusion: Afaik DataBinding will not let you set colored DataPoints. To make your code more effective using a Chart control, you can simply try using chart1.SuspendLayout and chart1.ResumeLayout to make the setup happen all in one.

However I would instead consider not using a Chart control in the first place.

The links to the two examples I gave in the first paragraph show two alternative ways:

  • The first one is all about drawing the heatmap in GDI+. This is really trivial and very efficient. (The details in the post are probebly not pertinent to your problem..) For simple scaling I suggest drawing into a Bitmap, which you assign to a Panel or PictureBox; create it in the ClientSize and set the Panel.ImageLayout (or the PictureBox.SizeMode) as Stretch.

  • The second example uses the Cells of a DataGridView as large 'pixels' for the heatmap...

See the second link for a method to create a nice List<Color>!

这篇关于将列表的列表绑定到图表控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆