Class that converts a color between values used in the Excel workbook and values used in the NodeXL graph.
This is called ColorConverter2 to distinguish it from System.Drawing.ColorConverter without requiring the specification of a namespace.

Colors are treated differently from the other value types that derive from TextValueConverterBase, so this class doesn't derive from that base class.

Inheritance: Object
Esempio n. 1
0
        //*************************************************************************
        //  Constructor: ReadWorkbookContext()
        //
        /// <summary>
        /// Initializes a new instance of the <see cref="ReadWorkbookContext" />
        /// class.
        /// </summary>
        //*************************************************************************

        public ReadWorkbookContext()
        {
            m_bIgnoreVertexLocations   = true;
            m_bFillIDColumns           = false;
            m_bPopulateVertexWorksheet = false;
            m_bReadEdgeWeights         = false;
            m_bReadGroups = false;
            m_bReadVertexColorFromGroups   = true;
            m_bReadVertexShapeFromGroups   = true;
            m_bReadVertexLabels            = false;
            m_bReadEdgeLabels              = false;
            m_bReadGroupLabels             = false;
            m_bReadVertexImages            = false;
            m_oDefaultVertexImageSize      = new Nullable <Single>();
            m_eDefaultVertexShape          = VertexShape.Disk;
            m_bReadAllEdgeAndVertexColumns = false;
            m_oGraphRectangle              = Rectangle.FromLTRB(0, 0, 100, 100);
            m_oColorConverter2             = new ColorConverter2();
            m_oBooleanConverter            = new BooleanConverter();
            m_oFontSizeConverter           = new FontSizeConverter();
            m_oEdgeWidthConverter          = new EdgeWidthConverter();
            m_oEdgeStyleConverter          = new EdgeStyleConverter();
            m_oVertexRadiusConverter       = new VertexRadiusConverter();

            m_oVertexLocationConverter =
                new VertexLocationConverter(m_oGraphRectangle);

            m_oVertexNameDictionary  = new Dictionary <String, IVertex>();
            m_oEdgeRowIDDictionary   = new Dictionary <Int32, IIdentityProvider>();
            m_oVertexRowIDDictionary = new Dictionary <Int32, IIdentityProvider>();

            AssertValid();
        }
Esempio n. 2
0
        SetVertexAttributesForAllGroups
        (
            ListObject oGroupTable
        )
        {
            Debug.Assert(oGroupTable != null);

            Range oVertexColorRange, oVertexShapeRange;

            Object [,] aoVertexColorValues, aoVertexShapeValues;

            if (
                !ExcelTableUtil.TryGetTableColumnDataAndValues(oGroupTable,
                                                               GroupTableColumnNames.VertexShape, out oVertexShapeRange,
                                                               out aoVertexShapeValues)
                ||
                !ExcelTableUtil.TryGetTableColumnDataAndValues(oGroupTable,
                                                               GroupTableColumnNames.VertexColor, out oVertexColorRange,
                                                               out aoVertexColorValues)
                )
            {
                return;
            }

            Int32 iGroups = aoVertexShapeValues.GetUpperBound(0);

            Debug.Assert(aoVertexColorValues.GetUpperBound(0) == iGroups);

            ColorConverter2 oColorConverter2 = new ColorConverter2();

            VertexShapeConverter oVertexShapeConverter =
                new VertexShapeConverter();

            for (Int32 i = 0; i < iGroups; i++)
            {
                Color       oColor;
                VertexShape eShape;

                GetVertexAttributes(i, iGroups, out oColor, out eShape);

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

                aoVertexColorValues[i + 1, 1] =
                    oColorConverter2.GraphToWorkbook(oColor);

                aoVertexShapeValues[i + 1, 1] =
                    oVertexShapeConverter.GraphToWorkbook(eShape);
            }

            oVertexColorRange.set_Value(Missing.Value, aoVertexColorValues);
            oVertexShapeRange.set_Value(Missing.Value, aoVertexShapeValues);
        }
Esempio n. 3
0
        TryGetColor
        (
            ExcelTableReader.ExcelTableRow oRow,
            String sColumnName,
            ColorConverter2 oColorConverter2,
            out Color oColor
        )
        {
            Debug.Assert(oRow != null);
            Debug.Assert(!String.IsNullOrEmpty(sColumnName));
            Debug.Assert(oColorConverter2 != null);
            AssertValid();

            oColor = Color.Empty;

            String sColor;

            if (!oRow.TryGetNonEmptyStringFromCell(sColumnName, out sColor))
            {
                return(false);
            }

            if (!oColorConverter2.TryWorkbookToGraph(sColor, out oColor))
            {
                Range oInvalidCell = oRow.GetRangeForCell(sColumnName);

                OnWorkbookFormatError(String.Format(

                                          "The cell {0} contains an unrecognized color.  Right-click the"
                                          + " cell and select Select Color on the right-click menu."
                                          ,
                                          ExcelUtil.GetRangeAddress(oInvalidCell)
                                          ),

                                      oInvalidCell
                                      );
            }

            return(true);
        }
