欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中dotnetcharting的用法实例详解

程序员文章站 2024-02-11 21:49:34
本文以实例形式详细讲述了dotnetcharting控件的用法。分享给大家供大家参考。具体用法分析如下: dotnetcharting 是一个很好用的图表控件,能画出很漂...

本文以实例形式详细讲述了dotnetcharting控件的用法。分享给大家供大家参考。具体用法分析如下:

dotnetcharting 是一个很好用的图表控件,能画出很漂亮的报表,一般常用到的主要有柱状图、饼图、折线图三种。
dotnetcharting 有web版、winform版多个版本可供使用,官方网址:http://www.dotnetcharting.com/ ,官网有很多示例(http://www.dotnetcharting.com/gallery/),而且有winform安装版示例和代码,如下图。
dotnetcharting 有网上破解版的,去百度或谷歌一搜一堆。
说下个人感受,该控件是国外开发的,虽然说这个控件挺好用的,但是感觉这个控件应该在有xp系统的时候就应该有了吧?是国外剩下的技术,咱们一直在用别人不用的技术,捧为珍宝。
有些技术有些人,有些本事就开始自大起来,小有成就的那点技术还藏着掖着。 呵呵。。。

C#中dotnetcharting的用法实例详解 

接下来上干货,前两天刚使用dotnetcharting 做了个统计报表,代码如下:
 

复制代码 代码如下:
using system;
using system.collections.generic;
using system.data;
using system.drawing;
using dotnetcharting.winforms;
using stationchart.model;
using stationchart.utility;
using chart = dotnetcharting.winforms.chart;
using series = dotnetcharting.winforms.series;
using seriescollection = dotnetcharting.winforms.seriescollection;
 
namespace managesystem
{
    // <summary>
    // 作者:薛江涛
    // 版本:v1.0.0
    // 时间:2014/10/9 11:49:48
    // </summary>
    public class showdata
    {
        #region 属性
        private string _phaysicalimagepath;//图片存放路径
        private string _xtitle;//图片x座标名称
        private string _ytitle;//图片y座标名称
        private string _seriesname;//图例名称
        private int _picwidth;//图片宽度
        private int _pichight;//图片高度
        private datatable _dt;//图片数据源
        private dataset _ds;//图片数据源
 
        private color _titleboxcolor;//图片标题背景色
        private font _titleboxfont;//图片标题字体
        private color _chartareabackgroundcolor;//图片背景颜色
        private font _xaxislabelfont;//x轴柱状图值字体
        private font _yaxislabelfont;//y轴柱状图值字体
        private font _xaxisdefaultticklabelfont;//x轴刻度字体
        private font _yaxisdefaultticklabelfont;//y轴刻度字体
        private seriestype _chartdefaultseriestype;//图片类型
        private charttype _charttype;//图片类型
        //private bool _ismonth; //是否是月显示
        private int _staticcolumnwidth;//设置柱状图每个单元格的宽度
        private int _numberpercision;//设置数值小数点
 
        /// <summary>
        /// 图片存放路径
        /// </summary>
        public string phaysicalimagepath
        {
            set { _phaysicalimagepath = value; }
            get { return _phaysicalimagepath; }
        }
 
        /// <summary>
        /// 图片标题
        /// </summary>
        public string title { get; set; }
 
        /// <summary>
        /// 图片标题
        /// </summary>
        public string xtitle
        {
            set { _xtitle = value; }
            get { return _xtitle; }
        }
        /// <summary>
        /// 图片标题
        /// </summary>
        public string ytitle
        {
            set { _ytitle = value; }
            get { return _ytitle; }
        }
 
        /// <summary>
        /// 图例名称
        /// </summary>
        public string seriesname
        {
            set { _seriesname = value; }
            get { return _seriesname; }
        }
        /// <summary>
        /// 图片宽度
        /// </summary>
        public int picwidth
        {
            set { _picwidth = value; }
            get { return _picwidth; }
        }
        /// <summary>
        /// 图片高度
        /// </summary>
        public int pichight
        {
            set { _pichight = value; }
            get { return _pichight; }
        }
        /// <summary>
        /// 图片数据源
        /// </summary>
        public datatable datasource
        {
            set { _dt = value; }
            get { return _dt; }
        }
        /// <summary>
        /// 图片数据源
        /// </summary>
        public dataset datasetsource
        {
            set { _ds = value; }
            get { return _ds; }
        }
 
        public int numberpercision
        {
            set { _numberpercision = value; }
            get { return _numberpercision; }
        }
 
