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格式设置:
文章来源地址https://www.toymoban.com/news/detail-523193.html
文章来源:https://www.toymoban.com/news/detail-523193.html
到了这里,关于调用office的Excel制作统计图,并保存成图片的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!