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

DevExpress之ChartControl实现时间轴实例

程序员文章站 2023-12-15 17:09:28
本文实例讲述了devexpress采用chartcontrol实现时间轴的方法,分享给大家供大家参考。具体实现方法如下: 关键代码如下: 复制代码 代码如下:using...

本文实例讲述了devexpress采用chartcontrol实现时间轴的方法,分享给大家供大家参考。具体实现方法如下:

关键代码如下:

复制代码 代码如下:
using system;
using system.data;
using system.windows.forms;
using devexpress.xtracharts;

namespace devexpresschart
{
    public partial class windatetime : form
    {
        public windatetime()
        {
            initializecomponent();
        }

        private void windatetime_load(object sender, eventargs e)
        {
            builderdevchart();
        }
        private datatable createchartdata()
        {
            datatable table = new datatable("table1");
            table.columns.add("date", typeof(datetime));
            table.columns.add("value", typeof(int32));
            table.rows.add(new object[] { new datetime(2014, 5, 21), 10 });
            table.rows.add(new object[] { new datetime(2014, 6, 22), 20 });
            table.rows.add(new object[] { new datetime(2014, 7, 23), 40 });
            table.rows.add(new object[] { new datetime(2014, 8, 24), 20 });
            table.rows.add(new object[] { new datetime(2014, 9, 25), 30 });
            return table;
        }
        private void builderdevchart()
        {
            series _lineseries = new series("利润", viewtype.line);
            _lineseries.argumentscaletype = scaletype.datetime;
            _lineseries.argumentdatamember = "date";
            _lineseries.valuedatamembers[0] = "value";
            _lineseries.datasource = createchartdata();
            chartcontrol1.series.add(_lineseries);

            chartcontrol1.settimeaxisx(datetimemeasurementunit.month, datetimemeasurementunit.month, "yyyy-mm");
            //---------------------------------------------------------------

            //_lineseries.valuescaletype = scaletype.datetime;
            //_lineseries.argumentdatamember = "value";
            //_lineseries.valuedatamembers[0] = "date";
            //_lineseries.datasource = createchartdata();
            //chartcontrol1.series.add(_lineseries);

            //xydiagram _diagram = (xydiagram)chartcontrol1.diagram;
            //_diagram.axisy.datetimemeasureunit = datetimemeasurementunit.month;
            //_diagram.axisy.datetimegridalignment = datetimemeasurementunit.month;
            //_diagram.axisy.datetimeoptions.format = datetimeformat.custom;
            //_diagram.axisy.datetimeoptions.formatstring = "mmmm";
        }
    }
}

复制代码 代码如下:
using devexpress.utils;
using devexpress.xtracharts;

namespace devexpresschart
{
    public static class chartutils
    {
        /// <summary>
        /// 增加数据筛选
        /// </summary>
        /// <param name="seriesbase">series</param>
        /// <param name="columnname">列名称</param>
        /// <param name="value">列名称对应的筛选数值</param>
        /// <param name="datafiltercondition">datafiltercondition枚举</param>
        public static void adddatafilter(this seriesbase series, string columnname, object value, datafiltercondition datafiltercondition)
        {
            series.datafilters.add(new datafilter(columnname, value.gettype().fullname, datafiltercondition, value));
        }

