简体   繁体   English

图表-水平对齐折线图和条形图

[英]Recharts - Horizontally Align Line and Bar Charts

I am using Recharts and have a component that displays a line chart followed by a bar chart. 我正在使用图表,并且具有一个显示折线图和条形图的组件。 The two charts are displaying the same seven day span, but the ticks are not aligned. 这两个图表显示的是相同的7天跨度,但刻度线未对齐。 The line chart starts on the Y Axis line, while the bar chart has left padding (by default). 折线图从Y轴直线开始,而条形图则为左填充(默认情况下)。

On the line chart I tried adding padding={{left: 100 }}, which got the starting of the X axes aligned, but the rest of the ticks were off, and more importantly it isn't responsive to the screen size. 在折线图上,我尝试添加padding = {{{left:100}},它使X轴的起点对齐,但是其余的刻度线都关闭了,更重要的是,它对屏幕尺寸没有响应。 As far as I can tell using something like '5%' is not allowed. 据我所知,不允许使用“ 5%”之类的东西。

On the bar chart I tried setting the padding to zero and a negative number, but ran into the same issues with responsive scaling and tick misalignment. 在条形图上,我尝试将padding设置为零和负数,但在响应缩放和刻度线未对齐方面遇到了相同的问题。

Is there a way to align the ticks and data on the two graphs? 有没有办法对齐两个图表上的刻度和数据?

Here is the code: 这是代码:

<ResponsiveContainer width="95%" height={300} >
  <LineChart width={350} height={300} data={this.props.data.activityResponse.edges} connectNulls={true} >
    <Legend verticalAlign="top" height={36}/>
    <Line type="monotone" dataKey="node.participation" stroke={this.participationColor} name="Participation" strokeWidth={this.strokeWidth} />
    <Line type="monotone" dataKey="node.focus" stroke={this.focusColor} name="Focus" strokeWidth={this.strokeWidth} />
    <XAxis dataKey="node.logDate" tickFormatter={this.formatXAxis} />
    <YAxis dataKey="node.participation" >
      <Label value="%" position="insideLeft" />
    </YAxis>
    <Tooltip content={this.renderResponseTooltip} />
  </LineChart>
</ResponsiveContainer>
<ResponsiveContainer width="95%" height={300} >
  <BarChart width={350} height={300} data={this.props.data.activityStep.edges} connectNulls={true} >
    <Legend verticalAlign="top" height={36}/>
    <Bar dataKey="node.stepCount" fill={this.stepColor} name="Steps" />
    <XAxis dataKey="node.logDate" tickFormatter={this.formatXAxis} />
    <YAxis dataKey="node.stepCount" >
    </YAxis>
    <Tooltip content={this.renderStepTooltip} />
  </BarChart>
</ResponsiveContainer>

折线图和条形图对齐

This link might help in this case. 在这种情况下,此链接可能会有所帮助。

Try syncId="anyId" on both LineChar and BarChar element. 在LineChar和BarChar元素上尝试syncId="anyId"

Rechart - SynchronizedLineChart 图表-SynchronizedLineChart

I'm not sure if you are still interested in this, since the question is pretty old, but I have found a solution that worked for me. 我不确定您是否仍然对此感兴趣,因为这个问题已经很老了,但是我找到了一个对我有用的解决方案。

You can use ComposedChart to solve this. 您可以使用ComposedChart解决此问题。 Taking your code, the solution should look something like this: 使用您的代码,解决方案应如下所示:

<ResponsiveContainer width="95%" height={300} >
  <ComposedChart data={this.props.data.activityResponse.edges} connectNulls={true} >
    <Legend verticalAlign="top" height={36}/>
    <Line type="monotone" dataKey="node.participation" stroke={this.participationColor} name="Participation" strokeWidth={this.strokeWidth} />
    <Line type="monotone" dataKey="node.focus" stroke={this.focusColor} name="Focus" strokeWidth={this.strokeWidth} />
    <XAxis dataKey="node.logDate" tickFormatter={this.formatXAxis} />
    <YAxis dataKey="node.participation" >
      <Label value="%" position="insideLeft" />
    </YAxis>
    <Tooltip content={this.renderResponseTooltip} />
  </ComposedChart>
</ResponsiveContainer>
<ResponsiveContainer width="95%" height={300} >
  <ComposedChart data={this.props.data.activityStep.edges} connectNulls={true} >
    <Legend verticalAlign="top" height={36}/>
    <Bar dataKey="node.stepCount" fill={this.stepColor} name="Steps" />
    <XAxis dataKey="node.logDate" tickFormatter={this.formatXAxis} />
    <YAxis dataKey="node.stepCount" >
    </YAxis>
    <Tooltip content={this.renderStepTooltip} />
  </ComposedChart>
</ResponsiveContainer>

I also removed your width / height arguments on the charts since the ResponsiveContainer will take care of that already. 我还删除了图表上的width / height参数,因为ResponsiveContainer已经解决了这个问题。

In my personal case, this solves the issue, when I try to horizontally align an Area and a Bar Chart. 就我个人而言,当我尝试水平对齐区域和条形图时,这解决了问题。

在此处输入图片说明

Providing a syncid for both charts, by adding that attribute to both ComposedCharts, the Tooltip will also follow in both charts simultaneously. 通过将属性添加到两个ComposedCharts中,为两个图表提供一个同步,工具提示也将同时出现在两个图表中。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM