Beispiel #1
0
            /// <summary>
            /// 设置图例对象的UI格式:图例方框的线型、背景、位置与大小等
            /// </summary>
            /// <param name="chart">图例对象所属的Chart对象</param>
            /// <param name="legend">要进行UI格式设置的图例对象</param>
            /// <remarks></remarks>
            private void SetLegendFormat(Excel.Chart chart, Excel.Legend legend)
            {
                //开始设置图例的格式
                //1、设置图例的格式
                legend.Format.Fill.Visible       = Office.MsoTriState.msoTrue;
                legend.Format.Fill.ForeColor.RGB = Information.RGB(255, 255, 255);                  //图例对象的背景色
                legend.Format.Shadow.Type        = Microsoft.Office.Core.MsoShadowType.msoShadow21; //图例的阴影
                                                                                                    //.Line.ForeColor.RGB = RGB(0, 0, 0)      ' 图例对象的边框
                legend.Format.TextFrame2.TextRange.Font.Name = AMEApplication.FontName_TNR;
                //2、设置图例的位置与大小
                //图例方框的高度
                float ExpectedHeight = System.Convert.ToSingle(this.Legend_Location.Legend_Height); // Data_Drawing_Format.Drawing_Mnt_RollingBase.Legend_Height

                legend.Select();
                //对于Chart中的图例的位置与大小,其设置的原则是:
                //对于Top值,其始终不改变图例的大小,而将图例对象一直向下推进,直到图例对象的下边缘到达Chart的下边缘为止;
                //对于Height值,其始终不改变图例的Top的位置,而将图例对象的下边缘一直向下拉伸,直到图例对象的下边缘到达Chart的下边缘为止;
                //所以,如果要将图例对象成功地定位到Chart底部,应该先设置其Height值为0,然后设置其Top值,最后再回过来设置其Height值。
                var selection = legend.Application.Selection  as Excel.Range;

                selection.Left   = 0;
                selection.Width  = this.Legend_Location.Legend_Width;
                selection.Height = 0; //以此来让Selection的Top值成功定位
                selection.Top    = chart.ChartArea.Height - ExpectedHeight;
                selection.Height = ExpectedHeight;
            }
Beispiel #2
0
            /// <summary>
            /// 更新图例,图例的绝对尺寸的更新一定要在设置要Chart的尺寸之后,
            /// 因为如果在设置好图例尺寸后再设置Chart尺寸,则图例尺寸会进行缩放。
            /// </summary>
            /// <param name="lst"></param>
            /// <remarks></remarks>
            protected void LegendRefresh(List <bool> lst)
            {
                Excel.Legend        lgd        = default(Excel.Legend);
                Excel.LegendEntries lgdEntries = default(Excel.LegendEntries);
                Excel.LegendEntry   lgdEnrty   = default(Excel.LegendEntry);
                this.Chart.HasLegend = false;
                this.Chart.HasLegend = true;
                lgd        = this.Chart.Legend;
                lgdEntries = lgd.LegendEntries() as Excel.LegendEntries;

                //一定要注意,这里对图例项进行删除的时候,要从尾部开始向开头位置倒着删除:
                //因为集合的Index的索引方式, 当元素被删除后, 其后面的元素就接替了这个元素的下标值
                for (int LegendIndex =
                         F_List_HasCurve.Count - 1 + cst_LboundOfSeriesInCollection; LegendIndex >= cst_LboundOfSeriesInCollection; LegendIndex--)
                {
                    bool hascurve = System.Convert.ToBoolean(F_List_HasCurve[LegendIndex - cst_LboundOfSeriesInCollection]);
                    if (!hascurve)
                    {
                        //在Visual Basic.NET中,如果用LegendEntries(Index)来索引LegendEntry对象,其第一个元素的下标值为0,
                        //而如果用LegendEntries.Item(Index)的方式来索引集合中的LegendEntry对象,则其第一个元素的下标值为1。
                        //而在VBA中,这两种方式索引集合中的LegendEntry对象,其第一个元素的下标值都是1;
                        lgdEnrty = lgdEntries.Item(LegendIndex);
                        lgdEnrty.Delete();
                    }
                }
                // 设置图例对象的位置与尺寸
                this.SetLegendFormat(this.Chart, lgd);
                this.Sheet_Drawing.Range("A1").Activate();
            }