Skip to content

Commit

Permalink
Change NDArray to have fixed dot position and minus sign
Browse files Browse the repository at this point in the history
  • Loading branch information
irdkwmnsb committed May 14, 2024
1 parent d7f1cd0 commit a4902d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/components/array/ndarray.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
color: #777;
tr {
td {
text-align: center;
border: 1px solid black;
}
}
Expand All @@ -19,4 +20,19 @@
}
}
}
}

.numberCell {
width: 100%;
display: flex;
.whole {
flex-grow: 1;
text-align: end;
}
.dot {

}
.rational {

}
}
17 changes: 16 additions & 1 deletion src/components/array/ndarray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ type VisNDArrayProps<T> = {
}
: unknown)

const NumberCell = ({value, roundTo} : {value: number, roundTo: number}) => {
const whole = Math.floor(value)
const rational = Math.round((Math.abs(value) % 1) * Math.pow(10, roundTo))
return <div className={styles.numberCell}>
<div className={styles.whole}>{(whole + "").replace("-", "−")}</div>
<div className={styles.dot}>.</div>
<div className={styles.rational} style={{
width: roundTo + "ch"
}}>{rational}</div>
</div>
}

export const VisNDArray = <T extends RenderableData,>(props: VisNDArrayProps<T>) => {
const maxElems = props.array.reduce((a, b) => Math.max(a, b.length), 0)
return <div>
Expand All @@ -34,7 +46,10 @@ export const VisNDArray = <T extends RenderableData,>(props: VisNDArrayProps<T>)
{row.map((el, colIndex) => (
<td key={rowIndex + "-" + colIndex}>
{
typeof el === "number" ? el.toFixed((props as VisNDArrayProps<typeof el>).roundTo ?? 2) : el
typeof el === "number" ?
<NumberCell value={el} roundTo={(props as VisNDArrayProps<typeof el>).roundTo ?? 2}/>
:
el
}
</td>
))}
Expand Down

0 comments on commit a4902d7

Please sign in to comment.