Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(subject previews): allow for single series JSON data subjects #7123

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions app/features/modelling/line-plot/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import { Chart } from 'chart.js/auto';

function getXYData(data) {
return {
data: data.x.map((x, index) => ({ x, y: data.y[index] })),
pointStyle: 'circle',
backgroundColor: '#ffffff',
borderColor: '#000000'
}
}
Comment on lines +3 to +10
Copy link
Contributor Author

@eatyourgreens eatyourgreens Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new, to handle subjects with { data: { x: number[], y: number[] }}. Otherwise, the rest of the code in this PR should be unchanged from the original, just refactored into smaller functions.


function getSeriesData(series, index) {
const { seriesData, seriesOptions } = series;
return {
data: seriesData,
label: seriesOptions.label || `Series ${index + 1}`,
pointStyle: seriesOptions.glyph,
backgroundColor: seriesOptions.color,
borderColor: '#000000'
};
}

function getDatasets(data) {
if (data?.datasets) {
return data.datasets;
}
if (data?.map) {
return data.map(getSeriesData);
}
if (data?.x && data?.y) {
return [getXYData(data)];
}
return [];
}

// Help at http://www.chartjs.org/docs/latest
class LinePlotModel {
constructor(canvas, { frame, src }, { onLoad, modelDidError }) {
Expand All @@ -8,24 +41,23 @@ class LinePlotModel {
this.frame = frame;
fetch(`${src}?=`)
.then(response => response.json())
.then((data) => {
let datasets;
const { chartOptions, data: FEMdata, ...rest } = data;
if (FEMdata) {
datasets = FEMdata.map( (data, index) => {
const { seriesData, seriesOptions } = data
return {
data: seriesData,
label: seriesOptions.label || `Series ${index + 1}`,
pointStyle: seriesOptions.glyph,
backgroundColor: seriesOptions.color,
borderColor: '#000000'
.then(data => {
if(data.x && data.y) {
return {
data: {
x: data.x,
y: data.y
},
chartOptions: {
xAxisLabel: 'Days',
yAxisLabel: 'Brightness'
}
});
}
if (data.datasets) {
datasets = data.datasets
};
}
return data;
})
.then(({ data, chartOptions, ...rest }) => {
const datasets = getDatasets(data);
console.log({
type: 'scatter',
data: {
Expand Down
Loading