simplify code by not mapping x property to dateTime property

This commit is contained in:
Conor Barnes 2022-09-06 14:27:01 -03:00
parent 2087a30b6b
commit 3ea747fae4
2 changed files with 13 additions and 34 deletions

View File

@ -85,7 +85,8 @@ export const DistributionChart: React.FC<DistributionChartProps> = (props) => {
); );
if (sampleSets.length) { if (sampleSets.length) {
for (const set of sampleSets) { for (const set of sampleSets) {
if (set.distribution.t.tag === "SampleSet") { // this must be duplicated to please typescript, more elegant solution probably exists if (set.distribution.t.tag === "SampleSet") {
// this must be duplicated to please typescript, more elegant solution probably exists
shapes.value[0].samples.push( shapes.value[0].samples.push(
...set.distribution.t.value.map((v) => ({ x: v, y: 0 })) ...set.distribution.t.value.map((v) => ({ x: v, y: 0 }))
); );
@ -102,33 +103,10 @@ export const DistributionChart: React.FC<DistributionChartProps> = (props) => {
); );
widthProp = 20; widthProp = 20;
} }
const predomain = shapes.value.flatMap((shape) => const domain = shapes.value.flatMap((shape) =>
shape.discrete.concat(shape.continuous) shape.discrete.concat(shape.continuous)
); );
const domain =
xAxis === "dateTime"
? predomain.map((p) => ({ dateTime: p.x, y: p.y }))
: predomain;
const data =
xAxis === "dateTime"
? shapes.value.map((val) => {
return {
...val,
continuous: val.continuous.map((p) => {
return { dateTime: p.x, y: p.y };
}),
discrete: val.discrete.map((p) => {
return { dateTime: p.x, y: p.y };
}),
samples: val.samples.map((p) => {
return { dateTime: p, y: 0 };
}),
};
})
: shapes.value;
return ( return (
<div style={{ width: widthProp }}> <div style={{ width: widthProp }}>
{logX && shapes.value.some(hasMassBelowZero) ? ( {logX && shapes.value.some(hasMassBelowZero) ? (
@ -138,7 +116,7 @@ export const DistributionChart: React.FC<DistributionChartProps> = (props) => {
) : ( ) : (
<Vega <Vega
spec={spec} spec={spec}
data={{ data, domain }} data={{ data: shapes.value, domain }}
width={widthProp - 10} width={widthProp - 10}
height={height} height={height}
actions={actions} actions={actions}

View File

@ -20,6 +20,7 @@ export type DistributionChartSpecOptions = {
xAxis?: "number" | "dateTime"; xAxis?: "number" | "dateTime";
}; };
/** X Scales */
export const linearXScale: LinearScale = { export const linearXScale: LinearScale = {
name: "xscale", name: "xscale",
clamp: true, clamp: true,
@ -47,9 +48,10 @@ export const timeXScale: TimeScale = {
type: "time", type: "time",
range: "width", range: "width",
nice: false, nice: false,
domain: { data: "domain", field: "dateTime" }, domain: { data: "domain", field: "x" },
}; };
/** Y Scales */
export const linearYScale: LinearScale = { export const linearYScale: LinearScale = {
name: "yscale", name: "yscale",
type: "linear", type: "linear",
@ -190,7 +192,7 @@ export function buildVegaSpec(
interpolate: { value: "linear" }, interpolate: { value: "linear" },
x: { x: {
scale: "xscale", scale: "xscale",
field: dateTime ? "dateTime" : "x", field: "x",
}, },
y: { y: {
scale: "yscale", scale: "yscale",
@ -237,7 +239,7 @@ export function buildVegaSpec(
update: { update: {
x: { x: {
scale: "xscale", scale: "xscale",
field: dateTime ? "dateTime" : "x", field: "x",
}, },
y: { y: {
scale: "yscale", scale: "yscale",
@ -267,14 +269,14 @@ export function buildVegaSpec(
size: [{ value: 100 }], size: [{ value: 100 }],
tooltip: { tooltip: {
signal: dateTime signal: dateTime
? "{ probability: datum.y, value: datetime(datum.dateTime) }" ? "{ probability: datum.y, value: datetime(datum.x) }"
: "{ probability: datum.y, value: datum.x }", : "{ probability: datum.y, value: datum.x }",
}, },
}, },
update: { update: {
x: { x: {
scale: "xscale", scale: "xscale",
field: dateTime ? "dateTime" : "x", field: "x",
offset: 0.5, // if this is not included, the circles are slightly left of center. offset: 0.5, // if this is not included, the circles are slightly left of center.
}, },
y: { y: {
@ -307,7 +309,7 @@ export function buildVegaSpec(
from: { data: "sample_facet" }, from: { data: "sample_facet" },
encode: { encode: {
enter: { enter: {
x: { scale: "xscale", field: dateTime ? "dateTime" : "x" }, x: { scale: "xscale", field: "x" },
width: { value: 0.1 }, width: { value: 0.1 },
y: { value: 25, offset: { signal: "height" } }, y: { value: 25, offset: { signal: "height" } },
@ -369,7 +371,6 @@ export function buildVegaSpec(
signal: signal:
"position ? position[0] < 0 ? 0 : position[0] > width ? 0 : 1 : 0", "position ? position[0] < 0 ? 0 : position[0] > width ? 0 : 1 : 0",
}, },
// opacity: { signal: "position ? 1 : 0" },
}, },
}, },
}, },