小吃我在本机反应中创建了一个条形图,使用户能够选择月份,并根据他们的选择呈现图表。然而,到目前为止我所做的只是更改 x 轴上的月份标签。我在条形图本身上实现用户选择时遇到问题。请参阅下文 -变量数据集的定义方式与官方文档中的方式相同。必须为每条条线定义颜色。我在想,如果可以在其中包含一个 for 循环,根据标签的数量,月份过滤器也许会起作用。我正在使用 applyDateFilter() 来调整图表中的标签。import {Chart,LineChart,BarChart,PieChart,ProgressChart,ContributionGraph} from 'react-native-chart-kit'import React, { Component } from 'react';import {useState, useEffect, useCallback} from 'react';import { View, Text, StyleSheet, ImageBackground, Icon, FlatList, Button, TextInput, Dimensions, SafeAreaView, Picker, Alert } from 'react-native';const initialData = [12, 19, 12, 25, 22, 10];const initialFrom = "0"const initialToMonth = "7"const months = [ { month: "Jan", value: "0" }, { month: "Feb", value: "1" }, { month: "Mar", value: "2" }, { month: "April", value: "3" }, { month: "May", value: "4" }, { month: "June", value: "5" }, ];const initialLevelsArr = [ "Jan", "Feb", "Mar", "April", "May", "June", ];const initialLabels = ["Jan", "Feb", "Mar", "April", "May", "June"];export default function FocusScreen() {const [datas, setDatas] = useState(initialData);const [from, setFrom] = useState(initialFrom);const [toMonth, setToMonth] = useState(initialToMonth);const [labels, setLabels] = useState(initialLabels);const applyDateFilter = () => { const newLabels = initialLevelsArr.slice( parseInt(from), parseInt(toMonth) + 1 ); setLabels(newLabels); console.log(labels) }const dataset = { labels: labels, datasets: [ { data: datas, colors: [ (opacity = 1) => `red`, (opacity = 1) => `blue`, (opacity = 1) => `yellow`, (opacity = 1) => `green`, (opacity = 1) => `purple`, (opacity = 1) => `orange` ] } ] }
1 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
尝试这个:
const applyDateFilter = () => {
const newLabels = initialLevelsArr.slice(
parseInt(from),
parseInt(toMonth) + 1
);
const newDatas = initialData.slice(
parseInt(from),
parseInt(toMonth) + 1
);
setLabels(newLabels);
setDatas(newDatas);
}
这样,过滤器也将应用于数据,而不仅仅是标签。
添加回答
举报
0/150
提交
取消