        public color titleboxcolor
        {
            get { return _titleboxcolor; }
            set { _titleboxcolor = value; }
        }
 
        public font titleboxfont
        {
            get { return _titleboxfont; }
            set { _titleboxfont = value; }
        }
 
        public color chartareabackgroundcolor
        {
            get { return _chartareabackgroundcolor; }
            set { _chartareabackgroundcolor = value; }
        }
 
        public font xaxislabelfont
        {
            get { return _xaxislabelfont; }
            set { _xaxislabelfont = value; }
        }
 
        public font yaxislabelfont
        {
            get { return _yaxislabelfont; }
            set { _yaxislabelfont = value; }
        }
 
        public font xaxisdefaultticklabelfont
        {
            get { return _xaxisdefaultticklabelfont; }
            set { _xaxisdefaultticklabelfont = value; }
        }
 
        public font yaxisdefaultticklabelfont
        {
            get { return _yaxisdefaultticklabelfont; }
            set { _yaxisdefaultticklabelfont = value; }
        }
 
        public seriestype chartdefaultseriestype
        {
            get { return _chartdefaultseriestype; }
            set { _chartdefaultseriestype = value; }
        }
 
        public charttype charttype
        {
            get { return _charttype; }
            set { _charttype = value; }
        }
 
        //public bool ismonth
        //{
        // get { return _ismonth; }
        // set { _ismonth = value; }
        //}
 
 
        public modelconseme.datechartenum seriestypee { get; set; }
 
        public int staticcolumnwidth
        {
            get { return _staticcolumnwidth; }
            set { _staticcolumnwidth = value; }
        }
 
        #endregion
 
        #region 构造函数
        public showdata()
        {
            //
            // todo: 在此处添加构造函数逻辑
            //
            numberpercision = 2;
        }
 
        public showdata(string phaysicalimagepath, string title, string xtitle, string ytitle, string seriesname)
        {
            _phaysicalimagepath = phaysicalimagepath;
            title = title;
            _xtitle = xtitle;
            _ytitle = ytitle;
            _seriesname = seriesname;
        }
        #endregion
 
        private static readonly object thislock = new object();
 
        #region 输出柱形图
        /// <summary>
        /// 柱形图
        /// </summary>
        /// <returns></returns>
        public void createcolumn(chart chart)
        {
            try
            {
                //清空图片
                chart.seriescollection.clear();
                //标题框设置
                //标题的颜色
                chart.titlebox.label.color = _titleboxcolor;
                //标题字体设置
                chart.titlebox.label.font = _titleboxfont;
 
                //控制柱状图颜色
                chart.shadingeffectmode = shadingeffectmode.one;
 
                chart.titlebox.position = titleboxposition.none;
 
                //图表背景颜色
                chart.chartarea.background.color = chartareabackgroundcolor;
                //1.图表类型
                chart.defaultseries.type = _chartdefaultseriestype;// seriestype.column;
                //chart.defaultseries.type = seriestype.cylinder;
                //2.图表类型
                //柱状图
                //chart.type = charttype.treemap;
                ////横向柱状图
                chart.type = _charttype;// charttype.combohorizontal
                ////横向柱状图
                //chart.type =_charttype;// charttype.gantt;
                ////饼状图
                //chart.type = charttype.pies;
 
                //y轴图表阴影颜色
                //chart.yaxis.alternategridbackground.color = color.fromargb(255, 250, 250, 250);
                chart.legendbox.headerlabel = new label("图表说明", new font("microsoft sans serif", 10f, fontstyle.bold, graphicsunit.point, 134));
                //chart.legendbox.headerlabel.font = new font("microsoft sans serif", 16f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134);
                chart.legendbox.label.font = new font("microsoft sans serif", 9f, fontstyle.bold, graphicsunit.point, 134);
                chart.palette = new color[] { color.fromargb(0, 156, 255), color.fromargb(255, 99, 49), color.fromargb(49, 255, 49), color.fromargb(255, 255, 0), };
 
                chart.title = title;
                //x轴柱状图值字体
                chart.xaxis.label.text = _xtitle;
                chart.xaxis.label.font = _xaxislabelfont;
                //设置x轴刻度值说明字体
                chart.xaxis.defaulttick.label.font = _xaxisdefaultticklabelfont;
 
                chart.xaxis.staticcolumnwidth = _staticcolumnwidth; //每个单元格的宽度
 
 
                //y轴柱状图值字体
                chart.yaxis.label.text = _ytitle;
                chart.yaxis.label.font = _yaxislabelfont;
                //设置y轴刻度值说明字体
                chart.yaxis.defaulttick.label.font = _yaxisdefaultticklabelfont;
 
                //y轴箭头标示
                chart.xaxis.name = xtitle;
                if (_charttype == charttype.combohorizontal)
                {
                    chart.xaxis.ticklabelpadding = 10;
                    chart.xaxis.line.startcap = system.drawing.drawing2d.linecap.square;
                    chart.xaxis.line.endcap = system.drawing.drawing2d.linecap.arrowanchor;
                    chart.xaxis.line.width = 5;//箭头宽度
                    chart.xaxis.line.color = color.gray;
                }
                else
                {
                    chart.yaxis.ticklabelpadding = 10;
                    chart.yaxis.line.startcap = system.drawing.drawing2d.linecap.square;
                    chart.yaxis.line.endcap = system.drawing.drawing2d.linecap.arrowanchor;
                    chart.yaxis.line.width = 5;//箭头宽度
                    chart.yaxis.line.color = color.gray;
                    //显示值格式化(小数点显示几位)
                    chart.yaxis.numberpercision = _numberpercision;
                }
 
                //图片存放路径
                chart.tempdirectory = system.environment.currentdirectory + "\\" + _phaysicalimagepath;
                //图表宽度
                chart.width = _picwidth;
                //图表高度
                chart.height = _pichight;
                chart.series.name = _seriesname;
                //单一图形
                //chart.series.data = _dt;
                //chart.seriescollection.add();
 
                //图例在标题行显示,但是没有合计信息
                //chart.titlebox.position = titleboxposition.fullwithlegend;
                //chart.titlebox.label.alignment = stringalignment.center;
                //chart.legendbox.position = legendboxposition.none; //不显示图例,指不在右侧显示,对上面一行的属性设置并没有影响
 
                chart.defaultseries.defaultelement.showvalue = true;
                chart.shadingeffect = true;
                chart.use3d = false;
                chart.series.defaultelement.showvalue = true;
                chart.seriescollection.add(getarraydata());
 
            }
            catch (exception ex)
            {
                loghelper.writeerrorlog(ex.tostring(), ex);
            }
        }
 
        /// <summary>
        /// 柱形图
        /// </summary>
        /// <returns></returns>
        public void createcolumn(chart chart, list<int> list)
        {
            try
            {
                chart.seriescollection.clear();
                //标题框设置
                //标题的颜色
                chart.titlebox.label.color = _titleboxcolor;
                //标题字体设置
                chart.titlebox.label.font = _titleboxfont;
 
                //控制柱状图颜色
                chart.shadingeffectmode = shadingeffectmode.five;
                //图表背景颜色
                chart.chartarea.background.color = chartareabackgroundcolor;
 
 
                //1.图表类型
                chart.defaultseries.type = _chartdefaultseriestype;// seriestype.column;
                //chart.defaultseries.type = seriestype.cylinder;
                //2.图表类型
                //柱状图
                //chart.type = charttype.treemap;
                ////横向柱状图
                chart.type = _charttype;// charttype.combohorizontal
                ////横向柱状图
                //chart.type =_charttype;// charttype.gantt;
                ////饼状图
                //chart.type = charttype.pies;
 
                //y轴图表阴影颜色
                //chart.yaxis.alternategridbackground.color = color.fromargb(255, 250, 250, 250);
                chart.legendbox.headerlabel = new label("图表说明", new font("microsoft sans serif", 10f, fontstyle.bold, graphicsunit.point, 134));
                //chart.legendbox.headerlabel.font = new font("microsoft sans serif", 16f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134);
                chart.legendbox.label.font = new font("microsoft sans serif", 9f, fontstyle.bold, graphicsunit.point, 134);
                chart.palette = new color[] { color.fromargb(0, 156, 255), color.fromargb(255, 99, 49), color.fromargb(49, 255, 49), color.fromargb(255, 255, 0), };
 
                chart.title = title;
                //x轴柱状图值字体
                chart.xaxis.label.text = _xtitle;
                chart.xaxis.label.font = _xaxislabelfont;
                //设置x轴刻度值说明字体
                chart.xaxis.defaulttick.label.font = _xaxisdefaultticklabelfont;
 
                chart.xaxis.staticcolumnwidth = _staticcolumnwidth; //每个单元格的宽度
 
 
                //y轴柱状图值字体
                chart.yaxis.label.text = _ytitle;
                chart.yaxis.label.font = _yaxislabelfont;
                //设置y轴刻度值说明字体
                chart.yaxis.defaulttick.label.font = _yaxisdefaultticklabelfont;
 
                //y轴箭头标示
                chart.xaxis.name = xtitle;
                if (_charttype == dotnetcharting.winforms.charttype.combohorizontal)
                {
                    chart.xaxis.ticklabelpadding = 10;
                    chart.xaxis.line.startcap = system.drawing.drawing2d.linecap.square;
                    chart.xaxis.line.endcap = system.drawing.drawing2d.linecap.arrowanchor;
                    chart.xaxis.line.width = 5;//箭头宽度
                    chart.xaxis.line.color = color.gray;
                }
                else
                {
                    chart.yaxis.ticklabelpadding = 10;
                    chart.yaxis.line.startcap = system.drawing.drawing2d.linecap.square;
                    chart.yaxis.line.endcap = system.drawing.drawing2d.linecap.arrowanchor;
                    chart.yaxis.line.width = 5;//箭头宽度
                    chart.yaxis.line.color = color.gray;
                    //显示值格式化(小数点显示几位)
                    chart.yaxis.numberpercision = _numberpercision;
                }
 
                //图片存放路径
                chart.tempdirectory = system.environment.currentdirectory + "\\" + _phaysicalimagepath;
                //图表宽度
                chart.width = _picwidth;
                //图表高度
                chart.height = _pichight;
                chart.series.name = _seriesname;
                //单一图形
                //chart.series.data = _dt;
                //chart.seriescollection.add();
 
                //图例在标题行显示,但是没有合计信息
                //chart.titlebox.position = titleboxposition.fullwithlegend;
                //chart.titlebox.label.alignment = stringalignment.center;
                //chart.legendbox.position = legendboxposition.none; //不显示图例,指不在右侧显示,对上面一行的属性设置并没有影响
 
                chart.defaultseries.defaultelement.showvalue = true;
                chart.shadingeffect = true;
                chart.use3d = false;
                chart.series.defaultelement.showvalue = true;
 
 
                chart.seriescollection.add(getarraydata(list));
 
            }
            catch (exception ex)
            {
 
                loghelper.writeerrorlog(ex.tostring(), ex);
            }
        }
 
 
        ///// <summary>
        ///// 柱形图,单一图片
        ///// </summary>
        ///// <returns></returns>
        //public void createcolumn(chart chart1)
        //{
        // chart1.title = _title;
        // chart1.xaxis.label.text = _xtitle;
        // chart1.yaxis.label.text = _ytitle;
        // chart1.tempdirectory = phaysicalimagepath;
        // chart1.width = _picwidth;
        // chart1.height = _pichight;
        // chart1.type = charttype.combo;
 
