如何控制winform mschart图例文本对齐c#?

如何设置图表图例对象中文本的对齐方式? 我尝试过使用:

myChartName.Legends["mySeriesName"].Alignment = stringAlignment.Near 

没有效果。 我也试图创建自定义图例项,再次没有效果。 文本总是在图例“框”中居中(与系列标记一起)。 我能够对齐的唯一文字是标题,但我的申请中不需要标题。

我迄今为止的研究表明,图例对象基本上是一个表(默认情况下)有两个单元格。 如果是这种情况,应该有一种方法来访问这些单元格并将它们作为表格单元格进行操作。 那么,是什么给出的? 为什么我无法访问图例对象的文本对齐属性? 很明显,有些东西我不知道,但对于我的生活,我似乎无法弄清楚这一点。 相当令人沮丧。

问题解决了。 CustomItem方法也不起作用,所以我尝试使用LegendCellColumn类。

我将LegendStyle从Column更改为Row,然后添加了两个CellColumns,一个用于系列符号,另一个用于图例文本。 设置对齐,边距和列宽(结果certificate是诀窍),瞧; 一个看起来像我想要的传奇。 这是具有类似问题的任何人的代码。

 chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.SeriesSymbol, "")); chartSel.Legends[ySeries.Name].CellColumns[0].Alignment = ContentAlignment.TopLeft; chartSel.Legends[ySeries.Name].CellColumns[0].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1); chartSel.Legends[ySeries.Name].CellColumns[0].MinimumWidth = 250; chartSel.Legends[ySeries.Name].CellColumns[0].MaximumWidth = 250; chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.Text, ySeries.Name)); chartSel.Legends[ySeries.Name].CellColumns[1].Alignment = ContentAlignment.MiddleLeft; chartSel.Legends[ySeries.Name].CellColumns[1].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1); chartSel.Legends[ySeries.Name].CellColumns[1].MinimumWidth = 1500; chartSel.Legends[ySeries.Name].CellColumns[1].MaximumWidth = 1500; 

它可能不是最有效的方法,但它确实有效。 从技术上讲,图例符号和文本仍然在对象中居中,但由于我强制两列的宽度,因此它具有左对齐的外观。

希望这可能有助于像我这样的新手避免几天惊愕。

我的理解是Legend对齐与Docking属性有关,而不是文本如何在图例中对齐。 因此,将“ Alignment ”设置为“近”意味着将图例框定位在“ Docking方向附近。

这在文本上很难解释。 MS Chart Samples有一个名为Chart features -> Legends -> Style and Auto Positioning的小节,它将帮助您使用这两个属性并了解它们的工作方式。

对于内部图例对齐,您可能需要使用Legend.CustomItems并单独定义LegendCell

 chart.Legends["Default"].CustomItems.Clear(); chart.Legends["Default"].CustomItems.Add(new LegendItem("LegendItem", Color.Red, "")); chart.Legends["Default"].CustomItems[0].Cells.Add(new LegendCell(LegendCellType.Text, "My text", ContentAlignment.MiddleLeft)); 

这在示例的Chart features -> Legends -> Legend Cells部分中进行了描述。

在继续尝试解决这个问题的同时,我偶然发现了以下MSDN库页面中的这些信息:

http://msdn.microsoft.com/en-us/library/dd456711(v=vs.100).aspx

“注意:您无法调整Chart.Legends集合中的各个图例项和单元格。要调整它们,请使用自定义图例项目。”

所以,回到CustomItem路线。 我从几个来源收集了这些代码(包括你,多米尼克,谢谢):

 chartSel.Legends.Add(ySeries.Name); chartSel.Series[ySeries.Name].IsVisibleInLegend = false; chartSel.Legends[ySeries.Name].CustomItems.Clear(); LegendItem li = new LegendItem(); li.ImageStyle = LegendImageStyle.Line; li.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.TopLeft); li.Cells[0].BackColor = Color.CornflowerBlue; //color is only to see the cell bounds li.Cells.Add(LegendCellType.Text, ySeries.Name, ContentAlignment.TopLeft); li.Cells[1].BackColor = Color.Aquamarine; //color is only to see the cell bounds chartSel.Legends[ySeries.Name].CustomItems.Add(li); 

从我能发现它应该工作,但事实并非如此。 它产生一个带有两个单元格的图例对象; 第一个是空的,第二个是左对齐的文本。 我试图发布它的图像,但系统说我还不值得。

为什么系列符号没有线也是一个谜,但这是另一个需要解决的问题。 文本理由问题是我目前关注的主要问题。 看起来单元格中的文本根据需要左对齐,但单元格本身在图例对象中居中; 不是我想要的。 我希望这些单元格在对象中也是左对齐的,因此第一个单元格对着图例对象的左边缘。

想法?