(plotMessage: {
type: "plot";
args: BertieGraphData;
})
| 592 | } |
| 593 | |
| 594 | export function formatPlotData(plotMessage: { |
| 595 | type: "plot"; |
| 596 | args: BertieGraphData; |
| 597 | }): GraphMessage { |
| 598 | let graphData = plotMessage.args; |
| 599 | if (graphData.type !== "table") { |
| 600 | graphData.data = ensureXandYinData(graphData); |
| 601 | |
| 602 | // Must ensure that the y value is a number |
| 603 | graphData.data.forEach((item, idx) => { |
| 604 | if (typeof item.y === "string") { |
| 605 | const num = Number(item.y); |
| 606 | if (!isNaN(num)) { |
| 607 | item.y = num; |
| 608 | } |
| 609 | } else if ( |
| 610 | typeof item.y === "object" && |
| 611 | !Array.isArray(item.y) && |
| 612 | item.y !== null |
| 613 | ) { |
| 614 | // Find the first number in the object and use that as y |
| 615 | const key = Object.keys(item.y).find( |
| 616 | (k) => !isNaN(item.y[k]) && item.y[k] !== null, |
| 617 | ); |
| 618 | if (key) { |
| 619 | item.y.y = item.y[key]; |
| 620 | delete item.y[key]; |
| 621 | } |
| 622 | graphData.data[idx] = { ...item, ...item.y }; |
| 623 | } |
| 624 | }); |
| 625 | } |
| 626 | return { |
| 627 | role: "graph", |
| 628 | content: { |
| 629 | type: |
| 630 | // Can have tables with only 1 data point |
| 631 | graphData.data.length === 1 && graphData.type !== "table" |
| 632 | ? "value" |
| 633 | : graphData.type, |
| 634 | // TODO: Fix below typing issues by making the functions type safe |
| 635 | // @ts-ignore |
| 636 | data: graphData.data, |
| 637 | xLabel: graphData.labels?.x ?? "", |
| 638 | yLabel: graphData.labels?.y ?? "", |
| 639 | graphTitle: graphData.title, |
| 640 | }, |
| 641 | }; |
| 642 | } |
| 643 | |
| 644 | export function ensureXandYinData( |
| 645 | graphData: BertieGraphData, |
no test coverage detected