-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
140 lines (123 loc) · 5.57 KB
/
Form1.cs
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
131
132
133
134
135
136
137
138
139
140
using CustomSeriesPointDrawingSample.Model;
using DevExpress.Drawing;
using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace CustomSeriesPointDrawingSample {
public partial class Form1 : Form {
object trackedPointArgument;
Dictionary<string, DXImage> photoCache = new Dictionary<string, DXImage>();
#region #Constants
const int borderSize = 5;
const int scaledPhotoWidth = 48;
const int scaledPhotoHeight = 51;
// Width and height of scaled photo with border.
const int totalWidth = 58;
const int totalHeight = 61;
// Rects required to create a custom legend series marker.
static readonly Rectangle photoRect = new Rectangle(
borderSize, borderSize,
scaledPhotoWidth, scaledPhotoHeight);
static readonly Rectangle totalRect = new Rectangle(
0, 0,
totalWidth, totalHeight);
#endregion
public Form1() {
InitializeComponent();
}
#region #ChartPreparation
private void Form1_Load(object sender, EventArgs e) {
chart.CustomDrawSeriesPoint += OnCustomDrawSeriesPoint;
chart.BoundDataChanged += OnBoundDataChanged;
chart.ObjectHotTracked += OnObjectHotTracked;
using (var context = new NwindDbContext()) {
chart.DataSource = PrepareDataSource(context.Orders);
InitPhotoCache(context.Employees);
}
chart.SeriesDataMember = "Year";
chart.SeriesTemplate.ArgumentDataMember = "Employee";
chart.SeriesTemplate.ValueDataMembers.AddRange("Value");
chart.SeriesTemplate.ToolTipPointPattern = "{S}: {A} ({VP:P})";
chart.SeriesTemplate.SeriesPointsSorting = SortingMode.Ascending;
}
#endregion
#region #AutogeneratedSeriesModifying
private void OnBoundDataChanged(object sender, EventArgs e) {
if (chart.Series.Count <= 1) return;
for (int i = 1; i < chart.Series.Count; ++i)
chart.Series[i].ShowInLegend = false;
}
#endregion
#region #CustomPointDrawing
private void OnCustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e) {
// Design a series marker image.
DXBitmap image = new DXBitmap(totalWidth, totalHeight);
bool isSelected = trackedPointArgument != null && e.SeriesPoint.Argument.Equals(trackedPointArgument);
using (DXGraphics graphics = DXGraphics.FromImage(image)) {
using (var fillBrush = isSelected ? (DXBrush)new DXHatchBrush(DXHatchStyle.DarkDownwardDiagonal,
e.LegendDrawOptions.Color,
e.LegendDrawOptions.ActualColor2)
: (DXBrush)new DXSolidBrush(e.LegendDrawOptions.Color)) {
graphics.FillRectangle(fillBrush, totalRect);
}
DXImage photo;
if (photoCache.TryGetValue(e.SeriesPoint.Argument, out photo))
graphics.DrawImage(photo, photoRect);
}
e.DXLegendMarkerImage = image;
e.DisposeLegendMarkerImage = true;
PieDrawOptions options = e.SeriesDrawOptions as PieDrawOptions;
if (isSelected && options != null) {
options.FillStyle.FillMode = DevExpress.XtraCharts.FillMode.Hatch;
((HatchFillOptions)options.FillStyle.Options).HatchStyle = HatchStyle.DarkDownwardDiagonal;
}
}
#endregion
private void OnObjectHotTracked(object sender, HotTrackEventArgs e) {
trackedPointArgument = e.HitInfo.InSeriesPoint ? e.HitInfo.SeriesPoint.Argument : null;
chart.Invalidate();
}
void InitPhotoCache(IEnumerable<Employee> employees) {
photoCache.Clear();
foreach (var employee in employees) {
using (MemoryStream stream = new MemoryStream(employee.Photo)) {
if (!photoCache.ContainsKey(employee.FullName))
photoCache.Add(employee.FullName, DXImage.FromStream(stream));
}
}
}
List<SalesPoint> PrepareDataSource(IEnumerable<Order> orders) {
var query = from o in orders
group o by new {
Year = o.OrderDate.Year,
Employee = o.Employee.FirstName + " " + o.Employee.LastName
}
into g
select new {
Employee = g.Key.Employee,
Year = g.Key.Year,
Values = g.Select(o => o.Freight.HasValue ? o.Freight.Value : 0)
};
List<SalesPoint> points = new List<SalesPoint>();
foreach (var item in query) {
points.Add(new SalesPoint {
Employee = item.Employee,
Year = item.Year,
Value = item.Values.Aggregate((d1, d2) => d1 + d2)
});
}
return points;
}
}
}
class SalesPoint {
public string Employee { get; set; }
public int Year { get; set; }
public decimal Value { get; set; }
}