簡體   English   中英

動畫 D3.js 條形圖。 從底部到頂部。 請檢查鏈接以獲取參考圖片

[英]Animate D3.js Bar Chart. From Bottom to Top. Please check the link for reference image

下面是我的代碼片段,請檢查 output 的鏈接。 [輸出圖演示][1](來自代碼的輸出是從上到下的動畫)
需要從底部到頂部對條形進行動畫處理目前我正在使用 animation 的過渡和持續時間,而 svgHeight 是條形高度,但不知何故,我最終得到了 [demo][1] 中顯示的 animation。 任何建議或幫助將不勝感激。 我已經嘗試調整圖形的一些高度和 y 屬性。


import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
import { transformAll } from '@angular/compiler/src/render3/r3_ast';

@Component({
  selector: 'app-scorebenchmarking',
  templateUrl: './scorebenchmarking.component.html',
  styleUrls: ['./scorebenchmarking.component.css'],
})
export class ScorebenchmarkingComponent implements OnInit {
  dataset = [3, 2, 2, 3, 1, 2, 1, 1, 1, 1];
  colors = [
    'green',
    'green',
    'green',
    'orange',
    'orange',
    'orange',
    'orange',
    'red',
    'red',
    'red'
  ];
  listcompanies = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  svgWidth = 900;
  svgHeight = 300;
  barPadding = 5;
  barWidth = this.svgWidth / this.dataset.length - 30;
  clickObject = 0;

  constructor() {}

  ngOnInit(): void {
    this.buildSvg();
  }

  buildSvg() {
    var svg = d3
      .select('svg')
      .attr('width', this.svgWidth)
      .attr('heigth', this.svgHeight);

    var yScale = d3
      .scaleLinear()
      .domain([0, d3.max(this.dataset)])
      .range([0, this.svgHeight - 30]);

    var barchart = svg
      .selectAll('rect')
      .data(this.dataset)
      .enter()
      .append('rect')
      .attr('class', (d: any, i: any) => {
        return 'rect' + (i + 1);
      })
      .attr('fill', (d: any, i: any) => {
        return this.colors[i];
      })
      .attr('y', (d: any) => {
        return this.svgHeight - yScale(d);
      })
      .attr('height', (d: any) => {
        return 0;
      })
      .attr('width', this.barWidth - this.barPadding)
      .attr('transform', (d: any, i: any) => {
        var translatenum = [this.barWidth * i, 0];
        return 'translate(' + translatenum + ')';
      })
      .on('click', (d: any) => {
        this.clickObject = d;
      })
      .on('mouseover', (d: any, i: any) => {
        d3.select('.rect' + (i + 1))
          .transition()
          .duration('600')
          .attr('opacity', '0.6');
      })
      .on('mouseout', (d: any, i: any) => {
        d3.selectAll('.rect' + (i + 1))
          .transition()
          .duration('600')
          .attr('opacity', '1');
      });

    var temp = d3
      .selectAll('rect')
      .transition()
      .duration(1500)
      .attr('height', (d: any) => {
        return yScale(d);
      });

    var text = svg
      .selectAll('text.no_of_companies')
      .data(this.dataset)
      .enter()
      .append('text')
      .text((d: any) => {
        return d;
      })
      .attr('y', (d: any, i: any) => {
        return this.svgHeight - yScale(d) - 6;
      })
      .attr('x', (d: any, i: any) => {
        return this.barWidth * i + 24;
      })
      .attr('fill', 'black');

    var sizeofaxis = (this.barWidth + this.barPadding) * 10 - 55;

    var line = svg
      .append('line')
      .attr('x1', 0)
      .attr('y1', this.svgHeight)
      .attr('x2', sizeofaxis)
      .attr('y2', this.svgHeight)
      .attr('stroke', 'black');

    var textforAxis = svg
      .selectAll('text.axis_labels')
      .data(this.listcompanies)
      .enter()
      .append('text')
      .text((d: any) => {
        return d;
      })
      .attr('y', (d: any, i: any) => {
        return this.svgHeight + 20;
      })
      .attr('x', (d: any, i: any) => {
        return this.barWidth * i + 24;
      })
      .attr('fill', 'black');
  }
}


>This is my code .
>PLease Help


  [1]: https://i.stack.imgur.com/CSpO9.gif

將初始高度設置為 yScale(d),將過渡高度設置為 0。在 d3 中,(0,0) 指的是左上角。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM