MSChart柱圖曲線圖等實現總結及封裝

來源:互聯網
上載者:User

.NET4.0已經包含了MsChart了,根據自己的使用簡單介紹下,希望對網友有協助

1、web.config配置

需要在<appSettings>中增加

    <!--分析圖片 參數設定-->
    <add key="ChartImageHandler" value="storage=file;timeout=20;dir=C:\Windows\temp;"/>

在<system.webServer>中增加
    <handlers>
      <remove name="ChartImageHandler"/>
      <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,POST,HEAD" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </handlers>
2、mschat實現封裝實現代碼

        /// <summary>
        /// 獲得繪圖物件
        /// </summary>
        /// <param name="page">頁面執行個體</param>
        /// <returns></returns>
        public Chart GetChart(Page page)
        {
            Chart result = new Chart();
            //映像屬性
            result.RenderType = RenderType.ImageTag;
            result.Width = Width;
            result.Height = Height;
            if (String.IsNullOrEmpty(Template))
            {
                result.Serializer.Content = SerializationContents.Default;
            }
            else
            {
                result.LoadTemplate(Template);
            }
            if (Titles != null)
            {
                //標題
                foreach (ChartTitle obj in Titles)
                {
                    result.Titles.Add(new Title(obj.Text, obj.Docking));
                }
            }
            if (MyAreas == null || MyAreas.Count == 0)
            {
                throw new Exception("映像的地區配置不可為空");
            }
            foreach (MyArea myArea in MyAreas)
            {
                //映像地區
                ChartArea item = new ChartArea(myArea.Name);
                if (myArea.XInterval.HasValue)
                {
                    item.AxisX.Interval = myArea.XInterval.Value;
                }
                else
                {
                    item.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
                }
                if (myArea.YInterval.HasValue)
                {
                    item.AxisY.Interval = myArea.YInterval.Value;
                }
                else
                {
                    item.AxisY.IntervalAutoMode = IntervalAutoMode.FixedCount;
                }
                if (myArea.XMinimum.HasValue)
                {
                    item.AxisX.Minimum = myArea.XMinimum.Value;
                }
                if (myArea.YMinimum.HasValue)
                {
                    item.AxisY.Minimum = myArea.YMinimum.Value;
                }
                item.Area3DStyle.Enable3D = myArea.IsShow3D;
                if (myArea.IsShow3D)
                {
                    item.Area3DStyle.IsClustered = myArea.IsClustered;
                    item.Area3DStyle.IsRightAngleAxes = myArea.IsRighTangleAxes;
                    if (myArea.Inclination > 0)
                    {
                        if (myArea.Inclination > 90)
                        {
                            item.Area3DStyle.Inclination = 90 - myArea.Inclination;
                        }
                        else
                        {
                            item.Area3DStyle.Inclination = myArea.Inclination;
                        }
                    }
                    if (myArea.Rotation > 0)
                    {
                        if (myArea.Rotation > 180)
                        {
                            item.Area3DStyle.Rotation = 180 - myArea.Rotation;
                        }
                        else
                        {
                            item.Area3DStyle.Rotation = myArea.Rotation;
                        }
                    }
                    item.Area3DStyle.LightStyle = myArea.LightStyle;
                    if (myArea.PointDepth > 0)
                    {
                        item.Area3DStyle.PointDepth = myArea.PointDepth;
                    }
                    if (myArea.PointGapDepth > 0)
                    {
                        item.Area3DStyle.PointGapDepth = myArea.PointGapDepth;
                    }
                }
                item.AxisX.IsLabelAutoFit = true;
                item.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont | LabelAutoFitStyles.IncreaseFont | LabelAutoFitStyles.WordWrap;
                item.AxisX.LabelStyle.Interval = 1;
                item.AxisX.LabelStyle.IntervalOffset = 1;
                item.AxisX.LabelStyle.IsStaggered = false;
                item.AxisX.LabelStyle.TruncatedLabels = true;
                if (!String.IsNullOrEmpty(myArea.XDataFormat))
                {
                    item.AxisX.LabelStyle.Format = myArea.XDataFormat;
                }
                if (!String.IsNullOrEmpty(myArea.YDataFormat))
                {
                    item.AxisY.LabelStyle.Format = myArea.YDataFormat;
                }
                //圖例
                if (myArea.Lengend != null && !IsExit(result.Legends, myArea.Lengend.Name))
                {
                    Legend legend = new Legend(myArea.Lengend.Name);
                    if (!String.IsNullOrEmpty(myArea.Lengend.Title))
                    {
                        legend.Title = myArea.Lengend.Title;
                    }
                    legend.Docking = myArea.Lengend.Docking;
                    legend.Enabled = true;
                    result.Legends.Add(legend);
                }
                //自身資料優先
                object ds = null;
                if (myArea.DataSource == null)
                {
                    ds = DataSource;
                }
                else
                {
                    ds = myArea.DataSource;
                }
                //資料及系列
                if (myArea.IsDataTable && ds != null)
                {
                    if (ds is DataView)
                    {
                        result.DataBindTable((DataView)ds, myArea.XField);
                    }
                    else
                    {
                        result.DataBindTable(((DataTable)ds).DefaultView, myArea.XField);
                    }
                }
                if (myArea.MySeries != null)
                {
                    foreach (MySerie mySerie in myArea.MySeries)
                    {
                        Series serie = new Series(mySerie.Name);
                        serie.ChartArea = myArea.Name;
                        serie.ChartType = mySerie.ChartType;
                        if (myArea.Lengend != null)
                        {
                            serie.Legend = myArea.Lengend.Name;
                            if (!String.IsNullOrEmpty(mySerie.Title))
                            {
                                serie.LegendText = mySerie.Title;
                            }
                        }
                        serie.IsValueShownAsLabel = mySerie.IsValueShownAsLabel;
                        if (mySerie.IsValueShownAsLabel && !String.IsNullOrEmpty(mySerie.DataFormat))
                        {
                            serie.LabelFormat = mySerie.DataFormat;
                        }
                        serie.XValueType = myArea.XValueType;
                        serie.YValueType = myArea.YValueType;
                        if (!String.IsNullOrEmpty(mySerie.CustomProperty))
                        {
                            foreach (string pv in DataHelper.GetStrings(mySerie.CustomProperty))
                            {
                                string[] pvs = DataHelper.GetStrings(pv, '=');
                                if (pvs != null && pvs.Length == 2)
                                {
                                    serie.SetCustomProperty(pvs[0], pvs[1]);
                                }
                            }
                        }
                        if (mySerie.Color.HasValue)
                        {
                            serie.Color = mySerie.Color.Value;
                        }
                        //標記
                        if (mySerie.MarkerSize > 0)
                        {
                            serie.MarkerSize = mySerie.MarkerSize;
                            serie.MarkerStyle = mySerie.MarkerStyle;
                            if (mySerie.MarkerColor.HasValue)
                            {
                                serie.MarkerColor = mySerie.MarkerColor.Value;
                            }
                            else if (mySerie.Color.HasValue)
                            {
                                serie.MarkerColor = mySerie.Color.Value;
                            }
                        }
                        //值
                        if (!myArea.IsDataTable && mySerie.ColumnNames != null)
                        {
                            if (ds == null && DataColumns != null)
                            {
                                //單條資料
                                foreach (string name in mySerie.ColumnNames)
                                {
                                    serie.Points.Add(GetDataPoint(null, name, mySerie, myArea));
                                }
                            }
                            else if (ds != null)
                            {
                                IList list = ReflectionDeal.GetDisplayList(ds);
                                List<int> iList = new List<int>();
                                if (myArea.RowIndexs == null)
                                {
                                    for (int i = 0; i < list.Count; i++)
                                    {
                                        iList.Add(i);
                                    }
                                }
                                foreach (int i in iList)
                                {
                                    serie.Points.Add(GetDataPoint(list[i], null, mySerie, myArea));
                                }
                            }
                        }
                        result.Series.Add(serie);
                    }
                }
                result.ChartAreas.Add(item);
            }
            result.Page = page;
            return result;
        }

3、產生並添加到介面

myChart.DataSource = dv;//支援DataSet DataTable DataView 或者對象集合(程式中使用反射獲得資料)
divChart2.Controls.Add(myChart.GetChart(Page));

 

試用請登入http://121.18.78.216/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.