程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> DevExpress之ChartControl完成柱狀圖演示實例

DevExpress之ChartControl完成柱狀圖演示實例

編輯:C#入門知識

DevExpress之ChartControl完成柱狀圖演示實例。本站提示廣大學習愛好者:(DevExpress之ChartControl完成柱狀圖演示實例)文章只能為提供參考,不一定能成為您想要的結果。以下是DevExpress之ChartControl完成柱狀圖演示實例正文


本文實例講述了DevExpress中ChartControl完成柱狀圖演示的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

症結代碼以下:

using System;
using System.Data;
using System.Windows.Forms;
using CSharpUtilHelpV2;
using DevExpress.XtraCharts;

namespace DevExpressChart
{
    public partial class winProduct : Form
    {
        public winProduct()
        {
            InitializeComponent();
        }
        static readonly string AccessFullPath = string.Format(@"{0}\Northwind.MDB", AppDomain.CurrentDomain.BaseDirectory);
        static DataTable ProductList = null;
        AccessUtilsV2 AccessHelper = null;
        private void winChat_Load(object sender, EventArgs e)
        {
            AccessHelper = new AccessUtilsV2(AccessFullPath);
            ProductList = AccessHelper.ExecuteDataTable("select * from Products", null);
            BuilderDevChart();
        }
        private void BuilderDevChart()
        {
            if (ProductList != null)
            {
                Series _barSeries = new Series("測試", ViewType.Bar);
                _barSeries.ArgumentDataMember = "ProductName";//x軸
                _barSeries.ValueDataMembers[0] = "UnitPrice";//Y軸
                _barSeries.DataSource = ProductList;
                _barSeries.AddDataFilter("CategoryID", 4, DataFilterCondition.Equal);
                _barSeries.SetColorEach(true);
                chartControl1.Series.Add(_barSeries);
                _barSeries.LegendPointOptions.PointView = PointView.ArgumentAndValues;

                //chartControl1.SeriesDataMember = "UnitPrice";
                //chartControl1.SeriesTemplate.ValueDataMembers[0] = "UnitPrice";
                //chartControl1.SeriesTemplate.ArgumentDataMember = "ProductName";
                //chartControl1.SeriesTemplate.ArgumentScaleType = ScaleType.Qualitative;
                //chartControl1.SeriesTemplate.AddDataFilter("CategoryID", 4, DataFilterCondition.Equal);
                //chartControl1.SeriesTemplate.LegendPointOptions.PointView = PointView.ArgumentAndValues;
                chartControl1.SetXLableAngle(-35);
                chartControl1.SetCrosshair(true);

                chartControl1.Legend.Direction = LegendDirection.LeftToRight;
                chartControl1.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
                chartControl1.Legend.AlignmentVertical = LegendAlignmentVertical.BottomOutside;
                chartControl1.DataSource = ProductList;

                chartControl1.AddTitle("Product List");
                //--------------------------
            }
        }
    }
}

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;
            }
        }
    }
}
運轉後果以下圖所示:

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved