假设您要创建一个类似于我的示例here 或here 的Heatmap,恐怕为此目的使用Chart 控件可能不是最佳选择。
虽然使用Charttype Point 来创建它并非不可能,但它会有几个问题..
您询问了DataBinding 以使整个事情更有效率。
您正在使用DataPoints 的列表,但是:
-
DataBinding 只是将值绑定到图表上,不是现成的 DataPoints
- 即使是值列表也有其limitations:
当使用列表或数组等非表格数据源时,您可以
仅绑定 Y 值,而不管数据绑定方法的类型
用过的。这是因为不能为 X 值指定列,并且
其他图表属性,例如 Tooltip。
如果您的 X 值不重要,这可能不是什么大问题。
嗯,many ways 可以在 Chart 和 Chart 级别使用 DataBinding。
甚至还有一个Points.DataBind 重载looks 就好像它适合绑定颜色一样支持扩展属性:
Points.DataBind
同上,加:
支持绑定扩展图表属性,如工具提示。
所以绑定到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;
应该像这样工作:
someSeries.Points.DataBind(DV, "xField", "yFields",
"MarkerColor=kolor,Color=kolor,Tooltip=tipp,Label=tipp");
然而,虽然Labels 和ToolTips 确实被绑定了,但DataPoint.Color 却没有:
这令人失望;毕竟DataPoint.Color 是bindable 属性。但它会被忽略。
Here 是支持的属性列表:
这些属性的列表如下:AxisLabel、Tooltip、Label、
LegendText、LegendTooltip 和 CustomPropertyName(一个
自定义属性)。
结论:Afaik DataBinding不会让你设置colored DataPoints。要使用Chart 控件使您的代码更有效,您可以简单地尝试使用chart1.SuspendLayout 和chart1.ResumeLayout 使设置全部完成。
不过,我会首先考虑不使用图表控件。
我在第一段中给出的两个示例的链接显示了两种替代方式:
第一个是关于在 GDI+ 中绘制热图的。这真的很简单而且非常有效。 (帖子中的详细信息可能与您的问题无关。)对于简单的缩放,我建议您将其绘制成Bitmap,然后将其分配给Panel 或PictureBox;在ClientSize 中创建它并将Panel.ImageLayout(或PictureBox.SizeMode)设置为Stretch。
第二个示例使用DataGridView 的Cells 作为热图的大“像素”...
查看第二个链接,了解创建漂亮List<Color> 的方法!