Esempio n. 4
0
        ReadColor
        (
            ExcelTableReader.ExcelTableRow oRow,
            String sColumnName,
            IMetadataProvider oEdgeOrVertex,
            String sColorKey,
            ColorConverter2 oColorConverter2
        )
        {
            Debug.Assert(oRow != null);
            Debug.Assert(!String.IsNullOrEmpty(sColumnName));
            Debug.Assert(oEdgeOrVertex != null);
            Debug.Assert(!String.IsNullOrEmpty(sColorKey));
            Debug.Assert(oColorConverter2 != null);
            AssertValid();

            Color oColor;

            if (TryGetColor(oRow, sColumnName, oColorConverter2, out oColor))
            {
                oEdgeOrVertex.SetValue(sColorKey, oColor);
            }
        }
    //*************************************************************************
    //  Constructor: ReadWorkbookContext()
    //
    /// <summary>
    /// Initializes a new instance of the <see cref="ReadWorkbookContext" />
    /// class.
    /// </summary>
    //*************************************************************************

    public ReadWorkbookContext()
    {
        m_bIgnoreVertexLocations = true;
        m_bFillIDColumns = false;
        m_bPopulateVertexWorksheet = false;
        m_bReadEdgeWeights = false;
        m_bReadGroups = false;
        m_bReadVertexColorFromGroups = true;
        m_bReadVertexShapeFromGroups = true;
        m_bReadVertexLabels = false;
        m_bReadEdgeLabels = false;
        m_bReadGroupLabels = false;
        m_bReadVertexImages = false;
        m_oDefaultVertexImageSize = new Nullable<Single>();
        m_eDefaultVertexShape = VertexShape.Disk;
        m_bReadAllEdgeAndVertexColumns = false;
        m_oGraphRectangle = Rectangle.FromLTRB(0, 0, 100, 100);
        m_oColorConverter2 = new ColorConverter2();
        m_oBooleanConverter = new BooleanConverter();
        m_oFontSizeConverter = new FontSizeConverter();
        m_oEdgeWidthConverter = new EdgeWidthConverter();
        m_oEdgeStyleConverter = new EdgeStyleConverter();
        m_oVertexRadiusConverter = new VertexRadiusConverter();

        m_oVertexLocationConverter =
            new VertexLocationConverter(m_oGraphRectangle);

        m_oVertexNameDictionary = new Dictionary<String, IVertex>();
        m_oEdgeRowIDDictionary = new Dictionary<Int32, IIdentityProvider>();
        m_oVertexRowIDDictionary = new Dictionary<Int32, IIdentityProvider>();

        AssertValid();
    }
