/// <summary> /// Handles the DoWorker event for the worker /// </summary> /// <param name="sender">Object that fired the event</param> /// <param name="e">.NET supplied event parameters</param> protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Dictionary<string, string> inputVariableList = ((GadgetParameters)e.Argument).InputVariableList; this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); CreateComplexSampleTableGridDelegate createGrid = new CreateComplexSampleTableGridDelegate(AddComplexSampleTableGrid); string freqVar = GadgetOptions.MainVariableName; string weightVar = GadgetOptions.WeightVariableName; string strataVar = string.Empty; bool includeMissing = GadgetOptions.ShouldIncludeMissing; if (inputVariableList.ContainsKey("stratavar")) { strataVar = inputVariableList["stratavar"]; } List<string> stratas = new List<string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { Configuration config = dashboardHelper.Config; string yesValue = config.Settings.RepresentationOfYes; string noValue = config.Settings.RepresentationOfNo; RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { GadgetOptions.CustomFilter = string.Empty; } List<string> columnNames = new List<string>(); columnNames.Add(GadgetOptions.MainVariableName); columnNames.Add(GadgetOptions.CrosstabVariableName); columnNames.Add(GadgetOptions.PSUVariableName); if(GadgetOptions.StrataVariableNames != null && GadgetOptions.StrataVariableNames.Count == 1 && !string.IsNullOrEmpty(GadgetOptions.StrataVariableNames[0])) { columnNames.Add(GadgetOptions.StrataVariableNames[0]); } if(!string.IsNullOrEmpty(GadgetOptions.WeightVariableName)) { columnNames.Add(GadgetOptions.WeightVariableName); } DataTable csTable = dashboardHelper.GenerateTable(columnNames, GadgetOptions.CustomFilter); if (csTable == null || csTable.Rows.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else { lock (staticSyncLock) { // Complex sample frequency actually uses the CS Tables class. StatisticsRepository.ComplexSampleTables csTables = new StatisticsRepository.ComplexSampleTables(); StatisticsRepository.ComplexSampleTables.CSTablesResults results = csTables.ComplexSampleTables(GadgetOptions.InputVariableList, csTable); if (!string.IsNullOrEmpty(results.ErrorMessage)) { throw new ApplicationException(results.ErrorMessage); } if (results.Rows == null || results.Rows.Count == 0) { throw new ApplicationException("No data was returned from the statistics repository."); } StatisticsRepository.ComplexSampleTables.TablesRow totalRow = new StatisticsRepository.ComplexSampleTables.TablesRow(); totalRow.Cells = new List<StatisticsRepository.ComplexSampleTables.CSRow>(); this.Dispatcher.BeginInvoke(createGrid, results); } } this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } finally { //stopwatch.Stop(); //Debug.Print("CS Frequency gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete with " + dashboardHelper.RecordCount.ToString() + " records and the following filters:"); //Debug.Print(dashboardHelper.DataFilters.GenerateDataFilterString()); } } }
protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); string freqVar = GadgetOptions.MainVariableName; string weightVar = GadgetOptions.WeightVariableName; string strataVar = string.Empty; string crosstabVar = GadgetOptions.CrosstabVariableName; bool includeMissing = GadgetOptions.ShouldIncludeMissing; List<string> stratas = new List<string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { GadgetOptions.CustomFilter = string.Empty; } if (!string.IsNullOrEmpty(crosstabVar.Trim())) { List<string> crosstabVarList = new List<string>(); crosstabVarList.Add(crosstabVar); foreach (Strata strata in DashboardHelper.GetStrataValuesAsDictionary(crosstabVarList, false, false)) { GadgetParameters parameters = new GadgetParameters(GadgetOptions); if (!string.IsNullOrEmpty(GadgetOptions.CustomFilter)) { parameters.CustomFilter = "(" + parameters.CustomFilter + ") AND " + strata.SafeFilter; } else { parameters.CustomFilter = strata.SafeFilter; } parameters.CrosstabVariableName = string.Empty; Dictionary<DataTable, List<DescriptiveStatistics>> stratifiedFrequencyTables = DashboardHelper.GenerateFrequencyTable(parameters); GenerateChartData(stratifiedFrequencyTables, strata); System.Threading.Thread.Sleep(200); } } else { Dictionary<DataTable, List<DescriptiveStatistics>> stratifiedFrequencyTables = DashboardHelper.GenerateFrequencyTable(GadgetOptions); GenerateChartData(stratifiedFrequencyTables); } this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } finally { stopwatch.Stop(); Debug.Print("Column chart gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete."); Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString()); } } }
protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); AberrationDetectionChartParameters AbDetChartParameters = (AberrationDetectionChartParameters)Parameters; string freqVar = AbDetChartParameters.ColumnNames[0]; string weightVar = AbDetChartParameters.WeightVariableName; string strataVar = string.Empty; bool includeMissing = AbDetChartParameters.IncludeMissing; int lagTime = DEFAULT_LAG_TIME; double deviations = DEFAULT_DEVIATIONS; int timePeriod = DEFAULT_TIME_PERIOD; if (!String.IsNullOrEmpty(AbDetChartParameters.LagTime)) { int.TryParse(AbDetChartParameters.LagTime, out lagTime); } if (!String.IsNullOrEmpty(AbDetChartParameters.Deviations)) { double.TryParse(AbDetChartParameters.Deviations, out deviations); } if (!String.IsNullOrEmpty(AbDetChartParameters.TimePeriod)) { int.TryParse(AbDetChartParameters.TimePeriod, out timePeriod); } List <string> stratas = new List <string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); AbDetChartParameters.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); AbDetChartParameters.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { AbDetChartParameters.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { AbDetChartParameters.CustomFilter = string.Empty; } Dictionary <DataTable, List <DescriptiveStatistics> > stratifiedFrequencyTables = DashboardHelper.GenerateFrequencyTable(AbDetChartParameters); List <AberrationChartData> dataList = new List <AberrationChartData>(); DataTable aberrationDetails = new DataTable(); aberrationDetails.Columns.Add(new DataColumn("Date", typeof(string))); aberrationDetails.Columns.Add(new DataColumn("Count", typeof(double))); aberrationDetails.Columns.Add(new DataColumn("Expected", typeof(double))); aberrationDetails.Columns.Add(new DataColumn("Difference", typeof(string))); foreach (KeyValuePair <DataTable, List <DescriptiveStatistics> > tableKvp in stratifiedFrequencyTables) { dataList = new List <AberrationChartData>(); aberrationDetails = new DataTable(); aberrationDetails.Columns.Add(new DataColumn("Date", typeof(string))); aberrationDetails.Columns.Add(new DataColumn("Count", typeof(double))); aberrationDetails.Columns.Add(new DataColumn("Expected", typeof(double))); aberrationDetails.Columns.Add(new DataColumn("Difference", typeof(string))); double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } string strataValue = tableKvp.Key.TableName; DataTable table = tableKvp.Key; AddFillerRows(table); double lastAvg = double.NegativeInfinity; double lastStdDev = double.NegativeInfinity; Queue <double> frame = new Queue <double>(); List <AberrationChartData> actualValues = new List <AberrationChartData>(); int rowCount = 1; var rows = from DataRow row in table.Rows orderby row[0] select row; foreach (System.Data.DataRow row in rows /*table.Rows*/) { if (!row[freqVar].Equals(DBNull.Value) || (row[freqVar].Equals(DBNull.Value) && includeMissing == true)) { DateTime displayValue = DateTime.Parse(row[freqVar].ToString()); frame.Enqueue((double)row["freq"]); AberrationChartData actualValue = new AberrationChartData(); actualValue.Date = displayValue; // strataValue; actualValue.Actual = (double)row[1]; //actualValue.S = row[0]; //dataList.Add(actualValue); if (frame.Count > lagTime - 1 /*6*/) { double[] frameArray = frame.ToArray(); double frameAvg = frameArray.Average(); frame.Dequeue(); double stdDev = CalculateStdDev(frameArray); if (lastAvg != double.NegativeInfinity) { actualValue.Expected = lastAvg; //dataList.Add(trendValue); if ((double)row["freq"] > lastAvg + (/*2.99*/ deviations * lastStdDev)) { double freq = (double)row["freq"]; actualValue.Aberration = freq; double dvs = (freq - lastAvg) / lastStdDev; aberrationDetails.Rows.Add(displayValue.ToShortDateString(), (double)row["freq"], Math.Round(lastAvg, 2), Math.Round(dvs, 2).ToString() + " standard deviations"); } } lastAvg = frameAvg; lastStdDev = stdDev; } dataList.Add(actualValue); rowCount++; } } this.Dispatcher.BeginInvoke(new AddChartDelegate(AddChart), dataList, aberrationDetails, strataValue); } this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } finally { stopwatch.Stop(); Debug.Print("Aberration Detection chart gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete."); Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString()); } } }
protected override void Construct() { if (!string.IsNullOrEmpty(CustomOutputHeading) && !CustomOutputHeading.Equals("(none)")) { headerPanel.Text = CustomOutputHeading; } smartTable = (bool)checkboxSmartTable.IsChecked; showPercents = (bool)checkboxRowColPercents.IsChecked; inputVariableList = new Dictionary<string, string>(); StrataGridList = new List<Grid>(); strataChiSquareGridList = new List<Grid>(); strata2x2GridList = new List<Controls.GadgetMatchedPairPanel>(); groupList = new List<StackPanel>(); gridDisclaimerList = new List<TextBlock>(); strataListPanels = new List<GadgetStrataListPanel>(); cbxExposureField.SelectionChanged += new SelectionChangedEventHandler(cbxField_SelectionChanged); cbxOutcomeField.SelectionChanged += new SelectionChangedEventHandler(cbxField_SelectionChanged); cbxFieldWeight.SelectionChanged += new SelectionChangedEventHandler(cbxField_SelectionChanged); //cbxFieldStrata.SelectionChanged += new SelectionChangedEventHandler(cbxField_SelectionChanged); lbxFieldStrata.SelectionChanged += new SelectionChangedEventHandler(cbxField_SelectionChanged); mnuCopy.Click += new RoutedEventHandler(mnuCopy_Click); mnuSendDataToHTML.Click += new RoutedEventHandler(mnuSendDataToHTML_Click); #if LINUX_BUILD mnuSendDataToExcel.Visibility = Visibility.Collapsed; #else mnuSendDataToExcel.Visibility = Visibility.Visible; Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot; Microsoft.Win32.RegistryKey excelKey = key.OpenSubKey("Excel.Application"); bool excelInstalled = excelKey == null ? false : true; key = Microsoft.Win32.Registry.ClassesRoot; excelKey = key.OpenSubKey("Excel.Application"); excelInstalled = excelKey == null ? false : true; if (!excelInstalled) { mnuSendDataToExcel.Visibility = Visibility.Collapsed; } else { mnuSendDataToExcel.Click += new RoutedEventHandler(mnuSendDataToExcel_Click); } #endif mnuSendToBack.Click += new RoutedEventHandler(mnuSendToBack_Click); mnuClose.Click += new RoutedEventHandler(mnuClose_Click); checkboxAllValues.Checked += new RoutedEventHandler(checkboxCheckChanged); checkboxAllValues.Unchecked += new RoutedEventHandler(checkboxCheckChanged); checkboxIncludeMissing.Checked += new RoutedEventHandler(checkboxCheckChanged); checkboxIncludeMissing.Unchecked += new RoutedEventHandler(checkboxCheckChanged); checkboxOutcomeContinuous.Checked += new RoutedEventHandler(checkboxCheckChanged); checkboxOutcomeContinuous.Unchecked += new RoutedEventHandler(checkboxCheckChanged); checkboxSmartTable.Checked += new RoutedEventHandler(checkboxCheckChanged); checkboxSmartTable.Unchecked += new RoutedEventHandler(checkboxCheckChanged); checkboxRowColPercents.Checked += new RoutedEventHandler(checkboxRowColPercents_Checked); checkboxRowColPercents.Unchecked += new RoutedEventHandler(checkboxRowColPercents_Checked); checkboxHorizontal.Checked += new RoutedEventHandler(checkboxHorizontal_Checked); checkboxHorizontal.Unchecked += new RoutedEventHandler(checkboxHorizontal_Unchecked); checkboxStrataSummaryOnly.Checked += new RoutedEventHandler(checkboxStrataSummaryOnly_CheckChanged); checkboxStrataSummaryOnly.Unchecked += new RoutedEventHandler(checkboxStrataSummaryOnly_CheckChanged); columnWarningShown = false; this.IsProcessing = false; this.GadgetStatusUpdate += new GadgetStatusUpdateHandler(RequestUpdateStatusMessage); this.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(IsCancelled); base.Construct(); requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); checkForCancellation = new CheckForCancellationDelegate(IsCancelled); YesValues = new List<string>(); NoValues = new List<string>(); #region Translation // ConfigExpandedTitle.Text = DashboardSharedStrings.GADGET_CONFIG_TITLE_MXN; // tblockExposureField.Text = DashboardSharedStrings.GADGET_EXPOSURE_VARIABLE; // tblockOutcomeField.Text = DashboardSharedStrings.GADGET_OUTCOME_VARIABLE; tblockWeightVariable.Text = DashboardSharedStrings.GADGET_WEIGHT_VARIABLE; // tblockStratifyBy.Text = DashboardSharedStrings.GADGET_STRATA_VARIABLE; expanderAdvancedOptions.Header = DashboardSharedStrings.GADGET_ADVANCED_OPTIONS; checkboxStrataSummaryOnly.Content = DashboardSharedStrings.GADGET_STRATA_SUMMARY_ONLY; checkboxIncludeMissing.Content = DashboardSharedStrings.GADGET_INCLUDE_MISSING; checkboxAllValues.Content = DashboardSharedStrings.GADGET_ALL_LIST_VALUES; checkboxCommentLegalLabels.Content = DashboardSharedStrings.GADGET_LIST_LABELS; checkboxOutcomeContinuous.Content = DashboardSharedStrings.GADGET_OUTCOME_CONTINUOUS; checkboxRowColPercents.Content = DashboardSharedStrings.GADGET_ROW_COL_PERCENTS; checkboxSmartTable.Content = DashboardSharedStrings.GADGET_SMART_2X2; checkboxHorizontal.Content = DashboardSharedStrings.GADGET_DISPLAY_STRATAS_HORIZONTALLY; tblockMaxColumnLength.Text = DashboardSharedStrings.GADGET_MAX_COL_NAME_LENGTH; btnRun.Content = DashboardSharedStrings.GADGET_RUN_BUTTON; #endregion // Translation }
protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); string freqVar = GadgetOptions.MainVariableName; string weightVar = GadgetOptions.WeightVariableName; string strataVar = string.Empty; bool includeMissing = GadgetOptions.ShouldIncludeMissing; List<string> stratas = new List<string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { GadgetOptions.CustomFilter = string.Empty; } Dictionary<DataTable, List<DescriptiveStatistics>> stratifiedFrequencyTables = DashboardHelper.GenerateFrequencyTable(GadgetOptions); List<XYChartData> dataList = new List<XYChartData>(); foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in stratifiedFrequencyTables) { double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } string strataValue = tableKvp.Key.TableName; DataTable table = tableKvp.Key; double max = 0; foreach (DataRow row in table.Rows) { XYChartData chartData = new XYChartData(); chartData.X = row[0].ToString(); chartData.Y = (double)row[1]; //chartData.Z = row[0]; //GetConfLimit(ref chartData, count); dataList.Add(chartData); max = max + (double)row[1]; } //foreach (DataRow row in table.Rows) //{ // max = max + (double)row[1]; //} double runningPercent = 0; foreach (DataRow row in table.Rows) { foreach (XYChartData chartData in dataList) { if (chartData.X.ToString() == row[0].ToString()) { chartData.Z = ((chartData.Y / max) * 100) + runningPercent; runningPercent = (double)chartData.Z; } } } } this.Dispatcher.BeginInvoke(new SetChartDataDelegate(SetChartData), dataList); this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } finally { stopwatch.Stop(); Debug.Print("Column chart gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete."); Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString()); } } }
/// <summary> /// Handles the DoWorker event for the worker /// </summary> /// <param name="sender">Object that fired the event</param> /// <param name="e">.NET supplied event parameters</param> protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Dictionary<string, string> inputVariableList = ((GadgetParameters)e.Argument).InputVariableList; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); AddFreqGridDelegate addGrid = new AddFreqGridDelegate(AddFreqGrid); SetGridTextDelegate setText = new SetGridTextDelegate(SetGridText); AddGridRowDelegate addRow = new AddGridRowDelegate(AddGridRow); SetGridBarDelegate setBar = new SetGridBarDelegate(SetGridBar); RenderFrequencyHeaderDelegate renderHeader = new RenderFrequencyHeaderDelegate(RenderFrequencyHeader); DrawFrequencyBordersDelegate drawBorders = new DrawFrequencyBordersDelegate(DrawOutputGridBorders); string freqVar = GadgetOptions.MainVariableName; string weightVar = GadgetOptions.WeightVariableName; string strataVar = string.Empty; bool showConfLimits = false; bool showCumulativePercent = false; bool includeMissing = GadgetOptions.ShouldIncludeMissing; if (inputVariableList.ContainsKey("stratavar")) { strataVar = inputVariableList["stratavar"]; } if (inputVariableList.ContainsKey("showconflimits")) { if (inputVariableList["showconflimits"].Equals("true")) { showConfLimits = true; } } if (inputVariableList.ContainsKey("showcumulativepercent")) { if (inputVariableList["showcumulativepercent"].Equals("true")) { showCumulativePercent = true; } } List<string> stratas = new List<string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { Configuration config = dashboardHelper.Config; string yesValue = config.Settings.RepresentationOfYes; string noValue = config.Settings.RepresentationOfNo; RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { GadgetOptions.CustomFilter = string.Empty; } Dictionary<DataTable, List<DescriptiveStatistics>> stratifiedFrequencyTables = dashboardHelper.GenerateFrequencyTable(GadgetOptions/*, freqVar, weightVar, stratas, string.Empty, useAllPossibleValues, sortHighLow, includeMissing, false*/); if (stratifiedFrequencyTables == null || stratifiedFrequencyTables.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), DashboardSharedStrings.GADGET_MSG_NO_DATA); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), DashboardSharedStrings.GADGET_MSG_OPERATION_CANCELLED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else { string formatString = string.Empty; foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in stratifiedFrequencyTables) { string strataValue = tableKvp.Key.TableName; double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } if (count == 0 && stratifiedFrequencyTables.Count == 1) { // this is the only table and there are no records, so let the user know this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), DashboardSharedStrings.GADGET_MSG_NO_DATA); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else if (count == 0) { continue; } DataTable frequencies = tableKvp.Key; if (frequencies.Rows.Count == 0) { continue; } this.Dispatcher.BeginInvoke(addGrid, strataVar, frequencies.TableName); } foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in stratifiedFrequencyTables) { string strataValue = tableKvp.Key.TableName; double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } if (count == 0) { continue; } DataTable frequencies = tableKvp.Key; if (frequencies.Rows.Count == 0) { continue; } string tableHeading = tableKvp.Key.TableName; if (stratifiedFrequencyTables.Count > 1) { tableHeading = freqVar;// +": " + strataVar + " = " + frequencies.TableName; } if (!showCumulativePercent) { this.Dispatcher.BeginInvoke(new SimpleCallback(HideCumulativePercent)); } if (!showConfLimits) { this.Dispatcher.BeginInvoke(new SimpleCallback(HideConfidenceIntervals)); } Field field = null; string columnType = string.Empty; foreach (DataRow fieldRow in dashboardHelper.FieldTable.Rows) { if (fieldRow["columnname"].Equals(freqVar)) { columnType = fieldRow["datatype"].ToString(); if (fieldRow["epifieldtype"] is Field) { field = fieldRow["epifieldtype"] as Field; } break; } } this.Dispatcher.BeginInvoke(renderHeader, strataValue, tableHeading); double AccumulatedTotal = 0; List<ConfLimit> confLimits = new List<ConfLimit>(); int rowCount = 1; foreach (System.Data.DataRow row in frequencies.Rows) { if (!row[freqVar].Equals(DBNull.Value) || (row[freqVar].Equals(DBNull.Value) && includeMissing == true)) { this.Dispatcher.Invoke(addRow, strataValue, 26); string displayValue = row[freqVar].ToString(); if (dashboardHelper.IsUserDefinedColumn(freqVar)) { displayValue = dashboardHelper.GetFormattedOutput(freqVar, row[freqVar]); } else { if (field != null && field is YesNoField) { if (row[freqVar].ToString().Equals("1")) displayValue = yesValue; else if (row[freqVar].ToString().Equals("0")) displayValue = noValue; } else if ((field != null && field is DateField) || (!dashboardHelper.DateColumnRequiresTime(frequencies, frequencies.Columns[0].ColumnName))) { displayValue = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:d}", row[freqVar]); } else if (field != null && field is TimeField) { displayValue = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:T}", row[freqVar]); } else { displayValue = dashboardHelper.GetFormattedOutput(freqVar, row[freqVar]); } } if (string.IsNullOrEmpty(displayValue)) { displayValue = config.Settings.RepresentationOfMissing; } double pct = 0; if (count > 0) pct = Convert.ToDouble(row["freq"]) / (count * 1.0); AccumulatedTotal += pct; if (count > 0) confLimits.Add(GetConfLimit(displayValue, Convert.ToDouble(row["freq"]), count)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(displayValue, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Left, rowCount, 0, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(row["freq"].ToString(), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 1, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(pct.ToString("P"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 2, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(AccumulatedTotal.ToString("P"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 3, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(confLimits[rowCount - 1].Lower.ToString("P"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 4, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(confLimits[rowCount - 1].Upper.ToString("P"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 5, Visibility.Visible)); this.Dispatcher.BeginInvoke(setBar, strataValue, rowCount, pct); rowCount++; } } this.Dispatcher.BeginInvoke(new AddGridFooterDelegate(RenderFrequencyFooter), strataValue, rowCount, (int)count); this.Dispatcher.BeginInvoke(drawBorders, strataValue); } stratifiedFrequencyTables.Clear(); } this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } finally { stopwatch.Stop(); Debug.Print("Frequency gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete with " + dashboardHelper.RecordCount.ToString() + " records and the following filters:"); Debug.Print(dashboardHelper.DataFilters.GenerateDataFilterString()); } } }
/// <summary> /// Handles the DoWorker event for the worker /// </summary> /// <param name="sender">Object that fired the event</param> /// <param name="e">.NET supplied event parameters</param> protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); AddAnovaDelegate addAnova = new AddAnovaDelegate(AddAnova); AddMeansGridDelegate addGrid = new AddMeansGridDelegate(AddMeansGrid); SetGridTextDelegate setText = new SetGridTextDelegate(SetGridText); AddGridRowDelegate addRow = new AddGridRowDelegate(AddGridRow); RenderMeansHeaderDelegate renderHeader = new RenderMeansHeaderDelegate(RenderMeansHeader); DrawMeansBordersDelegate drawBorders = new DrawMeansBordersDelegate(DrawOutputGridBorders); GadgetOptions.ShouldIgnoreRowLimits = true; GadgetOptions.ShouldIncludeFullSummaryStatistics = true; string precisionFormat = "F4"; if (GadgetOptions.InputVariableList.ContainsKey("precision")) { precisionFormat = GadgetOptions.InputVariableList["precision"]; precisionFormat = "F" + precisionFormat; } string meansVar = GadgetOptions.MainVariableName; string weightVar = GadgetOptions.WeightVariableName; string strataVar = string.Empty; bool hasData = false; if (GadgetOptions.StrataVariableNames != null && GadgetOptions.StrataVariableNames.Count > 0) { strataVar = GadgetOptions.StrataVariableNames[0]; } bool showAnova = true; if (GadgetOptions.InputVariableList.ContainsKey("showanova")) { bool.TryParse(GadgetOptions.InputVariableList["showanova"], out showAnova); } string crosstabVar = GadgetOptions.CrosstabVariableName; if (string.IsNullOrEmpty(crosstabVar)) { GadgetOptions.InputVariableList.Add("NeedsOutputGrid", "false"); } else { GadgetOptions.InputVariableList.Add("NeedsOutputGrid", "true"); } List<string> stratas = new List<string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { GadgetOptions.CustomFilter = string.Empty; } Dictionary<DataTable, List<DescriptiveStatistics>> stratifiedFrequencyTables = DashboardHelper.GenerateFrequencyTable(GadgetOptions); if (stratifiedFrequencyTables == null || stratifiedFrequencyTables.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), DashboardSharedStrings.GADGET_MSG_NO_DATA); //this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED); //this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); Debug.Print("Thread cancelled"); return; } else { //bool useSpecialFormatting = false; string formatString = string.Empty; StrataCount = stratifiedFrequencyTables.Count; foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in stratifiedFrequencyTables) { string strataValue = tableKvp.Key.TableName; double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } if (count == 0) { continue; } DataTable frequencies = tableKvp.Key; //dashboardHelper.GenerateFrequencyTable(freqVar, weightVar, out count); if (frequencies.Rows.Count == 0) { continue; } this.Dispatcher.BeginInvoke(addGrid, strataVar, frequencies.TableName); if (showAnova && tableKvp.Value[0].anovaPValue.HasValue) { this.Dispatcher.BeginInvoke(addAnova, strataValue, tableKvp.Value[0]); } } //if (GadgetStatusUpdate != null) //{ // this.Dispatcher.BeginInvoke(GadgetStatusUpdate, SharedStrings.DASHBOARD_GADGET_STATUS_DISPLAYING_OUTPUT); // System.Threading.Thread.Sleep(50); // necessary to prevent this message from overriding the 'no records selected' //} foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in stratifiedFrequencyTables) { string strataValue = tableKvp.Key.TableName; double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } if (count == 0) { continue; } else { hasData = true; } DataTable frequencies = tableKvp.Key; //dashboardHelper.GenerateFrequencyTable(freqVar, weightVar, out count); if (frequencies.Rows.Count == 0) { continue; } string tableHeading = tableKvp.Key.TableName; if (!string.IsNullOrEmpty(crosstabVar)) { tableHeading = meansVar + " * " + crosstabVar;// +": " + frequencies.TableName; } this.Dispatcher.BeginInvoke(renderHeader, strataValue, tableHeading, crosstabVar); int rowCount = 1; foreach (System.Data.DataColumn column in frequencies.Columns) { if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED); //this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); Debug.Print("Thread cancelled"); return; } string columnName = column.ColumnName; if (columnName.Equals("___sortvalue___")) { continue; } DescriptiveStatistics means = tableKvp.Value[rowCount - 1]; if (!columnName.ToLower().Equals(meansVar.ToLower())) { this.Dispatcher.Invoke(addRow, strataValue, 30); string displayValue = meansVar; if (tableKvp.Value.Count > 1) { displayValue = columnName; } string statsObs = means.observations.ToString(precisionFormat); string statsSum = Epi.SharedStrings.UNDEFINED; string statsMean = Epi.SharedStrings.UNDEFINED; string statsVar = Epi.SharedStrings.UNDEFINED; string statsStdDev = Epi.SharedStrings.UNDEFINED; string statsMin = Epi.SharedStrings.UNDEFINED; string statsMax = Epi.SharedStrings.UNDEFINED; string statsMedian = Epi.SharedStrings.UNDEFINED; string statsMode = Epi.SharedStrings.UNDEFINED; string statsQ1 = Epi.SharedStrings.UNDEFINED; string statsQ3 = Epi.SharedStrings.UNDEFINED; if (means.sum != null) statsSum = ((double)means.sum).ToString(precisionFormat); if (means.mean != null) statsMean = ((double)means.mean).ToString(precisionFormat); if (means.variance != null) statsVar = ((double)means.variance).ToString(precisionFormat); if (means.stdDev != null) statsStdDev = ((double)means.stdDev).ToString(precisionFormat); if (means.min != null) statsMin = ((double)means.min).ToString(precisionFormat); if (means.q1 != null) statsQ1 = ((double)means.q1).ToString(precisionFormat); if (means.median != null) statsMedian = ((double)means.median).ToString(precisionFormat); if (means.q3 != null) statsQ3 = ((double)means.q3).ToString(precisionFormat); if (means.max != null) statsMax = ((double)means.max).ToString(precisionFormat); if (means.mode != null) statsMode = ((double)means.mode).ToString(precisionFormat); if (statsObs.EndsWith("0") && (statsObs.Contains(".") || statsObs.Contains(","))) { statsObs = statsObs.TrimEnd('0').TrimEnd('.').TrimEnd(','); } if (statsSum.EndsWith("0") && (statsSum.Contains(".") || statsSum.Contains(","))) { statsSum = statsSum.TrimEnd('0').TrimEnd('.').TrimEnd(','); } this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(displayValue, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Left, rowCount, 0, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsObs, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 1, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsSum, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 2, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsMean, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 3, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsVar, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 4, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsStdDev, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 5, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsMin, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 6, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsQ1, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 7, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsMedian, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 8, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsQ3, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 9, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsMax, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 10, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, strataValue, new TextBlockConfig(statsMode, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 11, Visibility.Visible)); rowCount++; } } this.Dispatcher.BeginInvoke(drawBorders, strataValue); } } if (!hasData) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), DashboardSharedStrings.GADGET_MSG_NO_DATA); } else { this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } //this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); //this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } stopwatch.Stop(); Debug.Print("Means gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete with " + DashboardHelper.RecordCount.ToString() + " records and the following filters:"); Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString()); } }
protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); string freqVar = GadgetOptions.MainVariableName; string crosstabVar = GadgetOptions.CrosstabVariableName; string weightVar = GadgetOptions.WeightVariableName; string strataVar = string.Empty; bool includeMissing = GadgetOptions.ShouldIncludeMissing; List<string> stratas = new List<string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { GadgetOptions.CustomFilter = string.Empty; } DataView dv = DashboardHelper.GenerateView(GadgetOptions); List<XYChartData> dataList = new List<XYChartData>(); NumericDataValue minValue = null; NumericDataValue maxValue = null; foreach (DataRowView drv in dv) { DataRow row = drv.Row; if (row[freqVar] != DBNull.Value && row[freqVar] != null && row[crosstabVar] != DBNull.Value && row[crosstabVar] != null) { XYChartData chartData = new XYChartData(); chartData.X = Convert.ToDouble(row[freqVar]); chartData.Y = Convert.ToDouble(row[crosstabVar]); dataList.Add(chartData); NumericDataValue currentValue = new NumericDataValue() { DependentValue = Convert.ToDecimal(row[crosstabVar]), IndependentValue = Convert.ToDecimal(row[freqVar]) }; if (minValue == null) { minValue = currentValue; } else { if (currentValue.IndependentValue < minValue.IndependentValue) { minValue = currentValue; } } if (maxValue == null) { maxValue = currentValue; } else { if (currentValue.IndependentValue > maxValue.IndependentValue) { maxValue = currentValue; } } } } StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression(); Dictionary<string, string> inputVariableList = new Dictionary<string, string>(); inputVariableList.Add(crosstabVar, "dependvar"); inputVariableList.Add("intercept", "true"); inputVariableList.Add("includemissing", "false"); inputVariableList.Add("p", "0.95"); inputVariableList.Add(freqVar, "unsorted"); StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, dv.Table); this.Dispatcher.BeginInvoke(new SetChartDataDelegate(SetChartData), dataList, regresResults, maxValue, minValue); this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } finally { stopwatch.Stop(); Debug.Print("Column chart gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete."); Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString()); } } }
/// <summary> /// Handles the DoWorker event for the worker /// </summary> /// <param name="sender">Object that fired the event</param> /// <param name="e">.NET supplied event parameters</param> private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { bool needsRefresh = false; if (e != null && e.Argument is bool) { needsRefresh = !((bool)e.Argument); } IsPopulatingDataSet = true; RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); SetGadgetStatusHandler setGadgetProgress = new SetGadgetStatusHandler(RequestProgressUpdate); GadgetParameters inputs = new GadgetParameters(); inputs.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); inputs.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); inputs.GadgetCheckForProgress += new SetGadgetStatusHandler(setGadgetProgress); try { DashboardHelper.UserVarsNeedUpdating = true; DashboardHelper.PopulateDataSet(inputs); } catch (System.Data.EvaluateException) { this.Dispatcher.BeginInvoke(new SendNotificationMessageHandler(SendNotificationMessage), DashboardSharedStrings.ERROR_FILTER_NO_LONGER_VALID, true, NotificationButtonType.OK, true, 10); } catch (Exception ex) { Epi.Windows.MsgBox.ShowException(ex); } e.Result = needsRefresh; }
private void stackedChartWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); DataTable data = GetStackedColumnData(); if (data == null || data.Rows.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); System.Diagnostics.Debug.Print("Stacked chart thread was cancelled"); return; } else if (data != null && data.Rows.Count > 0) { //DataRow[] allRows; //if (dashboardHelper.IsUsingEpiProject && data.Rows[0][0].ToString().Equals(dashboardHelper.Config.Settings.RepresentationOfYes) && (dashboardHelper.IsColumnYesNo(data.Columns[0].ColumnName) || dashboardHelper.IsColumnBoolean(data.Columns[0].ColumnName))) //{ // allRows = data.Select(string.Empty, "[" + data.Columns[0].ColumnName + "] DESC"); //} //else //{ // allRows = data.Select(string.Empty, "[" + data.Columns[0].ColumnName + "]"); //} List<List<StringDataValue>> dataValues = new List<List<StringDataValue>>(); for (int i = 1; i < data.Columns.Count; i++) { List<StringDataValue> values = new List<StringDataValue>(); foreach (DataRow row in data.Rows) { StringDataValue val = new StringDataValue(); if (row[i] != DBNull.Value) { val.DependentValue = (int)row[i]; } else { val.DependentValue = 0; } val.IndependentValue = row[0].ToString(); values.Add(val); } dataValues.Add(values); } object[] result = new object[] { dataValues, data }; e.Result = result; this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } }
protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Configuration config = DashboardHelper.Config; Dictionary<string, string> setProperties = new Dictionary<string, string>(); setProperties.Add("Intercept", "true"); setProperties.Add("P", 0.95.ToString()); setProperties.Add("BLabels", config.Settings.RepresentationOfYes + ";" + config.Settings.RepresentationOfNo + ";" + config.Settings.RepresentationOfMissing); // TODO: Replace Yes, No, Missing with global vars //Dictionary<string, string> inputVariableList = (Dictionary<string, string>)e.Argument; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); SetGridTextDelegate setText = new SetGridTextDelegate(SetGridText); AddGridRowDelegate addRow = new AddGridRowDelegate(AddGridRow); //SetGridBarDelegate setBar = new SetGridBarDelegate(SetGridBar); System.Collections.Generic.Dictionary<string, System.Data.DataTable> Freq_ListSet = new Dictionary<string, System.Data.DataTable>(); //string customFilter = string.Empty; List<string> columnNames = new List<string>(); foreach (KeyValuePair<string, string> kvp in GadgetOptions.InputVariableList) { if (kvp.Value.ToLower().Equals("unsorted") || kvp.Value.ToLower().Equals("dependvar") || kvp.Value.ToLower().Equals("weightvar") || kvp.Value.ToLower().Equals("matchvar")) { columnNames.Add(kvp.Key); } else if (kvp.Value.ToLower().Equals("discrete")) { columnNames.Add(kvp.Key); } } try { lock (staticSyncLock) { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); DataTable regressTable = DashboardHelper.GenerateTable(columnNames); RegressionResults results = new RegressionResults(); if (regressTable == null || regressTable.Rows.Count <= 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); Debug.Print("Thread stopped due to errors."); return; } else if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); Debug.Print("Thread cancelled"); return; } else { StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression(); results.regressionResults = linearRegression.LinearRegression(GadgetOptions.InputVariableList, regressTable); results.correlationCoefficient = results.regressionResults.correlationCoefficient; results.regressionDf = results.regressionResults.regressionDf; results.regressionF = results.regressionResults.regressionF; results.regressionMeanSquare = results.regressionResults.regressionMeanSquare; results.regressionSumOfSquares = results.regressionResults.regressionSumOfSquares; results.residualsDf = results.regressionResults.residualsDf; results.residualsMeanSquare = results.regressionResults.residualsMeanSquare; results.residualsSumOfSquares = results.regressionResults.residualsSumOfSquares; results.totalDf = results.regressionResults.totalDf; results.totalSumOfSquares = results.regressionResults.totalSumOfSquares; results.errorMessage = results.regressionResults.errorMessage.Replace("<tlt>", string.Empty).Replace("</tlt>", string.Empty); if (!string.IsNullOrEmpty(results.errorMessage)) { throw new ApplicationException(results.errorMessage); } results.variables = new List<VariableRow>(); if (results.regressionResults.variables != null) { double tScore = 1.9; while (Epi.Statistics.SharedResources.PFromT(tScore, results.residualsDf) > 0.025) { tScore += 0.000001; } foreach (StatisticsRepository.LinearRegression.VariableRow vrow in results.regressionResults.variables) { VariableRow nrow = new VariableRow(); nrow.coefficient = vrow.coefficient; nrow.Ftest = vrow.Ftest; nrow.P = vrow.P; nrow.stdError = vrow.stdError; nrow.variableName = vrow.variableName; nrow.coefficientLower = nrow.coefficient - tScore * nrow.stdError; nrow.coefficientUpper = nrow.coefficient + tScore * nrow.stdError; results.variables.Add(nrow); } results.regressionFp = Epi.Statistics.SharedResources.PFromF(results.regressionF, results.regressionDf, results.residualsDf); this.Dispatcher.BeginInvoke(new SimpleCallback(RenderRegressionHeader)); int rowCount = 1; foreach (VariableRow row in results.variables) { this.Dispatcher.Invoke(addRow, grdRegress, 30); string displayValue = row.variableName; this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + displayValue + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Left, rowCount, 0, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.coefficient.ToString("F3") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 1, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.coefficientLower.ToString("F3") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 2, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.coefficientUpper.ToString("F3") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 3, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.stdError.ToString("F3") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 4, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.Ftest.ToString("F4") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 5, Visibility.Visible)); this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.P.ToString("F6") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 6, Visibility.Visible)); rowCount++; } //this.Dispatcher.BeginInvoke(new AddGridFooterDelegate(RenderFrequencyFooter), rowCount, count); this.Dispatcher.BeginInvoke(new SimpleCallback(DrawRegressionBorders)); } } this.Dispatcher.BeginInvoke(new RenderGridDelegate(RenderRegressionResults), results); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } finally { stopwatch.Stop(); Debug.Print("Linear regression gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete with " + DashboardHelper.RecordCount.ToString() + " records and the following filters:"); Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString()); } } //gridCells.singleTableResults = new StatisticsRepository.cTable().SigTable((double)gridCells.yyVal, (double)gridCells.ynVal, (double)gridCells.nyVal, (double)gridCells.nnVal, 0.95); }
void singleChartWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { GadgetParameters GadgetOptions = (GadgetParameters)((object[])e.Argument)[0]; string varName = GadgetOptions.MainVariableName;//((object[])e.Argument)[1].ToString(); string singleChartType = string.Empty; if (GadgetOptions.InputVariableList.ContainsKey("charttype")) { singleChartType = GadgetOptions.InputVariableList["charttype"]; } this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); Dictionary<DataTable, List<DescriptiveStatistics>> res = DashboardHelper.GenerateFrequencyTable(GadgetOptions); if (res == null || res.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); System.Diagnostics.Debug.Print("Single chart thread was cancelled"); return; } else { DataTable frequencies = new DataTable(); List<List<StringDataValue>> stratifiedValues = new List<List<StringDataValue>>(); axisLabelMaxLength = 0; //if (GadgetStatusUpdate != null) //{ //this.Dispatcher.BeginInvoke(GadgetStatusUpdate, SharedStrings.DASHBOARD_GADGET_STATUS_DISPLAYING_OUTPUT); System.Threading.Thread.Sleep(150); // necessary to prevent this message from overriding the 'no records selected' //} if (res.Count == 1 && !singleChartType.Equals("pie") && !singleChartType.Equals("pareto") && DashboardHelper.IsUsingEpiProject && DashboardHelper.View.Fields.Contains(GadgetOptions.MainVariableName) && (DashboardHelper.View.Fields[GadgetOptions.MainVariableName] is YesNoField || DashboardHelper.View.Fields[GadgetOptions.MainVariableName] is CheckBoxField)) { IsBooleanWithNoStratas = true; } else { IsBooleanWithNoStratas = false; } foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in res) { string strataValue = tableKvp.Key.TableName; double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } if (count == 0) { continue; } frequencies = tableKvp.Key; List<StringDataValue> dataValues = new List<StringDataValue>(); int maxLegendItems = 15; object[] itemArray = null; if (singleChartType.Equals("pie") && !frequencies.Columns[0].DataType.ToString().Equals("System.DateTime") && !frequencies.Columns[0].DataType.ToString().Equals("System.Double") && !frequencies.Columns[0].DataType.ToString().Equals("System.Single") && !frequencies.Columns[0].DataType.ToString().Equals("System.Int32")) { int rowStoppedAt = 0; DataTable condensedTable = new DataTable(frequencies.TableName); condensedTable = frequencies.Clone(); for (int i = 0; i < frequencies.Rows.Count; i++) { if (i < maxLegendItems) { condensedTable.Rows.Add(frequencies.Rows[i].ItemArray); } else if (i == maxLegendItems) { condensedTable.Rows.Add("All others", frequencies.Rows[i][1]); rowStoppedAt = i; } else { condensedTable.Rows[maxLegendItems][1] = (double)condensedTable.Rows[rowStoppedAt][1] + (double)frequencies.Rows[i][1]; } } if (condensedTable.Rows.Count > maxLegendItems) { itemArray = condensedTable.Rows[maxLegendItems].ItemArray; DataRow rowToRemove = condensedTable.Rows[maxLegendItems]; condensedTable.Rows.Remove(rowToRemove); } frequencies = condensedTable.Copy(); if (!DashboardHelper.IsColumnYesNo(GadgetOptions.MainVariableName) && !DashboardHelper.IsColumnBoolean(GadgetOptions.MainVariableName)) { frequencies = frequencies.Select(string.Empty, frequencies.Columns[0].ColumnName).CopyToDataTable().DefaultView.ToTable(frequencies.TableName); } if (itemArray != null) { frequencies.Rows.Add(itemArray); } } double sum = 0; double runningTotal = 0; double maxValue = 0; foreach (DataRow row in frequencies.Rows) { double value = (double)row["freq"]; sum = sum + value; if (value > maxValue) { maxValue = value; } } if (IsBooleanWithNoStratas) { foreach (DataRow row in frequencies.Rows) { runningTotal = runningTotal + (double)row["freq"]; double currentTotal = (runningTotal / sum) * maxValue; StringDataValue sdv = new StringDataValue() { DependentValue = (double)row["freq"], IndependentValue = strataValue, StratificationValue = row[varName].ToString(), CurrentMeanValue = currentTotal }; List<StringDataValue> listSdv = new List<StringDataValue>(); listSdv.Add(sdv); // dataValues.Add(new StringDataValue() { DependentValue = (double)row["freq"], IndependentValue = row[varName].ToString(), StratificationValue = string.Empty }); if (row[varName].ToString().Length > axisLabelMaxLength) { axisLabelMaxLength = row[varName].ToString().Length; } stratifiedValues.Add(listSdv); } } else { foreach (DataRow row in frequencies.Rows) { runningTotal = runningTotal + (double)row["freq"]; double currentTotal = (runningTotal / sum) * maxValue; dataValues.Add(new StringDataValue() { DependentValue = (double)row["freq"], IndependentValue = row[varName].ToString(), StratificationValue = strataValue, CurrentMeanValue = currentTotal }); if (row[varName].ToString().Length > axisLabelMaxLength) { axisLabelMaxLength = row[varName].ToString().Length; } } stratifiedValues.Add(dataValues); } } e.Result = stratifiedValues; if (stratifiedValues.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED); } else { this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } } } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } }
void scatterChartWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); string xAxisVar = GadgetOptions.MainVariableName; string yAxisVar = GadgetOptions.CrosstabVariableName; List<string> columnNames = new List<string>(); columnNames.Add(xAxisVar); columnNames.Add(yAxisVar); DataTable regressTable = DashboardHelper.GenerateTable(columnNames); if (regressTable == null || regressTable.Rows.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); System.Diagnostics.Debug.Print("Single chart thread was cancelled"); return; } else { List<NumericDataValue> dataValues = new List<NumericDataValue>(); NumericDataValue minValue = null; NumericDataValue maxValue = null; foreach (DataRow row in regressTable.Rows) { if (row[yAxisVar].Equals(DBNull.Value) || row[xAxisVar].Equals(DBNull.Value)) { continue; } NumericDataValue currentValue = new NumericDataValue() { DependentValue = Convert.ToDecimal(row[yAxisVar]), IndependentValue = Convert.ToDecimal(row[xAxisVar]) }; dataValues.Add(currentValue); if (minValue == null) { minValue = currentValue; } else { if (currentValue.IndependentValue < minValue.IndependentValue) { minValue = currentValue; } } if (maxValue == null) { maxValue = currentValue; } else { if (currentValue.IndependentValue > maxValue.IndependentValue) { maxValue = currentValue; } } } StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression(); Dictionary<string, string> inputVariableList = new Dictionary<string, string>(); inputVariableList.Add(yAxisVar, "dependvar"); inputVariableList.Add("intercept", "true"); inputVariableList.Add("includemissing", "false"); inputVariableList.Add("p", "0.95"); inputVariableList.Add(xAxisVar, "unsorted"); StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, regressTable); object[] result = new object[] { dataValues, regresResults, maxValue, minValue }; e.Result = result; this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } }
void epiCurveWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { object[] args = (object[])e.Argument; bool byEpiWeek = (bool)args[0]; string dateVar = args[1].ToString(); string caseStatusVar = args[2].ToString(); string dateFormat = args[3].ToString(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); //System.Threading.Thread.Sleep(100); DataTable data = GetEpiCurveData(byEpiWeek, dateVar, caseStatusVar); if (data != null && data.Rows.Count > 0) { DataRow[] allRows = data.Select(string.Empty, "[" + data.Columns[0].ColumnName + "]"); List<List<StringDataValue>> dataValues = new List<List<StringDataValue>>(); for (int i = 1; i < data.Columns.Count; i++) { List<StringDataValue> values = new List<StringDataValue>(); foreach (DataRow row in allRows) { StringDataValue val = new StringDataValue(); if (row[i] != DBNull.Value) { val.DependentValue = (int)row[i]; } else { val.DependentValue = 0; } if (byEpiWeek) { val.IndependentValue = row[0].ToString(); } else { if (dateFormat.ToLower().Equals("hours")) { val.IndependentValue = ((DateTime)row[0]).ToString("g"); } else if (dateFormat.ToLower().Equals("months")) { val.IndependentValue = ((DateTime)row[0]).ToString("MMM yyyy"); } else if (dateFormat.ToLower().Equals("years")) { val.IndependentValue = ((DateTime)row[0]).ToString("yyyy"); } else { val.IndependentValue = ((DateTime)row[0]).ToShortDateString(); } } values.Add(val); } dataValues.Add(values); } object[] retVals = new object[2]; retVals[0] = dataValues; retVals[1] = data; e.Result = retVals; this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } else if (data == null || data.Rows.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED); } else { this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } }
protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); string freqVar = GadgetOptions.MainVariableName; string weightVar = GadgetOptions.WeightVariableName; string strataVar = string.Empty; bool includeMissing = GadgetOptions.ShouldIncludeMissing; int lagTime = DEFAULT_LAG_TIME; int timePeriod = DEFAULT_TIME_PERIOD; double deviations = DEFAULT_DEVIATIONS; if (GadgetOptions.InputVariableList.ContainsKey("lagtime")) { int.TryParse(GadgetOptions.InputVariableList["lagtime"], out lagTime); } if (GadgetOptions.InputVariableList.ContainsKey("timeperiod")) { int.TryParse(GadgetOptions.InputVariableList["timeperiod"], out timePeriod); } if (GadgetOptions.InputVariableList.ContainsKey("deviations")) { double.TryParse(GadgetOptions.InputVariableList["deviations"], out deviations); } List<string> stratas = new List<string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { GadgetOptions.CustomFilter = string.Empty; } Dictionary<DataTable, List<DescriptiveStatistics>> stratifiedFrequencyTables = DashboardHelper.GenerateFrequencyTable(GadgetOptions); List<AberrationChartData> dataList = new List<AberrationChartData>(); DataTable aberrationDetails = new DataTable(); aberrationDetails.Columns.Add(new DataColumn("Date", typeof(string))); aberrationDetails.Columns.Add(new DataColumn("Count", typeof(double))); aberrationDetails.Columns.Add(new DataColumn("Expected", typeof(double))); aberrationDetails.Columns.Add(new DataColumn("Difference", typeof(string))); foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in stratifiedFrequencyTables) { dataList = new List<AberrationChartData>(); aberrationDetails = new DataTable(); aberrationDetails.Columns.Add(new DataColumn("Date", typeof(string))); aberrationDetails.Columns.Add(new DataColumn("Count", typeof(double))); aberrationDetails.Columns.Add(new DataColumn("Expected", typeof(double))); aberrationDetails.Columns.Add(new DataColumn("Difference", typeof(string))); double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } string strataValue = tableKvp.Key.TableName; DataTable table = tableKvp.Key; AddFillerRows(table); double lastAvg = double.NegativeInfinity; double lastStdDev = double.NegativeInfinity; Queue<double> frame = new Queue<double>(); List<AberrationChartData> actualValues = new List<AberrationChartData>(); int rowCount = 1; var rows = from DataRow row in table.Rows orderby row[0] select row; foreach (System.Data.DataRow row in rows /*table.Rows*/) { if (!row[freqVar].Equals(DBNull.Value) || (row[freqVar].Equals(DBNull.Value) && includeMissing == true)) { DateTime displayValue = DateTime.Parse(row[freqVar].ToString()); frame.Enqueue((double)row["freq"]); AberrationChartData actualValue = new AberrationChartData(); actualValue.Date = displayValue; // strataValue; actualValue.Actual = (double)row[1]; //actualValue.S = row[0]; //dataList.Add(actualValue); if (frame.Count > lagTime - 1 /*6*/) { double[] frameArray = frame.ToArray(); double frameAvg = frameArray.Average(); frame.Dequeue(); double stdDev = CalculateStdDev(frameArray); if (lastAvg != double.NegativeInfinity) { actualValue.Expected = lastAvg; //dataList.Add(trendValue); if ((double)row["freq"] > lastAvg + (/*2.99*/deviations * lastStdDev)) { double freq = (double)row["freq"]; actualValue.Aberration = freq; double dvs = (freq - lastAvg) / lastStdDev; aberrationDetails.Rows.Add(displayValue.ToShortDateString(), (double)row["freq"], Math.Round(lastAvg, 2), Math.Round(dvs, 2).ToString() + " standard deviations"); } } lastAvg = frameAvg; lastStdDev = stdDev; } dataList.Add(actualValue); rowCount++; } } this.Dispatcher.BeginInvoke(new AddChartDelegate(AddChart), dataList, aberrationDetails, strataValue); } this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); } finally { stopwatch.Stop(); Debug.Print("Column chart gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete."); Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString()); } } }
/// <summary> /// Closes the gadget /// </summary> protected override void CloseGadget() { if (worker != null && worker.WorkerSupportsCancellation) { worker.CancelAsync(); } if (worker != null) { worker.DoWork -= new System.ComponentModel.DoWorkEventHandler(worker_DoWork); worker.RunWorkerCompleted -= new System.ComponentModel.RunWorkerCompletedEventHandler(worker_WorkerCompleted); } if (baseWorker != null) { baseWorker.DoWork -= new System.ComponentModel.DoWorkEventHandler(Execute); } if (GadgetOptions != null) { GadgetOptions.GadgetStatusUpdate -= new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation -= new GadgetCheckForCancellationHandler(checkForCancellation); GadgetOptions = null; } this.GadgetStatusUpdate -= new GadgetStatusUpdateHandler(RequestUpdateStatusMessage); this.GadgetCheckForCancellation -= new GadgetCheckForCancellationHandler(IsCancelled); for (int i = 0; i < StrataGridList.Count; i++) { StrataGridList[i].Children.Clear(); } for (int i = 0; i < groupSvList.Count; i++) { groupSvList[i].Content = null; } for (int i = 0; i < StrataExpanderList.Count; i++) { StrataExpanderList[i].Content = null; } this.StrataGridList.Clear(); this.groupSvList.Clear(); this.StrataExpanderList.Clear(); cbxSortField.SelectionChanged -= new SelectionChangedEventHandler(cbxSortField_SelectionChanged); requestUpdateStatus = null; checkForCancellation = null; mnuRemove.Click -= new RoutedEventHandler(mnuRemove_Click); mnuSwapSortType.Click -= new RoutedEventHandler(mnuSwapSortType_Click); if (!IsHostedByEnter) { mnuSendToBack.Click -= new RoutedEventHandler(mnuSendToBack_Click); //mnuRefresh.Click -= new RoutedEventHandler(mnuRefresh_Click); mnuClose.Click -= new RoutedEventHandler(mnuClose_Click); } base.CloseGadget(); GadgetOptions = null; }
/// <summary> /// Handles the DoWorker event for the worker /// </summary> /// <param name="sender">Object that fired the event</param> /// <param name="e">.NET supplied event parameters</param> protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { //Dictionary<string, string> inputVariableList = ((GadgetParameters)e.Argument).InputVariableList; WordCloudParameters WordCloudParameters = (WordCloudParameters)Parameters; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); //string freqVar = GadgetOptions.MainVariableName; //string weightVar = GadgetOptions.WeightVariableName; //string strataVar = string.Empty; //bool includeMissing = GadgetOptions.ShouldIncludeMissing; if (WordCloudParameters.ColumnNames.Count > 0) { string freqVar = WordCloudParameters.ColumnNames[0]; } //if (inputVariableList.ContainsKey("stratavar")) //{ // strataVar = inputVariableList["stratavar"]; //} //List<string> stratas = new List<string>(); //if (!string.IsNullOrEmpty(strataVar)) //{ // stratas.Add(strataVar); //} try { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); //GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); //GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); WordCloudParameters.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); WordCloudParameters.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { WordCloudParameters.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { WordCloudParameters.CustomFilter = string.Empty; } //GadgetOptions.ShouldIgnoreRowLimits = true; // Dictionary<DataTable, List<DescriptiveStatistics>> stratifiedFrequencyTables = DashboardHelper.GenerateFrequencyTable(GadgetOptions/*, freqVar, weightVar, stratas, string.Empty, useAllPossibleValues, sortHighLow, includeMissing, false*/); // DataTable dt = DashboardHelper.ExtractFirstFrequencyTable(stratifiedFrequencyTables); //DataView dv = DashboardHelper.GenerateView(GadgetOptions); DataView dv = DashboardHelper.GenerateView(WordCloudParameters); Dictionary<char, Dictionary<string, double>> wordCloudDictionary1 = new Dictionary<char, Dictionary<string, double>>(); wordCloudDictionary1.Add('0', new Dictionary<string, double>()); wordCloudDictionary1.Add('1', new Dictionary<string, double>()); wordCloudDictionary1.Add('2', new Dictionary<string, double>()); wordCloudDictionary1.Add('3', new Dictionary<string, double>()); wordCloudDictionary1.Add('4', new Dictionary<string, double>()); wordCloudDictionary1.Add('5', new Dictionary<string, double>()); wordCloudDictionary1.Add('6', new Dictionary<string, double>()); wordCloudDictionary1.Add('7', new Dictionary<string, double>()); wordCloudDictionary1.Add('8', new Dictionary<string, double>()); wordCloudDictionary1.Add('9', new Dictionary<string, double>()); wordCloudDictionary1.Add('a', new Dictionary<string, double>()); wordCloudDictionary1.Add('b', new Dictionary<string, double>()); wordCloudDictionary1.Add('c', new Dictionary<string, double>()); wordCloudDictionary1.Add('d', new Dictionary<string, double>()); wordCloudDictionary1.Add('e', new Dictionary<string, double>()); wordCloudDictionary1.Add('f', new Dictionary<string, double>()); wordCloudDictionary1.Add('g', new Dictionary<string, double>()); wordCloudDictionary1.Add('h', new Dictionary<string, double>()); wordCloudDictionary1.Add('i', new Dictionary<string, double>()); wordCloudDictionary1.Add('j', new Dictionary<string, double>()); wordCloudDictionary1.Add('k', new Dictionary<string, double>()); wordCloudDictionary1.Add('l', new Dictionary<string, double>()); wordCloudDictionary1.Add('m', new Dictionary<string, double>()); wordCloudDictionary1.Add('n', new Dictionary<string, double>()); wordCloudDictionary1.Add('o', new Dictionary<string, double>()); wordCloudDictionary1.Add('p', new Dictionary<string, double>()); wordCloudDictionary1.Add('q', new Dictionary<string, double>()); wordCloudDictionary1.Add('r', new Dictionary<string, double>()); wordCloudDictionary1.Add('s', new Dictionary<string, double>()); wordCloudDictionary1.Add('t', new Dictionary<string, double>()); wordCloudDictionary1.Add('u', new Dictionary<string, double>()); wordCloudDictionary1.Add('v', new Dictionary<string, double>()); wordCloudDictionary1.Add('w', new Dictionary<string, double>()); wordCloudDictionary1.Add('x', new Dictionary<string, double>()); wordCloudDictionary1.Add('y', new Dictionary<string, double>()); wordCloudDictionary1.Add('z', new Dictionary<string, double>()); Dictionary<string, double> wordCloudDictionary = new Dictionary<string, double>(); if (WordCloudParameters.ColumnNames.Count > 0 && !String.IsNullOrEmpty(WordCloudParameters.ColumnNames[0])) { foreach (DataRowView dr in dv) { DataRow row = dr.Row; string[] strings = row[WordCloudParameters.ColumnNames[0]].ToString().Replace(", ", " ").Replace(". ", " ").ToLowerInvariant().Split(' '); foreach (string s in strings) { string s0 = s; if (s.Length > 0 && (s.Substring(s.Length - 1).Equals(",") || s.Substring(s.Length - 1).Equals("."))) s0 = s.Remove(s.Length - 1); char[] firstChars = s0.ToCharArray(); if (firstChars.Length > 0) { char firstChar = firstChars[0]; if (wordCloudDictionary1.ContainsKey(firstChar)) { if (wordCloudDictionary1[firstChar].ContainsKey(s0)) { wordCloudDictionary1[firstChar][s0]++; } else { //, . a am an and are as at but by for her his i if in is it me my not of on or our pm that the their this to us was we were will with would you your //string txtCommonWords = ", . a am an and are as at but by for her his i if in is it me my not of on or our pm that the their this to us was we were will with would you your"; //if (!s0.Equals(",") && !s0.Equals(".") && !s0.Equals("and") && !s0.Equals("the") && !s0.Equals("for") && // !s0.Equals("in") && !s0.Equals("on") && !s0.Equals("with") && !s0.Equals("of") && !s0.Equals("a") && !s0.Equals("an") // && !s0.Equals("to") && !s0.Equals("as") && !s0.Equals("we") && !s0.Equals("i") && !s0.Equals("me") && !s0.Equals("us") // && !s0.Equals("you") && !s0.Equals("this") && !s0.Equals("it") && !s0.Equals("is") && !s0.Equals("are") && !s0.Equals("or") // && !s0.Equals("my") && !s0.Equals("that") && !s0.Equals("if") && !s0.Equals("not") && !s0.Equals("were") && !s0.Equals("was") // && !s0.Equals("by") && !s0.Equals("at") && !s0.Equals("our") && !s0.Equals("your") && !s0.Equals("their") && !s0.Equals("will") // && !s0.Equals("would") && !s0.Equals("but") && !s0.Equals("his") && !s0.Equals("her") && !s0.Equals("am") && !s0.Equals("pm") // && s0.Length > 0) if(s0.Length > 0 && !WordCloudParameters.CommonWords.Contains(s0)) { wordCloudDictionary1[firstChar].Add(s0, 1.0); } } } } } } } Dictionary<string, double> wordCloudDictionary0 = new Dictionary<string, double>(); foreach (KeyValuePair<char, Dictionary<string, double>> kvp0 in wordCloudDictionary1) { foreach (KeyValuePair<string, double> kvp in kvp0.Value) { if (kvp.Value > 1.0) { wordCloudDictionary0.Add(kvp.Key, kvp.Value); } } } wordCloudDictionary = wordCloudDictionary0.OrderByDescending(g => g.Value).ToDictionary(g => g.Key, g => g.Value); wordCloudDictionary0.Clear(); int entryCount = 0; double totalWordCounts = 0; foreach (KeyValuePair<string, double> kvp in wordCloudDictionary) { wordCloudDictionary0.Add(kvp.Key, kvp.Value); totalWordCounts += kvp.Value; if (++entryCount > 49) break; } // wordCloudDictionary = wordCloudDictionary0.OrderBy(g => g.Key).ToDictionary(g => g.Key, g => g.Value); wordCloudDictionary = wordCloudDictionary0; /* // Sort so more frequent words are in the middle Dictionary<string, double> wordCloudDictionary1 = wordCloudDictionary0.OrderBy(g => g.Value).ToDictionary(g => g.Key, g => g.Value); wordCloudDictionary.Clear(); bool isEven = (wordCloudDictionary0.Count() % 2 == 0); int wcdItem = 0; foreach (KeyValuePair<string, double> kvp in wordCloudDictionary1) { wcdItem++; if (wcdItem % 2 == 1) wordCloudDictionary.Add(kvp.Key, kvp.Value); } wcdItem = 0; if (isEven) { foreach (KeyValuePair<string, double> kvp in wordCloudDictionary0) { wcdItem++; if (wcdItem % 2 == 1) wordCloudDictionary.Add(kvp.Key, kvp.Value); } } else { foreach (KeyValuePair<string, double> kvp in wordCloudDictionary0) { wcdItem++; if (wcdItem % 2 == 0) wordCloudDictionary.Add(kvp.Key, kvp.Value); } } // End altered sorting */ if (wordCloudDictionary == null || wordCloudDictionary.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), DashboardSharedStrings.GADGET_MSG_NO_DATA); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } this.Dispatcher.BeginInvoke(new AddDictionaryOutputDelegate(RenderWordCloudImage), wordCloudDictionary, totalWordCounts); this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } finally { stopwatch.Stop(); Debug.Print("Word cloud gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete with " + DashboardHelper.RecordCount.ToString() + " records and the following filters:"); Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString()); } } }
/// <summary> /// Used to construct the gadget and assign events /// </summary> /// <param name="dashboardHelper">The dashboard helper to attach to this gadget</param> protected override void Construct() { currentWidth = borderAll.ActualWidth; if (!string.IsNullOrEmpty(CustomOutputHeading)) { headerPanel.Text = CustomOutputHeading; } StrataGridList = new List<Grid>(); groupSvList = new List<ScrollViewer>(); StrataExpanderList = new List<Expander>(); gridLabelsList = new List<TextBlock>(); cbxSortField.SelectionChanged += new SelectionChangedEventHandler(cbxSortField_SelectionChanged); requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); checkForCancellation = new CheckForCancellationDelegate(IsCancelled); mnuCopy.Click += new RoutedEventHandler(mnuCopy_Click); mnuSendDataToHTML.Click += new RoutedEventHandler(mnuSendDataToHTML_Click); mnuRemoveSorts.Click += new RoutedEventHandler(mnuRemoveSorts_Click); #if LINUX_BUILD mnuSendDataToExcel.Visibility = Visibility.Collapsed; #else mnuSendDataToExcel.Visibility = Visibility.Visible; Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot; Microsoft.Win32.RegistryKey excelKey = key.OpenSubKey("Excel.Application"); bool excelInstalled = excelKey == null ? false : true; key = Microsoft.Win32.Registry.ClassesRoot; excelKey = key.OpenSubKey("Excel.Application"); excelInstalled = excelKey == null ? false : true; if (!excelInstalled) { mnuSendDataToExcel.Visibility = Visibility.Collapsed; } else { mnuSendDataToExcel.Click += new RoutedEventHandler(mnuSendDataToExcel_Click); } #endif mnuRemove.Click += new RoutedEventHandler(mnuRemove_Click); mnuSwapSortType.Click += new RoutedEventHandler(mnuSwapSortType_Click); if (!IsHostedByEnter) { mnuSendToBack.Click += new RoutedEventHandler(mnuSendToBack_Click); //mnuRefresh.Click += new RoutedEventHandler(mnuRefresh_Click); mnuClose.Click += new RoutedEventHandler(mnuClose_Click); mnuCenter.Click += new RoutedEventHandler(mnuCenter_Click); mnuSendDataToHTML.Visibility = System.Windows.Visibility.Visible; mnuClose.Visibility = System.Windows.Visibility.Visible; //mnuCenter.Visibility = System.Windows.Visibility.Collapsed;//Visible; // TODO: Re-enable. } else { mnuSendDataToHTML.Visibility = System.Windows.Visibility.Collapsed; mnuClose.Visibility = System.Windows.Visibility.Collapsed; mnuCenter.Visibility = System.Windows.Visibility.Collapsed; borderAll.ContextMenu = null; headerPanel.IsCloseButtonAvailable = false; headerPanel.IsDescriptionButtonAvailable = false; headerPanel.IsFilterButtonAvailable = false; headerPanel.IsOutputCollapseButtonAvailable = false; pathTriangle.Margin = new Thickness(pathTriangle.Margin.Left, pathTriangle.Margin.Top, 8, pathTriangle.Margin.Bottom); grdDimensions.Visibility = System.Windows.Visibility.Collapsed; } columnWarningShown = false; this.IsProcessing = false; this.MaxColumns = 25; this.GadgetStatusUpdate += new GadgetStatusUpdateHandler(RequestUpdateStatusMessage); this.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(IsCancelled); base.Construct(); lbxFields.SelectedIndex = -1; #region Translation mnuCenter.Header = DashboardSharedStrings.GADGET_CENTER; mnuClose.Header = LineListSharedStrings.CMENU_CLOSE; mnuCopy.Header = LineListSharedStrings.CMENU_COPY; mnuRemoveSorts.Header = LineListSharedStrings.CMENU_REMOVE_SORTING; mnuSendDataToExcel.Header = LineListSharedStrings.CMENU_EXCEL; mnuSendDataToHTML.Header = LineListSharedStrings.CMENU_HTML; mnuSendToBack.Header = LineListSharedStrings.CMENU_SEND_TO_BACK; mnuSwapSortType.Header = LineListSharedStrings.CMENU_SWAP_SORT; mnuRemove.Header = LineListSharedStrings.CMENU_REMOVE_FIELD; tblockListVariablesToDisplay.Text = LineListSharedStrings.LIST_VARIABLES_TO_DISPLAY; tblockSortVariables.Text = LineListSharedStrings.SORT_VARIABLES; tblockSortOrder.Text = LineListSharedStrings.SORT_ORDER; tblockLineListProperties.Text = LineListSharedStrings.LIST_CONFIG_TITLE; tblockGroupResultsBy.Text = LineListSharedStrings.GROUP_RESULTS_BY; tblockMaxVarNameLength.Text = LineListSharedStrings.MAX_VAR_NAME_LENGTH; tblockMaxRows.Text = LineListSharedStrings.MAX_ROWS_TO_DISPLAY; tblockMaxWidth.Text = DashboardSharedStrings.GADGET_MAX_WIDTH; tblockMaxHeight.Text = DashboardSharedStrings.GADGET_MAX_HEIGHT; tblockInst1.Text = LineListSharedStrings.INSTRUCTIONS_1; tblockInst2.Text = LineListSharedStrings.INSTRUCTIONS_2; checkboxTabOrder.Content = LineListSharedStrings.SORT_VARS_TAB_ORDER; checkboxUsePrompts.Content = LineListSharedStrings.USE_FIELD_PROMPTS; checkboxListLabels.Content = DashboardSharedStrings.GADGET_LIST_LABELS; checkboxAllowUpdates.Content = LineListSharedStrings.ALLOW_UPDATES; checkboxLineColumn.Content = LineListSharedStrings.SHOW_LINE_COLUMN; checkboxColumnHeaders.Content = LineListSharedStrings.SHOW_COLUMN_HEADINGS; checkboxShowNulls.Content = LineListSharedStrings.SHOW_MISSING_REPRESENTATION; btnRun.Content = LineListSharedStrings.GENERATE_LINE_LIST; #endregion // Translation }
protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { lock (syncLock) { Dictionary<string, string> inputVariableList = GadgetOptions.InputVariableList; this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState)); this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults)); SetGraphDelegate setGraph = new SetGraphDelegate(SetGraph); System.Collections.Generic.Dictionary<string, System.Data.DataTable> Freq_ListSet = new Dictionary<string, System.Data.DataTable>(); string freqVar = string.Empty; string weightVar = string.Empty; string strataVar = string.Empty; bool includeMissing = false; int lagTime = 7; double deviations = 3; if (inputVariableList.ContainsKey("freqvar")) { freqVar = inputVariableList["freqvar"]; } if (inputVariableList.ContainsKey("weightvar")) { weightVar = inputVariableList["weightvar"]; } if (inputVariableList.ContainsKey("stratavar")) { strataVar = inputVariableList["stratavar"]; } if (inputVariableList.ContainsKey("lagtime")) { int.TryParse(inputVariableList["lagtime"], out lagTime); } if (inputVariableList.ContainsKey("deviations")) { double.TryParse(inputVariableList["deviations"], out deviations); } if (inputVariableList.ContainsKey("includemissing")) { if (inputVariableList["includemissing"].Equals("true")) { includeMissing = true; } } deviations = deviations - 0.001; List<string> stratas = new List<string>(); if (!string.IsNullOrEmpty(strataVar)) { stratas.Add(strataVar); } try { RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage); CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled); GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus); GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation); if (this.DataFilters != null && this.DataFilters.Count > 0) { GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false); } else { GadgetOptions.CustomFilter = string.Empty; } Dictionary<DataTable, List<DescriptiveStatistics>> stratifiedFrequencyTables = dashboardHelper.GenerateFrequencyTable(GadgetOptions/*, freqVar, weightVar, stratas, string.Empty, useAllPossibleValues, sortHighLow, includeMissing*/); if (stratifiedFrequencyTables == null || stratifiedFrequencyTables.Count == 0) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), DashboardSharedStrings.GADGET_MSG_NO_DATA); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else if (worker.CancellationPending) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } else { string formatString = string.Empty; foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in stratifiedFrequencyTables) { AddFillerRows(tableKvp.Key); if (tableKvp.Key.Rows.Count > 366) { string exMessage = string.Format(DashboardSharedStrings.ERROR_TOO_MANY_VALUES, freqVar, tableKvp.Key.Rows.Count, 366); throw new ApplicationException(exMessage); } string strataValue = tableKvp.Key.TableName; double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } if (count == 0 && stratifiedFrequencyTables.Count == 1) { // this is the only table and there are no records, so let the user know this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); return; } DataTable frequencies = tableKvp.Key; if (frequencies.Rows.Count == 0) { continue; } } foreach (KeyValuePair<DataTable, List<DescriptiveStatistics>> tableKvp in stratifiedFrequencyTables) { string strataValue = tableKvp.Key.TableName; double count = 0; foreach (DescriptiveStatistics ds in tableKvp.Value) { count = count + ds.observations; } if (count == 0) { continue; } DataTable frequencies = tableKvp.Key; if (frequencies.Rows.Count == 0) { continue; } string tableHeading = tableKvp.Key.TableName; if (stratifiedFrequencyTables.Count > 1) { tableHeading = freqVar; } double lastAvg = double.NegativeInfinity; double lastStdDev = double.NegativeInfinity; Queue<double> frame = new Queue<double>(); List<SimpleDataValue> actualValues = new List<SimpleDataValue>(); List<SimpleDataValue> trendValues = new List<SimpleDataValue>(); List<SimpleDataValue> aberrationValues = new List<SimpleDataValue>(); List<DataGridRow> aberrationDetails = new List<DataGridRow>(); int rowCount = 1; foreach (System.Data.DataRow row in frequencies.Rows) { if (!row[freqVar].Equals(DBNull.Value) || (row[freqVar].Equals(DBNull.Value) && includeMissing == true)) { DateTime displayValue = DateTime.Parse(row[freqVar].ToString()); frame.Enqueue((double)row["freq"]); SimpleDataValue actualValue = new SimpleDataValue(); actualValue.IndependentValue = displayValue; actualValue.DependentValue = (double)row["freq"]; actualValues.Add(actualValue); if (frame.Count > lagTime - 1 /*6*/) { double[] frameArray = frame.ToArray(); double frameAvg = frameArray.Average(); frame.Dequeue(); double stdDev = CalculateStdDev(frameArray); if (lastAvg != double.NegativeInfinity) { SimpleDataValue trendValue = new SimpleDataValue(); trendValue.IndependentValue = displayValue; trendValue.DependentValue = lastAvg; trendValues.Add(trendValue); if ((double)row["freq"] > lastAvg + (/*2.99*/deviations * lastStdDev)) { SimpleDataValue aberrationValue = new SimpleDataValue(); aberrationValue.IndependentValue = displayValue; aberrationValue.DependentValue = (double)row["freq"]; aberrationValues.Add(aberrationValue); DataGridRow aberrationDetail = new DataGridRow() { Date = displayValue, Frequency = (double)row["freq"], RunningAverage = lastAvg, StandardDeviation = lastStdDev }; aberrationDetails.Add(aberrationDetail); } } lastAvg = frameAvg; lastStdDev = stdDev; } rowCount++; } } this.Dispatcher.BeginInvoke(setGraph, strataValue, actualValues, trendValues, aberrationValues, aberrationDetails); System.Threading.Thread.Sleep(1000); } } this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish)); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } catch (Exception ex) { this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message); this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState)); } } }
/// <summary> /// Closes the gadget /// </summary> protected override void CloseGadget() { if (worker != null && worker.WorkerSupportsCancellation) { worker.CancelAsync(); } if (worker != null) { worker.DoWork -= new System.ComponentModel.DoWorkEventHandler(worker_DoWork); worker.RunWorkerCompleted -= new System.ComponentModel.RunWorkerCompletedEventHandler(worker_WorkerCompleted); } if (baseWorker != null) { baseWorker.DoWork -= new System.ComponentModel.DoWorkEventHandler(Execute); } if (Parameters != null) { Parameters.GadgetStatusUpdate -= new GadgetStatusUpdateHandler(requestUpdateStatus); Parameters.GadgetCheckForCancellation -= new GadgetCheckForCancellationHandler(checkForCancellation); Parameters = null; } this.GadgetStatusUpdate -= new GadgetStatusUpdateHandler(RequestUpdateStatusMessage); this.GadgetCheckForCancellation -= new GadgetCheckForCancellationHandler(IsCancelled); panelMain.Children.Clear(); requestUpdateStatus = null; checkForCancellation = null; if (!IsHostedByEnter) { mnuSendToBack.Click -= new RoutedEventHandler(mnuSendToBack_Click); mnuClose.Click -= new RoutedEventHandler(mnuClose_Click); } base.CloseGadget(); Parameters = null; }