Example #1
0
        ImportGraph
        (
            IGraph sourceGraph,
            String [] edgeAttributes,
            String [] vertexAttributes,
            Boolean clearTablesFirst,
            Microsoft.Office.Interop.Excel.Workbook destinationNodeXLWorkbook
        )
        {
            Debug.Assert(sourceGraph != null);
            Debug.Assert(destinationNodeXLWorkbook != null);
            AssertValid();

            if (clearTablesFirst)
            {
                NodeXLWorkbookUtil.ClearAllNodeXLTables(destinationNodeXLWorkbook);
            }

            // Get the required table that contains edge data.  GetEdgeTable()
            // throws an exception if the table is missing.

            EdgeWorksheetReader oEdgeWorksheetReader = new EdgeWorksheetReader();

            ListObject oEdgeTable =
                oEdgeWorksheetReader.GetEdgeTable(destinationNodeXLWorkbook);

            // Get the required columns.

            Range oVertex1NameColumnData = null;
            Range oVertex2NameColumnData = null;

            if (
                !ExcelUtil.TryGetTableColumnData(oEdgeTable,
                                                 EdgeTableColumnNames.Vertex1Name, out oVertex1NameColumnData)
                ||
                !ExcelUtil.TryGetTableColumnData(oEdgeTable,
                                                 EdgeTableColumnNames.Vertex2Name, out oVertex2NameColumnData)
                )
            {
                ErrorUtil.OnMissingColumn();
            }

            // Import the edges and their attributes into the workbook.

            ImportEdges(sourceGraph, edgeAttributes, oEdgeTable,
                        oVertex1NameColumnData, oVertex2NameColumnData, !clearTablesFirst);

            // Populate the vertex worksheet with the name of each unique vertex in
            // the edge worksheet.

            (new VertexWorksheetPopulator()).PopulateVertexWorksheet(
                destinationNodeXLWorkbook, false);

            // Get the table that contains vertex data.

            ListObject oVertexTable;
            Range      oVertexNameColumnData = null;
            Range      oVisibilityColumnData = null;

            if (
                !ExcelUtil.TryGetTable(destinationNodeXLWorkbook,
                                       WorksheetNames.Vertices, TableNames.Vertices, out oVertexTable)
                ||
                !ExcelUtil.TryGetTableColumnData(oVertexTable,
                                                 VertexTableColumnNames.VertexName, out oVertexNameColumnData)
                ||
                !ExcelUtil.TryGetTableColumnData(oVertexTable,
                                                 CommonTableColumnNames.Visibility, out oVisibilityColumnData)
                )
            {
                ErrorUtil.OnMissingColumn();
            }

            // Import isolated vertices and the attributes for all the graph's
            // vertices.

            ImportVertices(sourceGraph, vertexAttributes, oVertexTable,
                           oVertexNameColumnData, oVisibilityColumnData);
        }
