From 974ca2335e25490d8b63c0a25c91695d4e423581 Mon Sep 17 00:00:00 2001 From: Sam Nolan Date: Sun, 13 Feb 2022 16:31:37 +1100 Subject: [PATCH] Add cdf tooltip and squiggle code control --- .../src/stories/SquiggleChart.stories.tsx | 7 +++- .../components/src/stories/SquiggleChart.tsx | 33 ++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/components/src/stories/SquiggleChart.stories.tsx b/packages/components/src/stories/SquiggleChart.stories.tsx index cf8eb1f4..32ccdc49 100644 --- a/packages/components/src/stories/SquiggleChart.stories.tsx +++ b/packages/components/src/stories/SquiggleChart.stories.tsx @@ -7,4 +7,9 @@ export default { component: SquiggleChart } -export const Default = () => +const Template = ({squiggleString}) => + +export const Default = Template.bind({}) +Default.args = { + squiggleString: "normal(5, 2)" +}; diff --git a/packages/components/src/stories/SquiggleChart.tsx b/packages/components/src/stories/SquiggleChart.tsx index 84e699ae..cc8f7c7a 100644 --- a/packages/components/src/stories/SquiggleChart.tsx +++ b/packages/components/src/stories/SquiggleChart.tsx @@ -62,7 +62,7 @@ let specification : Spec = { }, "update": { "fill": { - "signal": "{gradient: 'linear', x1: 1, y1: 1, x2: 0, y2: 1, stops: [ {offset: 0.0, color: 'steelblue'}, {offset: mousex, color: 'steelblue'}, {offset: mousex, color: 'blue'}, {offset: 1.0, color: 'blue'} ] }" + "signal": "{gradient: 'linear', x1: 1, y1: 1, x2: 0, y2: 1, stops: [ {offset: 0.0, color: 'steelblue'}, {offset: clamp(mousex, 0, 1), color: 'steelblue'}, {offset: clamp(mousex, 0, 1), color: 'blue'}, {offset: 1.0, color: 'blue'} ] }" }, "interpolate": {"value": "monotone"}, "fillOpacity": {"value": 1} @@ -79,11 +79,17 @@ function zip(a: Array, b: Array): Array>{ return [e, b[i]]; }) } +function zip3(a: Array, b: Array, c: Array): Array>{ + return a.map(function(e, i) { + return [e, b[i], c[i]]; + }) +} /** * Primary UI component for user interaction */ export const SquiggleChart = ({ squiggleString }: { squiggleString: string}) => { let result = run(squiggleString); + console.log(result); if (result.tag === "Ok") { let chartResult = result.value[0]; if(chartResult["NAME"] === "Float"){ @@ -93,8 +99,14 @@ export const SquiggleChart = ({ squiggleString }: { squiggleString: string}) => let shape = chartResult.VAL.shape; if(shape.tag === "Continuous"){ let xyShape = shape.value.xyShape; - let values = zip(xyShape.xs, xyShape.ys).map(([x,y]) => ({x: x, y: y})); - console.log(values) + let totalY = xyShape.ys.reduce((a, b) => a + b); + let total = 0; + let cdf = xyShape.ys.map(y => { + total += y; + return total / totalY; + }) + console.log(cdf) + let values = zip3(cdf, xyShape.xs, xyShape.ys).map(([c, x, y ]) => ({cdf: (c * 100).toFixed(2) + "%", x: x, y: y})); return ( } else if(shape.tag === "Discrete"){ let xyShape = shape.value.xyShape; - let values = zip(xyShape.xs, xyShape.ys).map(([x,y]) => ({x: x, y: y})); + let totalY = xyShape.ys.reduce((a, b) => a + b); + let total = 0; + let cdf = xyShape.ys.map(y => { + total += y; + return total / totalY; + }) + let values = zip3(cdf, xyShape.xs, xyShape.ys).map(([c, x,y]) => ({cdf: (c * 100).toFixed(2) + "%", x: x, y: y})); return ( ); } else if(shape.tag === "Mixed"){ + console.log(shape.value.integralSumCache) + console.log(shape.value.integralCache) let xyShape = shape.value.continuous.xyShape; let values = zip(xyShape.xs, xyShape.ys).map(([x,y]) => ({x: x, y: y})); @@ -127,6 +147,11 @@ export const SquiggleChart = ({ squiggleString }: { squiggleString: string}) => } } + else if(result.tag == "Error") { + // At this point, we came across an error. What was our error? + return (

{"Error parsing Squiggle: " + result.value}

) + + } return (

{"Invalid Response"}

) };