反应自定义颜色图表

React custom colors chart

我需要在 React 中为图表设置自定义颜色。 我正在使用库 apexcharts。 这是我在我的应用程序中用于另一个页面的图表配置文件。

import React from "react";
import Chart from "react-apexcharts";

export default function CChart(props) {

    let options = {
        theme: {
            palette: 'palette' + props.palette,

        },
        chart: {
            width: '100%',
            id: "basic-bar",
            toolbar: {
                show: false
            }
        },
        fill: {
            gradient: {
                shade: 'light',
                type: "horizontal",
                shadeIntensity: 0.5,
                gradientToColors: undefined,
                inverseColors: false,
                opacityFrom: 0.5,
                opacityTo: 1,
                stops: [0, 50, 100],
                colorStops: []
            },
        },
        title: {
            text: props.title
        },
        
        xaxis: {
            categories: props.data,
            labels: {
                style: {
                    colors: [],
                    fontSize: '10px',
                    fontFamily: 'Arial',
                    fontWeight: 400,
                    cssClass: 'apexcharts-xaxis-label',
                },
            }
        }
    };

    const series = [];

    if (props.series) {
        props.series.forEach(serie => {
            series.push({
                name: serie.name,
                data: serie.data.map(d => d[[props.product]])
            });
        });
    }

    return (
        <Chart
            options={options}
            series={series}
            type="line"
            width="500"
        />
    );
}

这是我显示图表的地方:

 <Grid container item md={12} style={style.charts} direction="column">
            <Grid item md={6} style={style.boxChart}>
                <CChart
                    title="Evolución Huella hídrica"
                    data={seasons.map(s => s.name)}
                    series={waterFootprintEvolution}
                    seriesLabel="Litros"
                    product="water_footprint"
                    palette="2"
                />

            </Grid>

我只需要更改一张图表的颜色,而不是另一张使用此模板的图表的颜色。 这是我要设置自定义颜色的图表

这是使用调色板的图表:

我是新手,所以任何帮助都会很棒。

非常感谢

对于有问题的图表组件,你能改变调色板的颜色吗?每个组件可以有不同的调色板颜色。 像这样:

 <Grid container item md={12} style={style.charts} direction="column">
            <Grid item md={6} style={style.boxChart}>
                <CChart
                    title="Evolución Huella hídrica"
                    data={seasons.map(s => s.name)}
                    series={waterFootprintEvolution}
                    seriesLabel="Litros"
                    product="water_footprint"
                    palette="5"
                />

            </Grid>

我通过在我的图表中添加一个新的 属性 解决了这个问题

 <CChart
                    title="Evolución Huella hídrica"
                    data={seasons.map(s => s.name)}
                    series={waterFootprintEvolution}
                    seriesLabel="Litros"
                    product="water_footprint"
                    colors ={colores}
                />

图表样式:

export default function CChart(props) {

let options = {
    
    theme: {
        palette: 'palette' + props.palette,

    },
    
    colors: props.colors,
    chart: {
        width: '100%',
        id: "basic-bar",
        toolbar: {
            show: false
        }
    },
    fill: {
        gradient: {
            shade: 'light',
            type: "horizontal",
            shadeIntensity: 0.5,
            gradientToColors: undefined,
            inverseColors: false,
            opacityFrom: 0.5,
            opacityTo: 1,
            stops: [0, 50, 100],
            colorStops: []
        },
    },
    title: {
        text: props.title
    },
    
    xaxis: {
        categories: props.data,
        labels: {
            style: {
                colors: [],
                fontSize: '10px',
                fontFamily: 'Arial',
                fontWeight: 400,
                cssClass: 'apexcharts-xaxis-label',
            },
        }
    }
};

const series = [];

if (props.series) {
    props.series.forEach(serie => {
        series.push({
            name: serie.name,
            data: serie.data.map(d => d[[props.product]])
        });
    });
}

return (
    <Chart
        options={options}
        series={series}
        type="line"
        width="500"
    />
);

}