【问题标题】:How to control winform mschart legend text alignment c#?如何控制winform mschart图例文本对齐c#?
【发布时间】:2012-10-06 16:23:23
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# text alignment mschart legend


    【解决方案1】:

    我的理解是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 部分中进行了描述。

    【讨论】:

    • 感谢您的回复。在图表上定位图例对象不是问题。问题是将文本定位在图例对象中。默认情况下,图例文本显示在图例对象的中心。我希望能够左对齐文本。那是我似乎无法弄清楚的。
    • chartSel.Legends.Add(ySeries.Name);
    【解决方案2】:

    在继续尝试弄清楚这一点时,我偶然发现了来自以下 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);
    

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

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

    想法?

    【讨论】:

      【解决方案3】:

      问题解决了。 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;
      

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

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

      【讨论】:

        猜你喜欢
        • 2013-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 2021-11-20
        • 2011-04-18
        • 2017-03-28
        相关资源
        最近更新 更多