简体   繁体   English

折线图上的动态线反应js

[英]Dynamically line on line chart react js

I'm trying to make dynamically line on line chart React.JS.我正在尝试在折线图 React.JS 上动态制作线条。 For example, if you choose year from 2019 - 2019 you can get only 1 line on graph.例如,如果您从 2019 年到 2019 年选择年份,您只能在图表上获得 1 条线。 But, if you choose 2019 - 2021 you can get 3 line on your graph.但是,如果您选择 2019 - 2021,您可以在图表上获得 3 条线。 But, I don't know how to realize it.但是,我不知道如何实现它。 Anyone know how to make line dynamically?有人知道如何动态制作线条吗?

{params.get("type") === "tahun" ? (<>{
      bayarBpjs.length != 0 && bayarBpjs.map(
        (_,index) => {
          return <><Link
            to="/detail-pemasukan"
            state={{
              labels: label,
              crumb: "Keuangan Tren\tPemasukan Detail",
              title: "Detail Tren Pemasukan Poli",
            }}
          >
            <div className="tren-pemasukan__tren__chart">
              
              <LineChart data={{
                labels: [],
                datasets: [
                  {
                    label: poli,
                    data: bayarBpjs[index],
                    borderColor: "green",
                    backgroundColor: "green",
                  },
                ],
              }} options={options(tanggal[index])}/>
              {isLoading}
            </div>
          </Link></>
        }
      )
      }

You could store an array of years selected in state.您可以存储在 state 中选择的年份数组。 Then map over them and render a LineChart for each element in the map.然后 map 覆盖它们,并为 map 中的每个元素渲染一个LineChart

Something like:就像是:

const [selectedDates, setSelectedDates] = useState([])

// set the selected date ranges on user input or however you're doing it
<Button onClick={(dates)=> setSelectedDates(dates)} />

// render your LineGraphs by mapping over the selectedDates 
return (
{selectedDates.map((date) => 
  <div>
    <LineChart .../>
  </div>
))};
)


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM