-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGoodBadCataloguePieChart.cs
304 lines (244 loc) · 11.1 KB
/
GoodBadCataloguePieChart.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
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Rdmp.Core.CommandExecution;
using Rdmp.Core.CommandExecution.AtomicCommands;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.Curation.Data.Dashboarding;
using Rdmp.Core.Icons.IconProvision;
using Rdmp.UI.DashboardTabs.Construction;
using Rdmp.UI.ItemActivation;
using Rdmp.UI.Refreshing;
using Rdmp.UI.SimpleDialogs;
using Rdmp.UI.TestsAndSetup.ServicePropogation;
namespace Rdmp.UI.PieCharts;
/// <summary>
/// Part of OverviewScreen, shows a pie chart showing ow many extractable columns are there which do not yet have descriptions in the Data Catalogue Database (See CatalogueItemUI)
///
/// <para>Each of these can either be displayed for a single catalogue or as a combined total across all active catalogues (not deprecated / internal etc)</para>
///
/// </summary>
public partial class GoodBadCataloguePieChart : RDMPUserControl, IDashboardableControl
{
private ToolStripButton btnSingleCatalogue = new("Single", CatalogueIcons.Catalogue.ImageToBitmap())
{ Name = "btnSingleCatalogue" };
private ToolStripButton btnAllCatalogues =
new("All", CatalogueIcons.AllCataloguesUsedByLoadMetadataNode.ImageToBitmap()) { Name = "btnAllCatalogues" };
private ToolStripButton btnRefresh = new("Refresh", FamFamFamIcons.text_list_bullets.ImageToBitmap())
{ Name = "btnRefresh" };
private ToolStripLabel toolStripLabel1 = new("Type:") { Name = "toolStripLabel1" };
private ToolStripButton btnShowLabels = new("Labels", FamFamFamIcons.text_align_left.ImageToBitmap())
{ Name = "btnShowLabels", CheckOnClick = true };
private List<ToolStripMenuItem> _flagOptions = new();
public GoodBadCataloguePieChart()
{
InitializeComponent();
btnViewDataTable.Image = CatalogueIcons.TableInfo.ImageToBitmap();
btnAllCatalogues.Click += btnAllCatalogues_Click;
btnSingleCatalogue.Click += btnSingleCatalogue_Click;
btnShowLabels.CheckStateChanged += btnShowLabels_CheckStateChanged;
btnRefresh.Click += btnRefresh_Click;
//put edit mode on for the designer
NotifyEditModeChange(false);
}
private void SetupFlags()
{
if (!firstTime)
return;
AddFlag("Non Extractable Catalogues", c => c.IncludeNonExtractableCatalogues,
(c, r) => c.IncludeNonExtractableCatalogues = r);
AddFlag("Deprecated Catalogues", c => c.IncludeDeprecatedCatalogues,
(c, r) => c.IncludeDeprecatedCatalogues = r);
AddFlag("Internal Catalogues", c => c.IncludeInternalCatalogues, (c, r) => c.IncludeInternalCatalogues = r);
AddFlag("Cold Storage Catalogues", c => c.IncludeColdStorageCatalogues,
(c, r) => c.IncludeColdStorageCatalogues = r);
AddFlag("Project Specific Catalogues", c => c.IncludeProjectSpecificCatalogues,
(c, r) => c.IncludeProjectSpecificCatalogues = r);
AddFlag("Non Extractable CatalogueItems", c => c.IncludeNonExtractableCatalogueItems,
(c, r) => c.IncludeNonExtractableCatalogueItems = r);
AddFlag("Internal Catalogue Items", c => c.IncludeInternalCatalogueItems,
(c, r) => c.IncludeInternalCatalogueItems = r);
AddFlag("Deprecated Catalogue Items", c => c.IncludeDeprecatedCatalogueItems,
(c, r) => c.IncludeDeprecatedCatalogueItems = r);
firstTime = false;
}
private void AddFlag(string caption, Func<GoodBadCataloguePieChartObjectCollection, bool> getProp,
Action<GoodBadCataloguePieChartObjectCollection, bool> setProp)
{
var btn = new ToolStripMenuItem(caption)
{
Checked = getProp(_collection)
};
btn.CheckedChanged += (sender, e) => { setProp(_collection, ((ToolStripMenuItem)sender).Checked); };
btn.CheckedChanged += (s, e) => GenerateChart();
btn.CheckOnClick = true;
_flagOptions.Add(btn);
}
private DashboardControl _dashboardControlDatabaseRecord;
private GoodBadCataloguePieChartObjectCollection _collection;
private void GenerateChart()
{
chart1.Visible = false;
lblNoIssues.Visible = false;
gbWhatThisIs.Text = _collection.IsSingleCatalogueMode
? $"Column Descriptions in {_collection.GetSingleCatalogueModeCatalogue()}"
: "Column Descriptions";
PopulateAsEmptyDescriptionsChart();
}
private void PopulateAsEmptyDescriptionsChart()
{
try
{
var catalogueItems = GetCatalogueItems();
if (!catalogueItems.Any())
{
chart1.DataSource = null;
chart1.Visible = false;
lblNoIssues.Visible = true;
return;
}
var countPopulated = 0;
var countNotPopulated = 0;
foreach (var ci in catalogueItems)
if (string.IsNullOrWhiteSpace(ci.Description))
countNotPopulated++;
else
countPopulated++;
var dt = new DataTable();
dt.Columns.Add("Count");
dt.Columns.Add("State");
dt.Rows.Add(new object[] { countNotPopulated, $"Missing ({countNotPopulated})" });
dt.Rows.Add(new object[] { countPopulated, $"Populated ({countPopulated})" });
chart1.Series[0].XValueMember = dt.Columns[1].ColumnName;
chart1.Series[0].YValueMembers = dt.Columns[0].ColumnName;
chart1.DataSource = dt;
chart1.DataBind();
chart1.Visible = true;
lblNoIssues.Visible = false;
}
catch (Exception e)
{
ExceptionViewer.Show($"{GetType().Name} failed to load data", e);
}
}
private CatalogueItem[] GetCatalogueItems()
{
if (!_collection.IsSingleCatalogueMode)
{
var catalogues = Activator.CoreChildProvider.AllCatalogues
.Where(c => _collection.Include(c, Activator.RepositoryLocator.DataExportRepository)).ToArray();
//if there are some
return catalogues.Any()
? catalogues.SelectMany(c => c.CatalogueItems).Where(ci => _collection.Include(ci)).ToArray()
: //get the extractable columns
Array.Empty<CatalogueItem>(); //there weren't any so Catalogues so won't be any ExtractionInformationsEither
}
return _collection.GetSingleCatalogueModeCatalogue().CatalogueItems;
}
public void RefreshBus_RefreshObject(object sender, RefreshObjectEventArgs e)
{
}
private bool _bLoading;
private bool firstTime = true;
public void SetCollection(IActivateItems activator, IPersistableObjectCollection collection)
{
_bLoading = true;
SetItemActivator(activator);
_collection = (GoodBadCataloguePieChartObjectCollection)collection;
if (firstTime)
SetupFlags();
btnAllCatalogues.Checked = !_collection.IsSingleCatalogueMode;
btnSingleCatalogue.Checked = _collection.IsSingleCatalogueMode;
btnShowLabels.Checked = _collection.ShowLabels;
CommonFunctionality.Add(btnAllCatalogues);
CommonFunctionality.Add(toolStripLabel1);
CommonFunctionality.Add(btnAllCatalogues);
CommonFunctionality.Add(btnSingleCatalogue);
CommonFunctionality.Add(btnShowLabels);
CommonFunctionality.Add(btnRefresh);
foreach (var mi in _flagOptions)
CommonFunctionality.AddToMenu(mi);
GenerateChart();
_bLoading = false;
}
public IPersistableObjectCollection GetCollection() => _collection;
public string GetTabName() => Text;
public string GetTabToolTip() => null;
public IPersistableObjectCollection ConstructEmptyCollection(DashboardControl databaseRecord)
{
_dashboardControlDatabaseRecord = databaseRecord;
return new GoodBadCataloguePieChartObjectCollection();
}
public void NotifyEditModeChange(bool isEditModeOn)
{
var l = new Point(Margin.Left, Margin.Right);
var s = new Size(Width - Margin.Horizontal, Height - Margin.Vertical);
CommonFunctionality.ToolStrip.Visible = isEditModeOn;
if (isEditModeOn)
{
gbWhatThisIs.Location = l with { Y = l.Y + 25 }; //move it down 25 to allow space for tool bar
gbWhatThisIs.Size = s with { Height = s.Height - 25 }; //and adjust height accordingly
}
else
{
gbWhatThisIs.Location = l;
gbWhatThisIs.Size = s;
}
}
private void btnAllCatalogues_Click(object sender, EventArgs e)
{
btnAllCatalogues.Checked = true;
btnSingleCatalogue.Checked = false;
_collection.SetAllCataloguesMode();
GenerateChart();
SaveCollectionChanges();
}
private void btnSingleCatalogue_Click(object sender, EventArgs e)
{
if (!Activator.SelectObject(new DialogArgs
{
TaskDescription = "Which Catalogue should the graph depict?"
}, Activator.RepositoryLocator.CatalogueRepository.GetAllObjects<Catalogue>(), out var selected)) return;
_collection.SetSingleCatalogueMode(selected);
btnAllCatalogues.Checked = false;
btnSingleCatalogue.Checked = true;
SaveCollectionChanges();
GenerateChart();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
GenerateChart();
}
private void SaveCollectionChanges()
{
if (_bLoading)
return;
_dashboardControlDatabaseRecord.SaveCollectionState(_collection);
}
private void btnShowLabels_CheckStateChanged(object sender, EventArgs e)
{
_collection.ShowLabels = btnShowLabels.Checked;
chart1.Series[0]["PieLabelStyle"] = _collection.ShowLabels ? "Inside" : "Disabled";
SaveCollectionChanges();
}
private void btnViewDataTable_Click(object sender, EventArgs e)
{
if (Activator.SelectObject(new DialogArgs
{
TaskDescription =
"The following CatalogueItem(s) do not currently have descriptions. Select one to navigate to it"
}, GetCatalogueItems().Where(ci => string.IsNullOrWhiteSpace(ci.Description)).ToArray(), out var selected))
{
var cmd = new ExecuteCommandShow(Activator, selected, 1);
cmd.Execute();
}
}
}