View Javadoc
1   /*
2    * Created on Nov 3, 2005
3    *
4    * Copyright (c) 2005, The JUNG Authors 
5    *
6    * All rights reserved.
7    *
8    * This software is open-source under the BSD license; see either
9    * "license.txt" or
10   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
11   */
12  package edu.uci.ics.jung.visualization.decorators;
13  
14  import com.google.common.base.Function;
15  
16  
17  /**
18   * Provides vertex sizes that are spaced proportionally between 
19   * min_size and max_size depending on 
20   * 
21   * @author Joshua O'Madadhain
22   */
23  public class InterpolatingVertexSizeTransformer<V> implements Function<V,Integer>
24  {
25      protected double min;
26      protected double max;
27      protected Function<V, ? extends Number> values;
28      protected int min_size;
29      protected int size_diff;
30      
31      public InterpolatingVertexSizeTransformer(Function<V, ? extends Number> values, 
32              int min_size, int max_size)
33      {
34          super();
35          if (min_size < 0 || max_size < 0)
36              throw new IllegalArgumentException("sizes must be non-negative");
37          if (min_size > max_size)
38              throw new IllegalArgumentException("min_size must be <= max_size");
39          this.min = 0;
40          this.max = 0;
41          this.values = values;
42          setMinSize(min_size);
43          setMaxSize(max_size);
44      }
45  
46      public Integer apply(V v)
47      {
48          Number n = values.apply(v);
49          double value = min;
50          if (n != null)
51              value = n.doubleValue();
52          min = Math.min(this.min, value);
53          max = Math.max(this.max, value);
54          
55          if (min == max)
56              return min_size;
57          
58          // interpolate between min and max sizes based on how big value is 
59          // with respect to min and max values
60          return min_size + (int)(((value - min) / (max - min)) * size_diff);
61      }
62      
63      public void setMinSize(int min_size)
64      {
65          this.min_size = min_size;
66      }
67  
68      public void setMaxSize(int max_size)
69      {
70          this.size_diff = max_size - this.min_size;
71      }
72  }