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());
                }
            }
        }
        public string BuildInputParams(
            string chartType, 
            string chartTitle, 
            string independentTitle, 
            string dependentTitle, 
            string independentValueFormat,
            string dependentValueFormat,
            string interval,
            string intervalUnit,
            object startFrom,
            DataTable regressionTable)
        {
            StringBuilder chartLineSeries = new StringBuilder();

            string independentValueType = "";
            string dependentValueType = "";

            chartType = chartType.Replace("\"", "");
            chartTitle = chartTitle.Replace("\"", "");
            independentTitle = independentTitle.Replace("\"", "");
            dependentTitle = dependentTitle.Replace("\"", "");

            switch (chartType.ToUpper().Replace(" ", ""))
            {
                case SilverlightStatics.Area:
                    chartType = SilverlightStatics.Area;
                    break;

                case SilverlightStatics.Column:
                    chartType = SilverlightStatics.Bar;
                    break;

                case SilverlightStatics.Bubble:
                    chartType = SilverlightStatics.Bubble;
                    break;

                case SilverlightStatics.EpiCurve:
                    chartType = SilverlightStatics.Histogram;
                    break;

                case SilverlightStatics.Bar:
                    chartType = SilverlightStatics.RotatedBar;
                    break;

                case SilverlightStatics.Line:
                    chartType = SilverlightStatics.Line;
                    break;

                case SilverlightStatics.Pie:
                    chartType = SilverlightStatics.Pie;
                    break;

                case SilverlightStatics.Scatter:
                    chartType = SilverlightStatics.Scatter;
                    break;

                case SilverlightStatics.Stacked:
                    chartType = SilverlightStatics.Stacked;
                    break;

                case SilverlightStatics.TreeMap:
                    chartType = SilverlightStatics.TreeMap;
                    break;

                default:
                    chartType = SilverlightStatics.Bar;
                    break;
            }

            chartLineSeries.Append("chartLineSeries=");

            DataTable distinct = regressionTable.DefaultView.ToTable(true, "SeriesName");

            if (regressionTable.Rows.Count == 0)
            {
                return string.Empty;
            }

            foreach (DataRow row in distinct.Rows)
            {
                string seriesName = row["SeriesName"].ToString();
                string filter = string.Format("SeriesName = '{0}'", seriesName);

                DataRow[] regressionData = regressionTable.Select(filter);

                if (chartLineSeries.ToString().EndsWith("chartLineSeries=") == false)
                {
                    chartLineSeries.Append(SilverlightStatics.SeparateLineSeries);
                }

                chartLineSeries.Append("(");
                chartLineSeries.Append("LineSeriesTitle");
                chartLineSeries.Append(seriesName);
                chartLineSeries.Append("LineSeriesTitle");

                Type indepValueType = typeof(string);
                indepValueType = regressionData[0]["Predictor"].GetType();
                independentValueType = indepValueType.ToString();

                chartLineSeries.Append(SilverlightStatics.IndependentValueFormat);
                chartLineSeries.Append(independentValueFormat);
                chartLineSeries.Append(SilverlightStatics.IndependentValueFormat);

                chartLineSeries.Append(SilverlightStatics.DependentValueFormat);
                chartLineSeries.Append(dependentValueFormat);
                chartLineSeries.Append(SilverlightStatics.DependentValueFormat);

                chartLineSeries.Append(SilverlightStatics.LineSeriesDataString);

                foreach (DataRow regression in regressionData)
                {
                    if (regression["Predictor"] is DateTime)
                    {
                        string dateTimeString = ((DateTime)regression["Predictor"]).ToString(_dateTimeFormat);
                        chartLineSeries.Append(dateTimeString);
                    }
                    else
                    {
                        chartLineSeries.Append(regression["Predictor"].ToString());
                    }

                    chartLineSeries.Append(SilverlightStatics.SeparateIndDepValues);
                    chartLineSeries.Append(regression["Response"].ToString());
                    chartLineSeries.Append(SilverlightStatics.SeparateDataPoints);
                }

                if (chartLineSeries.ToString().EndsWith(SilverlightStatics.SeparateDataPoints))
                {
                    string series = chartLineSeries.ToString().Substring(0, chartLineSeries.Length - SilverlightStatics.SeparateDataPoints.Length);
                    chartLineSeries = new StringBuilder();
                    chartLineSeries.Append(series);
                }

                chartLineSeries.Append(SilverlightStatics.LineSeriesDataString);
            }

            if (chartType == SilverlightStatics.Scatter)
            {
                List<NumericDataValue> dataValues = new List<NumericDataValue>();
                NumericDataValue minValue = null;
                NumericDataValue maxValue = null;
                foreach (DataRow row in regressionTable.Rows)
                {
                    if (row["Response"].Equals(DBNull.Value) || row["Predictor"].Equals(DBNull.Value))
                    {
                        continue;
                    }
                    NumericDataValue currentValue = new NumericDataValue() { DependentValue = Convert.ToDecimal(row["Response"]), IndependentValue = Convert.ToDecimal(row["Predictor"]) };
                    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("Response", "dependvar");
                inputVariableList.Add("intercept", "true");
                inputVariableList.Add("includemissing", "false");
                inputVariableList.Add("p", "0.95");
                inputVariableList.Add("Predictor", "unsorted");

                StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, regressionTable);

                object[] result = new object[] { dataValues, regresResults, maxValue, minValue };

                decimal coefficient = Convert.ToDecimal(regresResults.variables[0].coefficient);
                decimal constant = Convert.ToDecimal(regresResults.variables[1].coefficient);

                NumericDataValue newMaxValue = new NumericDataValue();
                newMaxValue.IndependentValue = maxValue.IndependentValue + 1;
                newMaxValue.DependentValue = (coefficient * maxValue.IndependentValue) + constant;
                NumericDataValue newMinValue = new NumericDataValue();
                newMinValue.IndependentValue = minValue.IndependentValue - 1;
                newMinValue.DependentValue = (coefficient * minValue.IndependentValue) + constant;

                chartLineSeries.Append("LineSeriesDataString)^((LineSeriesTitleLinear RegressionLineSeriesTitleIndependentValueFormatIndependentValueFormatDependentValueFormatDependentValueFormatLineSeriesDataString");
                chartLineSeries.Append(newMinValue.IndependentValue.ToString());
                chartLineSeries.Append("^sidv^");
                chartLineSeries.Append(newMinValue.DependentValue.ToString());
                chartLineSeries.Append("^&^");
                chartLineSeries.Append(newMaxValue.IndependentValue.ToString());
                chartLineSeries.Append("^sidv^");
                chartLineSeries.Append(newMaxValue.DependentValue.ToString());
                chartLineSeries.Append("LineSeriesDataString");
            }

            chartLineSeries.Append(")");

            string inputParams = string.Empty;

            if (chartType.Length > 0) inputParams += string.Format("chartType={0},", chartType);
            if (chartTitle.Length > 0) inputParams += string.Format("chartTitle={0},", chartTitle);

            if (independentTitle.Length > 0) inputParams += string.Format("independentLabel={0},", independentTitle);
            if (dependentTitle.Length > 0) inputParams += string.Format("dependentLabel={0},", dependentTitle);

            if (independentValueFormat.Length > 0) inputParams += string.Format("independentValueFormat={0},", independentValueFormat);
            if (dependentValueFormat.Length > 0) inputParams += string.Format("dependentValueFormat={0},", dependentValueFormat);

            if (independentValueType.Length > 0) inputParams += string.Format("independentValueType={0},", independentValueType);
            if (dependentValueType.Length > 0) inputParams += string.Format("dependentValueType={0},", dependentValueType);

            if (interval.Length > 0) inputParams += string.Format("interval={0},", interval);
            if (intervalUnit.Length > 0) inputParams += string.Format("intervalUnit={0},", intervalUnit);

            if (startFrom is DateTime)
            {
                startFrom = ((DateTime)startFrom).ToString(_dateTimeFormat);
            }
            else
            {
                startFrom = startFrom.ToString();
            }

            if (((string)startFrom).Length > 0) inputParams += string.Format("startFrom={0},", ((string)startFrom));

            inputParams += chartLineSeries.Replace(",", SilverlightStatics.Comma);
            return inputParams;
        }
        private void SetChartData(List<XYChartData> dataList, StatisticsRepository.LinearRegression.LinearRegressionResults regresResults, NumericDataValue maxValue, NumericDataValue minValue)
        {
            List<RegressionChartData> regressionDataList = new List<RegressionChartData>();

            if (regresResults.variables != null)
            {
                decimal coefficient = Convert.ToDecimal(regresResults.variables[0].coefficient);
                decimal constant = Convert.ToDecimal(regresResults.variables[1].coefficient);

                NumericDataValue newMaxValue = new NumericDataValue();
                newMaxValue.IndependentValue = maxValue.IndependentValue;
                newMaxValue.DependentValue = (coefficient * maxValue.IndependentValue) + constant;
                NumericDataValue newMinValue = new NumericDataValue();
                newMinValue.IndependentValue = minValue.IndependentValue;
                newMinValue.DependentValue = (coefficient * minValue.IndependentValue) + constant;

                tblockEquation.Text = "Y = (" + Math.Round(coefficient, 4).ToString() + ")X + " + Math.Round(constant, 4).ToString();

                List<NumericDataValue> regresValues = new List<NumericDataValue>();
                regresValues.Add(newMinValue);
                regresValues.Add(newMaxValue);

                RegressionChartData rChartData = new RegressionChartData();
                rChartData.X = (double)newMinValue.IndependentValue;
                rChartData.Z = (double)newMinValue.DependentValue;
                regressionDataList.Add(rChartData);

                rChartData = new RegressionChartData();
                rChartData.X = (double)newMaxValue.IndependentValue;
                rChartData.Z = (double)newMaxValue.DependentValue;
                regressionDataList.Add(rChartData);

                //bool foundMin = false;
                //bool foundMax = false;

                //foreach (XYChartData chartData in dataList)
                //{
                //    if ((double)chartData.X == (double)newMaxValue.IndependentValue)
                //    {
                //        RegressionChartData rChartData = new RegressionChartData();
                //        rChartData.X = (double)newMaxValue.IndependentValue;
                //        rChartData.Z = (double)newMaxValue.DependentValue;

                //        regressionDataList.Add(rChartData);

                //        //chartData.Z = (double)newMaxValue.DependentValue;
                //        foundMax = true;
                //    }
                //    if ((double)chartData.X == (double)newMinValue.IndependentValue)
                //    {
                //        //chartData.Z = (double)newMinValue.DependentValue;

                //        RegressionChartData rChartData = new RegressionChartData();
                //        rChartData.X = (double)newMaxValue.IndependentValue;
                //        rChartData.Z = (double)newMinValue.DependentValue;

                //        regressionDataList.Add(rChartData);

                //        foundMin = true;
                //    }
                //}

                //if (foundMax == false)
                //{
                //    XYChartData data = new XYChartData();
                //    data.X = (double)newMaxValue.IndependentValue;
                //    //data.Z = (double)newMaxValue.DependentValue;
                //    dataList.Add(data);

                //    RegressionChartData rChartData = new RegressionChartData();
                //    rChartData.X = data.X;
                //    rChartData.Z = (double)newMaxValue.DependentValue;
                //    regressionDataList.Add(rChartData);

                //    xAxis.MaxValue = data.X;
                //}

                //if (foundMin == false)
                //{
                //    XYChartData data = new XYChartData();
                //    data.X = (double)newMinValue.IndependentValue;
                //    //data.Z = (double)newMinValue.DependentValue;
                //    dataList.Add(data);

                //    RegressionChartData rChartData = new RegressionChartData();
                //    rChartData.X = data.X;
                //    rChartData.Z = (double)newMinValue.DependentValue;
                //    regressionDataList.Add(rChartData);

                //    xAxis.MinValue = data.X;
                //}
            }

            //xAxis.UseOnlyVisiblePointsToComputeRange = true;

            //xyChart.DataSource = dataList;
            series0.DataSource = dataList;
            series1.DataSource = regressionDataList;
            xyChart.Width = double.Parse(txtWidth.Text);
            xyChart.Height = double.Parse(txtHeight.Text);

            //xAxis.UseOnlyVisiblePointsToComputeRange = true;
        }
        public string BuildInputParams(
            string chartType,
            string chartTitle,
            string independentTitle,
            string dependentTitle,
            string independentValueFormat,
            string dependentValueFormat,
            string interval,
            string intervalUnit,
            object startFrom,
            DataTable regressionTable)
        {
            StringBuilder chartLineSeries = new StringBuilder();

            string independentValueType = "";
            string dependentValueType   = "";

            chartType        = chartType.Replace("\"", "");
            chartTitle       = chartTitle.Replace("\"", "");
            independentTitle = independentTitle.Replace("\"", "");
            dependentTitle   = dependentTitle.Replace("\"", "");

            switch (chartType.ToUpperInvariant().Replace(" ", ""))
            {
            case SilverlightStatics.Area:
                chartType = SilverlightStatics.Area;
                break;

            case SilverlightStatics.Column:
                chartType = SilverlightStatics.Bar;
                break;

            case SilverlightStatics.Bubble:
                chartType = SilverlightStatics.Bubble;
                break;

            case SilverlightStatics.EpiCurve:
                chartType = SilverlightStatics.Histogram;
                break;

            case SilverlightStatics.Bar:
                chartType = SilverlightStatics.RotatedBar;
                break;

            case SilverlightStatics.Line:
                chartType = SilverlightStatics.Line;
                break;

            case SilverlightStatics.Pie:
                chartType = SilverlightStatics.Pie;
                break;

            case SilverlightStatics.Scatter:
                chartType = SilverlightStatics.Scatter;
                break;

            case SilverlightStatics.Stacked:
                chartType = SilverlightStatics.Stacked;
                break;

            case SilverlightStatics.TreeMap:
                chartType = SilverlightStatics.TreeMap;
                break;

            default:
                chartType = SilverlightStatics.Bar;
                break;
            }

            chartLineSeries.Append("chartLineSeries=");

            DataTable distinct = regressionTable.DefaultView.ToTable(true, "SeriesName");

            if (regressionTable.Rows.Count == 0)
            {
                return(string.Empty);
            }

            foreach (DataRow row in distinct.Rows)
            {
                string seriesName = row["SeriesName"].ToString();
                string filter     = string.Format("SeriesName = '{0}'", seriesName);

                DataRow[] regressionData = regressionTable.Select(filter);

                if (chartLineSeries.ToString().EndsWith("chartLineSeries=") == false)
                {
                    chartLineSeries.Append(SilverlightStatics.SeparateLineSeries);
                }

                chartLineSeries.Append("(");
                chartLineSeries.Append("LineSeriesTitle");
                chartLineSeries.Append(seriesName);
                chartLineSeries.Append("LineSeriesTitle");

                Type indepValueType = typeof(string);
                indepValueType       = regressionData[0]["Predictor"].GetType();
                independentValueType = indepValueType.ToString();

                chartLineSeries.Append(SilverlightStatics.IndependentValueFormat);
                chartLineSeries.Append(independentValueFormat);
                chartLineSeries.Append(SilverlightStatics.IndependentValueFormat);

                chartLineSeries.Append(SilverlightStatics.DependentValueFormat);
                chartLineSeries.Append(dependentValueFormat);
                chartLineSeries.Append(SilverlightStatics.DependentValueFormat);

                chartLineSeries.Append(SilverlightStatics.LineSeriesDataString);

                foreach (DataRow regression in regressionData)
                {
                    if (regression["Predictor"] is DateTime)
                    {
                        string dateTimeString = ((DateTime)regression["Predictor"]).ToString(_dateTimeFormat);
                        chartLineSeries.Append(dateTimeString);
                    }
                    else
                    {
                        chartLineSeries.Append(regression["Predictor"].ToString());
                    }

                    chartLineSeries.Append(SilverlightStatics.SeparateIndDepValues);
                    chartLineSeries.Append(regression["Response"].ToString());
                    chartLineSeries.Append(SilverlightStatics.SeparateDataPoints);
                }

                if (chartLineSeries.ToString().EndsWith(SilverlightStatics.SeparateDataPoints))
                {
                    string series = chartLineSeries.ToString().Substring(0, chartLineSeries.Length - SilverlightStatics.SeparateDataPoints.Length);
                    chartLineSeries = new StringBuilder();
                    chartLineSeries.Append(series);
                }

                chartLineSeries.Append(SilverlightStatics.LineSeriesDataString);
            }

            if (chartType == SilverlightStatics.Scatter)
            {
                List <NumericDataValue> dataValues = new List <NumericDataValue>();
                NumericDataValue        minValue   = null;
                NumericDataValue        maxValue   = null;
                foreach (DataRow row in regressionTable.Rows)
                {
                    if (row["Response"].Equals(DBNull.Value) || row["Predictor"].Equals(DBNull.Value))
                    {
                        continue;
                    }
                    NumericDataValue currentValue = new NumericDataValue()
                    {
                        DependentValue = Convert.ToDecimal(row["Response"]), IndependentValue = Convert.ToDecimal(row["Predictor"])
                    };
                    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("Response", "dependvar");
                inputVariableList.Add("intercept", "true");
                inputVariableList.Add("includemissing", "false");
                inputVariableList.Add("p", "0.95");
                inputVariableList.Add("Predictor", "unsorted");

                StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, regressionTable);

                object[] result = new object[] { dataValues, regresResults, maxValue, minValue };

                decimal coefficient = Convert.ToDecimal(regresResults.variables[0].coefficient);
                decimal constant    = Convert.ToDecimal(regresResults.variables[1].coefficient);

                NumericDataValue newMaxValue = new NumericDataValue();
                newMaxValue.IndependentValue = maxValue.IndependentValue + 1;
                newMaxValue.DependentValue   = (coefficient * maxValue.IndependentValue) + constant;
                NumericDataValue newMinValue = new NumericDataValue();
                newMinValue.IndependentValue = minValue.IndependentValue - 1;
                newMinValue.DependentValue   = (coefficient * minValue.IndependentValue) + constant;

                chartLineSeries.Append("LineSeriesDataString)^((LineSeriesTitleLinear RegressionLineSeriesTitleIndependentValueFormatIndependentValueFormatDependentValueFormatDependentValueFormatLineSeriesDataString");
                chartLineSeries.Append(newMinValue.IndependentValue.ToString());
                chartLineSeries.Append("^sidv^");
                chartLineSeries.Append(newMinValue.DependentValue.ToString());
                chartLineSeries.Append("^&^");
                chartLineSeries.Append(newMaxValue.IndependentValue.ToString());
                chartLineSeries.Append("^sidv^");
                chartLineSeries.Append(newMaxValue.DependentValue.ToString());
                chartLineSeries.Append("LineSeriesDataString");
            }

            chartLineSeries.Append(")");

            string inputParams = string.Empty;

            if (chartType.Length > 0)
            {
                inputParams += string.Format("chartType={0},", chartType);
            }
            if (chartTitle.Length > 0)
            {
                inputParams += string.Format("chartTitle={0},", chartTitle);
            }

            if (independentTitle.Length > 0)
            {
                inputParams += string.Format("independentLabel={0},", independentTitle);
            }
            if (dependentTitle.Length > 0)
            {
                inputParams += string.Format("dependentLabel={0},", dependentTitle);
            }

            if (independentValueFormat.Length > 0)
            {
                inputParams += string.Format("independentValueFormat={0},", independentValueFormat);
            }
            if (dependentValueFormat.Length > 0)
            {
                inputParams += string.Format("dependentValueFormat={0},", dependentValueFormat);
            }

            if (independentValueType.Length > 0)
            {
                inputParams += string.Format("independentValueType={0},", independentValueType);
            }
            if (dependentValueType.Length > 0)
            {
                inputParams += string.Format("dependentValueType={0},", dependentValueType);
            }

            if (interval.Length > 0)
            {
                inputParams += string.Format("interval={0},", interval);
            }
            if (intervalUnit.Length > 0)
            {
                inputParams += string.Format("intervalUnit={0},", intervalUnit);
            }

            if (startFrom is DateTime)
            {
                startFrom = ((DateTime)startFrom).ToString(_dateTimeFormat);
            }
            else
            {
                startFrom = startFrom.ToString();
            }


            if (((string)startFrom).Length > 0)
            {
                inputParams += string.Format("startFrom={0},", ((string)startFrom));
            }

            inputParams += chartLineSeries.Replace(",", SilverlightStatics.Comma);
            return(inputParams);
        }
        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);
            }
        }
        private void RenderFinishScatterChart(List<NumericDataValue> dataValues, StatisticsRepository.LinearRegression.LinearRegressionResults results, NumericDataValue maxValue, NumericDataValue minValue)
        {
            Chart chart = new Chart();
            chart.Loaded += new RoutedEventHandler(chart_Loaded);
            chart.BorderThickness = new Thickness(0);
            ScatterSeries series = new ScatterSeries();

            LinearAxis xaxis = new LinearAxis();
            xaxis.Orientation = AxisOrientation.X;
            xaxis.Title = cbxScatterXAxisField.SelectedItem.ToString();
            series.IndependentAxis = xaxis;

            LinearAxis yaxis = new LinearAxis();
            yaxis.Orientation = AxisOrientation.Y;
            yaxis.Title = cbxScatterYAxisField.SelectedItem.ToString();
            series.DependentRangeAxis = yaxis;
            yaxis.ShowGridLines = (bool)checkboxShowHorizontalGridLines.IsChecked;

            series.IndependentValuePath = "IndependentValue";
            series.DependentValuePath = "DependentValue";
            series.ItemsSource = dataValues;
            chart.Series.Add(series);

            if (results.variables != null)
            {
                decimal coefficient = Convert.ToDecimal(results.variables[0].coefficient);
                decimal constant = Convert.ToDecimal(results.variables[1].coefficient);

                NumericDataValue newMaxValue = new NumericDataValue();
                newMaxValue.IndependentValue = maxValue.IndependentValue + 1;
                newMaxValue.DependentValue = (coefficient * maxValue.IndependentValue) + constant;
                NumericDataValue newMinValue = new NumericDataValue();
                newMinValue.IndependentValue = minValue.IndependentValue - 1;
                newMinValue.DependentValue = (coefficient * minValue.IndependentValue) + constant;

                List<NumericDataValue> regresValues = new List<NumericDataValue>();
                regresValues.Add(newMinValue);
                regresValues.Add(newMaxValue);

                LineSeries regression = new LineSeries();
                regression.DependentRangeAxis = yaxis;
                regression.IndependentAxis = xaxis;
                regression.IndependentValuePath = "IndependentValue";
                regression.DependentValuePath = "DependentValue";
                regression.ItemsSource = regresValues;
                chart.Series.Add(regression);

                series.LegendItems.Clear();
                regression.LegendItems.Clear();

                chart.Height = 600;
                if (dataValues.Count > 20)
                    chart.Width = dataValues.Count * 25;
                else
                    chart.Width = 800;
                pnlMain.Children.Clear();
                pnlMain.Children.Add(chart);
            }
            else
            {
                pnlMain.Children.Clear();
                RenderFinishWithWarning("Insufficient data to produce this chart.");
            }
            //RenderFinish();
        }
        private void SetChartData(List<XYChartData> dataList, StatisticsRepository.LinearRegression.LinearRegressionResults regresResults, NumericDataValue maxValue, NumericDataValue minValue)
        {
            List<RegressionChartData> regressionDataList = new List<RegressionChartData>();
                ScatterChartParameters chtParameters = (ScatterChartParameters)Parameters;

                if (regresResults.variables != null)
                {
                    decimal coefficient = Convert.ToDecimal(regresResults.variables[0].coefficient);
                    decimal constant = Convert.ToDecimal(regresResults.variables[1].coefficient);

                    NumericDataValue newMaxValue = new NumericDataValue();
                    newMaxValue.IndependentValue = maxValue.IndependentValue;
                    newMaxValue.DependentValue = (coefficient * maxValue.IndependentValue) + constant;
                    NumericDataValue newMinValue = new NumericDataValue();
                    newMinValue.IndependentValue = minValue.IndependentValue;
                    newMinValue.DependentValue = (coefficient * minValue.IndependentValue) + constant;

                    tblockEquation.Text = "Y = (" + Math.Round(coefficient, 4).ToString() + ")X + " + Math.Round(constant, 4).ToString();

                    List<NumericDataValue> regresValues = new List<NumericDataValue>();
                    regresValues.Add(newMinValue);
                    regresValues.Add(newMaxValue);

                    RegressionChartData rChartData = new RegressionChartData();
                    rChartData.X = (double)newMinValue.IndependentValue;
                    rChartData.Z = (double)newMinValue.DependentValue;
                    regressionDataList.Add(rChartData);

                    rChartData = new RegressionChartData();
                    rChartData.X = (double)newMaxValue.IndependentValue;
                    rChartData.Z = (double)newMaxValue.DependentValue;
                    regressionDataList.Add(rChartData);
                }

                //xAxis.UseOnlyVisiblePointsToComputeRange = true;

                series0.DataSource = dataList;
                series1.DataSource = regressionDataList;
                xyChart.Width = chtParameters.ChartWidth;
                xyChart.Height = chtParameters.ChartHeight;

                //xAxis.UseOnlyVisiblePointsToComputeRange = true;
        }