📄 customchart.vb
字号:
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Imports System.Data
' Main chart control class. Exposes properties to user.
' Uses helper class to calculate and draw chart shapes.
<DefaultEvent("ClickItem")> _
Public Class CustomChart
Inherits System.Windows.Forms.UserControl
' Internal fields.
Private m_List As New ArrayList()
Private m_ListDirty As Boolean = True
Private m_HitTestImage As Bitmap = Nothing
Private m_ShowLabel As Boolean = True
Private m_ShowValue As Boolean = False
Private m_ShowPercent As Boolean = False
Private m_PieChart As Boolean = True
Private m_AutoExpand As Boolean = True
' This is a weak-data-biding. That means it binds to a DataTable and
' specified table field, it抯 not as generic as other controls that
' data-bind to various objects.
Private m_DataView As DataView = Nothing
Private m_DataMember As String = String.Empty
' A helper class is used to calculate layout and draw charts.
' The helper class draws pie or bar charts depending on the
' exposed property from this class.
Private m_Drawing As ChartDrawing = Nothing
Private m_ChartColors As New ChartColors()
' Event. Pass the item that was clicked to container.
<Category("Action"), Description("A chart item was clicked.")> _
Public Event ClickItem As ClickItemEventHandler
' Properties.
<Browsable(False), Description("Table used for data binding.")> _
Public WriteOnly Property DataTable() As DataTable
Set(ByVal Value As DataTable)
' Get view for table, do this so we can hookup to the ListChanged event
' so the chart can be updated whenever the underlying data changes.
m_DataView = Value.DefaultView
AddHandler m_DataView.ListChanged, AddressOf OnDataChanged
DataBind()
End Set
End Property
<Browsable(False), _
Description("Field in table used for binding.")> _
Public WriteOnly Property DataMember() As String
Set(ByVal Value As String)
m_DataMember = Value
DataBind()
End Set
End Property
<Browsable(False), [ReadOnly](True), _
Description("Return number of items in chart.")> _
Public ReadOnly Property Count() As Integer
Get
Return m_List.Count
End Get
End Property
<Browsable(False), [ReadOnly](True), _
Description("Sum of all items in chart.")> _
Public ReadOnly Property TotalValue() As Integer
Get
' Go through and add up values of all items in the chart.
Dim Total As Integer = 0
Dim item As ChartItem
For Each item In m_List
Total += item.Value
Next item
Return Total
End Get
End Property
<DefaultValue(True), Category("Appearance"), _
Description("Show item labels.")> _
Public Property ShowLabel() As Boolean
Get
Return m_ShowLabel
End Get
Set(ByVal Value As Boolean)
m_ShowLabel = Value
End Set
End Property
<DefaultValue(False), Category("Appearance"), _
Description("Show item values.")> _
Public Property ShowValue() As Boolean
Get
Return m_ShowValue
End Get
Set(ByVal Value As Boolean)
m_ShowValue = Value
End Set
End Property
<DefaultValue(False), Category("Appearance"), _
Description("Show item percent.")> _
Public Property ShowPercent() As Boolean
Get
Return m_ShowPercent
End Get
Set(ByVal Value As Boolean)
m_ShowPercent = Value
End Set
End Property
<DefaultValue(True), Category("Appearance"), _
Description("Automatically expand clicked items.")> _
Public Property AutoExpand() As Boolean
Get
Return m_AutoExpand
End Get
Set(ByVal Value As Boolean)
m_AutoExpand = Value
End Set
End Property
<DefaultValue(True), Category("Appearance"), _
Description("Use pie or stacked bar chart.")> _
Public Property PieChart() As Boolean
Get
Return m_PieChart
End Get
Set(ByVal Value As Boolean)
m_PieChart = Value
' Create helper object that is used to draw the chart.
If m_PieChart Then
m_Drawing = New PieDrawing(Me)
Else
m_Drawing = New BarDrawing(Me)
End If
' Update the chart.
LayoutControls()
m_ListDirty = True
Invalidate()
End Set
End Property
' Internal properties. Used by helper drawing class.
<Browsable(False), [ReadOnly](True)> _
Friend ReadOnly Property List() As ArrayList
Get
Return m_List
End Get
End Property
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
m_Drawing = New PieDrawing(Me)
'This call is required by the Windows Form Designer.
InitializeComponent()
' we want to repaint when resized
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.UserPaint, True)
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents chart As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.chart = New System.Windows.Forms.PictureBox()
Me.SuspendLayout()
'
'chart
'
Me.chart.BackColor = System.Drawing.Color.Transparent
Me.chart.Dock = System.Windows.Forms.DockStyle.Fill
Me.chart.Name = "chart"
Me.chart.Size = New System.Drawing.Size(112, 112)
Me.chart.TabIndex = 0
Me.chart.TabStop = False
'
'CustomChart
'
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chart})
Me.Name = "CustomChart"
Me.Size = New System.Drawing.Size(112, 112)
Me.ResumeLayout(False)
End Sub
#End Region
' Public methods.
' Add item to chart.
Public Sub Add(ByVal Label As String, ByVal Value As Integer, ByVal Color As Color)
' Add item to internal list.
m_List.Add(New ChartItem(Label, Value, Color))
m_ListDirty = True
End Sub
' Override color table.
Public Sub SetColor(ByVal Index As Integer, ByVal NewColor As Color)
m_ChartColors.SetColor(Index, NewColor)
End Sub
' Expand or collapse a chart item.
Public Sub ExpandItem(ByVal Index As Integer, ByVal Expand As Boolean)
If Index < m_List.Count Then
Dim item As ChartItem = CType(m_List(Index), ChartItem)
If Not (item Is Nothing) Then
item.IsExpanded = Expand
Invalidate()
End If
End If
End Sub
' Expand or collapse all items in the chart.
Public Sub ExpandAll(ByVal Expand As Boolean)
Dim item As ChartItem
For Each item In m_List
item.IsExpanded = Expand
Next item
Invalidate()
End Sub
' Return chart item.
Public Function GetItem(ByVal Index As Integer) As ChartItem
Return CType(m_List(Index), ChartItem)
End Function
' Event handlers.
Private Sub CustomChart_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LayoutControls()
End Sub
Private Sub CustomChart_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
m_ListDirty = True
LayoutControls()
End Sub
' Handle all painting, setup painting info and pass along to drawing helper object.
Private Sub CustomChart_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Border space around chart.
Dim Border As Integer = ConstValues.ExpandSize + ConstValues.ShadowSize + 1
' Calculate area to draw chart.
Dim rc As Rectangle
If m_PieChart Then
rc = New Rectangle((chart.Width / 2) - (chart.Height / 2) + Border, Border, chart.Height - Border * 2, chart.Height - Border * 2)
Else
rc = New Rectangle(Border, Border, chart.Width - (Border * 2), chart.Height - (Border * 2))
End If
' See if need to update the layout of the chart items.
If m_ListDirty Then
m_Drawing.LayoutItems(rc)
m_ListDirty = False
End If
' Drawing surface for chart.
Dim g As Graphics = Graphics.FromImage(chart.Image)
g.Clear(Me.BackColor)
g.SmoothingMode = SmoothingMode.AntiAlias
' Drawing surface for hit testing.
Dim hitTest As Graphics = Graphics.FromImage(m_HitTestImage)
hitTest.Clear(Me.BackColor)
hitTest.SmoothingMode = SmoothingMode.AntiAlias
' Draw the chart, pass along to helper drawing object.
If Me.Count > 0 Then
m_Drawing.Draw(chart.Width, chart.Height, rc, g, hitTest)
Else
m_Drawing.DrawEmptyChart(g, rc)
End If
End Sub
Private Sub chart_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chart.MouseMove
Try
' Update the 憁ouse over
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -