Beispiel #1
0
 public Excel.Shape CreateArc(double x1, double y1, double x2, double y2, double length)
 {
     Excel.Shape cloudArc = null;
     try
     {
         string shapeName = AssemblyInfo.Title.ToLower();
         int    i         = 0;
         double angle     = 60;
         double segments  = angle / 10;
         float[,] arcArray = new float[Convert.ToInt32(segments) + 1, 2];
         double theta  = angle * Math.PI / 180;
         double xm     = (x1 + x2) / 2;
         double ym     = (y1 + y2) / 2;
         double xd     = (x2 - x1);
         double yd     = (y2 - y1);
         double d      = Math.Sqrt(xd * xd + yd * yd);
         double r      = d / 2 / Math.Sin(theta / 2);
         double xc     = xm + yd / (2 * Math.Tan(theta / 2));
         double yc     = ym - xd / (2 * Math.Tan(theta / 2));
         double dtheta = theta / segments;
         arcArray[0, 0] = Convert.ToSingle(x1);
         arcArray[0, 1] = Convert.ToSingle(y1);
         double a = Math.Atan2(y1 - yc, x1 - xc) - dtheta;
         for (i = 1; i <= Convert.ToInt32(segments) - 1; i++)
         {
             arcArray[i, 0] = Convert.ToSingle(xc + r * Math.Cos(a));
             arcArray[i, 1] = Convert.ToSingle(yc + r * Math.Sin(a));
             a = a - dtheta;
         }
         arcArray[i, 0] = Convert.ToSingle(x2);
         arcArray[i, 1] = Convert.ToSingle(y2);
         cloudArc       = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddPolyline(arcArray);
         cloudArc.Select();
         cloudArc.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + LineNbr.ToString();
         LineNbr      += 1;
         Globals.ThisAddIn.Application.Selection.Interior.Pattern = Excel.Constants.xlNone;
         SetLineColor();
         return(cloudArc);
     }
     catch (Exception ex)
     {
         ErrorHandler.DisplayMessage(ex);
         return(null);
     }
 }
Beispiel #2
0
        private static void RegistryMarkDrawMarksByWeek(
            Excel.Application xlApp, Excel.Worksheet ws,
            SortedDictionary <string, SortedDictionary <string, ItemRegistryMark> > marksByWeeks,
            List <string> uniqueInnerKeys,
            RegistryMarkChartType type)
        {
            string chartTitle;
            string hint;

            switch (type)
            {
            case RegistryMarkChartType.Total:
                chartTitle = "Всего оценок - хронология";
                hint       = "Отображены все оценки 'Плохо' + 'Средне' + 'Хорошо'";
                break;

            case RegistryMarkChartType.Percentage:
                chartTitle = "Соотношение оценок хорошо и плохо";
                hint       = "Отображены только оценки 'Хорошо' и 'Плохо'";
                break;

            case RegistryMarkChartType.KPI:
                chartTitle = "KPI - хронология";
                hint       = "KPI рассчитывается по формуле: 'Средне' + 'Хорошо' / 'Всего'";
                break;

            default:
                Logging.ToLog("Неизвестный тип оценки - " + type);
                return;
            }

            int row    = 1;
            int column = 1;

            if (type != RegistryMarkChartType.Total)
            {
                foreach (string innerKey in uniqueInnerKeys)
                {
                    ws.Cells[1, column].Value2 = innerKey;
                    column++;
                }
            }

            row++;

            foreach (KeyValuePair <string, SortedDictionary <string, ItemRegistryMark> > pair in marksByWeeks)
            {
                string rowTitle = string.Empty;
                ws.Cells[row, 1].Value2 = pair.Key;
                column = 2;

                foreach (KeyValuePair <string, ItemRegistryMark> weekMarks in pair.Value)
                {
                    object           value;
                    ItemRegistryMark mark = weekMarks.Value;

                    switch (type)
                    {
                    case RegistryMarkChartType.Total:
                        string[] markDate        = weekMarks.Key.Split('/');
                        string   markYear        = markDate[0].Trim(' ');
                        int      weekNumber      = Convert.ToInt32(markDate[1].Trim(' '));
                        string   currentRowTitle = pair.Key + " " + markYear;

                        if (string.IsNullOrEmpty(rowTitle))
                        {
                            rowTitle = currentRowTitle;
                            ws.Cells[row, 1].Value2 = currentRowTitle;
                        }
                        else
                        {
                            if (!rowTitle.Equals(currentRowTitle))
                            {
                                row++;
                                rowTitle = currentRowTitle;
                                ws.Cells[row, 1].Value2 = rowTitle;
                            }
                        }

                        column = weekNumber + 1;
                        value  = mark.MarkBad + mark.MarkMedium + mark.MarkGood;
                        break;

                    case RegistryMarkChartType.Percentage:
                        value = weekMarks.Value.MarkGood;
                        break;

                    case RegistryMarkChartType.KPI:
                        if (mark.MarkTotal > 0)
                        {
                            value = ((double)mark.MarkTotal - (double)mark.MarkBad) / (double)mark.MarkTotal;
                        }
                        else
                        {
                            value = string.Empty;
                        }
                        break;

                    default:
                        continue;
                    }

                    ws.Cells[row, column].Value2 = value;

                    if (type == RegistryMarkChartType.Percentage ||
                        type == RegistryMarkChartType.KPI)
                    {
                        ws.Cells[row, column].NumberFormat = "0%";
                    }

                    column++;
                }

                row++;
            }

            column = 1;
            ws.Cells[row, column].Value2 = "ИТОГО";
            foreach (string innerKey in uniqueInnerKeys)
            {
                column++;
                if (type == RegistryMarkChartType.KPI)
                {
                    double marksPositive = 0;
                    double marksTotal    = 0;
                    foreach (KeyValuePair <string, SortedDictionary <String, ItemRegistryMark> > pair in marksByWeeks)
                    {
                        ItemRegistryMark mark = pair.Value[innerKey];
                        marksPositive += mark.MarkTotal - mark.MarkBad;
                        marksTotal    += mark.MarkTotal;
                    }

                    if (marksTotal > 0)
                    {
                        ws.Cells[row, column].Value2 = marksPositive / marksTotal;
                    }
                    else
                    {
                        ws.Cells[row, column].Value2 = string.Empty;
                    }

                    ws.Cells[row, column].NumberFormat = "0%";
                }
                else
                {
                    ws.Cells[row, column].FormulaR1C1Local = "=СУММ(R[-" + (row - 2) + "]C:R[-1]C)";
                }
            }

            Excel.Shape shape = xlApp.ActiveSheet.Shapes.AddChart2(234, Excel.XlChartType.xlLineMarkers, 10, 200, 1350, 370);
            shape.Select();
            xlApp.ActiveChart.SetSourceData(ws.UsedRange);
            xlApp.ActiveChart.ChartTitle.Text = chartTitle;

            for (int i = 1; i <= marksByWeeks.Keys.Count; i++)
            {
                xlApp.ActiveChart.FullSeriesCollection(i).IsFiltered = true;
            }

            ws.Cells[row + 2, 1].Value2      = hint;
            ws.Cells[row + 2, 1].Font.Italic = true;

            ws.Range["A1"].Select();

            Marshal.ReleaseComObject(shape);
            shape = null;
        }