        // chart1.series.type = seriestype.cylinder;
        // chart1.series.name = _seriesname;
        // chart1.series.data = _dt;
        // chart1.seriescollection.add();
        // chart1.defaultseries.defaultelement.showvalue = true;
        // chart1.shadingeffect = true;
        // chart1.use3d = false;
        // chart1.series.defaultelement.showvalue = true;
        //}
 
        #endregion
 
        #region 输出饼图
        /// <summary>
        /// 饼图
        /// </summary>
        /// <returns></returns>
        public void createpie(chart chart)
        {
            try
            {
 
                chart.seriescollection.clear();
                //控制饼图颜色
                chart.shadingeffectmode = shadingeffectmode.two;
 
                chart.titlebox.position = titleboxposition.none;
                chart.title = title;
                //标题的颜色
                chart.titlebox.label.color = _titleboxcolor;
                //标题字体设置
                chart.titlebox.label.font = _titleboxfont;
 
                //图表说明
                chart.legendbox.headerlabel = new dotnetcharting.winforms.label("图表说明", new font("microsoft sans serif", 10f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134));
                //chart.legendbox.headerlabel.font = new font("microsoft sans serif", 16f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134);
                chart.legendbox.label.font = new font("microsoft sans serif", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134);
                chart.palette = new color[] { color.fromargb(0, 156, 255), color.fromargb(255, 99, 49), color.fromargb(49, 255, 49), color.fromargb(255, 255, 0), };
 
 
                chart.title = title;
                //x轴柱状图值字体
                chart.xaxis.label.text = _xtitle;
                chart.xaxis.label.font = _xaxislabelfont;
                //设置x轴刻度值说明字体
                chart.xaxis.defaulttick.label.font = _xaxisdefaultticklabelfont;
                chart.xaxis.label.text = _xtitle;
                chart.yaxis.label.text = _ytitle;
                chart.tempdirectory = _phaysicalimagepath;
                chart.width = _picwidth;
                chart.height = _pichight;
                chart.type = charttype.pie;
                chart.series.type = seriestype.cylinder;
                chart.series.name = _seriesname;
                chart.yaxis.numberpercision = _numberpercision;
 
                chart.shadingeffect = true;
                chart.use3d = false;
                chart.defaultseries.defaultelement.transparency = 20;//线条透明度
                chart.defaultseries.defaultelement.showvalue = true;
                chart.pielabelmode = dotnetcharting.winforms.pielabelmode.outside;
                chart.seriescollection.add(getpiearraydata());
                chart.series.defaultelement.showvalue = true;
 
            }
            catch (exception ex)
            {
                loghelper.writeerrorlog(ex.tostring(), ex);
            }
        }
 
