C#画图之饼图折线图的实现方法
作者:shichen2014 发布时间:2021-12-05 22:26:13
本文实例讲述了C#画图之饼图折线图的实现方法,是C#程序设计中非常实用的技巧。分享给大家供大家参考。具体方法分析如下:
显示图像的控件定义如下:
public PlaceHolder PlaceHolder1;
各个图像的类别名称如下:
PictureType 图形种类 5 chChartTypeBarClustered 簇状条形图 0 NULL
PictureType 图形种类 7 chChartTypeBarClustered3D 三维簇状条形图 0 NULL
PictureType 图形种类 6 chChartTypeBarStacked 堆积条形图 0 NULL
PictureType 图形种类 8 chChartTypeBarStacked3D 三维堆积条形图 0 NULL
PictureType 图形种类 1 chChartTypeColumnClustered 簇状柱形图 0 NULL
PictureType 图形种类 3 chChartTypeColumnClustered3D 三维簇状柱形图 0 NULL
PictureType 图形种类 2 chChartTypeColumnStacked 堆积柱状图 1 NULL
PictureType 图形种类 4 chChartTypeColumnStacked3D 三维堆积柱形图 0 NULL
PictureType 图形种类 13 chChartTypeLine 折线图 0 NULL
PictureType 图形种类 15 chChartTypeLineMarkers 数据点折线图 0 NULL
PictureType 图形种类 14 chChartTypeLineStacked 堆积折线图 0 NULL
PictureType 图形种类 16 chChartTypeLineStackedMarkers 堆积数据点折线图 0 NULL
PictureType 图形种类 17 chChartTypePie 饼图 1 NULL
PictureType 图形种类 19 chChartTypePie3D 三维饼图 0 NULL
PictureType 图形种类 18 chChartTypePieExploded 分离型饼图 0 NULL
PictureType 图形种类 20 chChartTypePieExploded3D 分离型三维饼图 0 NULL
PictureType 图形种类 9 chChartTypeSmoothLine 平滑线图 0 NULL
PictureType 图形种类 10 chChartTypeSmoothLineMarkers 数据点平滑线图 0 NULL
PictureType 图形种类 11 chChartTypeSmoothLineStacked 堆积平滑线图 0 NULL
PictureType 图形种类 12 chChartTypeSmoothLineStackedMarkers 堆积数据平滑线图 0 NULL
取图像的方法如下:
/// </summary>
/// <param name="dbDtViewWrk">传递的数据</param>
/// <param name="strAbsolutePath">绝对路径</param>
/// <param name="strRelativePath">相对路径</param>
/// <param name="ChartType">要画的图格式(饼图或者折线图等)</param>
/// <param name="strTitle">统计名称</param>
public void PaintToImage(DataTable dbDtViewWrk, string strAbsolutePath, string strRelativePath, ChartChartTypeEnum ChartType, string strTitle)
{
string strSeriesName = "图例";
//存放项目
string[] ItemsName = new string[dbDtViewWrk.Rows.Count];
//存放数据
string[] ItemsCount = new string[dbDtViewWrk.Rows.Count];
//刻度单位
int iUnit = 1;
//最大值
int iMaxValue = 0;
string strXdata = String.Empty;
string strYdata = String.Empty;
//为数组赋值
for (int i = 0; i < dbDtViewWrk.Rows.Count; i++)
{
ItemsName[i] = dbDtViewWrk.Rows[i][0].ToString(); //要统计的字段名字
ItemsCount[i] = dbDtViewWrk.Rows[i][5].ToString();//要统计的字段数据
}
//为x轴指定特定字符串,以便显示数据
// string strXdata = String.Empty;
foreach (string strData in ItemsName)
{
strXdata += strData + "\t";
}
// string strYdata = String.Empty;
//为y轴指定特定的字符串,以便与x轴相对应
foreach (string strValue in ItemsCount)
{
strYdata += strValue + "\t";
if (int.Parse(strValue) > iMaxValue)
{
iMaxValue = int.Parse(strValue);
}
}
if (iMaxValue > 20)
{
iUnit = iMaxValue / 10;
}
//创建ChartSpace对象来放置图表
ChartSpace laySpace = new ChartSpaceClass();
//在ChartSpace对象中添加图表
ChChart InsertChart = laySpace.Charts.Add(0);
//底座颜色
InsertChart.PlotArea.Interior.Color = "white";
//指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
InsertChart.Type = ChartType;//柱形图
//指定图表是否需要图例标注
InsertChart.HasLegend = true;
InsertChart.BarWidth = 0;
InsertChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
InsertChart.HasTitle = true;//为图表添加标题
InsertChart.Title.Caption = strTitle;//标题名称
//为x,y轴添加图示说明
if (ChartType.ToString().IndexOf("ChartTypePie") == -1)
{
InsertChart.Axes[0].Font.Size = 11; //X轴
InsertChart.Axes[1].Font.Size = 11; //Y轴
InsertChart.Legend.Font.Size = 11;
InsertChart.Axes[0].HasTitle = true;
InsertChart.Axes[0].Title.Caption = "";//月份
InsertChart.Axes[1].HasTitle = true;
//InsertChart.Axes[1].Scaling.SplitMinimum = 200;
InsertChart.Axes[1].Title.Caption = "数量";
InsertChart.Axes[1].MajorUnit = iUnit; //刻度单位设置
InsertChart.Axes[1].Scaling.Minimum = 0;//最小刻度=0
}
//添加一个series系列
InsertChart.SeriesCollection.Add(0);
//给定series系列的名字
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
//给定分类
strXdata = strXdata.Substring(0, strXdata.Length - 1);
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
//给定值
strYdata = strYdata.Substring(0, strYdata.Length - 1);
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
//添加标签
ChDataLabels dls = InsertChart.SeriesCollection[0].DataLabelsCollection.Add();
if (ChartType.ToString().IndexOf("ChartTypePie") != -1)
{
dls.Position = ChartDataLabelPositionEnum.chLabelPositionCenter;
dls.HasPercentage = false;
//dls.HasValue = false;
dls.HasCategoryName = false;
//指定图表是否需要图例标注
InsertChart.HasLegend = true;
InsertChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
}
//输出文件.
int iImageLength = 0;
int iImageWidth = 0;
//从Config文件取得设置
//iImageLength = int.Parse(WebConfigurationManager.AppSettings["ShowImageLength"]);
//iImageWidth = int.Parse(WebConfigurationManager.AppSettings["ShowImageWidth"]);
iImageLength = 450;
iImageWidth = 300;
string strImageName = ChartType.ToString() + "_" + Guid.NewGuid().ToString("N") + ".png";
laySpace.ExportPicture(strAbsolutePath + strImageName, "PNG", 450, 300);
//把图片添加到placeholder中,并在页面上显示
string strImageTag = "<IMG WIDTH='450' SRC='" + strRelativePath + strImageName + "'/>";
this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
// return strImageTag;
}
希望本文所述对大家的C#程序设计有所帮助。


猜你喜欢
- spring boot 使用POI读取Excel文件Excel文件目录Excel模板文件存了resourse目录下,如下图:<depe
- 本文实例讲述了JFreeChart插件实现的折线图效果。分享给大家供大家参考,具体如下:package com.lei.jfreechart
- 一、信号量(Semaphore)信号量(Semaphore)是由内核对象维护的int变量,当信号量为0时,在信号量上等待的线程会堵塞,信号量
- 本文实例讲述了Java实现的模糊匹配某文件夹下的文件并删除功能。分享给大家供大家参考,具体如下:package com.wyebd.gis;
- public class TimeUtil { public static final int SECO
- 1、打开侧面的maven,点击图中箭头指向的图标。2、出现如下弹窗,在Command line在中输入执行的指令。指令格式:install:
- AndroidMaifest.xml中声明权限<!-- 声明所有需要的权限(包括普通权限和危险权限) --><uses-p
- MyBatis简介MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参
- 本文实例为大家分享了java与微信小程序实现websocket长连接的具体代码,供大家参考,具体内容如下背景:需要在小程序实现地图固定坐标下
- 通俗的来说,Jackson是一个 Java 用来处理 JSON 格式数据的类库,其性能非常好。本文就来针对Jackson的用法做一个较为详细
- 本文实例为大家分享了Java实现学生管理系统的具体代码,供大家参考,具体内容如下package BookDemo_1; import jav
- Step 1.依赖bannerGradledependencies{ compile 'com.youth.banner
- C#之委托委托:顾名思义,让别人帮你办件事。委托是C#实现回调函数的一种机制。可能有人会问了,回调函数是个啥???举个例子:我现在是一家公司
- 1.最常用的方法是创建一个计数器,判断是否遇到‘\0',不是'\0'指针就往后加一。int my_strlen(co
- 在本篇介绍的Winform界面样式改变及存储操作中,是指基于DevExpress进行界面样式的变化。一般情况下,默认我们会为客户提供多种De
- 1.创建一个带Mesh的物体Unity中的网格作为组件不能脱离物体单独存在新建脚本CreateMeshpublic class Create
- 拒绝策略介绍线程池的拒绝策略,是指当任务添加到线程池中被拒绝,而采取的处理措施。当任务添加到线程池中之所以被拒绝,可能是由于:第一,线程池异
- /* String name = "adsbsadgsadgtewterfsdf"
- 马云说:“未来最大的资源就是数据,不参与大数据十年后一定会后悔。”毕竟出自wuli马大大之口,今年二月份我开始了学习大数据的道路,直到现在对
- 目录一、所使用的环境配置:二、项目简介三、知识点总结(代码和配置)SpringBoot:1.Mybatis-Plus配置文件,实现分页查询: