File size: 1,237 Bytes
14356bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { PO } from './constants.js'

/** Return CSS class for own-price elasticity colouring */
export function eCls(e) {
  if (e < -3) return 'e-high'
  if (e < -1.5) return 'e-med'
  if (e < 0) return 'e-low'
  return 'e-pos'
}

/** Format ₹ value as Cr / L */
export function fmtCr(v) {
  const a = Math.abs(v)
  if (a === 0) return '₹0'
  if (a >= 1e7) return (v >= 0 ? '+' : '-') + '₹' + (a / 1e7).toFixed(1) + 'Cr'
  return (v >= 0 ? '+' : '-') + '₹' + (a / 1e5).toFixed(1) + 'L'
}

/** Format litre volume */
export function fmtL(v) {
  const a = Math.abs(v)
  if (a >= 1e6) return (v >= 0 ? '+' : '−') + (a / 1e6).toFixed(2) + 'M L'
  if (a >= 1e3) return (v >= 0 ? '+' : '−') + (a / 1e3).toFixed(1) + 'K L'
  return (v >= 0 ? '+' : '') + Math.round(v) + 'L'
}

/** Sort grains by a given sort key */
export function sortModels(data, sortKey) {
  return [...data].sort((a, b) => {
    if (sortKey === 'vs')   return (b.VolSal || 0) - (a.VolSal || 0)
    if (sortKey === 'vs2')  return (b.ValShare || 0) - (a.ValShare || 0)
    if (sortKey === 'elas') return Math.abs(b.OwnE) - Math.abs(a.OwnE)
    if (sortKey === 'r2')   return (b.AdjR2 || 0) - (a.AdjR2 || 0)
    return (PO[a.Pack] || 9) - (PO[b.Pack] || 9)
  })
}