如何保持所有activeDots始终处于“活动”状态,而不是将鼠标悬停在数据点上(图表)

时间:2019-04-22 17:40:28

标签: javascript reactjs recharts

我从npm安装了“ recharts”软件包(最新版本1.5.0),并在React项目中正确加载了数据。

数据线和图表始终处于活动状态。我需要所有“ activeDots”始终保持活动状态,而不是仅在将鼠标悬停在数据点上时保持活动状态。

   <LineChart
        width={240}
        height={180}
        data={data}
        margin={{top: 15, right: 10, left: 10}}
      >
         <CartesianGrid horizontal={false} />
         <XAxis
          dataKey="x"
          stroke="rgba(0,0,0,0.4)"
          strokeWidth=".4"
          scale="time"
          type="number"
          domain={['auto', 'auto']}
          tick={{fontSize: 12}}
          tickFormatter = {(unixTime) => moment(unixTime).format('MMM D')}
         />
         <YAxis hide={true} />
         <Tooltip content={<CustomTooltip/>}/>
         <Line
          connectNulls={false}
          type="linear"
          dataKey="pv"
          stroke="#3F99F7"
          strokeWidth="3"
          activeDot={{ fill: '#3F99F7', stroke:'#fff', strokeWidth: 3, r: 11, className: "boxShadow" }}
          dot={{ fill: '#3F99F7', stroke:'#fff', strokeWidth: 2, r: 7, className: "boxShadow" }}
         />
      </LineChart>

不将鼠标悬停在数据点上: not hovering

将鼠标悬停在数据点上: hovering

1 个答案:

答案 0 :(得分:0)

这看起来像您想要的(紫色线):

const {LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend} = Recharts;
const data = [
      {name: 'Page A', uv: 4000, pv: 2400, amt: 2400},
      {name: 'Page B', uv: 3000, pv: 1398, amt: 2210},
      {name: 'Page C', uv: 2000, pv: 9800, amt: 2290},
      {name: 'Page D', uv: 2780, pv: 3908, amt: 2000},
      {name: 'Page E', uv: 1890, pv: 4800, amt: 2181},
      {name: 'Page F', uv: 2390, pv: 3800, amt: 2500},
      {name: 'Page G', uv: 3490, pv: 4300, amt: 2100},
];
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"/>
       <YAxis/>
       <CartesianGrid strokeDasharray="3 3"/>
       <Tooltip/>
       <Legend />
       <Line 
        type="monotone" 
        dataKey="pv" 
        stroke="#8884d8" 
        activeDot={{ fill: '#3F99F7', stroke:'#fff', strokeWidth: 3, r: 11, className: "boxShadow" }}
          dot={{ fill: '#3F99F7', stroke:'#fff', strokeWidth: 2, r: 7, className: "boxShadow" }}
        />
       <Line type="monotone" dataKey="uv" stroke="#82ca9d" />
      </LineChart>
    );
  }
})

ReactDOM.render(
  <SimpleLineChart />,
  document.getElementById('container')
);
body {
  margin: 0;
}

#container {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 10px;
  width: 800px;
  height: 800px;
  background-color: #fff;
}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script type="text/javascript" src="https://npmcdn.com/react@15.6.2/dist/react-with-addons.min.js"></script>
<script type="text/javascript" src="https://npmcdn.com/react-dom@15.6.2/dist/react-dom.min.js"></script>
<script type="text/javascript" src="https://npmcdn.com/prop-types/prop-types.min.js"></script>
<script type="text/javascript" src="https://npmcdn.com/recharts/umd/Recharts.min.js"></script>
<div id="container">
  <!-- This element's contents will be replaced with your component. -->
</div>

您的示例中或多或少有相同的代码,这使我相信您的代码中可能还有更多未找到的信息。