如何制作'csgocrash'样式图表

时间:2017-01-24 18:37:22

标签: javascript plotly

我想制作一张类似下图的类似图表,即绘制一条线,随着时间的推移逐渐增加。底部的数字是秒(通过了多少)。是否有任何js库可以做到这一点?我试过chart.js,但我无法实现我想要的目标。

what im trying to achieve

这是我尝试使用plotly.js

的内容
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
    <div style="width:400px;height:400px" id="myDiv"></div>
    <script>
        var numberX = 1;
        var numberY = 1;

        var data = [
        {
            x: [1, numberX],
            y: [2, numberY],
            type: 'scatter'
        }
        ];

        Plotly.newPlot('myDiv', data);

        setInterval(function(){
            numberX++;
            numberY++;

            var data = [
            {
                x: [1, numberX],
                y: [2, numberY],
                type: 'scatter'
            }
            ];

            Plotly.newPlot('myDiv', data);
        }, 1000);
    </script>

</body>
</html>

结果......并不令人印象深刻:

not impressive

1 个答案:

答案 0 :(得分:4)

下面是我写的一些代码,有一些空闲时间,这似乎是一个很好的编程挑战。无法完全正确,你必须改进它并玩价值观,但这是基本的想法:

var mult = 0;
var ruler;
var inc;
var time=0;
var multt = 0.01;
var done = false;
var randomFac ;

function setup() {
  createCanvas(500, 300);
  frameRate(15);
  ruler = new Ruler();
  inc = new Line();
  setInterval(myTimer, 50);
}

function draw() {
  background(30);

  stroke(150);
  strokeWeight(1);
  line(20, 20, 20, height - 20);
  line(20, height - 20, width - 20, height - 20);

  noStroke();
  fill(150);
  textSize(120);
  textAlign(CENTER);
  text(mult.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0] + "x", width / 2, height / 2 + 20);

  if (mult > randomFac){
    console.log("stop");
    done = true;
  }

  if (!done){
  mult += multt;}

  if (!done){
  ruler.show();
  inc.show();
  }
}

function Ruler() {
  this.spc = 150;
  this.b = ((height - 20) / this.spc) / 2;

  this.show = function() {
    for (var a = 0; a < (width - 20) / this.spc; a++) {
      textAlign(CENTER);
      textSize(10);
      noStroke();
      fill(150);
      text(a, a * this.spc + 20, height - 8);
    }
    for (var a = 0; a < (height - 20) / this.spc; a++) {
      textAlign(CENTER);
      textSize(8);
      noStroke();
      fill(150);
      text(this.b.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0] + "x", 10, a * this.spc + 10);
      this.b -= 0.5;
    }
    this.b = ((height - 20) / this.spc) / 2;
  }
}

function Line() {

  this.show = function() {
    stroke(155);
    strokeWeight(2);
    beginShape();
    vertex(21, height - 20);
    if (height - 20 - (mult * (ruler.spc * 2)) + 20 < 280) {
      vertex(0 + 20 + (time * (ruler.spc )) - 20, height - 20 - (mult * (ruler.spc * 2)) + 20);
    }
    endShape();

    if (0 + 20 + (time * (ruler.spc )) - 20 >= 480 ||  height - 20 - (mult * (ruler.spc * 2)) + 20 <= 20){
      ruler.spc -= 1;
    }
  }
}

function myTimer () {
  time += 0.05;
  multt += 0.00005;
}

我希望这能以某种方式帮助你,度过美好的一天。

您可以在此处看到代码:https://codepen.io/felipe_mare/pen/PWKZWQ