[C#] Tạo mẫu biểu đồ

C#

1. Giới thiệu

Hướng dẫn tạo biểu đồ như hình:

2. Code

Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

Namespace ConstLines
    Partial Public Class Form1
        Inherits Form


        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' Create an empty chart.
            'Dim chartControl1 As New ChartControl()

            ' Create a bar series and add points to it.
            Dim series1 As New Series("L-AC", ViewType.Line)
            series1.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True
            series1.Label.Border.Visibility = DevExpress.Utils.DefaultBoolean.False
            series1.Label.LineVisibility = DevExpress.Utils.DefaultBoolean.False
            series1.Label.BackColor = Color.Transparent

            series1.Points.Add(New SeriesPoint(250, New Double() {60}))
            series1.Points.Add(New SeriesPoint(500, New Double() {70}))
            series1.Points.Add(New SeriesPoint(1000, New Double() {75}))
            series1.Points.Add(New SeriesPoint(2000, New Double() {75}))
            series1.Points.Add(New SeriesPoint(4000, New Double() {80}))
            series1.Points.Add(New SeriesPoint(8000, New Double() {85}))

            Dim series2 As New Series("L-BC", ViewType.Line)
            series2.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True
            series2.Label.Border.Visibility = DevExpress.Utils.DefaultBoolean.False
            series2.Label.LineVisibility = DevExpress.Utils.DefaultBoolean.False
            series2.Points.Add(New SeriesPoint(250, New Double() {25}))
            series2.Points.Add(New SeriesPoint(500, New Double() {25}))
            series2.Points.Add(New SeriesPoint(1000, New Double() {55}))
            series2.Points.Add(New SeriesPoint(2000, New Double() {25}))
            series2.Points.Add(New SeriesPoint(4000, New Double() {30}))
            'series2.Points.Add(New SeriesPoint("8000", New Double() {0}))
            series2.Label.BackColor = Color.Transparent

            Dim series3 As New Series("L-AC masking", ViewType.Line)
            series3.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True
            series3.Label.Border.Visibility = DevExpress.Utils.DefaultBoolean.False
            series3.Label.LineVisibility = DevExpress.Utils.DefaultBoolean.False
            series3.Label.BackColor = Color.Transparent

            series3.Points.Add(New SeriesPoint(250, New Double() {100}))
            series3.Points.Add(New SeriesPoint(500, New Double() {115}))
            series3.Points.Add(New SeriesPoint(1000, New Double() {120}))
            series3.Points.Add(New SeriesPoint(2000, New Double() {105}))
            series3.Points.Add(New SeriesPoint(4000, New Double() {120}))
            series3.Points.Add(New SeriesPoint(8000, New Double() {100}))

            Dim series4 As New Series("L-BC masking", ViewType.Line)
            series4.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True
            series4.Label.Border.Visibility = DevExpress.Utils.DefaultBoolean.False
            series4.Label.LineVisibility = DevExpress.Utils.DefaultBoolean.False
            series4.Label.BackColor = Color.Transparent

            series4.Points.Add(New SeriesPoint(250, New Double() {40}))
            series4.Points.Add(New SeriesPoint(500, New Double() {30}))
            series4.Points.Add(New SeriesPoint(1000, New Double() {55}))
            series4.Points.Add(New SeriesPoint(2000, New Double() {55}))
            series4.Points.Add(New SeriesPoint(4000, New Double() {80}))
            'series4.Points.Add(New SeriesPoint("8000", New Double() {0}))

            ' Add the series to the chart.
            ChartControl1.Series.Add(series1)
            ChartControl1.Series.Add(series2)
            ChartControl1.Series.Add(series3)
            ChartControl1.Series.Add(series4)

            series1.ArgumentScaleType = ScaleType.Qualitative
            series2.ArgumentScaleType = ScaleType.Qualitative
            series3.ArgumentScaleType = ScaleType.Qualitative
            series4.ArgumentScaleType = ScaleType.Qualitative


            ' Access the view-type-specific options of the series.
            CType(series1.View, LineSeriesView).LineMarkerOptions.Kind = MarkerKind.Triangle
            CType(series1.View, LineSeriesView).MarkerVisibility = DevExpress.Utils.DefaultBoolean.True


            CType(series2.View, LineSeriesView).LineMarkerOptions.Kind = MarkerKind.Pentagon
            CType(series2.View, LineSeriesView).MarkerVisibility = DevExpress.Utils.DefaultBoolean.True
            CType(series2.View, LineSeriesView).LineStyle.DashStyle = DevExpress.XtraCharts.DashStyle.DashDot

            CType(series3.View, LineSeriesView).LineMarkerOptions.Kind = MarkerKind.Square
            CType(series3.View, LineSeriesView).MarkerVisibility = DevExpress.Utils.DefaultBoolean.True


            CType(series4.View, LineSeriesView).LineMarkerOptions.Kind = MarkerKind.Circle
            CType(series4.View, LineSeriesView).MarkerVisibility = DevExpress.Utils.DefaultBoolean.True
            CType(series4.View, LineSeriesView).LineStyle.DashStyle = DevExpress.XtraCharts.DashStyle.DashDot
            'CType(series1.View, LineSeriesView).LineStyle.DashStyle = DashStyle.Dash
            Dim diagram As XYDiagram = TryCast(ChartControl1.Diagram, XYDiagram)
            diagram.EnableAxisXScrolling = True
            diagram.EnableAxisYScrolling = True
            'diagram.AxisX.VisualRange.SetMinMaxValues("250", "8000")

            diagram.AxisX.Title.Text = "Tần số Hz"
            diagram.AxisX.Title.Font = New Font("Tahoma", 12, FontStyle.Bold, GraphicsUnit.Pixel)
            diagram.AxisX.Title.Visibility = DevExpress.Utils.DefaultBoolean.True
            diagram.AxisX.Tickmarks.MinorVisible = False
            diagram.AxisX.VisualRange.Auto = False
            diagram.AxisX.WholeRange.Auto = False



            diagram.AxisY.Title.Text = "Cường độ dB"
            diagram.AxisY.Reverse = True
            diagram.AxisY.Tickmarks.MinorVisible = False
            diagram.AxisY.Title.Visibility = DevExpress.Utils.DefaultBoolean.True
            diagram.AxisY.Title.Font = New Font("Tahoma", 12, FontStyle.Bold, GraphicsUnit.Pixel)
            'diagram.AxisX.GridLines.Color = Color.Red
            diagram.AxisX.GridLines.Visible = True


            ' Add the chart to the form.
            ChartControl1.Legend.Visibility = DevExpress.Utils.DefaultBoolean.True
            ChartControl1.Legend.AlignmentHorizontal = DevExpress.XtraCharts.LegendAlignmentHorizontal.Center
            ChartControl1.Legend.AlignmentVertical = DevExpress.XtraCharts.LegendAlignmentVertical.BottomOutside
            ChartControl1.Legend.Direction = DevExpress.XtraCharts.LegendDirection.LeftToRight
            ChartControl1.Titles.Add(New ChartTitle())
            ChartControl1.Titles(0).Text = "THÍNH LỰC ĐỒ - AUDIO DIAGRAM"
            ChartControl1.Titles(0).Font = New Font("Tahoma", 16, FontStyle.Bold, GraphicsUnit.Pixel)
            ChartControl1.Dock = DockStyle.Fill
            Me.Controls.Add(ChartControl1)
        End Sub

        Private Sub ChartControl1_CustomDrawSeries(sender As Object, e As CustomDrawSeriesEventArgs) Handles ChartControl1.CustomDrawSeries
            Dim image As New Bitmap(e.LegendMarkerSize.Width, e.LegendMarkerSize.Height)
            Using graphics As Graphics = Graphics.FromImage(image)
                graphics.FillRegion(New SolidBrush(e.SeriesDrawOptions.Color), New Region(image.GetBounds(Drawing.GraphicsUnit.Pixel)))
            End Using

            e.LegendMarkerImage = image
        End Sub
    End Class
End Namespace

Link download: audio_diagram_hospital

Các bạn dùng chương trình chuyển code VB sang C#.

Cảm ơn các bạn đã quan tâm 🤓🤓🤓

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *