public class CMSChart{private Chart chart;public CMSChart(Chart chart){this.chart = chart;}/// <summary>/// 保存图片/// </summary>/// <param name="fullPicName"></param>public void SaveAs(string fullPicName){ChartImageFormat format = ChartImageFormat.Jpeg;string ext = fullPicName.Substring(fullPicName.LastIndexOf('.') + 1);switch (ext.ToLower()){case "bmp": format = ChartImageFormat.Bmp; break;case "jpg": format = ChartImageFormat.Jpeg; break;case "png": format = ChartImageFormat.Png; break;case "tif": format = ChartImageFormat.Tiff; break;case "emf": format = ChartImageFormat.Emf; break;}chart.SaveImage(fullPicName, format);}/// <summary>/// /// </summary>/// <param name="dt">数据表,不能为空</param>/// <param name="xField">x轴数据字段,只有1个</param>/// <param name="yFields">y轴数据字段,多个</param>/// <param name="yTypes"></param>/// <param name="yTitles"></param>/// <param name="colors"></param>/// <param name="xTitle"></param>/// <param name="yTitle"></param>/// <param name="title"></param>/// <returns></returns>public bool ShowChart(DataTable dt, string xField, string[] yFields, string[] yTypes, string[] yTitles, Color[] colors, string xTitle, string yTitle, string title){bool result = false;try{chart.Titles.Clear();Title t = new Title(title, Docking.Top);chart.Titles.Add(title);chart.ChartAreas.Clear();chart.Series.Clear();ChartArea area = new ChartArea();//绘图区域高/宽相对于图表的高/宽的百分比area.Position.Height = 52;area.Position.Width = 96;//绘图区域起始位置坐标相对于图表高/宽的百分比area.Position.X = 2;area.Position.Y = 16;area.Name = "area" + yTitles;area.AxisX.Title = xTitle;area.AxisX.TitleAlignment = StringAlignment.Far;int Interval = dt.Rows.Count / 12;if (Interval < 1)Interval = 1;area.AxisX.Interval = Interval;area.AxisY.Title = yTitle;area.AxisX.MajorGrid.Enabled = false;area.AxisY.MajorGrid.Enabled = false; chart.ChartAreas.Add(area);double min = double.MaxValue, max = double.MinValue;for (int i = 0; i < yFields.Length; i++){ Series series = new Series();series.Color = colors[i];series.Name = yTitles[i]; switch (yTypes[i]){case "line": series.ChartType = SeriesChartType.Line;series.BorderWidth = 2;break;case "column": series.ChartType = SeriesChartType.Column; break;}foreach (DataRow row in dt.Rows){if (!Convert.IsDBNull(row[yFields[i]])){int x = Convert.ToInt32(row[xField]);double y = Convert.ToDouble(row[yFields[i]]);DataPoint p = new DataPoint(x, y);series.Points.Add(p);if (y > max)max = y;if (y < min)min = y;}}if (min > 0){double cha = max - min;if (cha < min * 0.3)area.AxisY.Minimum = Math.Round(min * 0.7, 0);}else if (max < 0){ }series.ChartArea = area.Name;series.LabelToolTip = string.Format("年:#VALX\r\n{0}:#VAL", yTitles[i]);series.ToolTip = string.Format("年:#VALX\r\n{0}:#VAL", yTitles[i]); chart.Series.Add(series); }}catch (Exception ex){ErrorManager.AddErrorToLog(ex);result = false;}return result;}/// <summary>/// /// </summary>/// <param name="dt"></param>/// <param name="xField"></param>/// <param name="yFields"></param>/// <param name="yTypes"></param>/// <param name="yTitles"></param>/// <param name="colors"></param>/// <param name="xTitle"></param>/// <param name="yTitle"></param>/// <param name="title"></param>/// <param name="fullPicName"></param>/// <returns></returns>public bool CreatePic(DataTable dt, string xField, string[] yFields, string[] yTypes, string[] yTitles, Color[] colors, string xTitle, string yTitle, string title, string fullPicName){bool result = false;try{ShowChart(dt, xField, yFields, yTypes, yTitles, colors, xTitle, yTitle, title);SaveAs(fullPicName);}catch (Exception ex){ErrorManager.AddErrorToLog(ex);result = false;}return result;}}
excel格式设置: