Empirical
scales.h
Go to the documentation of this file.
1 
10 #ifndef EMP_D3_SCALES_H
11 #define EMP_D3_SCALES_H
12 
13 #include "d3_init.h"
14 #include "utils.h"
15 
16 #include "../js_utils.h"
17 
18 namespace D3 {
19 
24  class Scale : public D3_Base {
25  protected:
26  Scale(int id) : D3_Base(id){;};
27 
28  public:
29  Scale() {;}
30 
32  Scale(bool derived){;};
33 
37  template <typename T, size_t SIZE>
40  EM_ASM_ARGS({js.objects[$0].range(emp_i.__incoming_array);}, this->id);
41  return *this;
42  }
43 
44  Scale& SetRange(double min, double max) {
45  EM_ASM_ARGS({js.objects[$0].range([$1, $2]);}, this->id, min, max);
46  return *this;
47  }
48 
51  template <typename T, size_t SIZE>
54  EM_ASM_ARGS({js.objects[$0].domain(emp_i.__incoming_array);}, this->id);
55  return *this;
56  }
57 
58  Scale& SetDomain(double min, double max) {
59  EM_ASM_ARGS({js.objects[$0].domain([$1, $2]);}, this->id, min, max);
60  return *this;
61  }
62 
64  Scale Copy() {
65  int new_id = EM_ASM_INT_V({return js.objects.next_id++});
66  EM_ASM_ARGS({
67  js.objects[$1] = js.objects[$0].copy();
68  }, this->id, new_id);
69  return Scale(new_id);
70  }
71 
73  double ApplyScale(double input) {
74  //TODO: make this work for other types
75  return EM_ASM_DOUBLE({return js.objects[$0]($1);},this->id, input);
76  }
77 
78  int ApplyScale(int input) {
79  return EM_ASM_INT({return js.objects[$0]($1);},this->id, input);
80  }
81 
82 
83  //TODO:Getters
84 
85  };
86 
87 
88  class QuantizeScale : public Scale {
89  public:
90  QuantizeScale() : Scale(true) {EM_ASM_ARGS({js.objects[$0]=d3.scaleQuantize()},this->id);}
91  QuantizeScale(bool derived) : Scale(true) {;}
92 
93  template <typename T>
94  double InvertExtent(T y) {
95  return EM_ASM_DOUBLE({return js.objects[$0].invertExtent($1);},
96  this->id, y);
97  }
98  };
99 
100  class QuantileScale : public QuantizeScale {
101  public:
102  QuantileScale() : QuantizeScale(true) { EM_ASM_ARGS({js.objects[$0] = d3.scaleQuantile();}, this->id);}
103  QuantileScale(bool derived) : QuantizeScale(true) {;}
104  //TODO: Quantiles()
105  };
106 
107  class ThresholdScale : public QuantizeScale {
108  public:
110  EM_ASM_ARGS({js.objects[$0] = d3.scaleThreshold()}, this->id);
111  }
112  ThresholdScale(bool derived) : QuantizeScale(true) {;}
113  };
114 
115  class IdentityScale : public Scale {
116  public:
117  IdentityScale() : Scale(true) {
118  EM_ASM_ARGS({js.objects[$0] = d3.scaleIdentity();}, this->id);
119  }
120 
121  IdentityScale(bool derived) : Scale(true){;}
122 
123  template <typename T>
124  double Invert(T y) {
125  return EM_ASM_DOUBLE({return js.objects[$0].invert($1);}, this->id, y);
126  }
127 
128  IdentityScale& SetTicks(int count) {
129  EM_ASM_ARGS({js.objects[$0].ticks($1);}, this->id, count);
130  return *this;
131  }
132 
133  IdentityScale& SetTickFormat(int count, std::string format) {
134  //TODO: format is technically optional, but what is the point of this
135  //function without it?
136  EM_ASM_ARGS({js.objects[$0].tick($1, Pointer_stringify($2));},
137  this->id, count, format.c_str());
138  return *this;
139  }
140  };
141 
142  class LinearScale : public IdentityScale {
143  public:
145  EM_ASM_ARGS({js.objects[$0] = d3.scaleLinear();}, this->id);
146  }
147 
148  LinearScale(bool derived) : IdentityScale(true) {;}
149 
150  template <typename T, size_t SIZE>
153  EM_ASM_ARGS({js.objects[$0].rangeRound(emp.__incoming_array);}, this->id);
154  return *this;
155  }
156 
157  LinearScale& SetRangeRound(double min, double max) {
158  EM_ASM_ARGS({js.objects[$0].rangeRound([$1, $2]);}, this->id, min, max);
159  return *this;
160  }
161 
162 
163  LinearScale& SetInterpolate(std::string factory) {
164  D3_CALLBACK_METHOD_1_ARG(interpolate, factory.c_str())
165  return *this;
166  }
167 
168  LinearScale& Clamp(bool clamp) {
169  EM_ASM_ARGS({js.objects[$0].clamp($1);}, this->id, clamp);
170  return *this;
171  }
172 
173  LinearScale& Nice(int count = -1) {
174  if (count != -1){
175  EM_ASM_ARGS({js.objects[$0].nice($1);}, this->id, count);
176  } else {
177  EM_ASM_ARGS({js.objects[$0].nice();}, this->id);
178  }
179  return *this;
180  }
181 
182  };
183 
184  class LogScale : public LinearScale {
185  public:
186  LogScale() : LinearScale(true) {
187  EM_ASM_ARGS({js.objects[$0] = d3.scaleLog();}, this->id);
188  }
189 
190  LogScale(bool derived) : LinearScale(true){;};
191 
192  };
193 
194  class PowScale : public LinearScale {
195  public:
196  PowScale() : LinearScale(true) {
197  EM_ASM_ARGS({js.objects[$0] = d3.scalePow();}, this->id);
198  }
199 
200  PowScale(bool derived) : LinearScale(true){;};
201 
202  PowScale& Exponent(double ex) {
203  EM_ASM_ARGS({js.objects[$0].exponent($1);}, this->id, ex);
204  return *this;
205  }
206  };
207 
209  return PowScale().Exponent(.5);
210  }
211 
212  class TimeScale : public LinearScale {
213  public:
214  TimeScale() : LinearScale(true) {
215  EM_ASM_ARGS({js.objects[$0] = d3.scaleTime();}, this->id);
216  }
217 
218  TimeScale(bool derived) : LinearScale(true){;};
219  };
220 
221  class OrdinalScale : public QuantizeScale {
222  public:
224  EM_ASM({js.objects[$0]= d3.scaleOrdinal();}, this->id);
225  }
226 
227  OrdinalScale(bool derived) : QuantizeScale(true){;}
228  };
229 
230 
232  public:
234  EM_ASM({js.objects[$0] = d3.scaleCategory10();}, this->id);
235  }
236  };
237 
239  public:
241  EM_ASM({js.objects[$0] = d3.scaleCategory20();}, this->id);
242  }
243  };
244 
246  public:
248  EM_ASM({js.objects[$0] = d3.scaleCategory20b();}, this->id);
249  }
250  };
251 
253  protected:
254  int id;
255  public:
257  EM_ASM({js.objects[$0] = d3.scaleCategory20c();}, this->id);
258  }
259  };
260 }
261 
262 #endif
Definition: scales.h:115
QuantileScale()
Definition: scales.h:102
LinearScale(bool derived)
Definition: scales.h:148
Category20bScale()
Definition: scales.h:247
Definition: array.h:42
LinearScale & Clamp(bool clamp)
Definition: scales.h:168
Definition: scales.h:231
Definition: scales.h:252
LinearScale & Nice(int count=-1)
Definition: scales.h:173
ThresholdScale()
Definition: scales.h:109
LinearScale & SetRangeRound(double min, double max)
Definition: scales.h:157
LinearScale & SetRangeRound(emp::array< T, SIZE > values)
Definition: scales.h:151
Category20cScale()
Definition: scales.h:256
Category20Scale()
Definition: scales.h:240
double InvertExtent(T y)
Definition: scales.h:94
double ApplyScale(double input)
Calculate the ouput for [input], based on the scale&#39;s scaling function.
Definition: scales.h:73
Scale & SetDomain(double min, double max)
Definition: scales.h:58
Definition: d3_init.h:43
OrdinalScale(bool derived)
Definition: scales.h:227
Definition: scales.h:245
PowScale SqrtScale()
Definition: scales.h:208
Scale & SetRange(emp::array< T, SIZE > values)
Definition: scales.h:38
Scale(bool derived)
Decoy constructor so we don&#39;t construct extra base scales.
Definition: scales.h:32
int id
Definition: scales.h:254
Definition: scales.h:107
ThresholdScale(bool derived)
Definition: scales.h:112
TimeScale()
Definition: scales.h:214
int id
Definition: d3_init.h:45
Scale(int id)
Definition: scales.h:26
Definition: scales.h:184
Definition: scales.h:221
Scale & SetRange(double min, double max)
Definition: scales.h:44
QuantileScale(bool derived)
Definition: scales.h:103
PowScale & Exponent(double ex)
Definition: scales.h:202
double Invert(T y)
Definition: scales.h:124
TimeScale(bool derived)
Definition: scales.h:218
Definition: scales.h:100
int ApplyScale(int input)
Definition: scales.h:78
Definition: scales.h:24
IdentityScale & SetTickFormat(int count, std::string format)
Definition: scales.h:133
Scale Copy()
Make a copy of this scale.
Definition: scales.h:64
LogScale()
Definition: scales.h:186
Scale & SetDomain(emp::array< T, SIZE > values)
Definition: scales.h:52
If we are in emscripten, make sure to include the header.
Definition: array.h:37
IdentityScale()
Definition: scales.h:117
QuantizeScale()
Definition: scales.h:90
LinearScale & SetInterpolate(std::string factory)
Definition: scales.h:163
IdentityScale & SetTicks(int count)
Definition: scales.h:128
LogScale(bool derived)
Definition: scales.h:190
Definition: axis.h:20
QuantizeScale(bool derived)
Definition: scales.h:91
#define D3_CALLBACK_METHOD_1_ARG(FUNC, ARG1)
Definition: utils.h:115
Definition: scales.h:238
Category10Scale()
Definition: scales.h:233
LinearScale()
Definition: scales.h:144
PowScale()
Definition: scales.h:196
IdentityScale(bool derived)
Definition: scales.h:121
Definition: scales.h:212
Scale()
Definition: scales.h:29
Definition: scales.h:142
PowScale(bool derived)
Definition: scales.h:200
void pass_array_to_javascript(C values)
Definition: js_utils.h:212
Definition: scales.h:194
Definition: scales.h:88
OrdinalScale()
Definition: scales.h:223