2

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

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

没有效果。我还尝试创建自定义图例项,再次导致无效。文本始终在图例“框”中居中(与系列标记一起)。我能够对齐的唯一文本是标题,但我的应用程序中不需要标题。

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

4

3 回答 3

2

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

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

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;

这可能不是最有效的方法,但它确实有效。从技术上讲,图例符号和文本仍然在对象中居中,但是因为我强制调整两列的宽度,所以它看起来像是左对齐的。

希望这可以帮助像我这样的另一个新手避免惊慌失措的日子。

于 2012-10-17T19:09:23.050 回答
0

我的理解是Legend对齐与Docking属性有关,而不是文本在图例中的对齐方式。所以设置Alignment为 Near 意味着将图例框定位在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在示例部分中进行了描述。

于 2012-10-16T15:30:37.540 回答
0

在继续尝试弄清楚这一点时,我偶然发现了来自以下 MSDN 库页面的这条信息:

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

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

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

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);

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

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

想法?

于 2012-10-16T22:53:34.313 回答