-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathOrganizationChart.cs
573 lines (502 loc) · 22.9 KB
/
OrganizationChart.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
using System;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Views;
using Android.Widget;
using Syncfusion.SfDiagram.Android;
using System.IO;
using System.Reflection;
using Android.Util;
namespace SampleBrowser
{
public partial class OrganizationChart : SamplePage
{
private bool isTablet;
SfDiagram diagram;
DataModel dataModel;
Dictionary<string, Color> FillColor;
Dictionary<string, Color> StrokeColor;
private ExpandButton expanded;
LinearLayout linearLayout4;
float currentDensity = 1;
public override View GetSampleContent(Context context)
{
Display display = ((Activity)context).WindowManager.DefaultDisplay;
DisplayMetrics displayMetrics = new DisplayMetrics();
display.GetMetrics(displayMetrics);
var wInches = displayMetrics.WidthPixels / (double)displayMetrics.DensityDpi;
var hInches = displayMetrics.HeightPixels / (double)displayMetrics.DensityDpi;
double screenDiagonal = Math.Sqrt(Math.Pow(wInches, 2) + Math.Pow(hInches, 2));
isTablet = screenDiagonal >= 7.0;
//Create SfDiagram.
diagram = new SfDiagram(context);
diagram.EnableSelection = false;
FillColor = new Dictionary<string, Color>();
FillColor.Add("Managing Director", Color.Rgb(239, 75, 93));
FillColor.Add("Project Manager", Color.Rgb(49, 162, 255));
FillColor.Add("Senior Manager", Color.Rgb(49, 162, 255));
FillColor.Add("Project Lead", Color.Rgb(0, 194, 192));
FillColor.Add("Senior S/W Engg", Color.Rgb(0, 194, 192));
FillColor.Add("Software Engg", Color.Rgb(0, 194, 192));
FillColor.Add("Team Lead", Color.Rgb(0, 194, 192));
FillColor.Add("Project Trainee", Color.Rgb(255, 129, 0));
StrokeColor = new Dictionary<string, Color>();
StrokeColor.Add("Managing Director", Color.Rgb(201, 32, 61));
StrokeColor.Add("Project Manager", Color.Rgb(23, 132, 206));
StrokeColor.Add("Senior Manager", Color.Rgb(23, 132, 206));
StrokeColor.Add("Project Lead", Color.Rgb(4, 142, 135));
StrokeColor.Add("Senior S/W Engg", Color.Rgb(4, 142, 135));
StrokeColor.Add("Software Engg", Color.Rgb(4, 142, 135));
StrokeColor.Add("Team Lead", Color.Rgb(4, 142, 135));
StrokeColor.Add("Project Trainee", Color.Rgb(206, 98, 9));
dataModel = new DataModel();
diagram.BackgroundColor = Color.White;
diagram.BeginNodeRender += Dia_BeginNodeRender;
dataModel.Data();
//To Represent DataSourceSttings Properties
DataSourceSettings settings = new DataSourceSettings();
settings.ParentId = "ReportingPerson";
settings.Id = "Name";
settings.DataSource = dataModel.employee;
diagram.DataSourceSettings = settings;
//(diagram.LayoutManager.Layout as DirectedTreeLayout).IsDraggable
//To Represent LayoutManager Properties
diagram.LayoutManager = new LayoutManager()
{
Layout = new DirectedTreeLayout()
{
Type = LayoutType.Organization,
HorizontalSpacing = 70 * MainActivity.Factor,
VerticalSpacing = 70 * MainActivity.Factor
}
};
for (int i = 0; i < diagram.Connectors.Count; i++)
{
diagram.Connectors[i].TargetDecoratorType = DecoratorType.None;
diagram.Connectors[i].Style.StrokeBrush = new SolidBrush(Color.Rgb(127, 132, 133));
}
diagram.NodeClicked += Diagram_NodeClicked;
diagram.ItemLongPressed += Diagram_ItemLongPressed;
diagram.Loaded += Diagram_Loaded;
int width = (int)(context.Resources.DisplayMetrics.WidthPixels - context.Resources.DisplayMetrics.Density) / 3;
linearLayout4 = new LinearLayout(context);
linearLayout4.Orientation = Android.Widget.Orientation.Vertical;
linearLayout4.SetMinimumHeight((int)(190 * currentDensity));
linearLayout4.SetMinimumWidth(width);
diagram.LayoutNodeDropped += Diagram_OnLayoutNodeDropped;
return diagram;
}
public override void Destroy()
{
if (diagram != null)
diagram.Dispose();
base.Destroy();
}
OverviewPanel overview;
private void Diagram_Loaded(object sender)
{
diagram.BringToView(diagram.Nodes[0]);
if (isTablet)
{
overview = new CustomOverview(diagram.Context);
diagram.OverviewPanel = overview;
overview.PreventRefresh = true;
overview.Layout(0, 0, 400, 400);
diagram.AddView(overview);
overview.ForceRefresh();
}
}
internal class CustomOverview : OverviewPanel
{
internal CustomOverview(Context context) : base(context)
{
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Paint paint = new Paint();
paint.SetStyle(Paint.Style.Stroke);
paint.StrokeWidth = 3 * Resources.DisplayMetrics.Density;
paint.Color = Color.Orange;
SetPadding(10, 10, 10, 10);
canvas.DrawRect(new Rect(Left, Top, Right, Bottom), paint);
}
}
public override View GetPropertyWindowLayout(Context context)
{
LinearLayout gridLinearLayout = new LinearLayout(context) { Orientation = Android.Widget.Orientation.Vertical };
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
layoutParams.TopMargin = (int)(25 * currentDensity);
gridLinearLayout.LayoutParameters = layoutParams;
gridLinearLayout.SetBackgroundResource(Resource.Drawable.LinearLayout_Border);
int width = (int)(context.Resources.DisplayMetrics.WidthPixels - context.Resources.DisplayMetrics.Density);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.Orientation = Android.Widget.Orientation.Vertical;
linearLayout.SetMinimumHeight((int)(190 * currentDensity));
linearLayout.SetMinimumWidth(width);
LinearLayout overviewLayout = new LinearLayout(context);
overviewLayout.SetPadding(0, (int)(10 * currentDensity), 0, (int)(10 * currentDensity));
overviewLayout.SetMinimumHeight((int)(30 * currentDensity));
TextView overviewLabel = new TextView(context)
{
Text = "Enable Overview",
Gravity = GravityFlags.Start,
TextSize = 15 * currentDensity
};
overviewLabel.SetMinimumHeight((int)(25 * currentDensity));
overviewLabel.SetWidth((int)(width * 0.4 * currentDensity));
Switch overviewSwitch = new Switch(context);
overviewSwitch.Checked = true;
overviewSwitch.CheckedChange += OverviewSwitch_CheckedChange;
overviewSwitch.Gravity = GravityFlags.Right;
overviewSwitch.SetMinimumHeight((int)(25 * currentDensity));
overviewSwitch.SetWidth((int)(width * 0.4 * currentDensity));
overviewLayout.AddView(overviewLabel);
overviewLayout.AddView(overviewSwitch);
LinearLayout interactionLayout = new LinearLayout(context);
interactionLayout.SetPadding(0, (int)(10 * currentDensity), 0, (int)(10 * currentDensity));
interactionLayout.SetMinimumHeight((int)(30 * currentDensity));
TextView interactionLabel = new TextView(context)
{
Text = "Change Hierarchy",
Gravity = GravityFlags.Start,
TextSize = 15 * currentDensity
};
interactionLabel.SetMinimumHeight((int)(25 * currentDensity));
interactionLabel.SetWidth((int)(width * 0.4 * currentDensity));
Switch interactionSwitch = new Switch(context);
interactionSwitch.CheckedChange += LayoutNodeDragSwitch_CheckedChange;
interactionSwitch.Gravity = GravityFlags.Right;
interactionSwitch.SetMinimumHeight((int)(25 * currentDensity));
interactionSwitch.SetWidth((int)(width * 0.4 * currentDensity));
interactionLayout.AddView(interactionLabel);
interactionLayout.AddView(interactionSwitch);
if (isTablet)
linearLayout.AddView(overviewLayout);
linearLayout.AddView(interactionLayout);
gridLinearLayout.AddView(linearLayout);
return gridLinearLayout;
}
private void OverviewSwitch_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
if (e.IsChecked)
{
overview.Visibility = ViewStates.Visible;
}
else
{
overview.Visibility = ViewStates.Invisible;
}
}
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)
{
List<Connector> connectors = draggedNode.InConnectors as List<Connector>;
Connector con;
bool hasChild = false;
for (int i = 0; i < connectors.Count; i++)
{
con = connectors[i];
con.SourceNode = droppedNode;
hasChild = true;
}
if (hasChild)
{
Node PrevParentNode = GetParent((draggedNode.Content as DiagramEmployee).ReportingPerson);
if (PrevParentNode != null && PrevParentNode.OutConnectors.Count() == 0)
{
(PrevParentNode.Content as DiagramEmployee).HasChild = false;
DrawTemplate(PrevParentNode);
}
DiagramEmployee ParentEmployee = (droppedNode.Content as DiagramEmployee);
(draggedNode.Content as DiagramEmployee).ReportingPerson = ParentEmployee.Name;
ParentEmployee.HasChild = true;
DrawTemplate(droppedNode);
ExpandAll(draggedNode);
}
(draggedNode.Content as DiagramEmployee).ReportingPerson = (droppedNode.Content as DiagramEmployee).Name;
droppedNode.IsExpanded = true;
diagram.LayoutManager.Layout.UpdateLayout();
if (overview != null)
overview.ForceRefresh();
}
}
}
private void ExpandAll(Node node)
{
if ((node.Content as DiagramEmployee).HasChild)
{
node.IsExpanded = true;
DrawTemplate(node);
if (node.OutConnectors.Count() > 0)
{
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;
}
private void LayoutNodeDragSwitch_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
if (e.IsChecked)
{
(diagram.LayoutManager.Layout as DirectedTreeLayout).IsDraggable = true;
}
else
{
(diagram.LayoutManager.Layout as DirectedTreeLayout).IsDraggable = false;
}
}
private void Diagram_ItemLongPressed(object sender, ItemLongPressedEventArgs args)
{
if (args.Item is Node)
{
DisplayInfo(args.Item as Node);
diagram.NodeClicked -= Diagram_NodeClicked;
}
}
private void Diagram_NodeClicked(object sender, NodeClickedEventArgs args)
{
Node node = args.Item;
if (node.IsExpanded)
{
node.IsExpanded = false;
if ((node.Content as DiagramEmployee).HasChild)
(node.Template.GetChildAt(0) as TextView).Text = "+";
}
else
{
node.IsExpanded = true;
if ((node.Content as DiagramEmployee).HasChild)
(node.Template.GetChildAt(0) as TextView).Text = "-";
}
expanded.Invalidate();
}
internal MemoryStream LoadResource(String name)
{
MemoryStream aMem = new MemoryStream();
var assm = Assembly.GetExecutingAssembly();
var path = String.Format("SampleBrowser.Resources.drawable.{0}", name);
var aStream = assm.GetManifestResourceStream(path);
aStream.CopyTo(aMem);
return aMem;
}
ViewGroup DrawTemplate(Node node)
{
if ((node.Content as DiagramEmployee).HasChild)
node.Width = 470 * MainActivity.Factor;
else
node.Width = 370 * MainActivity.Factor;
node.Height = 120 * MainActivity.Factor;
node.Style.StrokeWidth = 2 * MainActivity.Factor;
node.Style.StrokeBrush = new SolidBrush(Color.Black);
node.ShapeType = ShapeType.RoundedRectangle;
node.CornerRadius = 10 * MainActivity.Factor;
//TEMPLATE
var template = new Shape(this, diagram.Context, node, dataModel, FillColor, StrokeColor);
template.Layout(0, 0, (int)node.Width, (int)node.Height);
//EMP IMAGE
var img = new ImageView(diagram.Context);
var imageId = (node.Content as DiagramEmployee).ImageUrl;
if (imageId != null)
{
var imageData = LoadResource(imageId).ToArray();
img.SetImageBitmap(BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length));
}
img.SetPadding(0, (int)(10 * MainActivity.Factor), 0, 0);
img.Layout((int)(10 * MainActivity.Factor), (int)(10 * MainActivity.Factor), (int)(100 * MainActivity.Factor), (int)(100 * MainActivity.Factor));
//Name of the employee.
var name = new TextView(diagram.Context);
name.Text = (node.Content as DiagramEmployee).Name;
name.SetTextSize(Android.Util.ComplexUnitType.Px, 28 * MainActivity.Factor);
name.SetTextColor(Color.White);
name.SetPadding(0, (int)(10 * MainActivity.Factor), 0, 0);
name.Layout((int)(110 * MainActivity.Factor), (int)(10 * MainActivity.Factor), (int)node.Width, (int)node.Height);
//Designation of the employee.
var designation = new TextView(diagram.Context);
designation.Text = (node.Content as DiagramEmployee).Designation;
designation.SetTextSize(Android.Util.ComplexUnitType.Px, 28 * MainActivity.Factor);
designation.SetTextColor(Color.White);
designation.SetPadding(0, (int)(10 * MainActivity.Factor), 0, 0);
designation.Layout((int)(110 * MainActivity.Factor), (int)(50 * MainActivity.Factor), (int)node.Width, (int)node.Height);
if ((node.Content as DiagramEmployee).HasChild)
{
expanded = new ExpandButton(diagram.Context, node, FillColor);
template.SetExpanded(expanded);
expanded.Text = "-";
expanded.Typeface = Typeface.Create("Times New Roman", TypefaceStyle.Bold);
expanded.TextAlignment = TextAlignment.Center;
expanded.Gravity = GravityFlags.Center;
expanded.SetTextSize(Android.Util.ComplexUnitType.Px, 54 * MainActivity.Factor);
expanded.SetTextColor(Color.White);
expanded.SetPadding(0, (int)(10 * MainActivity.Factor), 0, 0);
expanded.SetColor(template.m_color);
expanded.SetStrokeColor(template.m_strokeColor);
expanded.Layout((int)(350 * MainActivity.Factor), 0, (int)(node.Width - 10 * MainActivity.Factor), (int)node.Height);
template.AddView(expanded);
}
//Add the view to the template instance.
template.AddView(img);
template.AddView(name);
template.AddView(designation);
node.Template = template;
return template;
}
private void DisplayInfo(Node node)
{
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(diagram.Context);
var employee = (node.Content as DiagramEmployee);
EditText editText = new EditText(diagram.Context);
editText.Text = "Name : " + employee.Name + "\n\n" + "Designation : " + employee.Designation + "\n\n" + "ID : " + employee.ID + "\n\n" + "DOJ : " + employee.DOJ;
editText.SetBackgroundColor(Color.WhiteSmoke);
editText.SetTextColor(Color.Black);
editText.Enabled = false;
alertBuilder.SetView(editText);
alertBuilder.SetCancelable(false);
alertBuilder.SetPositiveButton("OK", (senderAlert, args) =>
{
diagram.NodeClicked += Diagram_NodeClicked;
});
alertBuilder.Show();
}
internal class ExpandButton : TextView
{
private Node m_node;
private Color m_strokeColor;
private Paint paint;
private Dictionary<string, Color> m_fillColor;
internal ExpandButton(Context context, Node node, Dictionary<string, Color> fillColor) : base(context)
{
m_node = node;
SetWillNotDraw(false);
SetBackgroundColor(Color.Transparent);
m_fillColor = fillColor;
}
protected override void OnDraw(Canvas canvas)
{
paint = new Android.Graphics.Paint();
paint.AntiAlias = true;
paint.SetStyle(Android.Graphics.Paint.Style.Stroke);
paint.Color = m_strokeColor;
paint.StrokeWidth = 2 * MainActivity.Factor;
canvas.DrawLine(0, 0, 0, m_node.Height, paint);
base.OnDraw(canvas);
}
internal void SetColor(Color color)
{
SetBackgroundColor(color);
}
internal void SetStrokeColor(Color strokeColor)
{
m_strokeColor = strokeColor;
}
}
void Dia_BeginNodeRender(object sender, BeginNodeRenderEventArgs args)
{
//Set the node size and its properties.
var node = (args.Item as Node);
DrawTemplate(node);
}
public override void OnApplyChanges()
{
//Applies the changes to the layout.
(diagram.LayoutManager.Layout as DirectedTreeLayout).UpdateLayout();
}
internal class Shape : ViewGroup
{
internal Color m_color;
internal Color m_strokeColor;
private ExpandButton m_expanded;
private Node m_node;
private Dictionary<string, Color> m_fillColor;
private OrganizationChart m_chart;
internal Shape(OrganizationChart chart, Context context, Node node, DataModel dataModel, Dictionary<string, Color> fillColor, Dictionary<string, Color> strokeColor) : base(context)
{
m_chart = chart;
SetX(0);
SetY(0);
if (LayoutParameters == null)
LayoutParameters = new ViewGroup.LayoutParams((int)node.Width, (int)node.Height);
else
{
LayoutParameters.Height = (int)node.Width;
LayoutParameters.Width = (int)node.Height;
}
m_node = node;
SetBackgroundColor(Color.Transparent);
m_fillColor = fillColor;
SetColor(fillColor[(node.Content as DiagramEmployee).Designation]);
SetStrokeColor(strokeColor[(node.Content as DiagramEmployee).Designation]);
}
protected override void OnDraw(Canvas canvas)
{
float cornerRadius = 20 * MainActivity.Factor;
Paint paint = new Paint();
paint.SetStyle(Paint.Style.Stroke);
paint.StrokeWidth = 2 * MainActivity.Factor;
paint.Color = m_strokeColor;
paint.AntiAlias = true;
canvas.DrawRoundRect(Left, Top, Right, Bottom, cornerRadius, cornerRadius, paint);
paint = new Paint();
paint.SetStyle(Paint.Style.Fill);
paint.AntiAlias = true;
paint.Color = m_color;
canvas.DrawRoundRect(Left, Top, Right, Bottom, cornerRadius, cornerRadius, paint);
base.OnDraw(canvas);
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
}
internal void SetColor(Color color)
{
m_color = color;
}
internal void SetStrokeColor(Color color)
{
m_strokeColor = color;
}
internal void SetExpanded(ExpandButton expanded)
{
m_expanded = expanded;
}
}
}
}