        #region 输出曲线图
        /// <summary>
        /// 曲线图,昨日今日对比
        /// </summary>
        /// <returns></returns>
        public void createline(chart chart)
        {
            try
            {
                chart.seriescollection.clear();
 
                chart.titlebox.position = titleboxposition.none;
                //标题的颜色
                chart.titlebox.label.color = _titleboxcolor;
                //标题字体设置
                chart.titlebox.label.font = _titleboxfont;
                //图表背景颜色
                chart.chartarea.background.color = chartareabackgroundcolor;
 
                //x轴柱状图值字体
                chart.xaxis.label.font = _xaxislabelfont;
                //设置x轴刻度值说明字体
                chart.xaxis.defaulttick.label.font = _xaxisdefaultticklabelfont;
 
                //y轴柱状图值字体
                chart.yaxis.label.font = _yaxislabelfont;
                //设置y轴刻度值说明字体
                chart.yaxis.defaulttick.label.font = _yaxisdefaultticklabelfont;
 
                //y轴箭头标示
                chart.yaxis.ticklabelpadding = 0;
                chart.yaxis.line.startcap = system.drawing.drawing2d.linecap.square;
                chart.yaxis.line.endcap = system.drawing.drawing2d.linecap.arrowanchor;
                chart.yaxis.line.width = 5;//箭头宽度
                chart.yaxis.line.color = color.gray;
 
                chart.title = title;
                chart.xaxis.label.text = _xtitle;
                chart.yaxis.label.text = _ytitle;
                chart.tempdirectory = _phaysicalimagepath;
                // set the size
                chart.width = _picwidth;
                chart.height = _pichight;
                // set the temp directory
                chart.tempdirectory = "temp";
                // debug mode. ( will show generated errors if any )
                chart.debug = true;
                chart.type = charttype.combo;
                chart.defaultseries.type = seriestype.line;
                //chart.series.type = seriestype.line;
                chart.series.name = _seriesname;
                chart.defaultseries.defaultelement.showvalue = false;
                chart.shadingeffect = true;
                chart.use3d = false;
                chart.series.defaultelement.showvalue = true;
                chart.defaultseries.defaultelement.transparency = 20;//线条透明度
                chart.dategrouping = timeinterval.day;
                ////显示值格式化(小数点显示几位)
                chart.yaxis.numberpercision = _numberpercision;
 
                chart.overlapfooter = false;
 
                seriescollection mysc = getrandomdata();
                mysc[0].defaultelement.color = color.fromargb(255, 99, 49);
                mysc[1].defaultelement.color = color.fromargb(0, 156, 255);
 
                // add the random data.
                chart.seriescollection.add(mysc);
            }
            catch (exception ex)
            {
                loghelper.writeerrorlog(ex.tostring(), ex);
            }
        }
        #endregion
 
