简体   繁体   English

React D3 条形图可滚动 X 轴

[英]React D3 Bar Chart Scrollable X-Axis

I am a true newbie when it comes to D3.说到 D3,我是一个真正的新手。 I am currently trying my best to implement a bar chart that allows the user to scroll on the x-axis while the Y-Axis remains in place.我目前正在尽力实现一个条形图,允许用户在 Y 轴保持原位的同时在 x 轴上滚动。 I am using React as my framework.我使用 React 作为我的框架。

export default [
  { activity: 'Main Idea and Detail', value: 90, color: '#2A78E4' },
  { activity: 'Character and Plot', value: 80, color: '#2A78E4' },
  { activity: 'Elements of Poetry', value: 70, color: '#2A78E4' },
  { activity: 'Standard 8.10', value: 60, color: '#2A78E4' },
  { activity: '8.1.3', value: 50, color: '#2A78E4' },
  { activity: 'Skill 6', value: 40, color: '#2A78E4' },
  { activity: 'Skill 7', value: 30, color: '#2A78E4' },
  { activity: 'Skill 8', value: 21, color: '#2A78E4' },
  { activity: 'Skill 9', value: 10, color: '#2A78E4' },
  { activity: 'Skill 10', value: 5, color: '#2A78E4' },
  { activity: '8.1.34', value: 50, color: '#2A78E4' },
  { activity: 'Skill 60', value: 40, color: '#2A78E4' },
  { activity: 'Skill 70', value: 30, color: '#2A78E4' },
  { activity: 'Skill 80', value: 21, color: '#2A78E4' },
  { activity: 'Skill 90', value: 10, color: '#2A78E4' },
  { activity: 'Skill 100', value: 5, color: '#2A78E4' },
  { activity: 'Skill 900', value: 100, color: '#2A78E4' },
  { activity: 'Skill 1000', value: 50, color: '#2A78E4' },
  { activity: 'Skill -1', value: 5, color: '#2A78E4' },
  { activity: '8.1.35', value: 50, color: '#2A78E4' },
  { activity: 'Skill 160', value: 40, color: '#2A78E4' },
  { activity: 'Skill 10', value: 30, color: '#2A78E4' },
  { activity: 'Skill 20', value: 21, color: '#2A78E4' },
  { activity: 'Skill 80', value: 10, color: '#2A78E4' },
  { activity: 'Skill 650', value: 5, color: '#2A78E4' },
  { activity: 'Skill 300', value: 100, color: '#2A78E4' },
  { activity: 'Skill 3000', value: 50, color: '#2A78E4' }
];

My code I am using to generate my scales are:我用来生成比例的代码是:

generateScales = () => {
    const { height, margin, width } = this.state;
    const xScales = d3
      .scaleBand()
      .domain(this.props.data.map(d => d.activity))
      .range([margin.left, width - margin.right])
      .padding(0.5);

    const yScales = d3
      .scaleLinear()
      .domain([0, 100])
      .range([height - margin.bottom, 0]);
    this.setState({
      xScales,
      yScales
    });
  };

To generate my scales I use the renderAxis function:为了生成我的比例,我使用了 renderAxis 函数:

renderAxis = () => {
    const xAxisBottom = d3.axisBottom().scale(this.state.xScales);
    const axisLeft = d3.axisLeft().scale(this.state.yScales);
    d3.select(this.refs.xAxis).call(xAxisBottom);
    d3.select(this.refs.yAxis).call(axisLeft);
  };

I was trying to use this example as a clue, but I am unable to get this line to properly work "d3.behavior.drag().on("drag", display)" http://bl.ocks.org/cdagli/ce3045197f4b89367c80743c04bbb4b6 .我试图用这个例子作为线索,但我无法让这条线正常工作“d3.behavior.drag().on("drag", display)” http://bl.ocks.org/ cdagli/ce3045197f4b89367c80743c04bbb4b6

I receive an error that this is not part of the d3 module, but the example is clearly using it.我收到一个错误,指出这不是 d3 模块的一部分,但该示例显然正在使用它。 I guess I do not know where to start or how to tackle this problem.我想我不知道从哪里开始或如何解决这个问题。 I tried to solve it also by using css, but to no avail我也尝试通过使用 css 来解决它,但无济于事

I was able to create a sandbox and if anybody could give me a clue on how to achieve a scrollable X-axis while having the Y-axis stay in place it will be really appreciated.我能够创建一个沙箱,如果有人能给我一个关于如何在 Y 轴保持原位的同时实现可滚动 X 轴的线索,我将不胜感激。

https://codesandbox.io/s/k0pj5m8q93 https://codesandbox.io/s/k0pj5m8q93

I solved my issue by providing my scales with the correct data.我通过为我的体重秤提供正确的数据解决了我的问题。 My mistake was thinking that I can just arbitrarily insert any number and receive the correct coordinates.我的错误是认为我可以随意插入任何数字并接收正确的坐标。 For my particular situation, I have to feed the scales the correct data.对于我的特殊情况,我必须为秤提供正确的数据。

So for my given data model:所以对于我给定的数据模型:

export default [
  { activity: 'Main Idea and Detail', value: 10, color: '#2A78E4' },
  { activity: 'Character and Plot', value: 20, color: '#2A78E4' },
  { activity: 'Elements of Poetry', value: 31, color: '#2A78E4' },
  { activity: 'Skill 4', value: 40, color: '#2A78E4' },
  { activity: 'Skill 5', value: 56, color: '#2A78E4' },
  { activity: 'Skill 6', value: 60, color: '#2A78E4' },
  { activity: 'Skill 7', value: 60, color: '#2A78E4' },
  { activity: 'Skill 8', value: 71, color: '#2A78E4' },
  { activity: 'Skill 9', value: 70, color: '#2A78E4' },
  { activity: 'Skill 10', value: 90, color: '#2A78E4' }
];

I have to set my X axis to my activity by doing this:我必须通过这样做将我的 X 轴设置为我的活动:

 d3.line()
    .x((info, index) => {
      return xScales(info.activity);
    })  

Now that d3 is aware of my x-axis I can then set Y to any value as long as it does not exceed 100 which is the top of Y-axis.现在 d3 知道我的 x 轴,然后我可以将 Y 设置为任何值,只要它不超过 100,即 Y 轴的顶部。

So my Y-Axis looks like this now:所以我的 Y 轴现在看起来像这样:

.y(data => {
          return yScales(baseLine - 1.2);
        });

And my path looks like this:我的路径如下所示:

          <path
          d={path(data)}
          x={70}
          className="line"
          style={{ fill: 'green', strokeWidth: 2, stroke: 'red' }}
           />

I hope this makes some sense and helps someone who is trying to have a horizontal line for there bar charts.我希望这有一定道理,并能帮助那些试图为条形图绘制水平线的人。

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

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