如何控制的winform的MSChart图例文本对齐C#?图例、文本、winform、MSChart

2023-09-11 07:41:22 作者:吧唧一口老血

如何一组文本图表图例对象中的定位?我使用的尝试:

How does one set the alignment of text within a chart legend object? I've tried using:

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

使用没有任何影响。我也试着创建自定义图例项,再次造成任何影响。文本始终是(伴随着一系列的标记)的传说盒子为中心。我已经能够对准唯一的文字标题,但我并不在我的应用程序需要的标题。

With no effect. I've also tried to create custom legend items, again resulting in no effect. Text is ALWAYS centered (along with the series marker) in the legend "box". The only text I have been able to align is the title, but I don't need titles in my application.

我的研究至今说,传说中的对象基本上是与(默认)两个单元格的表。如果是这样的话应该有一种方法来访问那些细胞和操纵它们作为表格单元。那么,是什么原因呢?为什么我不能访问的传奇对象的文本对齐属性?显然,有什么我失踪,但对我的生活中,我似乎无法想出解决办法。相当令人沮丧。

My research to date says the legend object is basically a table with (by default) two cells. If that is the case there should be a way to access those cells and manipulate them as table cells. So, what gives? Why can't I access the text alignment properties of the legend object? Clearly, there is something I'm missing, but for the life of me I can't seem to figure this out. Quite frustrating.

推荐答案

问题解决了。定制项目的做法是行不通的两种,所以我尝试使用LegendCellColumn类。

Problem solved. The CustomItem approach wasn't working either, so I tried using the LegendCellColumn Class.

我改变了LegendStyle从列到行,然后添加了两个CellColumns,一为系列代号,一个是传说中的文字。设置对齐,边距和列的宽度(即原来是招),瞧;一个传说,看起来像我想要的。这里的code的人有类似的问题。

I changed the LegendStyle from Column to Row, then added two CellColumns, one for the series symbol and one for the legend text. Set the alignment, margins, and column widths (that turned out to be the trick), and voila; a legend that looks like I want. Here's the code for anyone with a similar issue.

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;

这可能不是最有效的方式来做到这一点,但它的作品。从技术上讲,图例符号和文本仍然集中在对象,但因为我迫使两列的宽度也有被左对齐的外观。

It's probably not the most efficient way to do it, but it works. Technically, the legend symbol and text are still centered in the object, but because I'm forcing the widths of the two columns it has the appearance of being left-justified.

希望这可以帮助像我这样的新手又避免恐慌的日子。

Hopefully, this may help another newbie like me avoid days of consternation.