# packages/core/src/features/viz/lib/numeric-ticks.ts
This is the source snapshot used to build these API details. [View this revision on GitHub](https://github.com/benlesh/pibbl/blob/272a94aaf62e0bd6ad8726a4c607a76a9ec44ca1/packages/core/src/features/viz/lib/numeric-ticks.ts#L39).

[Back to reference](/reference/components/numeric-ticks/)

<pre class="api-source"><code><span id="L1"><a href="#L1" aria-label="Line 1">1</a> import type { NumericDomain } from &quot;./types.js&quot;;</span>
<span id="L2"><a href="#L2" aria-label="Line 2">2</a> </span>
<span id="L3"><a href="#L3" aria-label="Line 3">3</a> /** Readable linear tick density. See {@link linearTicks}. */</span>
<span id="L4"><a href="#L4" aria-label="Line 4">4</a> export interface LinearTickOptions {</span>
<span id="L5"><a href="#L5" aria-label="Line 5">5</a>   /** Target number of intervals, from 2 to 100; defaults to 5. */</span>
<span id="L6"><a href="#L6" aria-label="Line 6">6</a>   readonly count?: number;</span>
<span id="L7"><a href="#L7" aria-label="Line 7">7</a> }</span>
<span id="L8"><a href="#L8" aria-label="Line 8">8</a> function parameters(domain: NumericDomain, options: LinearTickOptions) {</span>
<span id="L9"><a href="#L9" aria-label="Line 9">9</a>   const count = options.count ?? 5;</span>
<span id="L10"><a href="#L10" aria-label="Line 10">10</a>   if (!Number.isInteger(count) || count &lt; 2 || count &gt; 100)</span>
<span id="L11"><a href="#L11" aria-label="Line 11">11</a>     throw new RangeError(&quot;Linear tick count must be an integer from 2 to 100.&quot;);</span>
<span id="L12"><a href="#L12" aria-label="Line 12">12</a>   if (!Array.isArray(domain) || domain.length !== 2 || !domain.every(Number.isFinite))</span>
<span id="L13"><a href="#L13" aria-label="Line 13">13</a>     throw new RangeError(&quot;Linear tick domain requires two finite endpoints.&quot;);</span>
<span id="L14"><a href="#L14" aria-label="Line 14">14</a>   const lo = Math.min(...domain), hi = Math.max(...domain);</span>
<span id="L15"><a href="#L15" aria-label="Line 15">15</a>   const span = hi - lo;</span>
<span id="L16"><a href="#L16" aria-label="Line 16">16</a>   if (!Number.isFinite(span) || span === 0)</span>
<span id="L17"><a href="#L17" aria-label="Line 17">17</a>     throw new RangeError(&quot;Linear tick domain must have a finite nonzero span.&quot;);</span>
<span id="L18"><a href="#L18" aria-label="Line 18">18</a>   const raw = span / count;</span>
<span id="L19"><a href="#L19" aria-label="Line 19">19</a>   const power = 10 ** Math.floor(Math.log10(raw));</span>
<span id="L20"><a href="#L20" aria-label="Line 20">20</a>   const error = raw / power;</span>
<span id="L21"><a href="#L21" aria-label="Line 21">21</a>   let step = power * (error &gt;= Math.sqrt(50) ? 10 : error &gt;= Math.sqrt(10) ? 5 : error &gt;= Math.sqrt(2) ? 2 : 1);</span>
<span id="L22"><a href="#L22" aria-label="Line 22">22</a>   if (!Number.isFinite(step) || step === 0 || !Number.isSafeInteger(Math.floor(lo / step)) || !Number.isSafeInteger(Math.ceil(hi / step)))</span>
<span id="L23"><a href="#L23" aria-label="Line 23">23</a>     throw new RangeError(&quot;Linear tick spacing exceeds numeric precision.&quot;);</span>
<span id="L24"><a href="#L24" aria-label="Line 24">24</a>   if (Math.floor(hi / step) - Math.ceil(lo / step) + 1 &gt; 100) {</span>
<span id="L25"><a href="#L25" aria-label="Line 25">25</a>     const magnitude = 10 ** Math.floor(Math.log10(step));</span>
<span id="L26"><a href="#L26" aria-label="Line 26">26</a>     const factor = step / magnitude;</span>
<span id="L27"><a href="#L27" aria-label="Line 27">27</a>     step = magnitude * (factor &lt; 1.5 ? 2 : factor &lt; 3 ? 5 : 10);</span>
<span id="L28"><a href="#L28" aria-label="Line 28">28</a>   }</span>
<span id="L29"><a href="#L29" aria-label="Line 29">29</a>   return { lo, hi, step };</span>
<span id="L30"><a href="#L30" aria-label="Line 30">30</a> }</span>
<span id="L31"><a href="#L31" aria-label="Line 31">31</a> function clean(value: number) { return Number(value.toPrecision(15)) || 0; }</span>
<span id="L32"><a href="#L32" aria-label="Line 32">32</a> /**</span>
<span id="L33"><a href="#L33" aria-label="Line 33">33</a>  * Generates bounded ticks at readable 1, 2, or 5 × powers-of-ten intervals.</span>
<span id="L34"><a href="#L34" aria-label="Line 34">34</a>  * Endpoints are included only when aligned. Does not change scale geometry.</span>
<span id="L35"><a href="#L35" aria-label="Line 35">35</a>  * @param domain - Distinct finite endpoints, ascending or descending.</span>
<span id="L36"><a href="#L36" aria-label="Line 36">36</a>  * @param options - Target density; defaults to five intervals.</span>
<span id="L37"><a href="#L37" aria-label="Line 37">37</a>  * @returns Frozen ticks in domain order. See {@link niceLinearDomain}.</span>
<span id="L38"><a href="#L38" aria-label="Line 38">38</a>  */</span>
<span id="L39"><a href="#L39" aria-label="Line 39">39</a> export function linearTicks(domain: NumericDomain, options: LinearTickOptions = {}): readonly number[] {</span>
<span id="L40"><a href="#L40" aria-label="Line 40">40</a>   const { lo, hi, step } = parameters(domain, options);</span>
<span id="L41"><a href="#L41" aria-label="Line 41">41</a>   const start = Math.ceil(lo / step), end = Math.floor(hi / step);</span>
<span id="L42"><a href="#L42" aria-label="Line 42">42</a>   const values = Array.from({ length: Math.max(0, end - start + 1) }, (_, i) =&gt; clean((start + i) * step)).filter(v =&gt; v &gt;= lo &amp;&amp; v &lt;= hi);</span>
<span id="L43"><a href="#L43" aria-label="Line 43">43</a>   return Object.freeze(domain[0] &gt; domain[1] ? values.reverse() : values);</span>
<span id="L44"><a href="#L44" aria-label="Line 44">44</a> }</span>
<span id="L45"><a href="#L45" aria-label="Line 45">45</a> /**</span>
<span id="L46"><a href="#L46" aria-label="Line 46">46</a>  * Expands a linear domain to readable tick boundaries, preserving direction.</span>
<span id="L47"><a href="#L47" aria-label="Line 47">47</a>  * @param domain - Distinct finite numeric endpoints; never mutated.</span>
<span id="L48"><a href="#L48" aria-label="Line 48">48</a>  * @param options - Target density; defaults to five intervals.</span>
<span id="L49"><a href="#L49" aria-label="Line 49">49</a>  * @returns Frozen expanded endpoints. See {@link linearTicks}.</span>
<span id="L50"><a href="#L50" aria-label="Line 50">50</a>  */</span>
<span id="L51"><a href="#L51" aria-label="Line 51">51</a> export function niceLinearDomain(domain: NumericDomain, options: LinearTickOptions = {}): NumericDomain {</span>
<span id="L52"><a href="#L52" aria-label="Line 52">52</a>   const { lo, hi, step } = parameters(domain, options);</span>
<span id="L53"><a href="#L53" aria-label="Line 53">53</a>   const a = clean(Math.floor(lo / step) * step), b = clean(Math.ceil(hi / step) * step);</span>
<span id="L54"><a href="#L54" aria-label="Line 54">54</a>   if (!Number.isFinite(a) || !Number.isFinite(b)) throw new RangeError(&quot;Nice linear domain exceeds finite numeric bounds.&quot;);</span>
<span id="L55"><a href="#L55" aria-label="Line 55">55</a>   return Object.freeze(domain[0] &gt; domain[1] ? [b, a] : [a, b]);</span>
<span id="L56"><a href="#L56" aria-label="Line 56">56</a> }</span>
<span id="L57"><a href="#L57" aria-label="Line 57">57</a> /** Number formatting with explicit locale and literal unit affixes. See {@link formatNumber}. */</span>
<span id="L58"><a href="#L58" aria-label="Line 58">58</a> export interface NumberFormatOptions extends Intl.NumberFormatOptions {</span>
<span id="L59"><a href="#L59" aria-label="Line 59">59</a>   /** Locale for formatting; defaults to en-US. */</span>
<span id="L60"><a href="#L60" aria-label="Line 60">60</a>   readonly locale?: string;</span>
<span id="L61"><a href="#L61" aria-label="Line 61">61</a>   /** Literal prefix; defaults to empty. */</span>
<span id="L62"><a href="#L62" aria-label="Line 62">62</a>   readonly prefix?: string;</span>
<span id="L63"><a href="#L63" aria-label="Line 63">63</a>   /** Literal suffix, including any desired space; defaults to empty. */</span>
<span id="L64"><a href="#L64" aria-label="Line 64">64</a>   readonly suffix?: string;</span>
<span id="L65"><a href="#L65" aria-label="Line 65">65</a> }</span>
<span id="L66"><a href="#L66" aria-label="Line 66">66</a> /**</span>
<span id="L67"><a href="#L67" aria-label="Line 67">67</a>  * Formats a finite number with Intl precision, currency, percent, or unit options.</span>
<span id="L68"><a href="#L68" aria-label="Line 68">68</a>  * Rounded negative zero is displayed as positive zero.</span>
<span id="L69"><a href="#L69" aria-label="Line 69">69</a>  * @param value - Finite numeric value.</span>
<span id="L70"><a href="#L70" aria-label="Line 70">70</a>  * @param options - Intl options plus locale and literal affixes; default maximum fraction digits is 3.</span>
<span id="L71"><a href="#L71" aria-label="Line 71">71</a>  * @returns Formatted text. Invalid Intl options retain native errors. See {@link NumberFormatOptions}.</span>
<span id="L72"><a href="#L72" aria-label="Line 72">72</a>  */</span>
<span id="L73"><a href="#L73" aria-label="Line 73">73</a> export function formatNumber(value: number, options: NumberFormatOptions = {}): string {</span>
<span id="L74"><a href="#L74" aria-label="Line 74">74</a>   if (!Number.isFinite(value)) throw new RangeError(&quot;formatNumber value must be finite.&quot;);</span>
<span id="L75"><a href="#L75" aria-label="Line 75">75</a>   const { locale = &quot;en-US&quot;, prefix = &quot;&quot;, suffix = &quot;&quot;, ...intl } = options;</span>
<span id="L76"><a href="#L76" aria-label="Line 76">76</a>   const formatter = new Intl.NumberFormat(locale, intl);</span>
<span id="L77"><a href="#L77" aria-label="Line 77">77</a>   const parts = formatter.formatToParts(value);</span>
<span id="L78"><a href="#L78" aria-label="Line 78">78</a>   const zeroDigit = Array.from(formatter.formatToParts(0).find(p =&gt; p.type === &quot;integer&quot;)!.value).at(-1)!;</span>
<span id="L79"><a href="#L79" aria-label="Line 79">79</a>   const digits = parts.filter(p =&gt; p.type === &quot;integer&quot; || p.type === &quot;fraction&quot;);</span>
<span id="L80"><a href="#L80" aria-label="Line 80">80</a>   const zero = digits.length &gt; 0 &amp;&amp; digits.every(p =&gt; Array.from(p.value).every(digit =&gt; digit === zeroDigit));</span>
<span id="L81"><a href="#L81" aria-label="Line 81">81</a>   return prefix + formatter.format(zero ? 0 : value) + suffix;</span>
<span id="L82"><a href="#L82" aria-label="Line 82">82</a> }</span>
<span id="L83"><a href="#L83" aria-label="Line 83">83</a> </span></code></pre>

## Documentation version

Documentation built with @pibbl/core 0.0.2, revision 272a94a. ALPHA — NOT FOR PRODUCTION USE.
