NEWS

[DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform

[DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
Đăng bởi: Thảo Meo - Lượt xem: 1330 11:48:21, 12/01/2024EBOOK

Xin chào các bạn, bài viết hôm nay mình hướng dẫn các bạn vẽ biểu đồ Bar Chart của ChartControl trên Devexpress Winform C#.

[DEVEXPRESS] Vẽ biểu đồ Bar Chart trên winform C#

Dưới đây, là hình ảnh demo ứng dụng:

bar_chart_devexpress_2024

Source code C#:

using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BarChartDemo
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        class DataDemo
        {
            public string name { get; set; }
            public int value { get; set; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var listData = new List<DataDemo>();
            listData.Add(new DataDemo() { name = "Apple", value = 1500});
            listData.Add(new DataDemo() { name = "Banana", value = 450});
            listData.Add(new DataDemo() { name = "Orange", value = 250});
            listData.Add(new DataDemo() { name = "Lemon", value = 3500});
            listData.Add(new DataDemo() { name = "Strawberry", value = 800});
            listData.Add(new DataDemo() { name = "Longan", value = 15});
            listData.Add(new DataDemo() { name = "Mango", value = 0});
            listData.Add(new DataDemo() { name = "Avocado", value = 560});
            listData.Add(new DataDemo() { name = "Cherry", value = 150});

            chartControl1.DataSource = listData;

            Series series1 = new Series("fruit", ViewType.StackedBar);
            series1.ArgumentDataMember = "name";
            series1.ValueDataMembers.AddRange("value");
            series1.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;
            series1.Label.TextPattern = "{V:#,###.##}";
            series1.View.Color = ColorTranslator.FromHtml("#21A1F1");
            series1.Label.TextAlignment = StringAlignment.Far;


            chartControl1.Series.AddRange(new Series[] { series1 });
            var label = chartControl1.Series[0].Label as SideBySideBarSeriesLabel;
            if (label != null)
            {
                label.Position = BarSeriesLabelPosition.Top;
            }

            ((XYDiagram)chartControl1.Diagram).Rotated = true;       
            ((XYDiagram)chartControl1.Diagram).AxisX.Tickmarks.MinorVisible = false;
            ((XYDiagram)chartControl1.Diagram).AxisX.Reverse = true;
            // Disable minor tickmarks on the x-axis:
            XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
            diagram.AxisX.Tickmarks.MinorVisible = false;
            diagram.AxisX.Label.TextPattern = "{V:#,###.##}";         
            chartControl1.Titles.Add(new ChartTitle { Text = "Sales by Products - LAPTRINHVB.NET", DXFont = new Font("Tahoma", 14, FontStyle.Bold), TextColor=Color.Orange });        
            chartControl1.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
            chartControl1.Legend.MarkerMode = LegendMarkerMode.None;
            chartControl1.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
            chartControl1.Legend.AlignmentVertical = LegendAlignmentVertical.BottomOutside;
            chartControl1.Legend.Direction = LegendDirection.LeftToRight;

        }
    }
}

Các bạn chỉ cần thay thông tin database mẫu bằng truy vấn cơ sở dữ liệu để hiển thị là xong.

Thanks for watching!

 

DOWNLOAD SOURCE

Tags: bar chart devexpress c#chartcontrolchart c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
Đăng bởi: Thảo Meo - Lượt xem: 1330 11:48:21, 12/01/2024EBOOK