-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.vb
130 lines (104 loc) · 6.71 KB
/
Form1.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
Imports CustomSeriesPointDrawingSample.Model
Imports DevExpress.Drawing
Imports DevExpress.XtraCharts
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO
Imports System.Linq
Imports System.Windows.Forms
Namespace CustomSeriesPointDrawingSample
Public Partial Class Form1
Inherits System.Windows.Forms.Form
Private trackedPointArgument As Object
Private photoCache As System.Collections.Generic.Dictionary(Of String, DevExpress.Drawing.DXImage) = New System.Collections.Generic.Dictionary(Of String, DevExpress.Drawing.DXImage)()
#Region "#Constants"
Const borderSize As Integer = 5
Const scaledPhotoWidth As Integer = 48
Const scaledPhotoHeight As Integer = 51
' Width and height of scaled photo with border.
Const totalWidth As Integer = 58
Const totalHeight As Integer = 61
' Rects required to create a custom legend series marker.
Private Shared ReadOnly photoRect As System.Drawing.Rectangle = New System.Drawing.Rectangle(CustomSeriesPointDrawingSample.Form1.borderSize, CustomSeriesPointDrawingSample.Form1.borderSize, CustomSeriesPointDrawingSample.Form1.scaledPhotoWidth, CustomSeriesPointDrawingSample.Form1.scaledPhotoHeight)
Private Shared ReadOnly totalRect As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, CustomSeriesPointDrawingSample.Form1.totalWidth, CustomSeriesPointDrawingSample.Form1.totalHeight)
#End Region
Public Sub New()
Me.InitializeComponent()
End Sub
#Region "#ChartPreparation"
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
AddHandler Me.chart.CustomDrawSeriesPoint, AddressOf Me.OnCustomDrawSeriesPoint
AddHandler Me.chart.BoundDataChanged, AddressOf Me.OnBoundDataChanged
AddHandler Me.chart.ObjectHotTracked, AddressOf Me.OnObjectHotTracked
Using context = New CustomSeriesPointDrawingSample.Model.NwindDbContext()
Me.chart.DataSource = Me.PrepareDataSource(context.Orders)
Me.InitPhotoCache(context.Employees)
End Using
Me.chart.SeriesDataMember = "Year"
Me.chart.SeriesTemplate.ArgumentDataMember = "Employee"
Me.chart.SeriesTemplate.ValueDataMembers.AddRange("Value")
Me.chart.SeriesTemplate.ToolTipPointPattern = "{S}: {A} ({VP:P})"
Me.chart.SeriesTemplate.SeriesPointsSorting = DevExpress.XtraCharts.SortingMode.Ascending
End Sub
#End Region
#Region "#AutogeneratedSeriesModifying"
Private Sub OnBoundDataChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If Me.chart.Series.Count <= 1 Then Return
Dim i As Integer = 1
While i < Me.chart.Series.Count
Me.chart.Series(CInt((i))).ShowInLegend = False
Call System.Threading.Interlocked.Increment(i)
End While
End Sub
#End Region
#Region "#CustomPointDrawing"
Private Sub OnCustomDrawSeriesPoint(ByVal sender As Object, ByVal e As DevExpress.XtraCharts.CustomDrawSeriesPointEventArgs)
' Design a series marker image.
Dim image As DevExpress.Drawing.DXBitmap = New DevExpress.Drawing.DXBitmap(CustomSeriesPointDrawingSample.Form1.totalWidth, CustomSeriesPointDrawingSample.Form1.totalHeight)
Dim isSelected As Boolean = Me.trackedPointArgument IsNot Nothing AndAlso e.SeriesPoint.Argument.Equals(Me.trackedPointArgument)
Using graphics As DevExpress.Drawing.DXGraphics = DevExpress.Drawing.DXGraphics.FromImage(image)
Using fillBrush = If(isSelected, CType(New DevExpress.Drawing.DXHatchBrush(DevExpress.Drawing.DXHatchStyle.DarkDownwardDiagonal, e.LegendDrawOptions.Color, e.LegendDrawOptions.ActualColor2), DevExpress.Drawing.DXBrush), CType(New DevExpress.Drawing.DXSolidBrush(e.LegendDrawOptions.Color), DevExpress.Drawing.DXBrush))
graphics.FillRectangle(fillBrush, CustomSeriesPointDrawingSample.Form1.totalRect)
End Using
Dim photo As DevExpress.Drawing.DXImage
If Me.photoCache.TryGetValue(e.SeriesPoint.Argument, photo) Then graphics.DrawImage(photo, CustomSeriesPointDrawingSample.Form1.photoRect)
End Using
e.DXLegendMarkerImage = image
e.DisposeLegendMarkerImage = True
Dim options As DevExpress.XtraCharts.PieDrawOptions = TryCast(e.SeriesDrawOptions, DevExpress.XtraCharts.PieDrawOptions)
If isSelected AndAlso options IsNot Nothing Then
options.FillStyle.FillMode = DevExpress.XtraCharts.FillMode.Hatch
CType(options.FillStyle.Options, DevExpress.XtraCharts.HatchFillOptions).HatchStyle = System.Drawing.Drawing2D.HatchStyle.DarkDownwardDiagonal
End If
End Sub
#End Region
Private Sub OnObjectHotTracked(ByVal sender As Object, ByVal e As DevExpress.XtraCharts.HotTrackEventArgs)
Me.trackedPointArgument = If(e.HitInfo.InSeriesPoint, e.HitInfo.SeriesPoint.Argument, Nothing)
Me.chart.Invalidate()
End Sub
Private Sub InitPhotoCache(ByVal employees As System.Collections.Generic.IEnumerable(Of CustomSeriesPointDrawingSample.Model.Employee))
Me.photoCache.Clear()
For Each employee In employees
Using stream As System.IO.MemoryStream = New System.IO.MemoryStream(employee.Photo)
If Not Me.photoCache.ContainsKey(employee.FullName) Then Me.photoCache.Add(employee.FullName, DevExpress.Drawing.DXImage.FromStream(stream))
End Using
Next
End Sub
Private Function PrepareDataSource(ByVal orders As System.Collections.Generic.IEnumerable(Of CustomSeriesPointDrawingSample.Model.Order)) As List(Of SalesPoint)
Dim query = From o In orders Group o By __groupByKey1__ = New With {.Year = o.OrderDate.Year, .Employee = o.Employee.FirstName & " " & o.Employee.LastName} Into g = Group Select New With {.Employee = __groupByKey1__.Employee, .Year = __groupByKey1__.Year, .Values = g.[Select](Function(o) If(o.Freight.HasValue, o.Freight.Value, 0))}
Dim points As System.Collections.Generic.List(Of SalesPoint) = New System.Collections.Generic.List(Of SalesPoint)()
For Each item In query
points.Add(New SalesPoint With {.Employee = item.Employee, .Year = item.Year, .Value = item.Values.Aggregate(Function(d1, d2) d1 + d2)})
Next
Return points
End Function
End Class
End Namespace
Friend Class SalesPoint
Public Property Employee As String
Public Property Year As Integer
Public Property Value As Decimal
End Class