Skip to content

Commit

Permalink
Ported nearly all classes to AS3
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Aug 12, 2011
1 parent 2665be5 commit b3a8a0a
Show file tree
Hide file tree
Showing 23 changed files with 1,020 additions and 6 deletions.
6 changes: 0 additions & 6 deletions README

This file was deleted.

94 changes: 94 additions & 0 deletions lib/math/Random.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

/**
*
* Random
*
* @version 1.00 | Feb 7, 2010
* @author Justin Windle
*
**/

package math
{

/**
* Random
*/
public class Random
{

// ----------------------------------------------------------------
// CONSTANTS
// ----------------------------------------------------------------

private static var _num : Number = 1.0;
private static var _seed : Number = 1.0;

// ----------------------------------------------------------------
// METHODS
// ----------------------------------------------------------------

public static function randomSeed() : void
{
seed = Math.random() * 2147483647;
trace("seed: " + _seed);
}

public static function next() : Number
{
_num = ( _num * 16807 ) % 2147483647;
return _num * 4.656612875245797e-10;
}

public static function float( min : Number, max : Number = NaN ) : Number
{
if ( isNaN(max) )
{
max = min;
min = 0;
}

return next() * ( max - min ) + min;
}

public static function integer( min : Number, max : Number = NaN ) : int
{
if ( isNaN(max) )
{
max = min;
min = 0;
}

return int( (next() * ( max - min ) + min) + 0.5 );
}

public static function bool(chance : Number = 0.5) : Boolean
{
return Random.next() < chance;
}

public static function sign(change : Number = 0.5) : int
{
return next() < change ? 1 : -1;
}

public static function obj(list : Array) : *
{
return list[ int(next() * list.length) ];
}

// ----------------------------------------------------------------
// ACCESSORS
// ----------------------------------------------------------------

public static function get seed() : Number
{
return _seed;
}

public static function set seed(value : Number) : void
{
_seed = _num = value;
}
}
}
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
as3streamgraph
==============

Straight forward fork of the streamgraph generator by Lee Byron and Martin Wattenberg to ActionScript3. It allows to render streamgraphs in Flash, Flex and AIR applications.

Forked by Gregor Aisch.


Original readme
---------------

This is the processing application used to generate the images in the paper:
Stacked Graphs - Geometry & Aesthetics

It is published here as an educational library, to provide code examples to support the paper.