Example #2
0
        AutoFillByVertexCategory
        (
            Microsoft.Office.Interop.Excel.Workbook oWorkbook,
            Microsoft.Office.Interop.Excel.ListObject oEdgeTable,
            Microsoft.Office.Interop.Excel.ListObject oVertexTable,
            String sVertexCategoryColumnName,
            Boolean bShowVertexLabels,
            String sVertexLabelColumnName
        )
        {
            Debug.Assert(oWorkbook != null);
            Debug.Assert(oEdgeTable != null);
            Debug.Assert(oVertexTable != null);
            Debug.Assert(!String.IsNullOrEmpty(sVertexCategoryColumnName));

            Debug.Assert(!bShowVertexLabels ||
                         !String.IsNullOrEmpty(sVertexLabelColumnName));

            Range oVisibleCategoryRange = null;
            Range oVisibleShapeRange    = null;
            Range oVisibleColorRange    = null;
            Range oVisibleRadiusRange   = null;;

            if (
                !ExcelUtil.TryGetVisibleTableColumnData(oVertexTable,
                                                        sVertexCategoryColumnName, out oVisibleCategoryRange)
                ||
                !ExcelUtil.TryGetVisibleTableColumnData(oVertexTable,
                                                        VertexTableColumnNames.Shape, out oVisibleShapeRange)
                ||
                !ExcelUtil.TryGetVisibleTableColumnData(oVertexTable,
                                                        VertexTableColumnNames.Color, out oVisibleColorRange)
                ||
                !ExcelUtil.TryGetVisibleTableColumnData(oVertexTable,
                                                        VertexTableColumnNames.Radius, out oVisibleRadiusRange)
                )
            {
                ErrorUtil.OnMissingColumn();
            }

            // Loop through the visible areas to create a dictionary of categories.
            // The key is a unique category name and the value is a scheme index.
            // If the category column contains three unique categories, for
            // example, the dictionary entries will look like this:
            //
            // Category A, 0
            // Category B, 1
            // Category C, 2

            Dictionary <String, Int32> oCategoryDictionary =
                new Dictionary <String, Int32>();

            Areas oCategoryAreas = oVisibleCategoryRange.Areas;
            Int32 iAreas         = oCategoryAreas.Count;
            Int32 iSchemeIndex   = 0;

            for (Int32 iArea = 1; iArea <= iAreas; iArea++)
            {
                Object [,] aoCategoryValues = ExcelUtil.GetRangeValues(
                    oCategoryAreas[iArea]);

                Int32  iRows = aoCategoryValues.GetUpperBound(0);
                String sCategory;

                for (Int32 iRow = 1; iRow <= iRows; iRow++)
                {
                    if (ExcelUtil.TryGetNonEmptyStringFromCell(aoCategoryValues,
                                                               iRow, 1, out sCategory))
                    {
                        if (!oCategoryDictionary.ContainsKey(sCategory))
                        {
                            oCategoryDictionary[sCategory] = iSchemeIndex;
                            iSchemeIndex++;
                        }
                    }
                }
            }

            // Scheme index that will be used for vertices that have an empty
            // category cell.

            Int32 SchemeIndexForNoCategory = iSchemeIndex;

            Areas oShapeAreas  = oVisibleShapeRange.Areas;
            Areas oColorAreas  = oVisibleColorRange.Areas;
            Areas oRadiusAreas = oVisibleRadiusRange.Areas;

            Debug.Assert(oShapeAreas.Count == iAreas);
            Debug.Assert(oColorAreas.Count == iAreas);
            Debug.Assert(oRadiusAreas.Count == iAreas);

            ColorConverter2 oColorConverter2 = new ColorConverter2();

            VertexShapeConverter oVertexShapeConverter =
                new VertexShapeConverter();

            // Loop through the visible areas again, this time to fill in the
            // vertex shape, color, and radius columns.

            for (Int32 iArea = 1; iArea <= iAreas; iArea++)
            {
                Object [,] aoCategoryValues = ExcelUtil.GetRangeValues(
                    oCategoryAreas[iArea]);

                Range oShapeArea  = oShapeAreas[iArea];
                Range oColorArea  = oColorAreas[iArea];
                Range oRadiusArea = oRadiusAreas[iArea];

                Object [,] aoShapeValues  = ExcelUtil.GetRangeValues(oShapeArea);
                Object [,] aoColorValues  = ExcelUtil.GetRangeValues(oColorArea);
                Object [,] aoRadiusValues = ExcelUtil.GetRangeValues(oRadiusArea);

                Int32 iRows = aoCategoryValues.GetUpperBound(0);

                Debug.Assert(aoShapeValues.GetUpperBound(0) == iRows);
                Debug.Assert(aoColorValues.GetUpperBound(0) == iRows);
                Debug.Assert(aoRadiusValues.GetUpperBound(0) == iRows);

                String sCategory;

                for (Int32 iRow = 1; iRow <= iRows; iRow++)
                {
                    if (ExcelUtil.TryGetNonEmptyStringFromCell(aoCategoryValues,
                                                               iRow, 1, out sCategory))
                    {
                        iSchemeIndex = oCategoryDictionary[sCategory];
                    }
                    else
                    {
                        iSchemeIndex = SchemeIndexForNoCategory;
                    }

                    VertexShape eShape;
                    Color       oColor;
                    Single      fRadius;

                    GetVertexCategoryScheme(iSchemeIndex, out eShape, out oColor,
                                            out fRadius);

                    aoShapeValues[iRow, 1] =
                        oVertexShapeConverter.GraphToWorkbook(eShape);

                    // Write the color in a format that is understood by
                    // ColorConverter.ConvertFromString(), which is what
                    // WorksheetReaderBase uses.

                    aoColorValues[iRow, 1] =
                        oColorConverter2.GraphToWorkbook(oColor);

                    aoRadiusValues[iRow, 1] = fRadius;
                }

                oShapeArea.set_Value(Missing.Value, aoShapeValues);
                oColorArea.set_Value(Missing.Value, aoColorValues);
                oRadiusArea.set_Value(Missing.Value, aoRadiusValues);
            }

            // Fill in other columns with constants.

            FillColumnsWithConstants(

                oVertexTable, CommonTableColumnNames.Alpha,
                AlphaConverter.MaximumAlphaWorkbook,

                oEdgeTable, EdgeTableColumnNames.Color,
                oColorConverter2.GraphToWorkbook(
                    Color.FromArgb(179, 179, 179)),

                oEdgeTable, EdgeTableColumnNames.Width, 1.0F,

                oEdgeTable, CommonTableColumnNames.Alpha,
                AlphaConverter.MaximumAlphaWorkbook
                );

            // Save the results.

            SaveVertexCategoryResults(oWorkbook, sVertexCategoryColumnName,
                                      oCategoryDictionary);
        }