react-google-charts: 显示数字而不是百分比

huangapple go评论75阅读模式
英文:

react-google-charts: displaying number not percentage

问题

我有一个包含多个接口的项目,其中有一个用于显示统计数据的接口,

我使用以下库:

react-google-charts

但问题是数字显示为百分比,我希望它显示为数字:

react-google-charts: 显示数字而不是百分比

这意味着我需要数字'3'显示出来,

这个文件包含了使用react-google-charts库的分析图表,

import { Button, Card, Col, Row, Space } from 'antd';
import { FunctionComponent, useContext, useEffect, useState } from 'react';
import ReactChart from '../../common/chart';
import { typeChart } from '../../common/chart/bar-chart/data/enum';
import '../../../styles/dashboard/index.scss';
import { AuthContext, IAuthContext } from '../../../contexts/auth-context';
import colors from '../../../constants/colors';
import calendar1 from '../../../assets/icons/calendar1-icon.svg';
import calendar2 from '../../../assets/icons/calendar2-icon.svg';
import calendar3 from '../../../assets/icons/calendar3-icon.svg';
import thermometer from '../../../assets/icons/thermometer-icon.svg';
import waving from '../../../assets/icons/waving-hand-icon.svg';
import { useNavigate } from 'react-router';
import { FormattedMessage } from 'react-intl';
import { useQuery } from 'react-query';
import statisticCharts from '../../../api/nuclearMedicineApi/services/Statistic';
import { BsFillSquareFill } from 'react-icons/bs';
import { Chart } from "react-google-charts";
import schedule from '../../../api/nuclearMedicineApi/services/Schedule';
import { MachineCategory } from '../../../constants/enums';

interface DashboardProps { }
const Dashboard: FunctionComponent<DashboardProps> = () => {
    const auth = useContext<IAuthContext>(AuthContext);

    // **************************************************************

    const schedulePatientCount = useQuery('schedulePatientCount', () =>
        schedule.SchedulePatientCountGetAllLite({
            machineCategory: MachineCategory.Analysiss,
        }),
    ).data;

    var analysisPatientCount: any[] = [['', '']];

    const [analysisCountPatient, setAnalysisCountPatient] = useState<any[]>([['', '']])

    useEffect(() => {

        // ****** 10 ****** 
        schedulePatientCount.map((ap: any, index: any) => {
            if (ap?.count !== undefined && ap?.machine.label !== undefined) {
                if (ap?.count !== 0) {
                    let xyData: any[] = [ap?.machine?.label, ap?.count];
                    analysisPatientCount.push(xyData);
                    setAnalysisCountPatient(analysisPatientCount);
                    return analysisPatientCount;
                }
            }
        })

    }, [chartsQueryTopographyData, chartsQueryCityData, chartsQueryAgeData]);

    console.log('cfcfcf: ', analysisCountPatient);

    const options = {
        is3D: true,
    };

    // **************************************************************
    const current = new Date();
    const date = `${current.getFullYear()}/${current.getMonth() + 1
        }/${current.getDate()}`;
    const navigate = useNavigate();
    return (
        <>
            <div className='dashdoard-donutData'>
                {/* 10 charts */}
                <Row>
                    <Col className='mt-4' lg={12}>
                        <Card>
                            <Row>
                                <Col lg={19}>
                                    <h3>
                                        <FormattedMessage id='number-of-cancer-cases-by-city' />{' '}
                                    </h3>
                                </Col>
                            </Row>
                            <Row>
                                <Col lg={24}>
                                    <Chart
                                        chartType="PieChart"
                                        data={analysisCountPatient}
                                        options={options}
                                        width="100%"
                                        height="400px"
                                    />
                                </Col>
                            </Row>
                        </Card>
                    </Col>
                </Row>
            </div>
        </>
    );
};

export default Dashboard;
英文:

I have a project that contains several interfaces, and among these interfaces there is an interface to display statistics,

And i use the following library:

react-google-charts

But the problem is that the number appears as a percentage and I want it to appear as a number:

react-google-charts: 显示数字而不是百分比

that means i need the '3' number to appear,

this file contains analysis chart with react-google-charts library,

