// Comparator class for countries int IComparer.Compare(object x, object y) { DictionaryEntry e1 = (DictionaryEntry)x; DictionaryEntry e2 = (DictionaryEntry)y; StyleTreeMapInfo st1 = (StyleTreeMapInfo)e1.Value; StyleTreeMapInfo st2 = (StyleTreeMapInfo)e2.Value; // descendent order return(st2.iReleaseCount.CompareTo(st1.iReleaseCount)); }
/// <summary> /// Creates the TreeMap for the countries grouped per Style /// </summary> /// <param name="aRectangle"></param> /// <param name="db"></param> public object[, ,] BuildCountriesAreasTree(out int aQuantitativeDataIndex, out int aOrdinalDataIndex, out int aIdIndex, out int aLeafNodeLabelIndex, List <GMSToolTipComponent> toolTipComponents, out object[,,] aColorMapInput) { List <List <object> > dataRows = new List <List <object> >(); // Sort the countries so we can have only the N most popular ones ArrayList sortedCountries = new ArrayList(iDb.countries.Values); sortedCountries.Sort(new CountryComparer(CountryComparer.CRITERION.RELEASES)); Hashtable styles = new Hashtable(); int idCounter = 0; foreach (Country country in sortedCountries) { foreach (StyleTreeMapInfo styleInfo in styles.Values) { styleInfo.iReleaseCount = 0; } // iterate the releases and increment the counter for each style foreach (MusicBrainzRelease release in country.releases) { string styleName = release.freeDBAlbum.style.name; if (styles.ContainsKey(styleName)) { StyleTreeMapInfo info = (StyleTreeMapInfo)styles[styleName]; info.iReleaseCount++; } else { StyleTreeMapInfo info = new StyleTreeMapInfo(); info.iReleaseCount = 1; info.id = idCounter++; styles.Add(styleName, info); } } // Sort the styles in a decreasing way ArrayList sortedStyles = new ArrayList(styles); sortedStyles.Sort(new StyleTreeMapInfoComparer()); int styleLimiter = 30; foreach (DictionaryEntry entry in sortedStyles) { StyleTreeMapInfo styleInfo = (StyleTreeMapInfo)entry.Value; int releasesCount = styleInfo.iReleaseCount; if (releasesCount == 0) { continue; } if (styleLimiter == 0) { break; } string styleName = (string)entry.Key; string countryName = country.name; List <object> countryRow = new List <object>(); countryRow.Add(styleName); // Col 0: Leaf Labels countryRow.Add(countryName); // Col 1: Group Labels countryRow.Add(releasesCount); // Col 2: Area values countryRow.Add(styleInfo.id); // Col 3: Id dataRows.Add(countryRow); styleLimiter--; } } // Removing the id's gaps by creating increasing ids Hashtable idHash = new Hashtable(); int idCount = 0; foreach (List <object> row in dataRows) { int id = (int)row[3]; if (!idHash.ContainsKey(id)) { idHash.Add(id, idCount++); } } // the column order aQuantitativeDataIndex = 2; // Area values aOrdinalDataIndex = 1; // Group Labels aIdIndex = 3; // Id aLeafNodeLabelIndex = 0; // Leaf Label // ToolTip Components toolTipComponents.Add(new GMSToolTipComponent("Country", aOrdinalDataIndex)); toolTipComponents.Add(new GMSToolTipComponent("Music Style", aLeafNodeLabelIndex)); toolTipComponents.Add(new GMSToolTipComponent("Nr. Albums", aQuantitativeDataIndex)); // Create and Fill the DataCube object[, ,] dataCube = new object[4, dataRows.Count, 1]; aColorMapInput = new object[1, idHash.Count, 1]; // Fill the color map input int j = 0; foreach (int id in idHash.Values) { aColorMapInput[0, j++, 0] = id; } int i = 0; foreach (List <object> row in dataRows) { j = 0; foreach (object attribute in row) { if (j == 3) { dataCube[j++, i, 0] = idHash[attribute]; } else { dataCube[j++, i, 0] = attribute; } } ++i; } return(dataCube); }