Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added level progress labels #72

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wanikani/WanikaniHistory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function LoadableChart({placeholderTitle, children}) {
const [isLoaded, setIsLoaded] = useState(false)
return (
<ReactVisibilitySensor partialVisibility={true} onChange={(isVisible) => isVisible ? setIsLoaded(true) : null}>
<Card variant={'outlined'} style={{margin: '15px'}}>
<Card style={{margin: '15px'}}>
{isLoaded ? children : (
<div style={{height: '500px', textAlign: 'center'}}>
<Typography variant={'h5'}>{placeholderTitle}</Typography>
Expand Down
32 changes: 4 additions & 28 deletions src/wanikani/components/WanikaniFutureReviewsChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,6 @@ async function fetchFutureReviews() {
return data.filter(assignment => !assignment.data['burned_at'] || !assignment.data['available_at']);
}

function SimpleBar({x, y, width, offsetY, fill, style}) {
const path = `M ${x} ${offsetY}
L ${width + x} ${offsetY}
L ${width + x} ${y}
L ${x} ${y}
Z`;

return (
<path d={path}
fill={fill}
style={style}
/>
);
}

function WanikaniFutureReviewsChart() {
const [rawData, setRawData] = useState([]);
const [initialReviewCount, setInitialReviewCount] = useState(0);
Expand Down Expand Up @@ -213,24 +198,15 @@ function WanikaniFutureReviewsChart() {
return false;
}

function BarWithLabel({
arg, barWidth, maxBarWidth, val, startVal,
color, value, style, seriesIndex, index
}) {
function BarWithLabel(props) {
const {arg, val, value, seriesIndex, index} = props;

if (value === 0)
return (<></>);

const width = maxBarWidth * barWidth;

return (
<>
<SimpleBar x={arg - width / 2}
width={width}
y={val}
offsetY={startVal}
fill={color}
style={style}
/>
<BarSeries.Point {...props}/>

{isLabelVisible(seriesIndex, index) ? (
<Chart.Label
Expand Down
61 changes: 60 additions & 1 deletion src/wanikani/components/WanikaniLevelProgressChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ import {useState, useEffect} from "react";
import WanikaniApiService from "../service/WanikaniApiService.js";
import {EventTracker, Animation} from "@devexpress/dx-react-chart";

function LevelToolTip({text}) {
function parseTimestamp(text) {
const millis = parseFloat(text) * 86400000;
const hours = Math.floor((millis % 86400000) / 3600000);
const days = Math.floor(parseFloat(text));
return {
hours,
days
};
}

function LevelToolTip({text}) {
const {days, hours} = parseTimestamp(text);
return (
<div>
Days: {days}
Expand Down Expand Up @@ -44,6 +52,56 @@ function WanikaniLevelProgressChart() {
return () => isSubscribed = false;
}, []);

function BarWithLabel(props) {
const {arg, val, index} = props;
const {days, hours} = parseTimestamp(levelProgress[index].days)
const [animationStyle, setAnimationStyle] = useState({opacity: '0'});
const animationInitialDelay = 1_000;

useEffect(() => {
const startAnimation = setTimeout(() => {
setAnimationStyle({
opacity: '1',
transition: 'opacity 1s'
});
}, animationInitialDelay)

return () => clearTimeout(startAnimation);
}, []);

return (
<>
<BarSeries.Point {...props}
animation={(startCoords, endCoords, processAnimation, setAttributes, delay) => {
console.log({startCoords, endCoords, processAnimation, setAttributes, delay})
return props.animation(startCoords, endCoords, processAnimation, setAttributes, delay)
}}/>

{days > 0 ? (
<Chart.Label
x={arg}
y={val - 25}
textAnchor={'middle'}
style={{...animationStyle, fill: 'white', fontWeight: 'bold'}}
>
{days} d
</Chart.Label>
) : null}

{hours > 0 ? (
<Chart.Label
x={arg}
y={val - 10}
textAnchor={'middle'}
style={{...animationStyle, fill: 'white', fontWeight: 'bold'}}
>
{hours} h
</Chart.Label>
) : null}
</>
);
}

return (
<Chart data={levelProgress}>
<ValueAxis/>
Expand All @@ -52,6 +110,7 @@ function WanikaniLevelProgressChart() {
<BarSeries
valueField="days"
argumentField="level"
pointComponent={BarWithLabel}
/>
<EventTracker/>
<Animation/>
Expand Down