        /// <summary>
        /// 曲线图
        /// </summary>
        /// <returns></returns>
        seriescollection getrandomdata()
        {
            seriescollection sc = new seriescollection();
            try
            {
                datatable dt = _dt;
 
                var s = new series();
                var s2 = new series();
 
                switch (seriestypee)
                {
                    case modelconseme.datechartenum.daychart:
                        for (int i = 0; i < 24; i++)
                        {
                            s.name = "今天";
                            s2.name = "昨天";
                            element e = new element();
                            // 每元素的名称
                            e.name = (i).tostring();
                            // 每元素的大小数值
                            e.yvalue = 0;
 
                            element e2 = new element();
                            // 每元素的名称
                            e2.name = (i).tostring();
                            // 每元素的大小数值
                            e2.yvalue = 0;
 
                            s.elements.add(e);
                            s2.elements.add(e2);
                            sc.add(s);
                            sc.add(s2);
                        }
 
                        for (int i = 0; i < dt.rows.count; i++)
                        {
                            string sodayhh = dt.rows[i][1].tostring();
                            string today = dt.rows[i][1].tostring().substring(0, 10);
                            int hh = convert.toint32(sodayhh.substring(11));
                            if (datetime.now.tostring("yyyy-mm-dd").equals(today))
                            {
                                sc[0].elements[hh].yvalue = convert.todouble(dt.rows[i][2].tostring());
                            }
                            else
                            {
                                sc[1].elements[hh].yvalue = convert.todouble(dt.rows[i][2].tostring());
                            }
                        }
                        break;
                    case modelconseme.datechartenum.monthchart:
                        for (int i = 1; i < 32; i++)
                        {
                            s.name = "本月";
                            s2.name = "上月";
                            var e = new element();
                            // 每元素的名称
                            e.name = (i).tostring();
                            // 每元素的大小数值
                            e.yvalue = 0;
 
                            var e2 = new element();
                            // 每元素的名称
                            e2.name = (i).tostring();
                            // 每元素的大小数值
                            e2.yvalue = 0;
 
                            s.elements.add(e);
                            s2.elements.add(e2);
                            sc.add(s);
                            sc.add(s2);
                        }
 
                        for (int i = 0; i < dt.rows.count; i++)
                        {
                            string sodayhh = dt.rows[i][1].tostring();
                            string month = dt.rows[i][1].tostring().substring(0, 7);
                            int ovaule = convert.toint32(sodayhh.substring(8));
                            if (datetime.now.tostring("yyyy-mm").equals(month))
                            {
                                sc[0].elements[ovaule - 1].yvalue = convert.todouble(dt.rows[i][2].tostring());
                            }
                            else
                            {
                                sc[1].elements[ovaule - 1].yvalue = convert.todouble(dt.rows[i][2].tostring());
                            }
                        }
                        break;
                    case modelconseme.datechartenum.yearchart:
                        for (int i = 1; i < 13; i++)
                        {
                            s.name = "本年";
                            s2.name = "去年";
                            element e = new element();
                            // 每元素的名称
                            e.name = (i).tostring();
                            // 每元素的大小数值
                            e.yvalue = 0;
 
                            element e2 = new element();
                            // 每元素的名称
                            e2.name = (i).tostring();
                            // 每元素的大小数值
                            e2.yvalue = 0;
 
                            s.elements.add(e);
                            s2.elements.add(e2);
                            sc.add(s);
                            sc.add(s2);
                        }
 
                        for (int i = 0; i < dt.rows.count; i++)
                        {
                            string sodayhh = dt.rows[i][1].tostring();
                            string year = dt.rows[i][1].tostring().substring(0, 4);
                            int ovaule = sodayhh.contains("-") ? convert.toint32(sodayhh.substring(5)) : convert.toint32(sodayhh.substring(4));
                            if (datetime.now.tostring("yyyy").equals(year))
                            {
                                sc[0].elements[ovaule - 1].yvalue = convert.todouble(dt.rows[i][2].tostring());
                            }
                            else
                            {
                                sc[1].elements[ovaule - 1].yvalue = convert.todouble(dt.rows[i][2].tostring());
                            }
                        }
                        break;
                }
 
                //sc[0].palettename = palette.none;//.color = color.fromargb(49,255,49);
 
                return sc;
            }
            catch (exception ex)
            {
 
                loghelper.writeerrorlog(ex.tostring(), ex);
                return sc;
            }
        }
 
 
        /// <summary>
        /// 饼图
        /// </summary>
        /// <returns></returns>
        private seriescollection getpiearraydata()
        {
            seriescollection sc = new seriescollection();
            try
            {
                datatable dt = _dt;
 
 
                for (int i = 0; i < dt.rows.count; i++)
                {
                    series s = new series();
                    s.name = dt.rows[i][1].tostring();
                    element e = new element();
 
                    // 每元素的名称
                    e.name = dt.rows[i][1].tostring();
                    //设置柱状图值的字体
                    e.smartlabel.font = new system.drawing.font("microsoft sans serif", 10f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134);
                    //e.smartlabel.dynamicdisplay = true;
                    //e.smartlabel.autowrap = true;
                    // 每元素的大小数值
                    e.yvalue = convert.todouble(dt.rows[i][2].tostring());
                    //s.palettename = palette.three;
                    //s.defaultelement.color = color.fromargb(49, 255, 49);
                    s.elements.add(e);
                    sc.add(s);
                }
 
                //sc[0].palettename = palette.poppies;//.color = color.fromargb(49,255,49);
 
                //sc[0].defaultelement.color = color.fromargb(49, 255, 49);
                //sc[0].defaultelement.color = color.fromargb(49, 255, 49);
                //sc[1].defaultelement.color = color.fromargb(255, 255, 0);
                //sc[2].defaultelement.color = color.fromargb(255, 99, 49);
                //sc[3].defaultelement.color = color.fromargb(0, 156, 255);
                return sc;
 
            }
            catch (exception ex)
            {
 
                loghelper.writeerrorlog(ex.tostring(), ex);
                return sc;
            }
        }
 
