Skip to content

Commit 19de2ba

Browse files
committed
improve Analyst layout; better key ordering
1 parent 58a4bf3 commit 19de2ba

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

packages/analyst/src/hooks.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function useAnalyst() {
1818
data,
1919
error: config.error || dataError || (data ? null : "Loading..."),
2020
modes,
21-
form,
21+
form: form.some((field) => field.type !== "hidden") ? form : null,
2222
options,
2323
setOptions,
2424
};
@@ -38,6 +38,10 @@ export function useAnalystConfig() {
3838

3939
return {
4040
...analyst,
41+
initial_order:
42+
typeof analyst.initial_order === "string"
43+
? context[analyst.initial_order]
44+
: analyst.initial_order,
4145
url: render(analyst.url, context),
4246
title: render(analyst.title, context),
4347
};
@@ -198,7 +202,7 @@ const defaultOptions = { mode: "", date: "", value: "", value2: "", group: "" };
198202

199203
function makeForm(modes, currentMode) {
200204
if (!modes) {
201-
return null;
205+
return [];
202206
}
203207
const modeInfo = modes.find((mode) => mode.name === currentMode),
204208
form = [

packages/analyst/src/views/Analyst.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import React from "react";
22
import { useComponents } from "@wq/react";
33
import { Series, Scatter, Boxplot } from "@wq/chart";
44
import { useAnalyst } from "../hooks.js";
5+
import PropTypes from "prop-types";
56

6-
export default function Analyst() {
7+
export default function Analyst({ children }) {
78
const { View, Typography, AnalystDownload, AnalystTable, AnalystForm } =
89
useComponents(),
910
{
@@ -24,6 +25,8 @@ export default function Analyst() {
2425
if (error) {
2526
return (
2627
<View sx={{ p: 2 }}>
28+
{title && <Typography variant="h5">{title}</Typography>}
29+
{children}
2730
<Typography>{error}</Typography>
2831
</View>
2932
);
@@ -42,6 +45,7 @@ export default function Analyst() {
4245
<AnalystDownload url={url} title={title} formats={formats} />
4346
)}
4447
{!formats && title && <Typography variant="h5">{title}</Typography>}
48+
{children}
4549
{form && (
4650
<AnalystForm
4751
form={form}
@@ -85,3 +89,7 @@ export default function Analyst() {
8589
</View>
8690
);
8791
}
92+
93+
Analyst.propTypes = {
94+
children: PropTypes.node,
95+
};

packages/pandas/src/index.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export function parse(str, options = {}) {
124124
// to get a unique key.
125125
var datasetIndex = {};
126126
metadata.forEach(function (meta, i) {
127+
meta = reverseKeys(meta);
127128
var metaHash = hash(meta);
128129
var index = datasetIndex[metaHash];
129130
if (index === undefined) {
@@ -179,28 +180,45 @@ export function parse(str, options = {}) {
179180
datasets[i].data.push(d);
180181
});
181182
}
182-
return options.flatten ? flatten(datasets) : datasets;
183+
return options.flatten ? flatten(datasets, idColumns) : datasets;
183184
}
184185

185186
export async function get(url, options = {}) {
186-
const response = await fetch(url),
187-
text = await response.text(),
187+
const response = await fetch(url);
188+
if (!response.ok) {
189+
throw new Error(response.statusText);
190+
}
191+
const text = await response.text(),
188192
data = parse(text, options);
189193
return data;
190194
}
191195

192-
export function flatten(datasets) {
196+
export function flatten(datasets, idColumns = []) {
193197
const allData = [];
194198
datasets.forEach((dataset) => {
195199
const { data, ...meta } = dataset;
196200
data.forEach((row) => {
197-
allData.push({ ...meta, ...row });
201+
const ids = {};
202+
for (const idCol of idColumns) {
203+
ids[idCol] = row[idCol];
204+
}
205+
allData.push({ ...ids, ...meta, ...row });
198206
});
199207
});
200208
allData.datasets = datasets;
201209
return allData;
202210
}
203211

212+
function reverseKeys(obj) {
213+
const reversed = {};
214+
Object.keys(obj)
215+
.reverse()
216+
.forEach((key) => {
217+
reversed[key] = obj[key];
218+
});
219+
return reversed;
220+
}
221+
204222
function hash(obj) {
205223
var str = "";
206224
Object.keys(obj)

rest_pandas/static/app/.sha256

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
6b9ce6ca2a36570e77a6a6adc510efa019ad76df659f593871c2cc07cea2a200 js/analyst.js'
1+
0d4be11c5457110e14cba2be6e13c253e8ad95f3d95210d7936939db82f1ea44 js/analyst.js'
22
1ff371bab99d663bb8a7069efbf2360f79b8103d0e19dcb701818e22ae360421 js/chart.js

0 commit comments

Comments
 (0)