Skip to content

Instantly share code, notes, and snippets.

@htsign
Created June 24, 2017 09:18
Show Gist options
  • Select an option

  • Save htsign/c6d7e7007a79470ee28e54e581c043ab to your computer and use it in GitHub Desktop.

Select an option

Save htsign/c6d7e7007a79470ee28e54e581c043ab to your computer and use it in GitHub Desktop.
how to get scale type of d3 (require es6-shim)
class ChartHelper {
public static checkScaleType<T, R>(scale: Scale<T, R>): ScaleType {
const f = scale.copy.toString();
switch (true) {
case f.includes("d3_scale_linear") : return "linear";
case f.includes("d3_scale_identity"): return "identity";
case f.includes("d3_scale_pow") : return "power";
case f.includes("d3_scale_log") : return "log";
case f.includes("d3_scale_quantize"): return "quantize";
case f.includes("d3_scale_quantile"): return "quantile";
case f.includes("d3_scale_ordinal") : return "ordinal";
case f.includes("d3_time_scale") : return "time";
}
throw new TypeError("unexpected type");
}
}
type ScaleType = "linear" | "identity" | "power" | "log" | "quantize" | "quantile" | "ordinal" | "time";
type Scale<T, R> =
| d3.scale.Linear<T, R>
| d3.scale.Identity
| d3.scale.Pow<T, R>
| d3.scale.Log<T, R>
| d3.scale.Quantize<T>
| d3.scale.Quantile<T>
| d3.scale.Ordinal<T, R>
| d3.time.Scale<T, R>
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment