1. 组件 Line , Area , Bar , XAxis, YAxis 的 labels 支持自定义

    1. <BarChart width={600} height={300} data={data}>
    2. <XAxis dataKey="name" tick={renderCustomAxisTick} />
    3. <YAxis />
    4. <Bar type="monotone" dataKey="uv" barSize={30} fill="#8884d8"
    5. label={renderCustomBarLabel}/>
    6. </BarChart>
    7. ) ;

    2. 组件的形状可以自定义

    例如,当 <Bar /> 的属性 shape 是一个函数或者 react element 的时候,可以用来自定义柱子的形状。

    1. const renderBarChart = (
    2. <XAxis dataKey="name" tick={renderCustomAxisTick} />
    3. <YAxis />
    4. shape={<TriangleBar />} />
    5. </BarChart>
    6. ) ;

    3. Tooltip 的内容可以自定义

    我们经常需要在提示浮层中展示更多的信息,这时候就可以时候自定义的 content 。

    1. const renderBarChart = (
    2. <BarChart width={600} height={300} data={data}>
    3. <XAxis dataKey="name" tick={renderCustomAxisTick} />
    4. <YAxis />
    5. <Tooltip content={<CustomTooltip />}/>
    6. <Bar type="monotone" dataKey="uv" fill="#8884d8"
    7. shape={<TriangleBar />} />
    8. </BarChart>

    组件自定义 - 图3

    4. 组件样式支持 svg 属性

    5. Tooltip 和 Legend 支持自定义的 style

    Tooltip 和 Legend 是使用 html 实现的,开放了 style 相关的 api ,当然也可以通过 css 来改变样式。

    1. import { BarChart, Bar, XAxis, XAxis, Tooltip, Legend, CartesianGrid } from 'recharts';
    2. const data = [{name: 'Page A', uv: 400, pv: 2400, amt: 2400}, ...];
    3. const renderBarChart = (
    4. <BarChart width={600} height={300} data={data}>
    5. <XAxis dataKey="name" stroke="#8884d8" />
    6. <YAxis />
    7. <Tooltip wrapperStyle={{ width: 100, backgroundColor: '#ccc' }} />
    8. <Legend width={100} wrapperStyle={{ top: 40, right: 20, backgroundColor: '#f5f5f5', border: '1px solid #d5d5d5', borderRadius: 3, lineHeight: '40px' }} />
    9. <CartesianGrid stroke="#ccc" strokeDasharray="5 5" />
    10. <Bar type="monotone" dataKey="uv" fill="#8884d8" barSize={30} />
    11. ) ;
    • 组件自定义 - 图6uv