1

I'm trying to learn the mschart control. I want to create a histogram chart that has a stat panel to the right that displays statistics about the chart. I tried doing this by creating a Legend object docked to the right. Example graph

I would like the boxes to stack vertically to the right of the chart. Currently each box represents a Legend object in the LegendsCollection.

Code:

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

2 Answers 2

0

You can try this:

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

chart.Legends[component].Docking = Docking.Right;
1
  • Please supply explanation with your answer Jan 29, 2014 at 18:21
0

You can set the Position explicitly, referring to the Position of the 1st 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);

Note that all parts of an ElementPosition are given in percentage of the Element's container! So I added a 1% gap..

Setting a suitable Height is somewhat tricky ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.