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#程序设计有所帮助。
猜你喜欢
- 在SpringAMQP的发送方法中,接收消息的类型是Object,也就是说我们可以发送任意对象类型的消息,SpringAMQP会帮我们序列化
- 昨天有个刚学java的师弟发了个程序给我,说死活编译不过,老是报编码问题,自己试了一下,也出问题了...当我们编辑了一个Java源文件保存时
- 一、搭建 mvc 环境新建一个 module 模块,创建 maven 工程,步骤跟以前一样,各种配置文件内容也可以拷贝修改一下即可。二、创建
- 大体思路如果发总金额为 m的 n 个红包,先用一个长度为 n的临时数组 a 存放 n个随机双精度小数 ,然后用 sum表示数组
- 当你使用synchronized关键字的时候,是通过互斥器来保障线程安全以及对共享资源的同步访问。线程间也经常需要更进一步的协调
- Spring Security中的内置过滤器顺序是怎么维护的?我想很多开发者都对这个问题感兴趣。本篇我和大家一起探讨下这个问题。HttpSe
- 委托定义如下:public class SocketSp{ public delegate void ReceiveComplet
- springboot获取项目目录路径springboot部署后获取项目的路径//获取跟目录(绝对路径)File path = new Fil
- 前言说起网络爬虫,大家想起的估计都是 Python ,诚然爬虫已经是 Python 的代名词之一,相比 Java 来说就要逊色不少。有不少人
- 前言现在有这么个需求,网上购物,需要根据不同的规则计算商品折扣,比如VIP客户增加5%的折扣,购买金额超过1000元的增加10%的折扣等,而
- 今天下了个新浪微博的API研究研究,目前实现了发布微博功能,包括带图片的微博。为了安全,新浪微博的API中并没有提供用微博帐号密码登录的功能
- 一、使用策略枚举来优化if-else看到网上蛮多人推荐使用策略模式来优化if-else,但我总觉得,搞一堆策略类来优化大批量if-else,
- 一、项目运行环境配置:Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,
- 一.Unsafe类的源码分析JDK的rt.jar包中的Unsafe类提供了硬件级别的原子操作,Unsafe里面的方法都是native方法,通
- 目前为止我们已经了解了如何通过编程创建 CompletableFuture 对象以及如何获取返回值,虽然看起来这些操作已经比较方便,但还有进
- 在spring运行时,动态的添加bean,dapeng框架在解析xml的字段时,使用到了动态注册,注册了一个实现了FactoryBean类!
- 本文实例为大家分享了java实现飞机游戏的具体代码,供大家参考,具体内容如下MyGameFrame类:主要的调用类package sc.wh
- * 验证码地址:https://007.qq.com/online.html* 使用OpenCv模板匹配* 成功率90%左右* Java +
- sftp简介sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的网络的
- 本文实例讲述了C#实现将Email地址转成图片显示的方法。分享给大家供大家参考。具体实现方法如下:private final static