import { Button, Card, Col, Row, Space } from &#39;antd&#39;;
import { FunctionComponent, useContext, useEffect, useState } from &#39;react&#39;;
import ReactChart from &#39;../../common/chart&#39;;
import { typeChart } from &#39;../../common/chart/bar-chart/data/enum&#39;;
import &#39;../../../styles/dashboard/index.scss&#39;;
import { AuthContext, IAuthContext } from &#39;../../../contexts/auth-context&#39;;
import colors from &#39;../../../constants/colors&#39;;
import calendar1 from &#39;../../../assets/icons/calendar1-icon.svg&#39;;
import calendar2 from &#39;../../../assets/icons/calendar2-icon.svg&#39;;
import calendar3 from &#39;../../../assets/icons/calendar3-icon.svg&#39;;
import thermometer from &#39;../../../assets/icons/thermometer-icon.svg&#39;;
import waving from &#39;../../../assets/icons/waving-hand-icon.svg&#39;;
import { useNavigate } from &#39;react-router&#39;;
import { FormattedMessage } from &#39;react-intl&#39;;
import { useQuery } from &#39;react-query&#39;;
import statisticCharts from &#39;../../../api/nuclearMedicineApi/services/Statistic&#39;;
import { BsFillSquareFill } from &#39;react-icons/bs&#39;;
import { Chart } from &quot;react-google-charts&quot;;
import schedule from &#39;../../../api/nuclearMedicineApi/services/Schedule&#39;;
import { MachineCategory } from &#39;../../../constants/enums&#39;;
interface DashboardProps { }
const Dashboard: FunctionComponent&lt;DashboardProps&gt; = () =&gt; {
const auth = useContext&lt;IAuthContext&gt;(AuthContext);
// **************************************************************
const schedulePatientCount = useQuery(&#39;schedulePatientCount&#39;, () =&gt;
schedule.SchedulePatientCountGetAllLite({
machineCategory: MachineCategory.Analysiss,
}),
).data;
var analysisPatientCount: any[] = [[&#39;&#39;, &#39;&#39;]];
const [analysisCountPatient, setAnalysisCountPatient] = useState&lt;any[]&gt;([[&#39;&#39;, &#39;&#39;]])
useEffect(() =&gt; {
// ****** 10 ****** 
schedulePatientCount.map((ap: any, index: any) =&gt; {
if (ap?.count !== undefined &amp;&amp; ap?.machine.label !== undefined) {
if (ap?.count !== 0) {
let xyData: any[] = [ap?.machine?.label, ap?.count];
analysisPatientCount.push(xyData);
setAnalysisCountPatient(analysisPatientCount);
return analysisPatientCount;
}
}
})
}, [chartsQueryTopographyData, chartsQueryCityData, chartsQueryAgeData]);
console.log(&#39;cfcfcf: &#39;, analysisCountPatient);
const options = {
is3D: true,
};
// **************************************************************
const current = new Date();
const date = `${current.getFullYear()}/${current.getMonth() + 1
}/${current.getDate()}`;
const navigate = useNavigate();
return (
&lt;&gt;
&lt;div className=&#39;dashdoard-donutData&#39;&gt;
{/* 10 charts */}
&lt;Row&gt;
&lt;Col className=&#39;mt-4&#39; lg={12}&gt;
&lt;Card&gt;
&lt;Row&gt;
&lt;Col lg={19}&gt;
&lt;h3&gt;
&lt;FormattedMessage id=&#39;number-of-cancer-cases-by-city&#39; /&gt;{&#39; &#39;}
&lt;/h3&gt;
&lt;/Col&gt;
&lt;/Row&gt;
&lt;Row&gt;
&lt;Col lg={24}&gt;
&lt;Chart
chartType=&quot;PieChart&quot;
data={analysisCountPatient}
options={options}
width={&quot;100%&quot;}
height={&quot;400px&quot;}
/&gt;
&lt;/Col&gt;
&lt;/Row&gt;
&lt;/Card&gt;
&lt;/Col&gt;
&lt;/Row&gt;
&lt;/div&gt;
&lt;/&gt;
);
};
export default Dashboard;

答案1

得分: 1

You can add pieSliceText: 'value' to your options to display values instead of percentages, if you want to display the items with value "0" you can set the "sliceVisibilityThreshold" option as mentioned in the following example below:

import React from "react";
import { Chart } from "react-google-charts";

export const data = [
  ["Task", "Hours per Day"],
  ["Work", 11],
  ["Eat", 2],
  ["Commute", 2],
  ["Watch TV", 2],
  ["Sleep", 7],
  ["Null", 0]
];

export const options = {
  title: "My Daily Activities",
  is3D: true,
  pieSliceText: 'value',
  sliceVisibilityThreshold: 0
};

export function App() {
  return (
    <Chart
      chartType="PieChart"
      data={data}
      options={options}
      width="100%"
      height="400px"
    />
  );
}

the result will be as the following:

react-google-charts: 显示数字而不是百分比

英文:

You can add pieSliceText: &#39;value&#39; to your options to display values instead of percentages, if you want to display the items with value &quot;0&quot; you can set the &quot;sliceVisibilityThreshold&quot; option as mentioned in the following example below:

import React from &quot;react&quot;;
import { Chart } from &quot;react-google-charts&quot;;
export const data = [
[&quot;Task&quot;, &quot;Hours per Day&quot;],
[&quot;Work&quot;, 11],
[&quot;Eat&quot;, 2],
[&quot;Commute&quot;, 2],
[&quot;Watch TV&quot;, 2],
[&quot;Sleep&quot;, 7],
[&quot;Null&quot; , 0 ]
];
export const options = {
title: &quot;My Daily Activities&quot;,
is3D: true,
pieSliceText : &#39;value&#39;,
sliceVisibilityThreshold: 0
};
export function App() {
return (
&lt;Chart
chartType=&quot;PieChart&quot;
data={data}
options={options}
width={&quot;100%&quot;}
height={&quot;400px&quot;}
/&gt;
);
}

the result will be as the following:

react-google-charts: 显示数字而不是百分比

huangapple
  • 本文由 发表于 2023年2月8日 18:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定