Beispiel #3
0
        public Excel.Shape CreateHatchArea(double x, double y, double h, double w)
        {
            string shapeName = AssemblyInfo.Title.ToLower();
            double length    = Properties.Settings.Default.Markup_ShapeLineSpacing;
            double xx1       = 0;
            double yy1       = 0;
            double xx2       = 0;
            double yy2       = 0;
            double x1        = x + y;
            double x2        = Math.Floor(x1 / length);
            double x3        = (x2 + 1) * length;
            double xDiff     = x3 - x1;
            double xl        = x;
            double xr        = x + w;
            double xw        = w;
            double yt        = y;
            double yb        = y + h;
            double yw        = h;
            double xsp       = x + xDiff;
            double ysp       = yt + xDiff;

            Excel.Shape      hatchLine1 = null;
            Excel.Shape      hatchLine2 = null;
            Excel.Shape      hatchArea  = null;
            Excel.ShapeRange shapeRange = null;
            List <object>    shapesList = new List <object>();

            try
            {
                if (xw >= yw)
                {
                    while (xsp < xr + yw)
                    {
                        if (xsp - xl < yb - yt)
                        {
                            xx1 = xsp;
                            yy1 = yt;
                            xx2 = xl;
                            yy2 = yt + (xx1 - xl);
                        }
                        else
                        {
                            if (xsp <= xr)
                            {
                                xx1 = xsp;
                                yy1 = yt;
                                xx2 = xx1 - yw;
                                yy2 = yb;
                            }
                            else
                            {
                                xx2 = xsp - yw;
                                yy2 = yb;
                                xx1 = xr;
                                yy1 = yb - (xr - xx2);
                            }
                        }
                        hatchLine1 = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddLine(Convert.ToSingle(xx1), Convert.ToSingle(yy1), Convert.ToSingle(xx2), Convert.ToSingle(yy2));
                        hatchLine1.Select();
                        hatchLine1.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + LineNbr.ToString();
                        shapesList.Add(hatchLine1.Name);
                        SetLineColor();
                        xsp      = xsp + length;
                        LineNbr += 1;
                    }
                }
                else
                {
                    while (ysp < yb + xw)
                    {
                        if (ysp - yt < xw)
                        {
                            xx2 = xl;
                            yy2 = ysp;
                            xx1 = xl + (yy2 - yt);
                            yy1 = yt;
                        }
                        else
                        {
                            if (ysp <= yb)
                            {
                                xx2 = xl;
                                yy2 = ysp;
                                xx1 = xr;
                                yy1 = yy2 - xw;
                            }
                            else
                            {
                                xx1 = xr;
                                xx2 = xl + (ysp - yb);
                                yy2 = yb;
                                yy1 = yb - (xr - xx2);
                            }
                        }
                        hatchLine2 = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddLine(Convert.ToSingle(xx1), Convert.ToSingle(yy1), Convert.ToSingle(xx2), Convert.ToSingle(yy2));
                        hatchLine2.Select();
                        hatchLine2.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + LineNbr.ToString();
                        shapesList.Add(hatchLine2.Name);
                        SetLineColor();
                        ysp      = ysp + length;
                        LineNbr += 1;
                    }
                }
                object[] shapes = shapesList.ToArray();
                shapeRange = Globals.ThisAddIn.Application.ActiveSheet.Shapes.Range(shapes);
                shapeRange.Group();
                shapeRange.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat);
                hatchArea       = Globals.ThisAddIn.Application.ActiveSheet.Shapes(shapeRange.Name);
                Properties.Settings.Default.Markup_LastShapeName = shapeRange.Name;
                return(hatchArea);
            }

            catch (System.Runtime.InteropServices.COMException ex)
            {
                ErrorHandler.DisplayMessage(ex, true);
                return(null);
            }
            catch (Exception ex)
            {
                ErrorHandler.DisplayMessage(ex, true);
                return(null);
            }
            finally
            {
                if (shapeRange != null)
                {
                    Marshal.FinalReleaseComObject(shapeRange);
                }
                if (hatchLine1 != null)
                {
                    Marshal.FinalReleaseComObject(hatchLine1);
                }
                if (hatchLine2 != null)
                {
                    Marshal.FinalReleaseComObject(hatchLine2);
                }
            }
        }
