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

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

问题描述

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

foreach (NewPipe 管壁){foreach(管道中的数据点 x.returnPoints(xNum)){chart1.Series["Series1"].Points.Add(x);}xNum++;}

虽然这行得通,但似乎效率极低.另外,如果我可以更新我的列表列表并更新图表,而不是清除它并重新绘制它(或手动删除已删除的数据等),那也会很好.我熟悉 BindingList 结构,这是使用正确的工具吗?如果是,您如何将列表中的列表绑定到图表?(因为我确实在将数据存储在较低层列表中的列表中).也许我完全使用了错误的结构?(尽管列表列表实际上与数据的生成方式相当匹配).

解决方案

假设你想创建一个

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

这里 是支持的属性列表:

<块引用>

这些属性的列表如下:AxisLabel、Tooltip、Label、LegendText、LegendTooltip 和 CustomPropertyName(自定义属性).

结论:Afaik DataBinding让您设置彩色 DataPoints.为了使用 Chart 控件使您的代码更有效,您可以简单地尝试使用 chart1.SuspendLayoutchart1.ResumeLayout 使设置全部发生合二为一.

不过,我首先会考虑不使用 Chart 控件.

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

  • 第一个是关于在 GDI+ 中绘制热图.这真的很简单而且非常有效.(帖子中的细节可能与您的问题无关......)为了简单的缩放,我建议绘制到 Bitmap 中,您将其分配给 Panel图片框;在 ClientSize 中创建它并将 Panel.ImageLayout(或 PictureBox.SizeMode)设置为 Stretch.

  • 第二个示例使用 DataGridViewCells 作为热图的大像素"...

有关创建漂亮List的方法,请参阅第二个链接!

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天全站免登陆