项目中的工程模块需要用到图表来显示数据,后来选中了“OWC11”,效果如下:
CS代码:
void ToPrint(string categoryStr, string[] valueArr, string[] titleArr, string[] colorArr)
{
// 新建一个绘图空间
ChartSpace objCSpace = new ChartSpace();
// 在绘图空间中新建一个图表
ChChart objChart = objCSpace.Charts.Add(0);
//objChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered3D; // 指定图表的类型为3D柱状
objChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered; // 平面柱状
objChart.HasLegend = true; // 指定图表是否需要图例
// 标题
objChart.HasTitle = true;
objChart.Title.Caption = "年度水量消耗统计";
objChart.Title.Font.Size = 9;
objChart.Title.Font.Name = "宋体";
objChart.Title.Font.Bold = true;
objChart.PlotArea.Interior.Color = "#C0C0C0"; // 绘图区的背景颜色
objChart.PlotArea.Floor.Interior.Color = "#888888"; // 绘图区的底色
objChart.Legend.Font.Size = 9; // 图例的字体
// X, Y 轴的图示属性
//objChart.Axes[0].HasTitle = true;
//objChart.Axes[0].Title.Caption = "X:月";
//objChart.Axes[0].Title.Font.Size = 9;
//objChart.Axes[1].HasTitle = true;
//objChart.Axes[1].Title.Caption = "Y:量";
//objChart.Axes[1].Title.Font.Size = 9;
for (int i = 0; i < valueArr.Length; i++)
{
// 在图表中添加一个数据系列
ChSeries objSeries = objChart.SeriesCollection.Add(i);
objSeries.SetData(ChartDimensionsEnum.chDimSeriesNames, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), titleArr[i]);
objSeries.SetData(ChartDimensionsEnum.chDimCategories, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), categoryStr);
objSeries.SetData(ChartDimensionsEnum.chDimValues, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), valueArr[i]);
objSeries.Interior.Color = colorArr[i]; // 柱子内部的颜色
objSeries.GapWidth = 300; // 柱子之间的间隙
ChDataLabels objDataLabels = objChart.SeriesCollection[i].DataLabelsCollection.Add();
objDataLabels.HasValue = false; // 柱子上面的数字是否显示
objDataLabels.Font.Color = "Black"; // 柱子上面的数字颜色是黑色的
}
object objPic = objCSpace.GetPicture("GIF", 700, 300);
Response.Buffer = true;
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])objPic);
Response.End();
}
{
// 新建一个绘图空间
ChartSpace objCSpace = new ChartSpace();
// 在绘图空间中新建一个图表
ChChart objChart = objCSpace.Charts.Add(0);
//objChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered3D; // 指定图表的类型为3D柱状
objChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered; // 平面柱状
objChart.HasLegend = true; // 指定图表是否需要图例
// 标题
objChart.HasTitle = true;
objChart.Title.Caption = "年度水量消耗统计";
objChart.Title.Font.Size = 9;
objChart.Title.Font.Name = "宋体";
objChart.Title.Font.Bold = true;
objChart.PlotArea.Interior.Color = "#C0C0C0"; // 绘图区的背景颜色
objChart.PlotArea.Floor.Interior.Color = "#888888"; // 绘图区的底色
objChart.Legend.Font.Size = 9; // 图例的字体
// X, Y 轴的图示属性
//objChart.Axes[0].HasTitle = true;
//objChart.Axes[0].Title.Caption = "X:月";
//objChart.Axes[0].Title.Font.Size = 9;
//objChart.Axes[1].HasTitle = true;
//objChart.Axes[1].Title.Caption = "Y:量";
//objChart.Axes[1].Title.Font.Size = 9;
for (int i = 0; i < valueArr.Length; i++)
{
// 在图表中添加一个数据系列
ChSeries objSeries = objChart.SeriesCollection.Add(i);
objSeries.SetData(ChartDimensionsEnum.chDimSeriesNames, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), titleArr[i]);
objSeries.SetData(ChartDimensionsEnum.chDimCategories, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), categoryStr);
objSeries.SetData(ChartDimensionsEnum.chDimValues, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), valueArr[i]);
objSeries.Interior.Color = colorArr[i]; // 柱子内部的颜色
objSeries.GapWidth = 300; // 柱子之间的间隙
ChDataLabels objDataLabels = objChart.SeriesCollection[i].DataLabelsCollection.Add();
objDataLabels.HasValue = false; // 柱子上面的数字是否显示
objDataLabels.Font.Color = "Black"; // 柱子上面的数字颜色是黑色的
}
object objPic = objCSpace.GetPicture("GIF", 700, 300);
Response.Buffer = true;
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])objPic);
Response.End();
}
调用:
string[] categoryArr = new string[] { "1月份", "2月份", "3月份", "4月份", "5月份", "6月份" };
string categoryStr = string.Join(""t", categoryArr);
string[] valueArr1 = new string[] { "3832", "3167", "3575", "3816", "2651", "3494" };
string[] valueArr2 = new string[] { "17627.2", "14568.2", "12194.6", "16445", "17553.6", "16072.4" };
string[] valueArr3 = new string[] { "5748", "4750.5", "3976.5", "5362.5", "5724", "5241" };
string[] valueArr4 = new string[] { "23375.2", "19318.7", "16171.1", "21807.5", "23277.6", "21313.4" };
string valueStr1 = string.Join(""t", valueArr1);
string valueStr2 = string.Join(""t", valueArr2);
string valueStr3 = string.Join(""t", valueArr3);
string valueStr4 = string.Join(""t", valueArr4);
string[] valueArr = new string[] { valueStr1, valueStr2, valueStr3, valueStr4 };
string[] titleArr = new string[] { "消耗量m3", "水费(元)", "污水费(元)", "合计(元)" };
string[] colorArr = new string[] { "#9999FF", "#993366", "#FFFFCC", "#CCFFFF" };
ToPrint(categoryStr, valueArr, titleArr, colorArr);
string categoryStr = string.Join(""t", categoryArr);
string[] valueArr1 = new string[] { "3832", "3167", "3575", "3816", "2651", "3494" };
string[] valueArr2 = new string[] { "17627.2", "14568.2", "12194.6", "16445", "17553.6", "16072.4" };
string[] valueArr3 = new string[] { "5748", "4750.5", "3976.5", "5362.5", "5724", "5241" };
string[] valueArr4 = new string[] { "23375.2", "19318.7", "16171.1", "21807.5", "23277.6", "21313.4" };
string valueStr1 = string.Join(""t", valueArr1);
string valueStr2 = string.Join(""t", valueArr2);
string valueStr3 = string.Join(""t", valueArr3);
string valueStr4 = string.Join(""t", valueArr4);
string[] valueArr = new string[] { valueStr1, valueStr2, valueStr3, valueStr4 };
string[] titleArr = new string[] { "消耗量m3", "水费(元)", "污水费(元)", "合计(元)" };
string[] colorArr = new string[] { "#9999FF", "#993366", "#FFFFCC", "#CCFFFF" };
ToPrint(categoryStr, valueArr, titleArr, colorArr);