From 7e05d6300926718d621b3ef1a7cea8fba721248b Mon Sep 17 00:00:00 2001 From: Sam Nolan Date: Mon, 2 May 2022 20:40:15 +0000 Subject: [PATCH] Make a tooltip to restrict users from log scales --- .../src/components/DistributionChart.tsx | 126 ++++++++++-------- 1 file changed, 70 insertions(+), 56 deletions(-) diff --git a/packages/components/src/components/DistributionChart.tsx b/packages/components/src/components/DistributionChart.tsx index 47c105c8..38b96054 100644 --- a/packages/components/src/components/DistributionChart.tsx +++ b/packages/components/src/components/DistributionChart.tsx @@ -1,7 +1,7 @@ import * as React from "react"; import _ from "lodash"; import type { Distribution } from "@quri/squiggle-lang"; -import { distributionErrorToString, result, shape } from "@quri/squiggle-lang"; +import { distributionErrorToString } from "@quri/squiggle-lang"; import { Vega, VisualizationSpec } from "react-vega"; import * as chartSpecification from "../vega-specs/spec-distributions.json"; import { ErrorBox } from "./ErrorBox"; @@ -12,6 +12,7 @@ import { linearYScale, expYScale, } from "./DistributionVegaScales"; +import styled from "styled-components"; type DistributionChartProps = { distribution: Distribution; @@ -26,28 +27,29 @@ export const DistributionChart: React.FC = ({ }: DistributionChartProps) => { let [isLogX, setLogX] = React.useState(false); let [isExpY, setExpY] = React.useState(false); + let shape = distribution.pointSet(); const [sized, _] = useSize((size) => { - let shape = distribution.pointSet(); + var disableLog = false; if (shape.tag === "Ok") { - let spec = buildSpec(isLogX, isExpY, shape.value); - if (spec.tag == "Ok") { - let widthProp = width ? width - 20 : size.width - 10; - var result = ( -
- -
- ); - } else { - var result = ( - {spec.value.error} - ); + let massBelow0 = + shape.value.continuous.some((x) => x.x <= 0) || + shape.value.discrete.some((x) => x.x <= 0); + if (massBelow0) { + disableLog = true; } + let spec = buildVegaSpec(isLogX, isExpY); + let widthProp = width ? width - 20 : size.width - 10; + var result = ( +
+ +
+ ); } else { var result = ( @@ -59,7 +61,19 @@ export const DistributionChart: React.FC = ({ <> {result}
- + {disableLog ? ( + + ) : ( + + )}
@@ -68,44 +82,44 @@ export const DistributionChart: React.FC = ({ return sized; }; -type ViewError = { heading: string; error: string }; - -function buildSpec( - isLogX: boolean, - isExpY: boolean, - shape: shape -): result { - let someBelow0 = - shape.continuous.some((x) => x.x <= 0) || - shape.discrete.some((x) => x.x <= 0); - if (!(isLogX && someBelow0)) { - return { - tag: "Ok", - value: { - ...chartSpecification, - scales: [ - isLogX ? logXScale : linearXScale, - isExpY ? expYScale : linearYScale, - ], - } as VisualizationSpec, - }; - } else { - return { - tag: "Error", - value: { - heading: "Log Viewing error", - error: - "Distribution contains values lower than or equal to 0. Cannot view", - }, - }; - } +function buildVegaSpec(isLogX: boolean, isExpY: boolean): VisualizationSpec { + return { + ...chartSpecification, + scales: [ + isLogX ? logXScale : linearXScale, + isExpY ? expYScale : linearYScale, + ], + } as VisualizationSpec; } -export const CheckBox = ({ label, onChange, value }) => { +interface CheckBoxProps { + label: string; + onChange: (x: boolean) => void; + value: boolean; + disabled?: boolean; + tooltip?: string; +} + +const Label = styled.label<{ disabled: boolean }>` + ${(props) => props.disabled && "color: #999;"} +`; + +export const CheckBox = ({ + label, + onChange, + value, + disabled = false, + tooltip, +}: CheckBoxProps) => { return ( - - onChange(!value)} /> - + + onChange(!value)} + disabled={disabled} + /> + ); };