Recharts: how to custome XAxis label's position

Created on 24 Mar 2016  路  3Comments  路  Source: recharts/recharts

i try to add css

.recharts-cartesian-axis-label {
        position: relative;
        left: -100px;
        top: -9999px;
        color:red;
    }

but it not work

Most helpful comment

transform is a good way to change the postition of label. Because we can only use css to change some presentation attributes, but we can't change the position attributes such as x, y in css.

.recharts-cartesian-axis-label text {
  fill: red;
}

Another way is to set label into a function, or react element.

const renderLabel = (props) => {
    const { x, y, width, height } = props;

  return (
    <text x={x + width / 2} y={y - 10} className="customized-x-axis-label">Anything</text>
  );
};
const SimpleLineChart = React.createClass({
    render () {
    return (
        <LineChart width={600} height={300} data={data}
            margin={{top: 5, right: 30, left: 20, bottom: 5}}>
       <XAxis dataKey="name" label={renderLabel}/>
       <YAxis/>
       <CartesianGrid strokeDasharray="3 3"/>
       <Tooltip/>
       <Legend />
       <Line type="monotone" dataKey="pv" stroke="#8884d8" />
       <Line type="monotone" dataKey="uv" stroke="#82ca9d" />
      </LineChart>
    );
  }
})

http://jsfiddle.net/sh2bLsh2/

All 3 comments

i resolve it by using transform: translate
is there a better way ?

transform is a good way to change the postition of label. Because we can only use css to change some presentation attributes, but we can't change the position attributes such as x, y in css.

.recharts-cartesian-axis-label text {
  fill: red;
}

Another way is to set label into a function, or react element.

const renderLabel = (props) => {
    const { x, y, width, height } = props;

  return (
    <text x={x + width / 2} y={y - 10} className="customized-x-axis-label">Anything</text>
  );
};
const SimpleLineChart = React.createClass({
    render () {
    return (
        <LineChart width={600} height={300} data={data}
            margin={{top: 5, right: 30, left: 20, bottom: 5}}>
       <XAxis dataKey="name" label={renderLabel}/>
       <YAxis/>
       <CartesianGrid strokeDasharray="3 3"/>
       <Tooltip/>
       <Legend />
       <Line type="monotone" dataKey="pv" stroke="#8884d8" />
       <Line type="monotone" dataKey="uv" stroke="#82ca9d" />
      </LineChart>
    );
  }
})

http://jsfiddle.net/sh2bLsh2/

thanks! i may have to learn some base of svg

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adrianmcli picture adrianmcli  路  3Comments

emiloberg picture emiloberg  路  3Comments

patientplatypus picture patientplatypus  路  3Comments

Eric24 picture Eric24  路  3Comments

sdoomz picture sdoomz  路  3Comments