High performance Swift treemap layout engine for iOS and macOS.

Related tags

UI YMTreeMap
Overview

Dow 30

Synopsis

YMTreeMap is a high performance treemap layout engine for iOS and macOS, written in Swift.

The input to YMTreeMap is a list of arbitrary numbers, and the output will be a list of layout rectangles representing those numbers graphically. The order of the layout rectangles will match the original input order, so you should sort the input numbers the way that you want your treemap to be ordered. Finally, the size of the layout rectangles will represent the relative weighting of the ordinal input number that it cooresponds to.

YMTreeMap uses the "squarified" layout treemap algorithm. Squarified optimizes for low aspect ratios: meaning it generates rectangles that are as square as possible given a reasonable effort. While not perfectly optimal, the algorithm get pretty close while maintaining high-performance.

The output rectangles can easily be used to render the shapes using whatever rendering system you prefer:

  • Using CoreGraphics (in drawRect, etc)
  • Using OpenGL
  • Using a custom UICollectionView layout

Usage

Basic sample of how to draw a small heat map using random colors in Swift and Objective-C.

Swift

var randomColor: UIColor {
  return UIColor(red: CGFloat(arc4random_uniform(255) % 255) / 255.0,
                 green: CGFloat(arc4random_uniform(255) % 255) / 255.0,
                 blue: CGFloat(arc4random_uniform(255) % 255) / 255.0,
                 alpha: 1)
}

override func draw(_ rect: CGRect) {
  let values = [ 445, 203, 110, 105, 95, 65, 33, 21, 10 ].sorted()

  // These two lines are actual YMTreeMap usage!
  let treeMap = YMTreeMap(withValues: values)
  let treeMapRects = treeMap.tessellate(inRect: self.bounds)

  let context = UIGraphicsGetCurrentContext()

  treeMapRects.forEach { (treeMapRect) in
    randomColor.setFill()
    context?.fill(treeMapRect)
  }
}

Objective-C

#define RANDOM_COLOR [UIColor colorWithRed:(rand() % 255)/255.0 green:(rand() % 255)/255.0 blue:(rand() % 255)/255.0 alpha:1.0]

- (void)drawRect:(CGRect)rect
{
    NSArray<NSNumber *> *values = @[ @445, @203, @110, @105, @95, @65, @33, @21, @10 ];

    // These two lines are actual YMTreeMap usage!
    YMTreeMap *tm = [[YMTreeMap alloc] initWithValues:values];
    NSArray<NSValue *> *treeMapRects = [tm tessellateInRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();

    for (NSValue *rectVal in treeMapRects) {
        [RANDOM_COLOR setFill];
        CGContextFillRect(context, rectVal.CGRectValue);
    }
}

Examples

Here are examples of 30-item and 1000-item sorted treemaps in a verical-orientation.

30 items 1,000 items

Performance

Tests performed on-device after 3 seconds of idle time, in release configuration. All tests use the same input values, which were sorted lists of large integers.

Even with heat maps with up to 1,000 items, layout time is well controlled at 3.7ms. This should enable use within a scrolling table view.

Pure Swift

iPhone 7 Plus

  • [30 items ] Performed 1000 iterations in 114.331625ms; 8.746486/ms; 0.114332ms each
  • [1000 items] Performed 1000 iterations in 3743.449875ms; 0.267133/ms; 3.743450ms each

Objective-C bridging into Swift

iPhone 7 Plus

  • [30 items ] Performed 1000 iterations in 132.439167ms; 7.550636/ms; 0.132439ms each
  • [1000 items] Performed 1000 iterations in 4493.055292ms; 0.222566/ms; 4.493055ms each

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

YMTreeMap is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'YMTreeMap'

Further Reading

Various papers on treemapping layout algorithms and usability that helped influence this project. A copy of each paper is in the Research folder.

Squarified Treemaps - Mark Bruls, Kees Huizing, Jarke J. van Wijk

Ordered Treemap Layouts - Ben Shneiderman, Martin Wattenberg

Ordered and Quantum Treemaps: Making Effective Use of 2D Space to Display Hierarchies - Benjamin B. Bederson, Ben Shneiderman, Martin Wattenberg

Treemaps for space-constrained visualization of hierarchies - Ben Shneiderman

Treemapping on Wikipedia

Animated Exploration of Dynamic Graphs with Radial Layout - Ka-Ping Yee, Danyel Fisher, Rachna Dhamija, Marti Hearst

Author

Adam Kaplan, [email protected]

License

YMTreeMap is available under the MIT license. See the LICENSE file for more info.

You might also like...
Circular progress indicator for your macOS app
Circular progress indicator for your macOS app

CircularProgress Circular progress indicator for your macOS app This package is used in production by apps like Gifski and HEIC Converter. Requirement

Programmatic UI for macOS
Programmatic UI for macOS

Description: Programmatic UI Framework for macOS. Swift handles app logic, CSS/SVG handles design and JSON handles struture. Installation: Step 1: Add

⚙ Add a preferences window to your macOS app in minutes
⚙ Add a preferences window to your macOS app in minutes

Preferences Add a preferences window to your macOS app in minutes Just pass in some view controllers and this package will take care of the rest. Requ

macOS GUI Library for the Nim Programming Language
macOS GUI Library for the Nim Programming Language

NimCocoa NimCocoa is an experimental implementation of a Native GUI for the Nim programming language running on macOS. Rather than rely on low level c

A spotlight-inspired quick action bar for macOS.
A spotlight-inspired quick action bar for macOS.

DSFQuickActionBar A spotlight-inspired quick action bar for macOS. Why? I've seen this in other mac applications (particularly spotlight) and it's ver

Dump local MacOS user hashes to a hashcat-compatible string.

mimic Dumps local MacOS user hashes to a hashcat-compatible string. Simply uses Objective-C instead of dscl/defaults read or other noisy binaries, to

Fetch the star wars api from all the planets and list and show details using Swift UI and Combine

Star Wars Planets Fetch the star wars planet data by using stat war api, list and show details using SwiftUI and Combine frameworks 🔖 Swift UI Framew

A custom stretchable header view for UIScrollView or any its subclasses with UIActivityIndicatorView and iPhone X safe area support for content reloading. Built for iOS 10 and later.

Arale A custom stretchable header view for UIScrollView or any its subclasses with UIActivityIndicatorView support for reloading your content. Built f

🏞 A simple iOS photo and video browser with optional grid view, captions and selections written in Swift5.0
🏞 A simple iOS photo and video browser with optional grid view, captions and selections written in Swift5.0

Introduction 🏞 MediaBrowser can display one or more images or videos by providing either UIImage objects, PHAsset objects, or URLs to library assets,

Comments
  • Blog post question

    Blog post question

    Where can I find source code for this tutorial? https://yahooeng.tumblr.com/post/141143817861/using-nsurlprotocol-for-testing

    It seems mentioned github link is not valid anymore.

    opened by EvgenyKarkan 4
  • Pod missing from Cocoapods

    Pod missing from Cocoapods

    The podspec for YMTreeMap is missing from the Cocoapods repo and from the cocoapods.org pod reference; thus, simple install via Cocoapods doesn't work.

    opened by kauffmanelia 2
  • Version 1.1.0

    Version 1.1.0

    • Expose public init(withValues values: [Double]) to objc - this became non-visible to objc when I was updating the Finance app to swift 4, due to changes in objc inference.
    • Update version to swift 4
    • Pod install using cocoapods 1.5.0
    • Fix warnings
    opened by blanksblanks 0
Releases(1.1.0)
Owner
Yahoo
Yahoo is a Verizon Media brand. This organization is the home to many of the active open source projects published by engineers at Yahoo and Verizon Media.
Yahoo
Safari Web Extension to customize your search engine.

Safari Web Extension to customize your search engine. Search queries made from the Safari address bar are appended to the custom search engine URL. No

Marvin Häuser 3 Nov 16, 2022
A fancy hexagonal layout for displaying data like your Apple Watch

Hexacon is a new way to display content in your app like the Apple Watch SpringBoard Highly inspired by the work of lmmenge. Special thanks to zenly f

Gautier Gédoux 340 Dec 4, 2022
Windless makes it easy to implement invisible layout loading view.

Windless Windless makes it easy to implement invisible layout loading view. Contents Requirements Installation Usage Looks Credits Communication Licen

ArLupin 940 Dec 22, 2022
An alternative to UIStackView for common Auto Layout patterns.

StackLayout StackLayout builds on Auto Layout to make some of the most common layouts easier to manage. It creates the constraints that you need and a

Bridger Maxwell 76 Jun 29, 2022
UIPheonix is a super easy, flexible, dynamic and highly scalable UI framework + concept for building reusable component/control-driven apps for macOS, iOS and tvOS

UIPheonix is a super easy, flexible, dynamic and highly scalable UI framework + concept for building reusable component/control-driven apps for macOS, iOS and tvOS

Mohsan Khan 29 Sep 9, 2022
Super awesome Swift minion for Core Data (iOS, macOS, tvOS)

⚠️ Since this repository is going to be archived soon, I suggest migrating to NSPersistentContainer instead (available since iOS 10). For other conven

Marko Tadić 306 Sep 23, 2022
A window arrangement manager for macOS like BetterSnapTool and Magnet

A window arrangement manager for macOS like BetterSnapTool and Magnet. You can split the foremost window to the left half of the screen, the left two-thirds, etc.

Takuto NAKAMURA (Kyome) 65 Dec 9, 2022
SheetPresentation for SwiftUI. Multiple devices support: iOS, watchOS, tvOS, macOS, macCatalyst.

SheetPresentation for SwiftUI. Multiple devices support: iOS, watchOS, tvOS, macOS, macCatalyst.

Aben 13 Nov 17, 2021
Demonstration of LegoArtFilter for iOS/macOS

LegoArtFilterDemo Demonstration of LegoArtFilter for iOS/macOS. This project runs on both iOS (14≤) and macOS (11≤). Libraries LegoColors LegoArtFilte

Takuto NAKAMURA (Kyome) 1 Oct 16, 2021
Create macOS apps with Swift packages instead of Xcode projects

Swift Bundler A Swift Package Manager wrapper that allows the creation of MacOS apps with Swift packages instead of Xcode projects. My motivation is t

null 182 Dec 25, 2022