How to control winform mschart legend text alignment c#?

后端 未结 3 854
误落风尘
误落风尘 2021-01-21 19:06

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

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


        
相关标签:
3条回答
  • 2021-01-21 19:48

    My understanding is that Legend alignment relates to the Docking property, not really how the text is aligned within the legend. So setting Alignment to Near means positioning the legend box near the Docking direction.

    It is quite hard to explain this textually. The MS Chart Samples have a subsection named Chart features -> Legends -> Style and Auto Positioning which will help you play with those two properties and understand how they are meant to work.

    For inner legend alignment, you may need to use Legend.CustomItems and define LegendCell individually.

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

    This is described inside the Chart features -> Legends -> Legend Cells section of the samples.

    0 讨论(0)
  • 2021-01-21 20:05

    While continuing to try to figure this out I stumbled on this tidbit of info from the following MSDN library page:

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

    "NOTE: You cannot adjust the individual legend items and cells in the Chart.Legends collection. To adjust them, use custom legend items."

    So, back to the CustomItem route. I've got this code gleaned from several sources (including you, Dominique, thanks):

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

    From what I can find it should work, but it doesn't. It results in a legend object with two cells; the first is empty and the second has left-justified text. I tried to post an image of it, but the system says I'm not yet worthy.

    Why there is no line for the series symbol is also a mystery, but that's another problem to solve. The text justification issue is my main concern at the moment. It looks like the text within the cells is left-justified as desired, but the cells themselves are centered in the legend object; not what I wanted. I want those cells to be left-justified in the object too, so the first cell is against the left edge of the legend object.

    Ideas?

    0 讨论(0)
  • 2021-01-21 20:06

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

    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.

    0 讨论(0)
提交回复
热议问题