-
Notifications
You must be signed in to change notification settings - Fork 204
/
Organization Chart.cs
424 lines (400 loc) · 17.2 KB
/
Organization Chart.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#region Copyright Syncfusion Inc. 2001-2024.
// Copyright Syncfusion Inc. 2001-2024. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using CoreGraphics;
using Syncfusion.SfDiagram.iOS;
using UIKit;
using Foundation;
using System.Linq;
namespace SampleBrowser
{
public partial class OrganizationalChart : SampleView
{
SfDiagram diagram;
DataModel datamodel;
Dictionary<string, UIColor> FillColor;
Dictionary<string, CGColor> StrokeColor;
UIPickerView selectionPicker1;
private UILabel overviewLabel;
private UISwitch overviewSwitch;
private UILabel dragLabel;
private UISwitch dragSwitch;
OverviewPanel overviewPanel;
public OrganizationalChart()
{
selectionPicker1 = new UIPickerView();
this.OptionView = new UIView();
string deviceType = UIDevice.CurrentDevice.Model;
overviewLabel = new UILabel();
overviewLabel.Text = "Enable Overview";
overviewLabel.TextColor = UIColor.Black;
overviewLabel.TextAlignment = UITextAlignment.Left;
overviewLabel.BackgroundColor = UIColor.Clear;
overviewLabel.Frame = new CGRect(this.Frame.X + 10, 70, 150, 30);
overviewSwitch = new UISwitch();
overviewSwitch.On = true;
overviewSwitch.Frame = new CGRect(this.Frame.X + 250, 70, 50, 30);
overviewSwitch.TouchUpInside += OverviewSwitch_TouchUpInside;
overviewSwitch.BackgroundColor = UIColor.Clear;
dragLabel = new UILabel();
dragLabel.Text = "Change Hierarchy";
dragLabel.TextColor = UIColor.Black;
dragLabel.TextAlignment = UITextAlignment.Left;
dragLabel.BackgroundColor = UIColor.Clear;
dragLabel.Frame = new CGRect(this.Frame.X + 10, 10, 150, 30);
dragSwitch = new UISwitch();
dragSwitch.On = false;
dragSwitch.Frame = new CGRect(this.Frame.X + 250, 10, 50, 30);
dragSwitch.TouchUpInside += dragSwitch_TouchUpInside;
dragSwitch.BackgroundColor = UIColor.Clear;
diagram = new SfDiagram();
//Dictionary collection
FillColor = new Dictionary<string, UIColor>();
FillColor.Add("Managing Director", UIColor.FromRGB(239, 75, 93));
FillColor.Add("Project Manager", UIColor.FromRGB(49, 162, 255));
FillColor.Add("Senior Manager", UIColor.FromRGB(49, 162, 255));
FillColor.Add("Project Lead", UIColor.FromRGB(0, 194, 192));
FillColor.Add("Senior S/W Engg", UIColor.FromRGB(0, 194, 192));
FillColor.Add("Software Engg", UIColor.FromRGB(0, 194, 192));
FillColor.Add("Team Lead", UIColor.FromRGB(0, 194, 192));
FillColor.Add("Project Trainee", UIColor.FromRGB(255, 129, 0));
StrokeColor = new Dictionary<string, CGColor>();
StrokeColor.Add("Managing Director", UIColor.FromRGB(201, 32, 61).CGColor);
StrokeColor.Add("Project Manager", UIColor.FromRGB(23, 132, 206).CGColor);
StrokeColor.Add("Senior Manager", UIColor.FromRGB(23, 132, 206).CGColor);
StrokeColor.Add("Project Lead", UIColor.FromRGB(4, 142, 135).CGColor);
StrokeColor.Add("Senior S/W Engg", UIColor.FromRGB(4, 142, 135).CGColor);
StrokeColor.Add("Software Engg", UIColor.FromRGB(4, 142, 135).CGColor);
StrokeColor.Add("Team Lead", UIColor.FromRGB(4, 142, 135).CGColor);
StrokeColor.Add("Project Trainee", UIColor.FromRGB(206, 98, 9).CGColor);
diagram.BeginNodeRender += Dia_BeginNodeRender;
diagram.ItemLongPressed += Dia_ItemLongPressed;
diagram.LayoutNodeDropped += Diagram_OnLayoutNodeDropped;
diagram.BackgroundColor = UIColor.White;
diagram.EnableSelectors = false;
diagram.NodeClicked += Dia_NodeClicked;
diagram.Loaded += Dia_Loaded;
//Initialize Method
datamodel = new DataModel();
datamodel.Data();
//To Represent DataSourceSttings Properties
DataSourceSettings settings = new DataSourceSettings();
settings.ParentId = "ReportingPerson";
settings.Id = "Name";
settings.DataSource = datamodel.employee;
diagram.DataSourceSettings = settings;
//To Represent LayoutManager Properties
diagram.LayoutManager = new LayoutManager()
{
Layout = new DirectedTreeLayout()
{
Type = LayoutType.Organization,
HorizontalSpacing = 35,
}
};
for (int i = 0; i < diagram.Connectors.Count; i++)
{
diagram.Connectors[i].TargetDecoratorType = DecoratorType.None;
diagram.Connectors[i].Style.StrokeBrush = new SolidBrush(UIColor.FromRGB(127, 132, 133));
diagram.Connectors[i].Style.StrokeWidth = 1;
}
this.AddSubview(diagram);
diagram.Width = (float)this.Frame.Width;
diagram.Height = (float)this.Frame.Height;
OptionView.AddSubview(dragLabel);
OptionView.AddSubview(dragSwitch);
if (deviceType == "iPad")
{
OptionView.AddSubview(overviewLabel);
OptionView.AddSubview(overviewSwitch);
overviewPanel = new OverviewPanel();
overviewPanel.Layer.BorderColor = UIColor.Orange.CGColor;
overviewPanel.Layer.BorderWidth = 2;
overviewPanel.Frame = new CGRect(0,
0,
UIScreen.MainScreen.Bounds.Width / 2,
UIScreen.MainScreen.Bounds.Height / 4);
diagram.AddSubview(overviewPanel);
diagram.OverviewPanel = overviewPanel;
}
}
private void Diagram_OnLayoutNodeDropped(object sender, LayoutNodeDroppedEventArgs args)
{
Node draggedNode = args.DraggedItem as Node;
Node droppedNode = args.DroppedItem as Node;
bool contain = true;
if (draggedNode != null && draggedNode != droppedNode)
{
Node ParentNode = GetParent((droppedNode.Content as DiagramEmployee).ReportingPerson);
do
{
if (ParentNode != draggedNode)
{
contain = false;
}
else
{
contain = true;
break;
}
ParentNode = GetParent((ParentNode.Content as DiagramEmployee).ReportingPerson);
} while (ParentNode != null);
if (!contain)
{
Connector con;bool hasChild = false;
foreach (Connector connector in draggedNode.InConnectors)
{
con = connector;
con.SourceNode = droppedNode;
hasChild = true;
}
if (hasChild)
{
Node PrevParentNode = GetParent((draggedNode.Content as DiagramEmployee).ReportingPerson);
bool noChild = true;
foreach(Connector c in PrevParentNode.OutConnectors)
{
noChild = false;
}
if (PrevParentNode != null && noChild)
{
(PrevParentNode.Content as DiagramEmployee).HasChild = false;
DrawTemplate(PrevParentNode);
}
DiagramEmployee ParentEmployee = (droppedNode.Content as DiagramEmployee);
(draggedNode.Content as DiagramEmployee).ReportingPerson = ParentEmployee.Name;
ParentEmployee.HasChild = true;
ExpandAll(draggedNode);
UpdateTemplate(draggedNode);
UpdateTemplate(droppedNode);
}
droppedNode.IsExpanded = true;
diagram.LayoutManager.Layout.UpdateLayout();
if (overviewPanel != null)
overviewPanel.ForceRefresh();
}
}
}
private void UpdateTemplate(Node node)
{
var template = new UIView();
template.Frame = new CGRect(0, 0, node.Width, node.Height);
template.BackgroundColor = FillColor[(node.Content as DiagramEmployee).Designation];
template.Layer.BorderColor = StrokeColor[((node.Content as DiagramEmployee).Designation)];
template.Layer.CornerRadius = 4;
template.Layer.BorderWidth = 1;
//EMP IMAGE
var img = new UIImageView();
img.Frame = new CGRect(5, 8, 35, 35);
img.Image = UIImage.FromBundle((node.Content as DiagramEmployee).ImageUrl);
//EMP NAME
var name = new UILabel()
{
Text = (node.Content as DiagramEmployee).Name,
Font = UIFont.FromName(".SF UI Text", 12),
TextColor = UIColor.FromRGB(255, 255, 255),
Frame = new CGRect(45, -10, node.Width, node.Height)
};
name.Layer.ShouldRasterize = true;
name.Layer.RasterizationScale = UIScreen.MainScreen.Scale;
//EMP DESIGNATION
var designation = new UILabel()
{
Text = (node.Content as DiagramEmployee).Designation,
Font = UIFont.FromName(".SF UI Text", 12),
TextColor = UIColor.FromRGB(255, 255, 255),
Frame = new CGRect(45, 8, node.Width, node.Height)
};
designation.Layer.ShouldRasterize = true;
designation.Layer.RasterizationScale = UIScreen.MainScreen.Scale;
if ((node.Content as DiagramEmployee).HasChild)
{
var temp = 35;
if (node.Width == 190)
{
temp = 0;
}
else
temp = 35;
template.Frame = new CGRect(0, 0, node.Width + temp, node.Height);
var indication = new UILabel();
indication.Frame = new CGRect(template.Frame.Width - 35, template.Frame.Height / 2 - 22.5, 40, 40);
indication.Font = UIFont.FromName(".SF UI Text", 35);
indication.TextAlignment = UITextAlignment.Center;
indication.TextColor = UIColor.White;
indication.Text = "-";
template.AddSubview(indication);
var indicationLine = new UIView();
indicationLine.Frame = new CGRect(template.Frame.Width - 35, 0, 1, template.Frame.Height);
indicationLine.BackgroundColor = new UIColor(template.Layer.BorderColor);
template.AddSubview(indicationLine);
indication.Layer.ShouldRasterize = true;
indication.Layer.RasterizationScale = UIScreen.MainScreen.Scale;
}
template.AddSubview(img);
template.AddSubview(name);
template.AddSubview(designation);
node.Template = template;
}
private void ExpandAll(Node node)
{
if ((node.Content as DiagramEmployee).HasChild)
{
node.IsExpanded = true;
UpdateTemplate(node);
if (node.OutConnectors.Any())
{
foreach (var c in node.OutConnectors)
{
if (c.TargetNode != null)
{
ExpandAll(c.TargetNode);
}
}
}
}
}
private Node GetParent(string parentId)
{
foreach (Node node in diagram.Nodes)
{
if ((node.Content as DiagramEmployee).Name == parentId)
{
return node;
}
}
return null;
}
void dragSwitch_TouchUpInside(object sender, EventArgs e)
{
if ((sender as UISwitch).On)
{
(diagram.LayoutManager.Layout as DirectedTreeLayout).IsDraggable = true;
}
else
{
(diagram.LayoutManager.Layout as DirectedTreeLayout).IsDraggable = false;
}
}
void OverviewSwitch_TouchUpInside(object sender, EventArgs e)
{
if ((sender as UISwitch).On)
{
overviewPanel.Hidden = false;
}
else
{
overviewPanel.Hidden = true;
}
}
void Dia_Loaded(object sender)
{
diagram.BringToView(diagram.Nodes[0]);
}
void Dia_BeginNodeRender(object sender, BeginNodeRenderEventArgs args)
{
var node = (args.Item as Node);
node.Width = 155;
node.Height = 50;
DrawTemplate(node);
}
void DrawTemplate(Node node)
{
var template = new UIView();
template.Frame = new CGRect(0, 0, node.Width, node.Height);
template.BackgroundColor = FillColor[(node.Content as DiagramEmployee).Designation];
template.Layer.BorderColor = StrokeColor[((node.Content as DiagramEmployee).Designation)];
template.Layer.CornerRadius = 4;
template.Layer.BorderWidth = 1;
//EMP IMAGE
var img = new UIImageView();
img.Frame = new CGRect(5, 8, 35, 35);
img.Image = UIImage.FromBundle((node.Content as DiagramEmployee).ImageUrl);
//EMP NAME
var name = new UILabel()
{
Text = (node.Content as DiagramEmployee).Name,
Font = UIFont.FromName(".SF UI Text", 12),
TextColor = UIColor.FromRGB(255, 255, 255),
Frame = new CGRect(45, -10, node.Width, node.Height)
};
name.Layer.ShouldRasterize = true;
name.Layer.RasterizationScale = UIScreen.MainScreen.Scale;
//EMP DESIGNATION
var designation = new UILabel()
{
Text = (node.Content as DiagramEmployee).Designation,
Font = UIFont.FromName(".SF UI Text", 12),
TextColor = UIColor.FromRGB(255, 255, 255),
Frame = new CGRect(45, 8, node.Width, node.Height)
};
designation.Layer.ShouldRasterize = true;
designation.Layer.RasterizationScale = UIScreen.MainScreen.Scale;
if ((node.Content as DiagramEmployee).HasChild)
{
template.Frame = new CGRect(0, 0, node.Width + 35, node.Height);
var indication = new UILabel();
indication.Frame = new CGRect(node.Width - 2, node.Height / 2 - 22.5, 40, 40);
indication.Font = UIFont.FromName(".SF UI Text", 35);
indication.TextAlignment = UITextAlignment.Center;
indication.TextColor = UIColor.White;
indication.Text = "-";
template.AddSubview(indication);
var indicationLine = new UIView();
indicationLine.Frame = new CGRect(node.Width - 5, 0, 1, node.Height);
indicationLine.BackgroundColor = new UIColor(template.Layer.BorderColor);
template.AddSubview(indicationLine);
indication.Layer.ShouldRasterize = true;
indication.Layer.RasterizationScale = UIScreen.MainScreen.Scale;
}
template.AddSubview(img);
template.AddSubview(name);
template.AddSubview(designation);
node.Template = template;
}
void Dia_ItemLongPressed(object sender, ItemLongPressedEventArgs args)
{
if (!((diagram.LayoutManager.Layout as DirectedTreeLayout).IsDraggable))
{
var node = args.Item as Node;
var info = new UIAlertView();
info.AddButton("OK");
info.Message = "Name : " + (node.Content as DiagramEmployee).Name + "\n" +
"Designation : " + (node.Content as DiagramEmployee).Designation + "\n" +
"ID : " + (node.Content as DiagramEmployee).ID + "\n" +
"DOJ : " + (node.Content as DiagramEmployee).DOJ + "\n" +
"Reporting Person : " + (node.Content as DiagramEmployee).ReportingPerson;
info.Show();
}
}
void Dia_NodeClicked(object sender, NodeClickedEventArgs args)
{
if (args.Item.IsExpanded)
{
if ((args.Item.Content as DiagramEmployee).HasChild)
{
((args.Item.Template as UIView).Subviews[0] as UILabel).Font = UIFont.SystemFontOfSize(30);
((args.Item.Template as UIView).Subviews[0] as UILabel).Text = "+";
}
args.Item.IsExpanded = false;
}
else
{
if ((args.Item.Content as DiagramEmployee).HasChild)
{
((args.Item.Template as UIView).Subviews[0] as UILabel).Font = UIFont.SystemFontOfSize(35);
((args.Item.Template as UIView).Subviews[0] as UILabel).Text = "-";
}
args.Item.IsExpanded = true;
}
}
}
}