首页 > 代码库 > 利用MsChart控件绘制多曲线图表(转载)

利用MsChart控件绘制多曲线图表(转载)

在.Net4.0框架中,微软已经将Mschart控件集成了进来,以前一直在web下面用过,原来winform下的Mschart控件更加简单更加方便,今天我们用mschart绘制一个多曲线图,话不多说,直接上效果图:

技术分享

发现,MsChart 的显示效果还是不错的。

代码如下,供大家参考:

public partial class FrmMain : Form    {        public FrmMain()        {            InitializeComponent();            InitializeChart();            this.Load += new EventHandler(FrmMain_Load);            this.myChart.GetToolTipText += new EventHandler<ToolTipEventArgs>(myChart_GetToolTipText);        }        void FrmMain_Load(object sender, EventArgs e)        {            float[][] data = http://www.mamicode.com/new float[3][];"时间:{0};数值:{1:F1} ", DateTime.FromOADate(dp.XValue),dp.YValues[0]);            }        }        /// <summary>        /// 初始化Char控件样式        /// </summary>        public void InitializeChart()        {            #region 设置图表的属性            //图表的背景色            myChart.BackColor = Color.FromArgb(211, 223, 240);            //图表背景色的渐变方式            myChart.BackGradientStyle = GradientStyle.TopBottom;            //图表的边框颜色、            myChart.BorderlineColor = Color.FromArgb(26, 59, 105);            //图表的边框线条样式            myChart.BorderlineDashStyle = ChartDashStyle.Solid;            //图表边框线条的宽度            myChart.BorderlineWidth = 2;            //图表边框的皮肤            myChart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;            #endregion            #region 设置图表的Title            Title title = new Title();            //标题内容            title.Text = "多曲线图演示";            //标题的字体            title.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, FontStyle.Bold);            //标题字体颜色            title.ForeColor = Color.FromArgb(26, 59, 105);            //标题阴影颜色            title.ShadowColor = Color.FromArgb(32, 0, 0, 0);            //标题阴影偏移量            title.ShadowOffset = 3;            myChart.Titles.Add(title);            #endregion            #region 设置图表区属性            //图表区的名字            ChartArea chartArea = new ChartArea("Default");            //背景色            chartArea.BackColor = Color.FromArgb(64, 165, 191, 228);            //背景渐变方式            chartArea.BackGradientStyle = GradientStyle.TopBottom;            //渐变和阴影的辅助背景色            chartArea.BackSecondaryColor = Color.White;            //边框颜色            chartArea.BorderColor = Color.FromArgb(64, 64, 64, 64);            //阴影颜色            chartArea.ShadowColor = Color.Transparent;            //设置X轴和Y轴线条的颜色和宽度            chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);            chartArea.AxisX.LineWidth = 1;            chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);            chartArea.AxisY.LineWidth = 1;            //设置X轴和Y轴的标题            chartArea.AxisX.Title = "横坐标标题";            chartArea.AxisY.Title = "纵坐标标题";            //设置图表区网格横纵线条的颜色和宽度            chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);            chartArea.AxisX.MajorGrid.LineWidth = 1;            chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);            chartArea.AxisY.MajorGrid.LineWidth = 1;            myChart.ChartAreas.Add(chartArea);            #endregion            #region 图例及图例的位置            Legend legend = new Legend();            legend.Alignment = StringAlignment.Center;            legend.Docking = Docking.Bottom;            this.myChart.Legends.Add(legend);            #endregion        }        //设置Series样式        private Series SetSeriesStyle(int i)        {            Series series = new Series(string.Format("第{0}条数据", i + 1));            //Series的类型            series.ChartType = SeriesChartType.Line;            //Series的边框颜色            series.BorderColor = Color.FromArgb(180, 26, 59, 105);            //线条宽度            series.BorderWidth = 3;            //线条阴影颜色            series.ShadowColor = Color.Black;            //阴影宽度            series.ShadowOffset = 2;            //是否显示数据说明            series.IsVisibleInLegend = true;            //线条上数据点上是否有数据显示            series.IsValueShownAsLabel = false;            //线条上的数据点标志类型            series.MarkerStyle = MarkerStyle.Circle;            //线条数据点的大小            series.MarkerSize = 8;            //线条颜色            switch (i)            {                case 0:                    series.Color = Color.FromArgb(220, 65, 140, 240);                    break;                case 1:                    series.Color = Color.FromArgb(220, 224, 64, 10);                    break;                case 2:                    series.Color = Color.FromArgb(220, 120, 150, 20);                    break;            }            return series;        }    }

另外附上微软Demo的下载地址:http://archive.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=4418

Demo 分为WEB版本和WinForm版本,样式和图表内容很全,希望对大家有所帮助。

转载:http://www.cnblogs.com/lxblog/

利用MsChart控件绘制多曲线图表(转载)