Move from react-native-gifted-charts to react-native-svg-charts

This commit is contained in:
Brandon Presley 2022-07-09 12:51:13 +12:00
parent 7a5ff97dfc
commit 830ad980fc
2 changed files with 43 additions and 36 deletions

View File

@ -1,9 +1,10 @@
import React, {useContext, useEffect, useState} from 'react'; import React, {useContext, useEffect, useState} from 'react';
import {useColorScheme} from 'react-native'; import {useColorScheme, View} from 'react-native';
import {LineChart} from 'react-native-gifted-charts';
import {Button, Dialog, Portal} from 'react-native-paper'; import {Button, Dialog, Portal} from 'react-native-paper';
import {DatabaseContext} from './App'; import {DatabaseContext} from './App';
import Best from './best'; import Best from './best';
import {AreaChart, Grid, LineChart, YAxis} from 'react-native-svg-charts';
import * as shape from 'd3-shape';
export default function ViewBest({ export default function ViewBest({
best, best,
@ -12,52 +13,56 @@ export default function ViewBest({
best?: Best; best?: Best;
setBest: (best?: Best) => void; setBest: (best?: Best) => void;
}) { }) {
const [data, setData] = useState< const [data, setData] = useState<number[]>([]);
{value: number; label: string; labelComponent: any}[]
>([]);
const [labels, setLabels] = useState<string[]>([]); const [labels, setLabels] = useState<string[]>([]);
const [unit, setUnit] = useState<string>();
const db = useContext(DatabaseContext); const db = useContext(DatabaseContext);
const dark = useColorScheme() === 'dark'; const dark = useColorScheme() === 'dark';
const selectBest = `
SELECT max(weight) AS weight, STRFTIME('%Y-%m-%d', created) as created
FROM sets
WHERE name = ?
GROUP BY name, STRFTIME('%Y-%m-%d', created)
`;
const refresh = async () => {
const [result] = await db.executeSql(selectBest, [best?.name]);
if (result.rows.length === 0) return;
console.log('ViewBest.refresh', result.rows.raw());
setData(
result.rows.raw().map(row => ({
value: row.weight,
label: row.created,
labelComponent: () => null,
})),
);
};
useEffect(() => { useEffect(() => {
const selectBest = `
SELECT max(weight) AS weight, STRFTIME('%Y-%m-%d', created) as created, unit
FROM sets
WHERE name = ?
GROUP BY name, STRFTIME('%Y-%m-%d', created)
`;
const refresh = async () => {
const [result] = await db.executeSql(selectBest, [best?.name]);
if (result.rows.length === 0) return;
console.log(`${ViewBest.name}.${refresh.name}:`, result.rows.raw());
setData(result.rows.raw().map(row => row.weight));
setUnit(result.rows.item(0).unit);
};
refresh(); refresh();
}, [best]); }, [best]);
const contentInset = {top: 20, bottom: 20};
return ( return (
<Portal> <Portal>
<Dialog visible={!!best} onDismiss={() => setBest(undefined)}> <Dialog visible={!!best} onDismiss={() => setBest(undefined)}>
<Dialog.Title>{best?.name}</Dialog.Title> <Dialog.Title>{best?.name}</Dialog.Title>
<Dialog.Content> <Dialog.Content>
<LineChart <View style={{height: 200, flexDirection: 'row'}}>
data={data} <YAxis
curved data={data}
isAnimated contentInset={contentInset}
yAxisLabelSuffix="kg" svg={{
color={dark ? '#3498db' : 'black'} fill: 'grey',
dataPointsColor={dark ? '#f1c40f' : 'black'} fontSize: 10,
thickness={5} }}
width={240} numberOfTicks={10}
/> formatLabel={value => `${value}${unit}`}
/>
<LineChart
style={{flex: 1, marginLeft: 16}}
data={data}
svg={{stroke: 'rgb(134, 65, 244)'}}
curve={shape.curveNatural}
contentInset={contentInset}>
<Grid />
</LineChart>
</View>
</Dialog.Content> </Dialog.Content>
<Dialog.Actions> <Dialog.Actions>
<Button icon="close" onPress={() => setBest(undefined)}> <Button icon="close" onPress={() => setBest(undefined)}>

View File

@ -15,7 +15,9 @@
"@react-navigation/material-top-tabs": "^6.2.1", "@react-navigation/material-top-tabs": "^6.2.1",
"@react-navigation/native": "^6.0.10", "@react-navigation/native": "^6.0.10",
"@react-navigation/native-stack": "^6.6.2", "@react-navigation/native-stack": "^6.6.2",
"@types/d3-shape": "^3.1.0",
"@types/react-native-sqlite-storage": "^5.0.2", "@types/react-native-sqlite-storage": "^5.0.2",
"@types/react-native-svg-charts": "^5.0.12",
"@types/react-native-vector-icons": "^6.4.11", "@types/react-native-vector-icons": "^6.4.11",
"babel-plugin-transform-remove-console": "^6.9.4", "babel-plugin-transform-remove-console": "^6.9.4",
"date-fns": "^2.28.0", "date-fns": "^2.28.0",
@ -23,7 +25,6 @@
"react-devtools": "^4.24.7", "react-devtools": "^4.24.7",
"react-native": "0.69.1", "react-native": "0.69.1",
"react-native-gesture-handler": "^2.5.0", "react-native-gesture-handler": "^2.5.0",
"react-native-gifted-charts": "^1.2.41",
"react-native-linear-gradient": "^2.6.2", "react-native-linear-gradient": "^2.6.2",
"react-native-pager-view": "^5.4.24", "react-native-pager-view": "^5.4.24",
"react-native-paper": "^4.12.2", "react-native-paper": "^4.12.2",
@ -32,6 +33,7 @@
"react-native-screens": "^3.14.0", "react-native-screens": "^3.14.0",
"react-native-sqlite-storage": "^6.0.1", "react-native-sqlite-storage": "^6.0.1",
"react-native-svg": "^12.3.0", "react-native-svg": "^12.3.0",
"react-native-svg-charts": "^5.4.0",
"react-native-tab-view": "^3.1.1", "react-native-tab-view": "^3.1.1",
"react-native-vector-icons": "^9.2.0" "react-native-vector-icons": "^9.2.0"
}, },