cheminfo / common-spectrum

https://cheminfo.github.io/common-spectrum/.
MIT License
3 stars 2 forks source link

`common-spectrum` could also provide basic `getAnnotations` #64

Open kjappelbaum opened 3 years ago

kjappelbaum commented 3 years ago

something basic as

export function getAnnotations(peaks, options = {}) {
  const { fillColor = 'green', strokeColor = 'red', creationFct } = options;

  if (!peaks) return [];
  let annotations = peaks.map((peak) => {
    let annotation = {
      line: 1,
      type: 'rect',
      strokeColor: strokeColor,
      strokeWidth: 0,
      fillColor: fillColor,
    };
    if (creationFct) {
      creationFct(annotation, peak);
    }
    createAnnotation(annotation, peak, options);
    return annotation;
  });

  return annotations;
}

function createAnnotation(annotation, peak, options = {}) {
  const { showAssignment = true, assignmentAngle = -45 } = options;
  let labels = [];
  let line = 0;

  if (showAssignment) {
    labels.push({
      text: peak.assignment,
      size: '18px',
      angle: assignmentAngle,
      anchor: 'left',
      color: 'darkred',
      position: {
        x: peak.x,
        y: peak.y,
        dy: `${-15 - line * 14}px`,
      },
    });
    line++;
  }

  annotation.labels = labels;

  annotation.position = [
    {
      x: peak.x,
      y: peak.y,
      dy: '-10px',
      dx: '-1px',
    },
    {
      x: peak.x,
      y: peak.y,
      dy: '-5px',
      dx: '1px',
    },
  ];
}
heinemannj commented 2 years ago

For the uv-spectrum and ir-spectrum packages this is already in place but only in js. Can you please integrate - this is really useful staff.

Many thanks!