Skip to content

Commit

Permalink
Added function to format labels
Browse files Browse the repository at this point in the history
Partially addresses #7
  • Loading branch information
jkshenton committed Sep 9, 2022
1 parent f4b6e96 commit 9cb553a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/core/sidebars/MVSidebarEFG.js
Expand Up @@ -44,9 +44,9 @@ function MVSidebarEFG(props) {
<MVButton onClick={() => { efgint.ellipsoidScale = 0; }} disabled={!efgint.hasEllipsoids}>Auto scale</MVButton>
<MVRadioGroup label='Show labels' onSelect={(v) => { efgint.labelsMode = v; }} selected={efgint.labelsMode} name='efg_label_radio' color={'var(--efg-color-2)'}>
<MVRadioButton value='none'>None</MVRadioButton>
<MVRadioButton value='aniso'>Anisotropy (au)</MVRadioButton>
{/* <MVRadioButton value='aniso'>Anisotropy (au)</MVRadioButton> */}
<MVRadioButton value='asymm'>Asymmetry</MVRadioButton>
<MVRadioButton value='Q'>Quadrupole Coupling (kHz)</MVRadioButton>
<MVRadioButton value='Q'>Quadrupole Coupling</MVRadioButton>
</MVRadioGroup>
<MVRadioGroup label='Use color scale' onSelect={(v) => { efgint.colorScaleType = v; }} selected={ efgint.colorScaleType } disabled={!efgint.colorScaleAvailable}
name='efg_cscale_radio' color={'var(--efg-color-2)'}>
Expand Down
7 changes: 5 additions & 2 deletions src/core/store/listeners/labels.js
Expand Up @@ -2,7 +2,7 @@
* Listeners for the rendering of labels
*/

import { addPrefix, getSel, getNMRData } from '../utils';
import { addPrefix, getSel, getNMRData, formatNumber } from '../utils';
import { selColor, msColor, efgColor } from './colors';

function makeLabelListener(name, color, shiftfunc) {
Expand Down Expand Up @@ -36,7 +36,10 @@ function makeLabelListener(name, color, shiftfunc) {

if (name !== 'sel_sites') {
let [units, values] = getNMRData(next_view, mode, name, ref_table);
label_texts = values.map((v) => v.toFixed(2) + ' ' + units);
// use formatNumber to get the right number of decimals
let precision = 2;
label_texts = values.map((v) => formatNumber(v, units, precision));

}
else {
// Non-NMR labels
Expand Down
48 changes: 46 additions & 2 deletions src/core/store/utils.js
Expand Up @@ -81,16 +81,59 @@ function getNMRData(view, datatype, tenstype='ms', reftable=null) {
case 'Q':
values = tensors.map((T, i) => {
let iD = view.atoms[i].isotopeData;
return T.efgAtomicToHz(iD.Q).haeberlen_eigenvalues[2]/1e3;
return T.efgAtomicToHz(iD.Q).haeberlen_eigenvalues[2];
});
units = 'kHz';
units = 'Hz'; // if this changes, change formatNumber as well!
break;
default:
break;
}

return [units, values];
}
function formatNumber(value, unit, precision=2) {
// function to adapt metric prefix to number given a unit string
// and a precision
// returns a string with the number and the unit

// special hadnling for kHz
if (unit === 'Hz') {
// -- handle negative values -- //
if (value < 0) {
return '-' + formatNumber(-value, unit, precision);
}

// -- handle positive values -- //

// suppress label completely if value is less than 1e-8 kHz
if (value < 1e-8) {
return '';
}

let prefix = '';
// convert to MHz if value is greater than 1 MHz
if (value > 1e6) {
value /= 1e6;
prefix = 'M';
// hard-coded precision for MHz
precision = 3;
}
// Convert to kHz if value is > 1 kHz but < 1 MHz
else if (value > 1e3) {
value /= 1e3;
prefix = 'k';
// hard-coded precision for kHz
precision = 1;
}
else {
// hard-coded precision for Hz
precision = 0;
}
unit = prefix + 'Hz';

}
return value.toFixed(precision) + ' ' + unit;
}

function getLinkLabel(a1, a2, linktype) {

Expand Down Expand Up @@ -156,6 +199,7 @@ export {
addPrefix,
getSel,
getNMRData,
formatNumber,
getLinkLabel,
BaseInterface,
DataCheckInterface
Expand Down

0 comments on commit 9cb553a

Please sign in to comment.