1

我正在尝试学习 mschart 控件。我想创建一个直方图,右侧有一个统计面板,显示有关图表的统计信息。我尝试通过创建一个停靠在右侧的 Legend 对象来做到这一点。示例图

我希望这些框垂直堆叠在图表的右侧。目前,每个框都代表 LegendsCollection 中的一个 Legend 对象。

代码:

 private void CreateStatPanel( Chart chart ) {
         var legend = new Legend
            {
               Title = "Basic Stats",
               TitleAlignment = StringAlignment.Near,
               TitleBackColor = Color.LightGray,
               Docking = Docking.Right,
               BorderColor = Color.LightGray,
               BorderWidth = 1,
               BorderDashStyle = ChartDashStyle.Solid,
            };

         var item = new LegendItem();

         var column = new LegendCell
            {
               CellType = LegendCellType.Text,
               BackColor = Color.White,
               ForeColor = Color.Black,
               Text = "54 Data Values ",
               Alignment = ContentAlignment.MiddleRight
            };
         var item2 = new LegendItem();
         var column2 = new LegendCell
            {
               CellType = LegendCellType.Text,
               BackColor = Color.White,
               ForeColor = Color.Black,
               Text = "Maximum \t 14",
               Alignment = ContentAlignment.MiddleRight
            };
         var box = new Legend
            {
               Title = "Subgroup Stats",
               TitleAlignment = StringAlignment.Near,
               TitleBackColor = Color.LightGray,
               BorderColor = Color.LightGray,
               BorderWidth = 1,
               BorderDashStyle = ChartDashStyle.Solid
            };
         var boxRowOne = new LegendItem();
         var boxRowCell = new LegendCell
            {
               CellType = LegendCellType.Text,
               BackColor = Color.White,
               ForeColor = Color.Black,
               Text = "n=6",
               Alignment = ContentAlignment.MiddleRight
            };
         var boxRowTwo = new LegendItem();
         var boxRowTwoCell = new LegendCell
            {
               CellType = LegendCellType.Text,
               BackColor = Color.White,
               ForeColor = Color.Black,
               Text = "Estimated Sigma",
               Alignment = ContentAlignment.MiddleLeft
            };
         var boxRowTwoCellTwo = new LegendCell
            {
                  CellType = LegendCellType.Text,
               BackColor = Color.White,
               ForeColor = Color.Black,
               Text = "1.82",
               Alignment = ContentAlignment.MiddleLeft

            };

         item.Cells.Add( column );
         item2.Cells.Add( column2 );
         boxRowOne.Cells.Add(boxRowCell);
         boxRowTwo.Cells.Add(boxRowTwoCell);
         boxRowTwo.Cells.Add(boxRowTwoCellTwo);
         box.CustomItems.Add(boxRowOne);
         box.CustomItems.Add(boxRowTwo);
         legend.CustomItems.Add( item );
         legend.CustomItems.Add( item2 );
         chart.Legends.Add( legend );
         chart.Legends.Add( box );
         chart.Series[ 0 ].IsVisibleInLegend = false;
      }
4

2 回答 2

0

你可以试试这个:

chart.Legends[component].LegendStyle = LegendStyle.Column;

chart.Legends[component].Docking = Docking.Right;
于 2014-01-29T18:01:16.967 回答
0

您可以Position明确设置,参考Position1st Legend

Legend L1 = chart10.Legends[0];
Legend L2 = new Legend("Legend Two");
L2.CustomItems.Add(new LegendItem("Legens Item 2.1", Color.Fuchsia, ""));

L2.Position = new ElementPosition(L1.Position.X, L1.Position.Bottom + 1, 
                                  L1.Position.Width, L1.Position.Height);

请注意,an 的所有部分都以元素容器的百分比ElementPosition给出!所以我加了一个差距..1%

设置一个合适Height的有点棘手;-)

于 2016-08-27T12:43:02.410 回答