Esempio n. 6
0
        ThisWorkbook_AttributesEditedInGraph
        (
            Object sender,
            AttributesEditedEventArgs e
        )
        {
            Debug.Assert(e != null);
            AssertValid();

            // The key is the row ID stored in the table's ID column and the value
            // is the one-based row number relative to the worksheet.

            Dictionary <Int32, Int32> oRowIDDictionary;

            if (
                e.EditedEdgeAttributes == null
                ||
                !m_oSheets1And2Helper.TableExists
                ||
                !m_oSheets1And2Helper.TryGetAllRowIDs(out oRowIDDictionary)
                )
            {
                return;
            }

            Microsoft.Office.Interop.Excel.ListObject oEdgeTable =
                Edges.InnerObject;

            Globals.ThisWorkbook.ShowWaitCursor = true;

            // Get the columns that might need to be updated.  These columns are
            // not required.

            Microsoft.Office.Interop.Excel.Range oColorColumnData,
                                                 oWidthColumnData, oStyleColumnData, oAlphaColumnData,
                                                 oVisibilityColumnData, oLabelColumnData,
                                                 oLabelTextColorColumnData, oLabelFontSizeColumnData;

            Object [,] aoColorValues          = null;
            Object [,] aoWidthValues          = null;
            Object [,] aoStyleValues          = null;
            Object [,] aoAlphaValues          = null;
            Object [,] aoVisibilityValues     = null;
            Object [,] aoLabelValues          = null;
            Object [,] aoLabelTextColorValues = null;
            Object [,] aoLabelFontSizeValues  = null;

            ExcelTableUtil.TryGetTableColumnDataAndValues(oEdgeTable,
                                                          EdgeTableColumnNames.Color, out oColorColumnData,
                                                          out aoColorValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oEdgeTable,
                                                          EdgeTableColumnNames.Width, out oWidthColumnData,
                                                          out aoWidthValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oEdgeTable,
                                                          EdgeTableColumnNames.Style, out oStyleColumnData,
                                                          out aoStyleValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oEdgeTable,
                                                          CommonTableColumnNames.Alpha, out oAlphaColumnData,
                                                          out aoAlphaValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oEdgeTable,
                                                          CommonTableColumnNames.Visibility, out oVisibilityColumnData,
                                                          out aoVisibilityValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oEdgeTable,
                                                          EdgeTableColumnNames.Label, out oLabelColumnData,
                                                          out aoLabelValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oEdgeTable,
                                                          EdgeTableColumnNames.LabelTextColor, out oLabelTextColorColumnData,
                                                          out aoLabelTextColorValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oEdgeTable,
                                                          EdgeTableColumnNames.LabelFontSize, out oLabelFontSizeColumnData,
                                                          out aoLabelFontSizeValues);

            ColorConverter2    oColorConverter2    = new ColorConverter2();
            EdgeStyleConverter oEdgeStyleConverter = new EdgeStyleConverter();

            EdgeVisibilityConverter oEdgeVisibilityConverter =
                new EdgeVisibilityConverter();

            // Loop through the IDs of the edges whose attributes were edited
            // in the graph.

            EditedEdgeAttributes oEditedEdgeAttributes = e.EditedEdgeAttributes;

            foreach (Int32 iID in e.EdgeIDs)
            {
                // Look for the row that contains the ID.

                Int32 iRowOneBased;

                if (!oRowIDDictionary.TryGetValue(iID, out iRowOneBased))
                {
                    continue;
                }

                iRowOneBased -= oEdgeTable.Range.Row;

                if (oEditedEdgeAttributes.Color.HasValue && aoColorValues != null)
                {
                    aoColorValues[iRowOneBased, 1] =
                        oColorConverter2.GraphToWorkbook(
                            oEditedEdgeAttributes.Color.Value);
                }

                if (oEditedEdgeAttributes.Width.HasValue && aoWidthValues != null)
                {
                    aoWidthValues[iRowOneBased, 1] =
                        oEditedEdgeAttributes.Width.Value.ToString();
                }

                if (oEditedEdgeAttributes.Style.HasValue && aoStyleValues != null)
                {
                    aoStyleValues[iRowOneBased, 1] =
                        oEdgeStyleConverter.GraphToWorkbook(
                            oEditedEdgeAttributes.Style.Value);
                }

                if (oEditedEdgeAttributes.Alpha.HasValue && aoAlphaValues != null)
                {
                    aoAlphaValues[iRowOneBased, 1] =
                        oEditedEdgeAttributes.Alpha.Value.ToString();
                }

                if (oEditedEdgeAttributes.Visibility.HasValue &&
                    aoVisibilityValues != null)
                {
                    aoVisibilityValues[iRowOneBased, 1] =
                        oEdgeVisibilityConverter.GraphToWorkbook(
                            oEditedEdgeAttributes.Visibility.Value);
                }

                if (!String.IsNullOrEmpty(oEditedEdgeAttributes.Label))
                {
                    aoLabelValues[iRowOneBased, 1] = oEditedEdgeAttributes.Label;
                }

                if (oEditedEdgeAttributes.LabelTextColor.HasValue &&
                    aoLabelTextColorValues != null)
                {
                    aoLabelTextColorValues[iRowOneBased, 1] =
                        oColorConverter2.GraphToWorkbook(
                            oEditedEdgeAttributes.LabelTextColor.Value);
                }

                if (oEditedEdgeAttributes.LabelFontSize.HasValue &&
                    aoLabelFontSizeValues != null)
                {
                    aoLabelFontSizeValues[iRowOneBased, 1] =
                        oEditedEdgeAttributes.LabelFontSize.Value.ToString();
                }
            }

            // Activate this worksheet first, because writing to an inactive
            // worksheet causes problems with the selection in Excel.

            ExcelActiveWorksheetRestorer oExcelActiveWorksheetRestorer =
                m_oSheets1And2Helper.GetExcelActiveWorksheetRestorer();

            ExcelActiveWorksheetState oExcelActiveWorksheetState =
                oExcelActiveWorksheetRestorer.ActivateWorksheet(this.InnerObject);

            try
            {
                m_oSheets1And2Helper.SetColumnDataValues(
                    oColorColumnData, aoColorValues,
                    oWidthColumnData, aoWidthValues,
                    oStyleColumnData, aoStyleValues,
                    oAlphaColumnData, aoAlphaValues,
                    oVisibilityColumnData, aoVisibilityValues,
                    oLabelColumnData, aoLabelValues,
                    oLabelTextColorColumnData, aoLabelTextColorValues,
                    oLabelFontSizeColumnData, aoLabelFontSizeValues
                    );
            }
            finally
            {
                oExcelActiveWorksheetRestorer.Restore(oExcelActiveWorksheetState);
            }

            Globals.ThisWorkbook.ShowWaitCursor = false;
        }
Esempio n. 7
0
        ReadGroupTable
        (
            ListObject oGroupTable,
            ReadWorkbookContext oReadWorkbookContext,
            HashSet <String> oSkippedGroupNames,
            HashSet <String> oHiddenGroupNames
        )
        {
            Debug.Assert(oGroupTable != null);
            Debug.Assert(oReadWorkbookContext != null);
            Debug.Assert(oSkippedGroupNames != null);
            Debug.Assert(oHiddenGroupNames != null);
            AssertValid();

            if (oReadWorkbookContext.FillIDColumns)
            {
                FillIDColumn(oGroupTable);
            }

            Dictionary <String, ExcelTemplateGroupInfo> oGroupNameDictionary =
                new Dictionary <String, ExcelTemplateGroupInfo>();

            ColorConverter2 oColorConverter2 =
                oReadWorkbookContext.ColorConverter2;

            GroupVisibilityConverter oGroupVisibilityConverter =
                new GroupVisibilityConverter();

            BooleanConverter oBooleanConverter =
                oReadWorkbookContext.BooleanConverter;

            ExcelTableReader oExcelTableReader = new ExcelTableReader(oGroupTable);

            foreach (ExcelTableReader.ExcelTableRow oRow in
                     oExcelTableReader.GetRows())
            {
                // Get the group information.

                String      sGroupName;
                Color       oVertexColor;
                VertexShape eVertexShape;

                if (
                    !oRow.TryGetNonEmptyStringFromCell(GroupTableColumnNames.Name,
                                                       out sGroupName)
                    ||
                    !TryGetColor(oRow, GroupTableColumnNames.VertexColor,
                                 oColorConverter2, out oVertexColor)
                    ||
                    !TryGetVertexShape(oRow, GroupTableColumnNames.VertexShape,
                                       out eVertexShape)
                    )
                {
                    continue;
                }

                ReadVisibility(oRow, oGroupVisibilityConverter, sGroupName,
                               oSkippedGroupNames, oHiddenGroupNames);

                Boolean bCollapsed = false;
                Boolean bCollapsedCellValue;

                if (
                    TryGetBoolean(oRow, GroupTableColumnNames.Collapsed,
                                  oBooleanConverter, out bCollapsedCellValue)
                    &&
                    bCollapsedCellValue
                    )
                {
                    bCollapsed = true;
                }

                String sCollapsedAttributes;

                if (!oRow.TryGetNonEmptyStringFromCell(
                        GroupTableColumnNames.CollapsedAttributes,
                        out sCollapsedAttributes))
                {
                    sCollapsedAttributes = null;
                }

                Int32            iRowIDAsInt32;
                Nullable <Int32> iRowID = null;

                if (oRow.TryGetInt32FromCell(CommonTableColumnNames.ID,
                                             out iRowIDAsInt32))
                {
                    iRowID = iRowIDAsInt32;
                }

                ExcelTemplateGroupInfo oExcelTemplateGroupInfo =
                    new ExcelTemplateGroupInfo(sGroupName, iRowID, oVertexColor,
                                               eVertexShape, bCollapsed, sCollapsedAttributes);

                if (oReadWorkbookContext.ReadGroupLabels)
                {
                    String sLabel;

                    if (oRow.TryGetNonEmptyStringFromCell(
                            GroupTableColumnNames.Label, out sLabel))
                    {
                        oExcelTemplateGroupInfo.Label = sLabel;
                    }
                }

                if (!oReadWorkbookContext.IgnoreVertexLocations)
                {
                    System.Drawing.PointF oCollapsedLocation;

                    if (TryGetLocation(oRow, GroupTableColumnNames.CollapsedX,
                                       GroupTableColumnNames.CollapsedY,
                                       oReadWorkbookContext.VertexLocationConverter,
                                       out oCollapsedLocation))
                    {
                        oExcelTemplateGroupInfo.CollapsedLocation =
                            oCollapsedLocation;
                    }
                }

                try
                {
                    oGroupNameDictionary.Add(sGroupName, oExcelTemplateGroupInfo);
                }
                catch (ArgumentException)
                {
                    Range oInvalidCell = oRow.GetRangeForCell(
                        GroupTableColumnNames.Name);

                    OnWorkbookFormatError(String.Format(

                                              "The cell {0} contains a duplicate group name.  There"
                                              + " can't be two rows with the same group name."
                                              ,
                                              ExcelUtil.GetRangeAddress(oInvalidCell)
                                              ),

                                          oInvalidCell
                                          );
                }
            }

            return(oGroupNameDictionary);
        }
 SetUp()
 {
     m_oColorConverter2 = new ColorConverter2();
 }
    TryMapToColor
    (
        ListObject table,
        String sourceColumnName,
        String destinationColumnName,
        Boolean useSourceNumber1,
        Boolean useSourceNumber2,
        Double sourceNumber1,
        Double sourceNumber2,
        Color destinationColor1,
        Color destinationColor2,
        Boolean ignoreOutliers,
        Boolean useLogs,
        out Double sourceCalculationNumber1,
        out Double sourceCalculationNumber2,
        out Int32 decimalPlaces
    )
    {
        Debug.Assert(table != null);
        Debug.Assert( !String.IsNullOrEmpty(sourceColumnName) );
        Debug.Assert( !String.IsNullOrEmpty(destinationColumnName) );
        Debug.Assert(!useLogs || !useSourceNumber1 || sourceNumber1 > 0);
        Debug.Assert(!useLogs || !useSourceNumber2 || sourceNumber2 > 0);

        sourceCalculationNumber1 = sourceCalculationNumber2 = Double.MinValue;
        decimalPlaces = Int32.MinValue;

        Range oVisibleSourceRange, oVisibleDestinationRange;

        if ( !TryGetVisibleSourceAndDestination(table, sourceColumnName,
            destinationColumnName, out oVisibleSourceRange,
            out oVisibleDestinationRange) )
        {
            return (false);
        }

        // Get the source calculation range, which is the range of source
        // numbers used in calculating the mapping of source numbers to
        // destination numbers.

        if ( !TryGetSourceCalculationRange(oVisibleSourceRange,
            useSourceNumber1, useSourceNumber2, sourceNumber1, sourceNumber2,
            ignoreOutliers, useLogs, out sourceCalculationNumber1,
            out sourceCalculationNumber2) )
        {
            return (false);
        }

        // Create an object that maps a range of numbers to a range of colors.

        Double dSourceCalculationNumberWithLog1 =
            GetLogIfRequested(sourceCalculationNumber1, useLogs);

        Double dSourceCalculationNumberWithLog2 =
            GetLogIfRequested(sourceCalculationNumber2, useLogs);

        ColorGradientMapper oColorGradientMapper = GetColorGradientMapper(
            dSourceCalculationNumberWithLog1, dSourceCalculationNumberWithLog2,
            destinationColor1, destinationColor2);

        ColorConverter2 oColorConverter2 = new ColorConverter2();

        // Loop through the areas.

        Int32 iAreas = oVisibleSourceRange.Areas.Count;

        Debug.Assert(iAreas == oVisibleDestinationRange.Areas.Count);

        for (Int32 iArea = 1; iArea <= iAreas; iArea++)
        {
            Range oSourceArea = oVisibleSourceRange.Areas[iArea];
            Range oDestinationArea = oVisibleDestinationRange.Areas[iArea];

            Debug.Assert(oSourceArea.Rows.Count ==
                oDestinationArea.Rows.Count);

            Object [,] aoSourceAreaValues =
                ExcelUtil.GetRangeValues(oSourceArea);

            Object [,] aoDestinationAreaValues =
                ExcelUtil.GetRangeValues(oDestinationArea);

            Int32 iRows = oSourceArea.Rows.Count;

            for (Int32 iRow = 1; iRow <= iRows; iRow++)
            {
                // If the source cell doesn't contain a number, skip it.

                Double dSourceNumber;

                if ( !TryGetNumber( aoSourceAreaValues[iRow, 1], useLogs,
                    out dSourceNumber) )
                {
                    continue;
                }

                dSourceNumber = GetLogIfRequested(dSourceNumber, useLogs);

                Color oDestinationColor =
                    oColorGradientMapper.ColorMetricToColor(dSourceNumber);

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

                aoDestinationAreaValues[iRow, 1] =
                    oColorConverter2.GraphToWorkbook(oDestinationColor);
            }

            oDestinationArea.set_Value(Missing.Value, aoDestinationAreaValues);
        }

        decimalPlaces = GetDecimalPlaces(table, sourceColumnName);

        return (true);
    }
    TryMapToColor
    (
        ListObject table,
        String sourceColumnName,
        String destinationColumnName,
        out ICollection<String> categoryNames
    )
    {
        Debug.Assert(table != null);
        Debug.Assert( !String.IsNullOrEmpty(sourceColumnName) );
        Debug.Assert( !String.IsNullOrEmpty(destinationColumnName) );

        categoryNames = null;

        Range oVisibleSourceRange, oVisibleDestinationRange;

        if ( !TryGetVisibleSourceAndDestination(table, sourceColumnName,
            destinationColumnName, out oVisibleSourceRange,
            out oVisibleDestinationRange) )
        {
            return (false);
        }

        Int32 iAreas = oVisibleSourceRange.Areas.Count;
        Int32 iColorIndex = 0;
        ColorConverter2 oColorConverter2 = new ColorConverter2();

        // The key is a unique category name and the value is a color index.

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

        // The first pass through this loop populates the dictionary using the
        // values in the source column.  The second pass fills in the
        // destination column.  This can't be done in one pass because the
        // total number of unique colors is required before the colors can be
        // generated.

        for (Int32 iPassCount = 0; iPassCount < 2; iPassCount++)
        {
            for (Int32 iArea = 1; iArea <= iAreas; iArea++)
            {
                Range oSourceArea = oVisibleSourceRange.Areas[iArea];
                Range oDestinationArea = oVisibleDestinationRange.Areas[iArea];

                Object [,] aoSourceAreaValues =
                    ExcelUtil.GetRangeValues(oSourceArea);

                Object [,] aoDestinationAreaValues =
                    ExcelUtil.GetRangeValues(oDestinationArea);

                Int32 iRows = oSourceArea.Rows.Count;

                for (Int32 iRow = 1; iRow <= iRows; iRow++)
                {
                    String sCategory;

                    if ( ExcelUtil.TryGetNonEmptyStringFromCell(
                        aoSourceAreaValues, iRow, 1, out sCategory) )
                    {
                        if (iPassCount == 0)
                        {
                            if ( !oCategoryDictionary.ContainsKey(sCategory) )
                            {
                                oCategoryDictionary[sCategory] = iColorIndex;
                                iColorIndex++;
                            }
                        }
                        else
                        {
                            Color oUniqueColor = ColorUtil.GetUniqueColor(
                                oCategoryDictionary[sCategory],
                                oCategoryDictionary.Count);

                            aoDestinationAreaValues[iRow, 1] =
                                oColorConverter2.GraphToWorkbook(oUniqueColor);
                        }
                    }
                }

                if (iPassCount == 1)
                {
                    oDestinationArea.set_Value(Missing.Value,
                        aoDestinationAreaValues);
                }
            }
        }

        categoryNames = oCategoryDictionary.Keys;

        return (categoryNames.Count > 0);
    }
Esempio n. 11
0
    SetVertexAttributesForAllGroups
    (
        ListObject oGroupTable
    )
    {
        Debug.Assert(oGroupTable != null);

        Range oVertexColorRange, oVertexShapeRange;
        Object [,] aoVertexColorValues, aoVertexShapeValues;

        if (
            !ExcelTableUtil.TryGetTableColumnDataAndValues(oGroupTable,
                GroupTableColumnNames.VertexShape, out oVertexShapeRange,
                out aoVertexShapeValues)
            ||
            !ExcelTableUtil.TryGetTableColumnDataAndValues(oGroupTable,
                GroupTableColumnNames.VertexColor, out oVertexColorRange,
                out aoVertexColorValues)
            )
        {
            return;
        }

        Int32 iGroups = aoVertexShapeValues.GetUpperBound(0);
        Debug.Assert(aoVertexColorValues.GetUpperBound(0) == iGroups);

        ColorConverter2 oColorConverter2 = new ColorConverter2();

        VertexShapeConverter oVertexShapeConverter =
            new VertexShapeConverter();

        for (Int32 i = 0; i < iGroups; i++)
        {
            Color oColor;
            VertexShape eShape;

            GetVertexAttributes(i, iGroups, out oColor, out eShape);

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

            aoVertexColorValues[i + 1, 1] =
                oColorConverter2.GraphToWorkbook(oColor);

            aoVertexShapeValues[i + 1, 1] =
                oVertexShapeConverter.GraphToWorkbook(eShape);
        }

        oVertexColorRange.set_Value(Missing.Value, aoVertexColorValues);
        oVertexShapeRange.set_Value(Missing.Value, aoVertexShapeValues);
    }
Esempio n. 12
0
    ThisWorkbook_AttributesEditedInGraph
    (
        Object sender,
        AttributesEditedEventArgs e
    )
    {
        Debug.Assert(e != null);
        AssertValid();

        // The key is the row ID stored in the table's ID column and the value
        // is the one-based row number relative to the worksheet.

        Dictionary<Int32, Int32> oRowIDDictionary;

        if (
            e.EditedVertexAttributes == null
            ||
            !m_oSheets1And2Helper.TableExists
            ||
            !m_oSheets1And2Helper.TryGetAllRowIDs(out oRowIDDictionary)
            )
        {
            return;
        }

        Microsoft.Office.Interop.Excel.ListObject oVertexTable =
            Vertices.InnerObject;

        Globals.ThisWorkbook.ShowWaitCursor = true;

        // Get the columns that might need to be updated.  These columns are
        // not required.

        Microsoft.Office.Interop.Excel.Range oColorColumnData,
            oShapeColumnData, oRadiusColumnData, oAlphaColumnData,
            oVisibilityColumnData, oLabelColumnData, oLabelFillColorColumnData,
            oLabelPositionColumnData, oToolTipColumnData, oLockedColumnData,
            oMarkedColumnData;

        Object [,] aoColorValues = null;
        Object [,] aoShapeValues = null;
        Object [,] aoRadiusValues = null;
        Object [,] aoAlphaValues = null;
        Object [,] aoVisibilityValues = null;
        Object [,] aoLabelValues = null;
        Object [,] aoLabelFillColorValues = null;
        Object [,] aoLabelPositionValues = null;
        Object [,] aoToolTipValues = null;
        Object [,] aoLockedValues = null;
        Object [,] aoMarkedValues = null;

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            VertexTableColumnNames.Color, out oColorColumnData,
            out aoColorValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            VertexTableColumnNames.Shape, out oShapeColumnData,
            out aoShapeValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            VertexTableColumnNames.Radius, out oRadiusColumnData,
            out aoRadiusValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            CommonTableColumnNames.Alpha, out oAlphaColumnData,
            out aoAlphaValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            CommonTableColumnNames.Visibility, out oVisibilityColumnData,
            out aoVisibilityValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            VertexTableColumnNames.Label, out oLabelColumnData,
            out aoLabelValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            VertexTableColumnNames.LabelFillColor,
            out oLabelFillColorColumnData, out aoLabelFillColorValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            VertexTableColumnNames.LabelPosition, out oLabelPositionColumnData,
            out aoLabelPositionValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            VertexTableColumnNames.ToolTip, out oToolTipColumnData,
            out aoToolTipValues);

        ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
            VertexTableColumnNames.Locked, out oLockedColumnData,
            out aoLockedValues);

        if ( TryGetMarkedColumnData(out oMarkedColumnData) )
        {
            aoMarkedValues = ExcelUtil.GetRangeValues(oMarkedColumnData);
        }

        ColorConverter2 oColorConverter2 = new ColorConverter2();

        VertexShapeConverter oVertexShapeConverter =
            new VertexShapeConverter();

        VertexVisibilityConverter oVertexVisibilityConverter =
            new VertexVisibilityConverter();

        VertexLabelPositionConverter oVertexLabelPositionConverter =
            new VertexLabelPositionConverter();

        BooleanConverter oBooleanConverter = new BooleanConverter();

        // Loop through the IDs of the vertices whose attributes were edited
        // in the graph.

        EditedVertexAttributes oEditedVertexAttributes =
            e.EditedVertexAttributes;

        foreach (Int32 iID in e.VertexIDs)
        {
            // Look for the row that contains the ID.

            Int32 iRowOneBased;

            if ( !oRowIDDictionary.TryGetValue(iID, out iRowOneBased) )
            {
                continue;
            }

            iRowOneBased -= oVertexTable.Range.Row;

            if (oEditedVertexAttributes.Color.HasValue &&
                aoColorValues != null)
            {
                aoColorValues[iRowOneBased, 1] =
                    oColorConverter2.GraphToWorkbook(
                        oEditedVertexAttributes.Color.Value);
            }

            if (oEditedVertexAttributes.Shape.HasValue &&
                aoShapeValues != null)
            {
                aoShapeValues[iRowOneBased, 1] =
                    oVertexShapeConverter.GraphToWorkbook(
                        oEditedVertexAttributes.Shape.Value);
            }

            if (oEditedVertexAttributes.Radius.HasValue &&
                aoRadiusValues != null)
            {
                aoRadiusValues[iRowOneBased, 1] =
                    oEditedVertexAttributes.Radius.Value.ToString();
            }

            if (oEditedVertexAttributes.Alpha.HasValue &&
                aoAlphaValues != null)
            {
                aoAlphaValues[iRowOneBased, 1] =
                    oEditedVertexAttributes.Alpha.Value.ToString();
            }

            if (oEditedVertexAttributes.Visibility.HasValue &&
                aoVisibilityValues != null)
            {
                aoVisibilityValues[iRowOneBased, 1] =
                    oVertexVisibilityConverter.GraphToWorkbook(
                        oEditedVertexAttributes.Visibility.Value);
            }

            if ( !String.IsNullOrEmpty(oEditedVertexAttributes.Label) )
            {
                aoLabelValues[iRowOneBased, 1] = oEditedVertexAttributes.Label;
            }

            if (oEditedVertexAttributes.LabelFillColor.HasValue &&
                aoLabelFillColorValues != null)
            {
                aoLabelFillColorValues[iRowOneBased, 1] =
                    oColorConverter2.GraphToWorkbook(
                        oEditedVertexAttributes.LabelFillColor.Value);
            }

            if (oEditedVertexAttributes.LabelPosition.HasValue &&
                aoLabelPositionValues != null)
            {
                aoLabelPositionValues[iRowOneBased, 1] =
                    oVertexLabelPositionConverter.GraphToWorkbook(
                        oEditedVertexAttributes.LabelPosition.Value);
            }

            if ( !String.IsNullOrEmpty(oEditedVertexAttributes.ToolTip) )
            {
                aoToolTipValues[iRowOneBased, 1] =
                    oEditedVertexAttributes.ToolTip;
            }

            if (oEditedVertexAttributes.Locked.HasValue &&
                aoLockedValues != null)
            {
                aoLockedValues[iRowOneBased, 1] =
                    oBooleanConverter.GraphToWorkbook(
                        oEditedVertexAttributes.Locked.Value);
            }

            if (oEditedVertexAttributes.Marked.HasValue &&
                aoMarkedValues != null)
            {
                aoMarkedValues[iRowOneBased, 1] =
                    oBooleanConverter.GraphToWorkbook(
                        oEditedVertexAttributes.Marked.Value);
            }
        }

        // Activate this worksheet first, because writing to an inactive
        // worksheet causes problems with the selection in Excel.

        ExcelActiveWorksheetRestorer oExcelActiveWorksheetRestorer =
            m_oSheets1And2Helper.GetExcelActiveWorksheetRestorer();

        ExcelActiveWorksheetState oExcelActiveWorksheetState =
            oExcelActiveWorksheetRestorer.ActivateWorksheet(this.InnerObject);

        try
        {
            m_oSheets1And2Helper.SetColumnDataValues(
                oColorColumnData, aoColorValues,
                oShapeColumnData, aoShapeValues,
                oRadiusColumnData, aoRadiusValues,
                oAlphaColumnData, aoAlphaValues,
                oVisibilityColumnData, aoVisibilityValues,
                oLabelColumnData, aoLabelValues,
                oLabelFillColorColumnData, aoLabelFillColorValues,
                oLabelPositionColumnData, aoLabelPositionValues,
                oToolTipColumnData, aoToolTipValues,
                oLockedColumnData, aoLockedValues,
                oMarkedColumnData, aoMarkedValues
                );
        }
        finally
        {
            oExcelActiveWorksheetRestorer.Restore(oExcelActiveWorksheetState);
        }

        Globals.ThisWorkbook.ShowWaitCursor = false;
    }
        Convert <TGroup>
        (
            ICollection <TGroup> groups,
            GroupToGroupVertices <TGroup> groupToGroupVertices,
            GroupToString <TGroup> groupToGroupName,
            Boolean collapseGroups,
            GroupToString <TGroup> groupToCollapsedGroupAttributes
        )
        {
            Debug.Assert(groups != null);
            Debug.Assert(groupToGroupVertices != null);

            // These columns are needed:
            //
            // * Group name on the group worksheet.
            //
            // * Vertex color on the group worksheet.
            //
            // * Vertex shape on the group worksheet.
            //
            // * Group name on the group-vertex worksheet.
            //
            // * Vertex name on the group-vertex worksheet.
            //
            // * Vertex ID on the group-vertex worksheet.  This gets copied from
            //   the hidden ID column on the Vertex worksheet via an Excel
            //   formula.

            // These columns might be needed:
            //
            // * Collapsed flag on the group worksheet.
            //
            // * Collapsed group attributes on the group worksheet.


            List <GraphMetricValueOrdered> oGroupNamesForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oVertexColorsForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oVertexShapesForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oCollapsedFlagsForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oCollapsedAttributesForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oGroupNamesForGroupVertexWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oVertexNamesForGroupVertexWorksheet =
                new List <GraphMetricValueOrdered>();

            // This column contains a constant value, which is a formula.

            GraphMetricValueOrdered [] aoVertexIDsForGroupVertexWorksheet =
            {
                new GraphMetricValueOrdered(
                    GroupManager.GetExcelFormulaForVertexID())
            };

            Int32 iGroups = groups.Count;
            Int32 iGroup  = 0;

            ColorConverter2 oColorConverter2 = new ColorConverter2();

            VertexShapeConverter oVertexShapeConverter =
                new VertexShapeConverter();

            GraphMetricValueOrdered oTrueGraphMetricValueOrdered =
                new GraphMetricValueOrdered(
                    (new BooleanConverter()).GraphToWorkbook(true));

            foreach (TGroup oGroup in
                     from oGroup in groups
                     orderby groupToGroupVertices(oGroup).Count descending
                     select oGroup)
            {
                String sGroupName;

                if (groupToGroupName == null)
                {
                    sGroupName = 'G' + (iGroup + 1).ToString(
                        CultureInfo.InvariantCulture);
                }
                else
                {
                    sGroupName = groupToGroupName(oGroup);
                }

                Color       oColor;
                VertexShape eShape;

                GroupManager.GetVertexAttributes(iGroup, iGroups, out oColor,
                                                 out eShape);

                GraphMetricValueOrdered oGroupNameGraphMetricValue =
                    new GraphMetricValueOrdered(sGroupName);

                oGroupNamesForGroupWorksheet.Add(oGroupNameGraphMetricValue);

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

                oVertexColorsForGroupWorksheet.Add(
                    new GraphMetricValueOrdered(
                        oColorConverter2.GraphToWorkbook(oColor)));

                oVertexShapesForGroupWorksheet.Add(
                    new GraphMetricValueOrdered(
                        oVertexShapeConverter.GraphToWorkbook(eShape)));

                if (collapseGroups)
                {
                    oCollapsedFlagsForGroupWorksheet.Add(
                        oTrueGraphMetricValueOrdered);
                }

                if (groupToCollapsedGroupAttributes != null)
                {
                    oCollapsedAttributesForGroupWorksheet.Add(
                        new GraphMetricValueOrdered(
                            groupToCollapsedGroupAttributes(oGroup)));
                }

                Int32 iVertices = 0;

                foreach (IVertex oVertex in groupToGroupVertices(oGroup))
                {
                    oGroupNamesForGroupVertexWorksheet.Add(
                        oGroupNameGraphMetricValue);

                    oVertexNamesForGroupVertexWorksheet.Add(
                        new GraphMetricValueOrdered(
                            ExcelTextUtil.ForceCellText(oVertex.Name)));

                    iVertices++;
                }

                iGroup++;
            }

            List <GraphMetricColumn> oGraphMetricColumns =
                new List <GraphMetricColumn>();

            oGraphMetricColumns.AddRange(

                new GraphMetricColumn [] {
                new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                             TableNames.Groups,
                                             GroupTableColumnNames.Name,
                                             ExcelTableUtil.AutoColumnWidth, null,
                                             CellStyleNames.Required,
                                             oGroupNamesForGroupWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                             TableNames.Groups,
                                             GroupTableColumnNames.VertexColor,
                                             ExcelTableUtil.AutoColumnWidth, null,
                                             CellStyleNames.VisualProperty,
                                             oVertexColorsForGroupWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                             TableNames.Groups,
                                             GroupTableColumnNames.VertexShape,
                                             ExcelTableUtil.AutoColumnWidth, null,
                                             CellStyleNames.VisualProperty,
                                             oVertexShapesForGroupWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.GroupVertices,
                                             TableNames.GroupVertices,
                                             GroupVertexTableColumnNames.GroupName,
                                             ExcelTableUtil.AutoColumnWidth, null, null,
                                             oGroupNamesForGroupVertexWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.GroupVertices,
                                             TableNames.GroupVertices,
                                             GroupVertexTableColumnNames.VertexName,
                                             ExcelTableUtil.AutoColumnWidth, null, null,
                                             oVertexNamesForGroupVertexWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.GroupVertices,
                                             TableNames.GroupVertices,
                                             GroupVertexTableColumnNames.VertexID,
                                             ExcelTableUtil.AutoColumnWidth, null, null,
                                             aoVertexIDsForGroupVertexWorksheet
                                             ),
            });

            if (collapseGroups)
            {
                oGraphMetricColumns.Add(
                    new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                                 TableNames.Groups,
                                                 GroupTableColumnNames.Collapsed,
                                                 ExcelTableUtil.AutoColumnWidth, null,
                                                 CellStyleNames.VisualProperty,
                                                 oCollapsedFlagsForGroupWorksheet.ToArray()
                                                 ));
            }

            if (groupToCollapsedGroupAttributes != null)
            {
                oGraphMetricColumns.Add(
                    new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                                 TableNames.Groups,
                                                 GroupTableColumnNames.CollapsedAttributes,
                                                 ExcelTableUtil.AutoColumnWidth, null,
                                                 CellStyleNames.DoNotEdit,
                                                 oCollapsedAttributesForGroupWorksheet.ToArray()
                                                 ));
            }

            return(oGraphMetricColumns.ToArray());
        }
    TryGetColor
    (
        ExcelTableReader.ExcelTableRow oRow,
        String sColumnName,
        ColorConverter2 oColorConverter2,
        out Color oColor
    )
    {
        Debug.Assert(oRow != null);
        Debug.Assert( !String.IsNullOrEmpty(sColumnName) );
        Debug.Assert(oColorConverter2 != null);
        AssertValid();

        oColor = Color.Empty;

        String sColor;

        if ( !oRow.TryGetNonEmptyStringFromCell(sColumnName, out sColor) )
        {
            return (false);
        }

        if ( !oColorConverter2.TryWorkbookToGraph(sColor, out oColor) )
        {
            Range oInvalidCell = oRow.GetRangeForCell(sColumnName);

            OnWorkbookFormatError( String.Format(

                "The cell {0} contains an unrecognized color.  Right-click the"
                + " cell and select Select Color on the right-click menu."
                ,
                ExcelUtil.GetRangeAddress(oInvalidCell)
                ),

                oInvalidCell
            );
        }

        return (true);
    }
    ReadColor
    (
        ExcelTableReader.ExcelTableRow oRow,
        String sColumnName,
        IMetadataProvider oEdgeOrVertex,
        String sColorKey,
        ColorConverter2 oColorConverter2
    )
    {
        Debug.Assert(oRow != null);
        Debug.Assert( !String.IsNullOrEmpty(sColumnName) );
        Debug.Assert(oEdgeOrVertex != null);
        Debug.Assert( !String.IsNullOrEmpty(sColorKey) );
        Debug.Assert(oColorConverter2 != null);
        AssertValid();

        Color oColor;

        if ( TryGetColor(oRow, sColumnName, oColorConverter2, out oColor) )
        {
            oEdgeOrVertex.SetValue(sColorKey, oColor);
        }
    }