This code is copyright under the BSD license.
36 changes: 36 additions & 0 deletions src/net/vis4/streamgraph/BasicLateOnsetSort.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.vis4.streamgraph
{
import net.vis4.streamgraph.Layer;

/**
* BasicLateOnsetSort
* Sorts by onset, but does not partition to the outsides of the graph in
* order to illustrate short-sighted errors found during design process.
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public class BasicLateOnsetSort extends LayerSort
{

override public function getName():String
{
return "Late Onset Sorting, Top to Bottom";
}

override public function sort(layers:Vector.<Layer>):Vector.<Layer>
{
// first sort by onset
layers.sort(onsetComparator);

return layers;
}

private function onsetComparator(a:Layer, b:Layer):int
{
return a.onset < b.onset ? -1 : a.onset == b.onset ? 0 : 1;
}

}

}
60 changes: 60 additions & 0 deletions src/net/vis4/streamgraph/BelievableDataSource.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.vis4.streamgraph
{
import math.Random;

/**
* BelievableDataSource
* Create test data for layout engine.
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public class BelievableDataSource implements DataSource
{

public function BelievableDataSource(seed:int = 2) {
// seeded, so we can reproduce results
Random.seed = seed;
}

public function make(numLayers:int, sizeArrayLength:int):Vector.<Layer>
{
var layers:Vector.<Layer> = new Vector.<Layer>(numLayers);

for (var i:int = 0; i < numLayers; i++) {
var name:String = "Layer #" + i;
var size:Vector.<Number> = new Vector.<Number>(sizeArrayLength);
size = makeRandomArray(sizeArrayLength);
layers[i] = new Layer(name, size);
}

return layers;
}

protected function makeRandomArray(n:int):Vector.<Number>
{
var x:Vector.<Number> = new Vector.<Number>(n);

// add a handful of random bumps
for (var i:int=0; i<5; i++) {
addRandomBump(x);
}

return x;
}

protected function addRandomBump(x:Vector.<Number>):void
{
var height:Number = 1 / Random.next(),
cx:Number = 2 * Random.next() - 0.5,
r:Number = Random.next() / 10;

for (var i:int = 0; i < x.length; i++) {
var a:Number = (i / x.length - cx) / r;
x[i] = height * Math.exp( -a * a);
}
}

}

}
23 changes: 23 additions & 0 deletions src/net/vis4/streamgraph/COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2008, Lee Byron, Martin Wattenberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The names of the contributors may NOT be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL LEE BYRON BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 changes: 18 additions & 0 deletions src/net/vis4/streamgraph/ColorPicker.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.vis4.streamgraph
{

/**
* ColorPicker
* Interface for new coloring algorithms.
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public interface ColorPicker
{
function colorize(layers:Vector.<Layer>):void;

function getName():String;
}

}
17 changes: 17 additions & 0 deletions src/net/vis4/streamgraph/DataSource.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.vis4.streamgraph
{
/**
* DataSource
* Interface for creating a data source
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public interface DataSource
{

function make(numLayers:int, sizeArrayLength:int):Vector.<Layer>;

}

}
29 changes: 29 additions & 0 deletions src/net/vis4/streamgraph/InverseVolatilitySort.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.vis4.streamgraph
{
import net.vis4.streamgraph.Layer;

/**
* InverseVolatilitySort
* Sorts an array of layers by their volatility, placing the most volatile
* layers along the insides of the graph, illustrating how disruptive this
* volatility can be to a stacked graph.
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public class InverseVolatilitySort extends VolatilitySort
{

override public function getName():String
{
return "Inverse Volatility Sorting, Evenly Weighted";
}

override protected function volatilityComparator(a:Layer, b:Layer):int
{
return super.volatilityComparator(a, b) * -1;
}

}

}
79 changes: 79 additions & 0 deletions src/net/vis4/streamgraph/LastFMColorPicker.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.vis4.streamgraph
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLRequest;
import net.vis4.streamgraph.Layer;
/**
* ...
* @author gka
*/
public class LastFMColorPicker extends EventDispatcher implements ColorPicker
{

public var source:Bitmap;

protected var _callback:Function; // will be called after image has loaded.

public function LastFMColorPicker(src:String, callback:Function)
{
_callback = callback;

var ldr:Loader = new Loader();

ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onSourceLoaded);

ldr.load(new URLRequest(src));

}

protected function onSourceLoaded(e:Event):void
{
source = Bitmap(LoaderInfo(e.target).content);
_callback();
}

public function getName():String
{
return "Listening History Color Scheme";
}


public function colorize(layers:Vector.<Layer>):void
{
// find the largest layer to use as a normalizer
var maxSum:Number = 0;
for (var i:int = 0; i < layers.length; i++) {
maxSum = Math.max(maxSum, layers[i].sum);
}

// find the color for each layer
for (i = 0; i < layers.length; i++) {
var normalizedOnset:Number = layers[i].onset / layers[i].size.length,
normalizedSum:Number = layers[i].sum / maxSum,
shapedSum:Number = 1.0 - Math.sqrt(normalizedSum);

layers[i].rgb = get_(normalizedOnset, shapedSum);
}
}

protected function get_(g1:Number, g2:Number):uint
{
// get pixel coordinate based on provided parameters
var x:int = Math.floor(g1 * source.bitmapData.width),
y:int = Math.floor(g2 * source.bitmapData.height);

// ensure that the pixel is within bounds.
x = Math.min(source.bitmapData.width-1, Math.max(0, x));
y = Math.min(source.bitmapData.height - 1, Math.max(0, y));

// return the color at the requested pixel
return source.bitmapData.getPixel(x,y);
}

}

}
Loading

0 comments on commit b3a8a0a

Please sign in to comment.