        /// <summary>
        /// 柱状图
        /// </summary>
        /// <returns></returns>
        private seriescollection getarraydata()
        {
            seriescollection sc = new seriescollection();
            try
            {
                datatable dt = _dt;
                series s = new series();
                for (int i = 0; i < dt.rows.count; i++)
                {
                    s.name = dt.rows[i][1].tostring();
                    if (s.name == "") continue;
                    var e = new element();
 
                    // 每元素的名称
                    e.name = dt.rows[i][1].tostring();
                    //设置柱状图值的字体
                    e.smartlabel.font = new system.drawing.font("microsoft sans serif", 10f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134);
                    //e.smartlabel.dynamicdisplay = true;
                    //e.smartlabel.autowrap = true;
                    // 每元素的大小数值
                    e.yvalue = convert.todouble(dt.rows[i][2].tostring());
                    //调整柱子颜色
                    //s.palettename = palette.three;
                    s.palette = new color[] { color.fromargb(0, 156, 255), color.fromargb(255, 99, 49), color.fromargb(49, 255, 49), color.fromargb(255, 255, 0), };
 
                    s.elements.add(e);
                }
                sc.add(s);
                return sc;
 
            }
            catch (exception ex)
            {
 
                loghelper.writeerrorlog(ex.tostring(), ex);
                return sc;
            }
        }
 
 
        /// <summary>
        /// 柱状图
        /// </summary>
        /// <returns></returns>
        private seriescollection getarraydata(list<int> list)
        {
            seriescollection sc = new seriescollection();
            try
            {
 
                datatable dt = _ds.tables[list[0]];
                datatable dt2 = _ds.tables[list[1]];
 
                dictionary<string, string> oildict = new dictionary<string, string>();
                dictionary<string, string> oildicy = new dictionary<string, string>();
 
                for (int i = 0; i < dt.rows.count; i++)
                {
                    oildict.add(dt.rows[i][1].tostring(), dt.rows[i][2].tostring());
                }
                for (int i = 0; i < dt2.rows.count; i++)
                {
                    oildicy.add(dt2.rows[i][1].tostring(), dt2.rows[i][2].tostring());
                }
 
                foreach (keyvaluepair<string, string> keyvalue in oildict)
                {
                    if (!oildicy.containskey(keyvalue.key))
                    {
                        oildicy.add(keyvalue.key, "0");
                    }
                }
 
                foreach (keyvaluepair<string, string> keyvalue in oildicy)
                {
                    if (!oildict.containskey(keyvalue.key))
                    {
                        oildict.add(keyvalue.key, "0");
                    }
                }
                var oilist = new list<dictionary<string, string>> { oildict, oildicy };
 
                for (int a = 0; a < oilist.count; a++)
                {
                    var dicn = oilist[a];
                    series s = new series();
                    switch (seriestypee)
                    {
                        case modelconseme.datechartenum.daychart:
                            s.name = a == 0 ? "今天" : "昨天";
                            break;
                        case modelconseme.datechartenum.monthchart:
                            s.name = a == 0 ? "本月" : "上月";
                            break;
                        case modelconseme.datechartenum.yearchart:
                            s.name = a == 0 ? "今年" : "去年";
                            break;
                    }
 
 
                    foreach (keyvaluepair<string, string> keyvalue in dicn)
                    {
                        element e = new element();
 
                        // 每元素的名称
                        e.name = keyvalue.key;
                        //设置柱状图值的字体
                        e.smartlabel.font = new system.drawing.font("microsoft sans serif", 10f,
                            system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134);
                        //e.smartlabel.dynamicdisplay = true;
                        //e.smartlabel.autowrap = true;
                        // 每元素的大小数值
                        e.yvalue = convert.todouble(keyvalue.value);
                        //s.palettename = palette.poppies;
                        //s.defaultelement.color = color.fromargb(49, 255, 49);
                        s.elements.add(e);
                    }
                    sc.add(s);
                }
                return sc;
 
            }
            catch (exception ex)
            {
 
                loghelper.writeerrorlog(ex.tostring(), ex);
                return sc;
            }
        }
 
 
        /// <summary>
        /// 柱状图2
        /// </summary>
        /// <returns></returns>
        private seriescollection getarraydata2()
        {
            seriescollection sc = new seriescollection();
            try
            {
                datatable dt = _dt;
 
                for (int a = 0; a < 2; a++)
                {
                    series s = new series();
                    s.name = (a == 0 ? "今天" : "昨天");
                    for (int i = 0; i < dt.rows.count; i++)
                    {
                        element e = new element();
 
                        // 每元素的名称
                        e.name = dt.rows[i][1].tostring();
                        //设置柱状图值的字体
                        e.smartlabel.font = new system.drawing.font("microsoft sans serif", 10f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, 134);
                        //e.smartlabel.dynamicdisplay = true;
                        //e.smartlabel.autowrap = true;
                        // 每元素的大小数值
                        e.yvalue = convert.todouble(dt.rows[i][2].tostring());
                        s.palettename = palette.poppies;
                        //s.defaultelement.color = color.fromargb(49, 255, 49);
                        s.elements.add(e);
                    }
 
                    sc.add(s);
                }
                return sc;
 
            }
            catch (exception ex)
            {
 
                loghelper.writeerrorlog(ex.tostring(), ex);
                return sc;
            }
        }
        #endregion
    }
}

 
调用方法:
复制代码 代码如下:
showdata show = new showdata();
//show.pichight = 494;
//show.picwidth = 1336;
show.seriesname = "具体详情";
show.phaysicalimagepath = "chartimages";
show.titleboxcolor = color.blue;
show.titleboxfont = new font("arial", 10, fontstyle.bold);
show.chartareabackgroundcolor = color.honeydew;
show.xaxislabelfont = new system.drawing.font("microsoft sans serif", 10f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
show.yaxislabelfont = new system.drawing.font("microsoft sans serif", 10f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
show.xaxisdefaultticklabelfont = new system.drawing.font("microsoft sans serif", 10f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
show.yaxisdefaultticklabelfont = new system.drawing.font("microsoft sans serif", 10f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
show.chartdefaultseriestype = seriestype.column;
show.datasetsource = dsset;
show.seriestypee = modelconseme.datechart;

//饼图
show.ytitle = "油量(升)";
//show.title = stroils + strdate + strconsume + "销售油量报表统计图"; 
show.xtitle = "";
show.datasource = dsset.tables[0];
show.createpie(this.chart1);

//柱状图(两条柱子对比)
 show.ytitle = "油量(升)";
label2.text = strdate + stroils + strconsume + @"油品对比图";
show.xtitle = "油品";
//show.datasource = dsset.tables[2];
show.staticcolumnwidth = 0;
show.createcolumn(chart2, new list<int>()
{
0,
1
});

//曲线图
show.ytitle = "油量(升)";
label5.text = strdate + stroils + strconsume + @"油量环比图";
show.xtitle = "时间";
show.datasource = dsset.tables[4];
show.createline(this.chart5);

//单个柱子
var show = new showdata
{
    title = "近30天加油会员统计",
    xtitle = "人数",
    ytitle = "会员分类",
    seriesname = "具体详情",
    phaysicalimagepath = "chartimages",
    datasource = dsset.tables[0],
    titleboxcolor = color.blue,
    titleboxfont = new font("arial", 10, fontstyle.bold),
    chartareabackgroundcolor = color.honeydew,
    xaxislabelfont = new font("microsoft sans serif", 10f, fontstyle.bold, graphicsunit.point, 134),
    yaxislabelfont = new font("microsoft sans serif", 10f, fontstyle.bold, graphicsunit.point, 134),
    xaxisdefaultticklabelfont =
 new font("microsoft sans serif", 10f, fontstyle.bold, graphicsunit.point, 134),
    yaxisdefaultticklabelfont =
 new font("microsoft sans serif", 10f, fontstyle.bold, graphicsunit.point, 134),
    chartdefaultseriestype = seriestype.column,
    charttype = charttype.combohorizontal,
    staticcolumnwidth = 80
};
show.createcolumn(chart1);

show.numberpercision = 0;
show.createpie(chart2);

运行效果图如下:

C#中dotnetcharting的用法实例详解 

C#中dotnetcharting的用法实例详解

C#中dotnetcharting的用法实例详解

希望本文所述对大家的c#程序设计有所帮助。