使用D3可滚动X轴实现BarChart

Lou*_*345 8 bar-chart d3.js reactjs

我目前正在尽力实现一个条形图,允许用户在Y轴保持原位的同时在x轴上滚动.我使用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' }
];
Run Code Online (Sandbox Code Playgroud)

我用来生成我的比例的代码是:

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
    });
  };
Run Code Online (Sandbox Code Playgroud)

要生成我的比例,我使用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);
  };
Run Code Online (Sandbox Code Playgroud)

我试图用这个例子作为线索,但我无法让这一行正常工作"d3.behavior.drag().on("drag",display)" http://bl.ocks.org/ cdagli/ce3045197f4b89367c80743c04bbb4b6.

我收到一个错误,这不是d3模块的一部分,但示例显然正在使用它.我想我不知道从哪里开始或如何解决这个问题.我试图通过使用CSS来解决它,但无济于事

我能够创建一个沙箱,如果有人能给我一个关于如何实现可滚动的X轴同时让Y轴保持在原位的线索,那么使用React将非常感激.以上示例是正确的地方吗?

我试图通过使用缩放功能解决我的问题,它似乎是最好的解决方案.在使用d3.event.transform.rescaleX时,我仍然遇到问题,似乎rescaleX方法没有出现在d3.event.transform对象上.我真的很沮丧,因为所有的例子似乎都在使用这个功能.任何帮助将不胜感激.

https://codesandbox.io/s/k0pj5m8q93

Bor*_*jić 6

Code Sandbox with solution:https: //codesandbox.io/s/4q5zxmy3zw

React Scroll Bar Chart基于:http: //bl.ocks.org/cdagli/ce3045197f4b89367c80743c04bbb4b6

D3用于计算,React用于渲染.我希望这将有所帮助.