Skip to content

Commit

Permalink
improve Analyst layout; better key ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Oct 20, 2023
1 parent 58a4bf3 commit 19de2ba
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
8 changes: 6 additions & 2 deletions packages/analyst/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function useAnalyst() {
data,
error: config.error || dataError || (data ? null : "Loading..."),
modes,
form,
form: form.some((field) => field.type !== "hidden") ? form : null,
options,
setOptions,
};
Expand All @@ -38,6 +38,10 @@ export function useAnalystConfig() {

return {
...analyst,
initial_order:
typeof analyst.initial_order === "string"
? context[analyst.initial_order]
: analyst.initial_order,
url: render(analyst.url, context),
title: render(analyst.title, context),
};
Expand Down Expand Up @@ -198,7 +202,7 @@ const defaultOptions = { mode: "", date: "", value: "", value2: "", group: "" };

function makeForm(modes, currentMode) {
if (!modes) {
return null;
return [];
}
const modeInfo = modes.find((mode) => mode.name === currentMode),
form = [
Expand Down
10 changes: 9 additions & 1 deletion packages/analyst/src/views/Analyst.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React from "react";
import { useComponents } from "@wq/react";
import { Series, Scatter, Boxplot } from "@wq/chart";
import { useAnalyst } from "../hooks.js";
import PropTypes from "prop-types";

export default function Analyst() {
export default function Analyst({ children }) {
const { View, Typography, AnalystDownload, AnalystTable, AnalystForm } =
useComponents(),
{
Expand All @@ -24,6 +25,8 @@ export default function Analyst() {
if (error) {
return (
<View sx={{ p: 2 }}>
{title && <Typography variant="h5">{title}</Typography>}
{children}
<Typography>{error}</Typography>
</View>
);
Expand All @@ -42,6 +45,7 @@ export default function Analyst() {
<AnalystDownload url={url} title={title} formats={formats} />
)}
{!formats && title && <Typography variant="h5">{title}</Typography>}
{children}
{form && (
<AnalystForm
form={form}
Expand Down Expand Up @@ -85,3 +89,7 @@ export default function Analyst() {
</View>
);
}

Analyst.propTypes = {
children: PropTypes.node,
};
28 changes: 23 additions & 5 deletions packages/pandas/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export function parse(str, options = {}) {
// to get a unique key.
var datasetIndex = {};
metadata.forEach(function (meta, i) {
meta = reverseKeys(meta);
var metaHash = hash(meta);
var index = datasetIndex[metaHash];
if (index === undefined) {
Expand Down Expand Up @@ -179,28 +180,45 @@ export function parse(str, options = {}) {
datasets[i].data.push(d);
});
}
return options.flatten ? flatten(datasets) : datasets;
return options.flatten ? flatten(datasets, idColumns) : datasets;
}

export async function get(url, options = {}) {
const response = await fetch(url),
text = await response.text(),
const response = await fetch(url);
if (!response.ok) {
throw new Error(response.statusText);
}
const text = await response.text(),
data = parse(text, options);
return data;
}

export function flatten(datasets) {
export function flatten(datasets, idColumns = []) {
const allData = [];
datasets.forEach((dataset) => {
const { data, ...meta } = dataset;
data.forEach((row) => {
allData.push({ ...meta, ...row });
const ids = {};
for (const idCol of idColumns) {
ids[idCol] = row[idCol];
}
allData.push({ ...ids, ...meta, ...row });
});
});
allData.datasets = datasets;
return allData;
}

function reverseKeys(obj) {
const reversed = {};
Object.keys(obj)
.reverse()
.forEach((key) => {
reversed[key] = obj[key];
});
return reversed;
}

function hash(obj) {
var str = "";
Object.keys(obj)
Expand Down
2 changes: 1 addition & 1 deletion rest_pandas/static/app/.sha256
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
6b9ce6ca2a36570e77a6a6adc510efa019ad76df659f593871c2cc07cea2a200 js/analyst.js'
0d4be11c5457110e14cba2be6e13c253e8ad95f3d95210d7936939db82f1ea44 js/analyst.js'
1ff371bab99d663bb8a7069efbf2360f79b8103d0e19dcb701818e22ae360421 js/chart.js

0 comments on commit 19de2ba

Please sign in to comment.