Skip to content

Commit

Permalink
Chart UI fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jiji14 committed Apr 25, 2024
1 parent 18e99bb commit 2281c7e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 29 deletions.
1 change: 1 addition & 0 deletions www/js/appTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const AppTheme = {
silver: '#d9d9d9',
skyblue: '#7fcaea',
navy: '#0077aa',
orange: '#f6a063',
},
roundness: 5,
};
Expand Down
11 changes: 8 additions & 3 deletions www/js/components/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export type Props = {
isHorizontal?: boolean;
timeAxis?: boolean;
stacked?: boolean;
hideLegend?: boolean;
showLegend?: boolean;
reverse?: boolean;
enableTooltip?: boolean;
};
const Chart = ({
records,
Expand All @@ -45,8 +46,9 @@ const Chart = ({
isHorizontal,
timeAxis,
stacked,
hideLegend = false,
showLegend = true,
reverse = true,
enableTooltip = true,
}: Props) => {
const { colors } = useTheme();
const [numVisibleDatasets, setNumVisibleDatasets] = useState(1);
Expand Down Expand Up @@ -201,7 +203,10 @@ const Chart = ({
},
plugins: {
legend: {
display: hideLegend,
display: showLegend,
},
tooltip: {
enabled: enableTooltip,
},
...(lineAnnotations?.length && {
annotation: {
Expand Down
46 changes: 41 additions & 5 deletions www/js/metrics/SurveyDoughnutCharts.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
import React from 'react';
import { View, Text } from 'react-native';
import { Icon } from 'react-native-paper';
import { useTranslation } from 'react-i18next';
import { useAppTheme } from '../appTheme';
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js';
import { Doughnut } from 'react-chartjs-2';

ChartJS.register(ArcElement, Tooltip, Legend);

export const LabelPanel = ({ first, second }) => {
const { colors } = useAppTheme();

return (
<View style={styles.labelWrapper}>
<View style={styles.labelItem}>
<View style={{ backgroundColor: colors.navy, width: 20, height: 20 }} />
<Text>{first}</Text>
</View>
<View style={styles.labelItem}>
<View style={{ backgroundColor: colors.orange, width: 20, height: 20 }} />
<Text>{second}</Text>
</View>
</View>
);
};

const SurveyDoughnutCharts = () => {
const { colors } = useAppTheme();
const { t } = useTranslation();
const myResonseRate = 68;
const othersResponseRate = 41;

const renderDoughnutChart = (rate) => {
const renderDoughnutChart = (rate, chartColor, myResponse) => {
const data = {
datasets: [
{
data: [rate, 100 - rate],
backgroundColor: [colors.navy, colors.silver],
borderColor: [colors.navy, colors.silver],
backgroundColor: [chartColor, colors.silver],
borderColor: [chartColor, colors.silver],
borderWidth: 1,
},
],
};
return (
<View style={{ position: 'relative' }}>
<View style={styles.textWrapper}>
{myResponse ? (
<Icon source="account" size={40} />
) : (
<Icon source="account-group" size={40} />
)}
<Text>{rate}%</Text>
</View>
<Doughnut
Expand All @@ -53,9 +76,10 @@ const SurveyDoughnutCharts = () => {
<View>
<Text style={styles.chartTitle}>{t('main-metrics.survey-response-rate')}</Text>
<View style={styles.chartWrapper}>
{renderDoughnutChart(myResonseRate)}
{renderDoughnutChart(othersResponseRate)}
{renderDoughnutChart(myResonseRate, colors.navy, true)}
{renderDoughnutChart(othersResponseRate, colors.orange, false)}
</View>
<LabelPanel first={t('main-metrics.you')} second={t('main-metrics.others')} />
</View>
);
};
Expand All @@ -71,6 +95,7 @@ const styles: any = {
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
marginBottom: 20,
},
textWrapper: {
position: 'absolute',
Expand All @@ -80,6 +105,17 @@ const styles: any = {
alignItems: 'center',
justifyContent: 'center',
},
labelWrapper: {
alignSelf: 'center',
display: 'flex',
gap: 10,
},
labelItem: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
gap: 10,
},
};

export default SurveyDoughnutCharts;
21 changes: 7 additions & 14 deletions www/js/metrics/SurveyLeaderboardCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@ const SurveyLeaderboardCard = () => {
const [tab, setTab] = useState('leaderboard');
const myLabel = '#3';

const getLeaderboardLabelColor = (l) => {
if (l === myLabel) {
return colors.skyblue;
} else {
return colors.silver;
}
};

const renderBarChart = () => {
const records = [
{ label: '#1', x: 91, y: '#1: 🏆' },
{ label: '#2', x: 72, y: '#2: 🥈' },
{ label: '#3', x: 68, y: '#3: 🥉' },
{ label: '#1', x: 91, y: '🏆 #1:' },
{ label: '#2', x: 72, y: '🥈 #2:' },
{ label: '#3', x: 68, y: '🥉 #3:' },
{ label: '#4', x: 57, y: '#4:' },
{ label: '#5', x: 50, y: '#5:' },
{ label: '#6', x: 40, y: '#6:' },
Expand All @@ -41,10 +33,11 @@ const SurveyLeaderboardCard = () => {
isHorizontal={true}
timeAxis={false}
stacked={false}
getColorForLabel={(l) => getLeaderboardLabelColor(l)}
getColorForChartEl={(l) => getLeaderboardLabelColor(l)}
hideLegend={true}
getColorForLabel={(l) => (l === myLabel ? colors.skyblue : colors.silver)}
getColorForChartEl={(l) => (l === myLabel ? colors.skyblue : colors.silver)}
showLegend={false}
reverse={false}
enableTooltip={false}
/>
<View style={styles.statusTextWrapper}>
<Text>{t('main-metrics.you-are-in')}</Text>
Expand Down
44 changes: 37 additions & 7 deletions www/js/metrics/SurveyTripCategoriesCard.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
import React from 'react';
import { View, Text } from 'react-native';
import { Card } from 'react-native-paper';
import { cardStyles } from './MetricsTab';
import { useTranslation } from 'react-i18next';
import BarChart from '../components/BarChart';
import { useAppTheme } from '../appTheme';
import { LabelPanel } from './SurveyDoughnutCharts';

const SurveyTripCategoriesCard = () => {
const { colors } = useAppTheme();
const { t } = useTranslation();
const records = [
{ label: 'EV Roaming trip', x: 'EV Roaming trip', y: 91 },
{ label: 'EV Return trip', x: 'EV Return trip', y: 72 },
{ label: 'Gas Car trip', x: 'Gas Car trip', y: 68 },
{
label: 'Response',
x: 'EV Roaming trip',
y: 20,
},
{
label: 'No Response',
x: 'EV Roaming trip',
y: 20,
},
{
label: 'Response',
x: 'EV Return trip',
y: 30,
},
{
label: 'No Response',
x: 'EV Return trip',
y: 40,
},
{
label: 'Response',
x: 'Gas Car trip',
y: 50,
},
{
label: 'No Response',
x: 'Gas Car trip',
y: 10,
},
];

return (
Expand All @@ -30,12 +59,13 @@ const SurveyTripCategoriesCard = () => {
axisTitle=""
isHorizontal={false}
timeAxis={false}
stacked={false}
getColorForLabel={() => colors.navy}
getColorForChartEl={() => colors.navy}
hideLegend={true}
stacked={true}
getColorForLabel={(l) => (l === 'Response' ? colors.navy : colors.orange)}
getColorForChartEl={(l) => (l === 'Response' ? colors.navy : colors.orange)}
showLegend={false}
reverse={false}
/>
<LabelPanel first={t('main-metrics.response')} second={t('main-metrics.no-response')} />
</Card.Content>
</Card>
);
Expand Down

0 comments on commit 2281c7e

Please sign in to comment.