Skip to content

Commit

Permalink
fix: export getJSGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin M. Jablonka committed Apr 8, 2021
1 parent b718bb2 commit 5731884
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 6 deletions.
205 changes: 204 additions & 1 deletion dist/isotherm-analysis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* isotherm-analysis - Parse and analyze isotherms
* @version v0.4.0
* @version v0.4.1
* @link https://github.com/cheminfo/isotherm-analysis#readme
* @license MIT
*/
Expand Down Expand Up @@ -552,6 +552,34 @@
};
}

/**
* Filter out all the points for which x <= 0. Useful to display log scale data
* @param {DataXY} [data={}]
* @return {{x:[],y:[]}} An object with the filtered data
*/

function xyFilterXPositive(data = {}) {
xyCheck(data);
const {
x,
y
} = data;
const newX = [];
const newY = [];

for (let i = 0; i < x.length; i++) {
if (x[i] > 0) {
newX.push(x[i]);
newY.push(y[i]);
}
}

return {
x: newX,
y: newY
};
}

function rescale(input) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

Expand Down Expand Up @@ -4721,6 +4749,169 @@ ${indent}columns: ${matrix.columns}
}
}

function addStyle(serie, spectrum, options = {}) {
let {
color = '#A9A9A9',
opacity = 1,
lineWidth = 1
} = options; // eslint-disable-next-line @typescript-eslint/prefer-regexp-exec

if (color.match(/#[0-9A-F]{6}$/i)) {
color = (color + (opacity * 255 >> 0).toString(16)).toUpperCase();
} else {
color = color.replace(/rgb ?\((.*)\)/, `rgba($1,${opacity})`);
}

serie.style = [{
name: 'unselected',
style: {
line: {
color,
width: lineWidth,
dash: 1
}
}
}, {
name: 'selected',
style: {
line: {
color,
width: lineWidth + 2,
dash: 1
}
}
}];
serie.name = spectrum.label || spectrum.id;
}

const COLORS = ['#FFB300', '#803E75', '#FF6800', '#A6BDD7', '#C10020', '#CEA262', '#817066', '#007D34', '#F6768E', '#00538A', '#FF7A5C', '#53377A', '#FF8E00', '#B32851', '#F4C800', '#7F180D', '#93AA00', '#593315', '#F13A13', '#232C16'];

/**
* Generate a jsgraph chart format from an array of Analysis
*/

function getJSGraph$1(analyses, options = {}) {
const {
colors = COLORS,
opacities = [1],
linesWidth = [1],
selector = {},
normalization,
xAxis = {},
yAxis = {}
} = options;
let series = [];
let xLabel = '';
let yLabel = '';

for (let i = 0; i < analyses.length; i++) {
const analysis = analyses[i];
let serie = {};
let currentData = analysis.getNormalizedSpectrum({
selector,
normalization
});
if (!currentData) continue;
if (!xLabel) xLabel = currentData.variables.x.label;
if (!yLabel) yLabel = currentData.variables.y.label;
addStyle(serie, analysis, {
color: colors[i % colors.length],
opacity: opacities[i % opacities.length],
lineWidth: linesWidth[i % linesWidth.length]
});
serie.data = {
x: currentData.variables.x.data,
y: currentData.variables.y.data
};

if (xAxis.logScale) {
serie.data = xyFilterXPositive(serie.data);
}

series.push(serie);
}

return {
axes: {
x: {
label: xLabel,
unit: '',
flipped: false,
display: true,
...xAxis
},
y: {
label: yLabel,
unit: '',
flipped: false,
display: true,
...yAxis
}
},
series
};
}

function getNormalizationAnnotations$1(filter = {}, boundary = {
y: {
min: '0px',
max: '2000px'
}
}) {
let {
exclusions = []
} = filter;
let annotations = [];
exclusions = exclusions.filter(exclusion => !exclusion.ignore);
annotations = exclusions.map(exclusion => {
let annotation = {
type: 'rect',
position: [{
x: exclusion.from,
y: boundary.y.min
}, {
x: exclusion.to,
y: boundary.y.max
}],
strokeWidth: 0,
fillColor: 'rgba(255,255,224,1)'
};
return annotation;
});

if (filter.from !== undefined) {
annotations.push({
type: 'rect',
position: [{
x: Number.MIN_SAFE_INTEGER,
y: boundary.y.min
}, {
x: filter.from,
y: boundary.y.max
}],
strokeWidth: 0,
fillColor: 'rgba(255,255,224,1)'
});
}

if (filter.to !== undefined) {
annotations.push({
type: 'rect',
position: [{
x: filter.to,
y: boundary.y.min
}, {
x: Number.MAX_SAFE_INTEGER,
y: boundary.y.max
}],
strokeWidth: 0,
fillColor: 'rgba(255,255,224,1)'
});
}

return annotations;
}

function appendDistinctParameter(values, key, value) {
if (!values[key]) {
values[key] = {
Expand Down Expand Up @@ -11588,6 +11779,11 @@ ${points.join('\n')}
return toJcamps(analysis, options).join('\n');
}

const JSGraph = {
getJSGraph: getJSGraph$1,
getNormalizationAnnotations: getNormalizationAnnotations$1
};

function lineSplitTrim(line) {
return lineSplit(line)[1].trim();
}
Expand Down Expand Up @@ -28957,13 +29153,20 @@ ${points.join('\n')}
return analysis;
}

const {
getJSGraph,
getNormalizationAnnotations
} = JSGraph;

exports.AnalysesManager = AnalysesManager;
exports.Analysis = Analysis;
exports.fromBelsorp = fromBelsorp;
exports.fromIGA = fromIGA;
exports.fromJcamp = fromJcamp;
exports.fromMicrometricsCSV = fromMicrometricsCSV;
exports.fromMicrometricsTXT = fromMicrometricsTXT;
exports.getJSGraph = getJSGraph;
exports.getNormalizationAnnotations = getNormalizationAnnotations;
exports.toJcamp = toJcamp;
exports.toJcamps = toJcamps;

Expand Down
2 changes: 1 addition & 1 deletion dist/isotherm-analysis.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/isotherm-analysis.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/isotherm-analysis.min.js.map

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ import {
fromJcamp,
toJcamp,
toJcamps,
JSGraph,
} from 'common-spectrum';

export { Analysis, AnalysesManager, fromJcamp, toJcamp, toJcamps };
const { getJSGraph, getNormalizationAnnotations } = JSGraph;
export {
Analysis,
AnalysesManager,
fromJcamp,
toJcamp,
toJcamps,
getJSGraph,
getNormalizationAnnotations,
};

export { fromIGA } from './from/fromIGA';
export { fromMicrometricsTXT } from './from/fromMicrometricsTXT';
Expand Down

0 comments on commit 5731884

Please sign in to comment.