【问题标题】:React Native Chart Kit - BarChart color fading out of each bar from top to bottomReact Native Chart Kit - 条形图颜色从上到下淡出每个条形图
【发布时间】:2020-10-26 07:17:12
【问题描述】:

我正在使用 react native chart kit yarn add react-native-chart-kit 来可视化数据,这是我的代码:

import React from "react";
import { View, Text, Dimensions } from "react-native";
import RandomColor from "randomcolor";
import {
  LineChart,
  BarChart,
  PieChart,
  ProgressChart,
  ContributionGraph,
  StackedBarChart,
} from "react-native-chart-kit";

const Chart = ({ classList }) => {
  return (
    <View>
      <Text>Bezier Line Chart</Text>
      <BarChart
        data={{
          labels: classList.map((classItem) => {
            return classItem.classname;
          }),
          datasets: [
            {
              data: classList.map((classItem) => {
                return classItem.studentList.length;
              }),
            },
          ],
        }}
        width={Dimensions.get("window").width} 
        height={220}
        yAxisInterval={1} 
        chartConfig={{
          backgroundColor: "#ffffff",
          backgroundGradientFrom: "#ffffff",
          backgroundGradientTo: "#ffffff",
          decimalPlaces: 2, 
          color: () => "blue",
          labelColor: () => "black",
          style: {
            borderRadius: 16,
          },
          propsForDots: {
            r: "6",
            strokeWidth: "1",
            stroke: "#ffa726",
          },
        }}
        bezier
        style={{
          marginVertical: 8,
          borderRadius: 16,
        }}
      />
    </View>
  );
};

export default Chart;

这就是它的返回方式:

如您所见,它有效,但并不完美。每个条的末端颜色从上到下逐渐淡出,与白色背景颜色不相配。所以,我想让每个条形的颜色都是实心的。有什么办法可以解决吗?

【问题讨论】:

标签: javascript reactjs react-native mobile frontend


【解决方案1】:

使用flatColor={true} & withCustomBarColorFromData={true}

喜欢这个

 const data = {
        labels: ["label1", "label2", "label3"],
        datasets: [{
                data: [50, 45, 28],
                colors: [
                    () => "blue"
                ]}]
 };
<BarChart
                
     data={data}
     width={Dimensions.get("window").width - 20}
     height={220}
     chartConfig={{
       //....
     }}
     withCustomBarColorFromData={true}
     flatColor={true}
  />

【讨论】:

    【解决方案2】:

    使用上述答案后flatColor={true} & withCustomBarColorFromData={true}

    如果您希望数据集中的颜色是动态的,请使用以下代码:

     const data = {
            labels: ["label1", "label2", "label3"],
            datasets: [
                      {
                         data: [50, 45, 28],
                        colors: data.colorName.map(item => {
                          return () => item.color;
                        }),
                      },
                    ],
     };
    

    【讨论】:

    • 答案主要是复制粘贴另一个答案。它不回答实际问题,而是向其中添加附加信息。
    • 嘿@AxelMeier,我只想说如何使用上面的答案传递颜色动态。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多