Beispiel #4
0
        public void CreateRevisionTriangle()
        {
            Excel.Shape      shpTriangle = null;
            Excel.Shape      txtTriangle = null;
            Excel.ShapeRange shapeRange  = null;
            try
            {
                if (ErrorHandler.IsEnabled(true) == false)
                {
                    return;
                }
                ErrorHandler.CreateLogRecord();
                string shapeName = AssemblyInfo.Title.ToLower();
                Single[,] triArray = new Single[4, 2];
                double x         = 0;
                double y         = Globals.ThisAddIn.Application.Selection.Top;
                double h         = Globals.ThisAddIn.Application.Selection.RowHeight;
                double w         = Convert.ToInt32(h * 2.2 / Math.Sqrt(3));
                double f         = Globals.ThisAddIn.Application.Selection.Font.Size;
                double selWidth  = Globals.ThisAddIn.Application.Selection.Width;
                double selLeft   = Globals.ThisAddIn.Application.Selection.Left;
                double selHorAli = Globals.ThisAddIn.Application.Selection.HorizontalAlignment;
                double xlAliCntr = Convert.ToDouble(Excel.XlHAlign.xlHAlignCenter);

                if (selHorAli == xlAliCntr & selWidth > w)
                {
                    x = selLeft + (selWidth - w) / 2;
                }
                else
                {
                    x = selLeft;
                }

                triArray[0, 0] = Convert.ToSingle(x + w / 2);
                triArray[0, 1] = Convert.ToSingle(y);
                triArray[1, 0] = Convert.ToSingle(x);
                triArray[1, 1] = Convert.ToSingle(y + h);
                triArray[2, 0] = Convert.ToSingle(x + w);
                triArray[2, 1] = Convert.ToSingle(y + h);
                triArray[3, 0] = Convert.ToSingle(x + w / 2);
                triArray[3, 1] = Convert.ToSingle(y);

                shpTriangle             = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddPolyline(triArray);
                shpTriangle.Name        = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + "1";
                shpTriangle.Line.Weight = Convert.ToSingle(1.5);

                //add a textbox to the triangle
                txtTriangle = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, Convert.ToSingle(x), Convert.ToSingle(y + h * 0.2), Convert.ToSingle(w), Convert.ToSingle(h * 0.8));
                txtTriangle.Select();
                txtTriangle.TextEffect.Text = Properties.Settings.Default.Markup_TriangleRevisionCharacter;
                Globals.ThisAddIn.Application.Selection.Font.Color          = Properties.Settings.Default.Markup_ShapeLineColor;
                Globals.ThisAddIn.Application.Selection.Font.Size           = f;
                Globals.ThisAddIn.Application.Selection.Border.LineStyle    = Excel.Constants.xlNone;
                Globals.ThisAddIn.Application.Selection.Interior.ColorIndex = Excel.Constants.xlNone;
                Globals.ThisAddIn.Application.Selection.Shadow         = false;
                Globals.ThisAddIn.Application.Selection.RoundedCorners = false;
                txtTriangle.TextFrame.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                txtTriangle.TextFrame.VerticalAlignment   = Excel.XlVAlign.xlVAlignCenter;
                txtTriangle.TextFrame.AutoSize            = true;
                txtTriangle.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + "2";

                //group both shapes together
                object[] shapes = { shpTriangle.Name, txtTriangle.Name };
                shapeRange = Globals.ThisAddIn.Application.ActiveSheet.Shapes.Range(shapes);
                shapeRange.Group();
                shapeRange.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + "3";
                shpTriangle.Select();
                Globals.ThisAddIn.Application.Selection.Interior.Pattern = Excel.XlPattern.xlPatternNone;
                SetLineColor();
                Globals.ThisAddIn.Application.ActiveCell.Select();
                Properties.Settings.Default.Markup_LastShapeName = shapeRange.Name;
            }
            catch (Exception ex)
            {
                ErrorHandler.DisplayMessage(ex);
            }
            finally
            {
                if (shapeRange != null)
                {
                    Marshal.ReleaseComObject(shapeRange);
                }
                if (txtTriangle != null)
                {
                    Marshal.ReleaseComObject(txtTriangle);
                }
                if (shpTriangle != null)
                {
                    Marshal.ReleaseComObject(shpTriangle);
                }
            }
        }