当类型="类别"时,XAx在Rechart的ScatterChart中重复出现。

时间:2017-11-22 17:49:20

标签: javascript reactjs d3.js charts recharts

在下面的图片/代码中,应该只出现一次hi,hello和bye。关于它为什么重复的任何想法?

在浏览器中进行修改:http://jsfiddle.net/ndLhnegs/318/编辑:我在这里做了另一个更简单的示例:http://jsfiddle.net/7coegorj/2/

enter image description here

React Rechart组件:

const {Scatter, ScatterChart, XAxis, YAxis, CartesianGrid, Tooltip, Legend} = Recharts;
const Chart = React.createClass({
  render(){
      const selected = [
        {value:'obj1',label:'Obj1'},
        {value:'obj2',label:'Obj2'},
        {value:'obj3',label:'Obj3'},
        {value:'obj4',label:'Obj4'},
      ]
      const scatters = selected.map((s) => {
        let data = [
          {x:'hi',y:Math.random() * 10},
          {x:'hello',y:Math.random() * 10},
          {x:'bye',y:Math.random() * 10},
        ]
        return (
          <Scatter
            key={s.label}
            name={s.label}
            data={data}
            fill='#000'
            line
            shape="cross" />
        );
      });
      return (
        <ScatterChart width={600} height={400} margin={{ top: 20, right: 20, bottom: 20, left: 20 }}>
          <XAxis  dataKey='x' name='Macro' />
          <YAxis type="number" dataKey={'y'} name='Grams' unit='g' />
          <CartesianGrid />
          <Tooltip cursor={{ strokeDasharray: '3 3' }} />
          <Legend />
          {scatters}
        </ScatterChart >
      );
    }
})

ReactDOM.render(
  <Chart />,
  document.getElementById('container')
);

在此处提交了一个GitHub问题 - https://github.com/recharts/recharts/issues/1034

1 个答案:

答案 0 :(得分:2)

要解决此问题,我相信您可以使用http://recharts.org/en-US/api/XAxis#allowDuplicatedCategory

中的文档中的allowDuplicatedCategories={false}