        /// <summary>
        /// 设置x轴lable角度
        /// </summary>
        /// <param name="chart">chartcontrol</param>
        /// <param name="angle">角度</param>
        public static void setxlableangle(this chartcontrol chart, int angle)
        {
            xydiagram _xydiagram = (xydiagram)chart.diagram;
            if (_xydiagram != null)
                _xydiagram.axisx.label.angle = angle;
        }
        /// <summary>
        ///  设置y轴lable角度
        /// </summary>
        /// <param name="chart">chartcontrol</param>
        /// <param name="angle">角度</param>
        public static void setylableangle(this chartcontrol chart, int angle)
        {
            xydiagram _xydiagram = (xydiagram)chart.diagram;
            _xydiagram.axisy.label.angle = angle;
        }
        /// <summary>
        /// 设置coloreach
        /// </summary>
        /// <param name="chart">chartcontrol</param>
        /// <param name="coloreach">是否设置成coloreach</param>
        public static void setcoloreach(this series series, bool coloreach)
        {
            seriesviewcoloreachsupportbase coloreachview = (seriesviewcoloreachsupportbase)series.view;
            if (coloreachview != null)
            {
                coloreachview.coloreach = coloreach;
            }
        }
        /// <summary>
        /// 设置是否显示十字标线
        /// </summary>
        /// <param name="chart">chartcontrol</param>
        /// <param name="crosshair">是否显示十字标线</param>
        public static void setcrosshair(this chartcontrol chart, bool crosshair)
        {
            chart.crosshairenabled = crosshair ? defaultboolean.true : defaultboolean.false;
            chart.crosshairoptions.showargumentlabels = crosshair;
            chart.crosshairoptions.showargumentline = crosshair;
            chart.crosshairoptions.showvaluelabels = crosshair;
            chart.crosshairoptions.showvalueline = crosshair;
        }
        /// <summary>
        /// 新增chartcontrol的title文字
        /// </summary>
        /// <param name="chart">chartcontrol</param>
        /// <param name="title">title文字</param>
        public static void addtitle(this chartcontrol chart, string title)
        {
            charttitle _title = new charttitle();
            _title.text = title;
            chart.titles.add(_title);
        }
        /// <summary>
        /// 饼状series设置成百分比显示
        /// </summary>
        /// <param name="series">series</param>
        public static void setpiepercentage(this series series)
        {
            if (series.view is pieseriesview)
            {
                ((piepointoptions)series.pointoptions).percentoptions.valueaspercent = true;
                ((piepointoptions)series.pointoptions).valuenumericoptions.format = numericformat.percent;
                ((piepointoptions)series.pointoptions).valuenumericoptions.precision = 0;
            }
        }
        /// <summary>
        /// 将x轴格式化成时间轴
        /// </summary>
        /// <param name="chart">chartcontrol</param>
        /// <param name="datetimemeasureunit">x轴刻度单位</param>
        /// <param name="datetimegridalignment">x轴刻度间距的单位</param>
        public static void settimeaxisx(this chartcontrol chart, datetimemeasurementunit datetimemeasureunit, datetimemeasurementunit datetimegridalignment)
        {
            xydiagram _diagram = (xydiagram)chart.diagram;
            if (_diagram != null)
            {
                _diagram.axisx.datetimemeasureunit = datetimemeasurementunit.month;//x轴刻度单位
                _diagram.axisx.datetimegridalignment = datetimemeasurementunit.month;//x轴刻度间距
            }
        }
        /// <summary>
        /// 将x轴格式化成时间轴
        /// </summary>
        /// <param name="chart">chartcontrol</param>
        /// <param name="datetimemeasureunit">x轴刻度单位</param>
        /// <param name="datetimegridalignment">x轴刻度间距的单位</param>
        /// <param name="formatstring">时间格式;eg:yyyy-mm</param>
        public static void settimeaxisx(this chartcontrol chart, datetimemeasurementunit datetimemeasureunit, datetimemeasurementunit datetimegridalignment, string formatstring)
        {
            xydiagram _diagram = (xydiagram)chart.diagram;
            if (_diagram != null)
            {
                _diagram.axisx.datetimemeasureunit = datetimemeasurementunit.month;//x轴刻度单位
                _diagram.axisx.datetimegridalignment = datetimemeasurementunit.month;//x轴刻度间距
                _diagram.axisx.datetimeoptions.format = datetimeformat.custom;
                _diagram.axisx.datetimeoptions.formatstring = formatstring;
            }
        }
    }
}


运行效果如下图所示:

DevExpress之ChartControl实现时间轴实例

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

上一篇:

下一篇: