方程图的动画javascript [英] animation of a graph of an equation javascript

查看:57
本文介绍了方程图的动画javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题上陷入困境,不知道该把我的手放在哪里.我必须在javascript中绘制等式 y = x ^ 3

I'm stuck on this issue and don't know where to put my hands. I have to draw in javascript the animation of the graph of the equation y = x ^ 3

我是什么意思?

知道y(例如例如y = 10 )我希望图从等式 y = x ^ 3的(0; 0)到(x; 10)开始我也想创建一个在动画过程中可以单击的按钮,并告诉我在那个精确时刻

knowing y (for example y = 10) I would like the graph to start from (0; 0) up to (x; 10) following the equation y = x ^ 3 also I would like to create a button which can be clicked during the animation and tells me what y is the graph at that precise moment

现在,由于有chart.js,我设法做到了:

for now thanks to chart.js i managed to do this:

JS

 var ctx = document.getElementById("myChart");
   var data = {
   labels: [1, 2, 3, 4, 5],
   datasets: [
       {
      
           function: function(x) { return x*x*x },
           borderColor: "rgba(153, 102, 255, 1)",
           data: [],
           fill: true
       }]
    };

Chart.pluginService.register({
    beforeInit: function(chart) {
        var data = chart.config.data;
        for (var i = 0; i < data.datasets.length; i++) {
            for (var j = 0; j < data.labels.length; j++) {
                var fct = data.datasets[i].function,
                    x = data.labels[j],
                    y = fct(x);
                data.datasets[i].data.push(y);
            }
        }
    }
});

var myBarChart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

HTML

<canvas id="myChart"></canvas>

结果

现在我只有图,没有动画,我不能选择最大值y

for now I only have the graph, there is no animation and I cannot select the maximum y

我该怎么办?

推荐答案

要在y轴上设置最大数量,可以根据需要使用 max 属性或 suggestedMax 确保如果数据变大,轴会适应.对于动画,您可以编写自定义逻辑,如下面的示例所示.我只不知道如何在点击时获得其值:

To set max amount on your y Axes you can use the max property or suggestedMax if you want to make sure that if the data goes bigger the axis adapts. For the animation you can write custom logic as in the example underneath. I only dont know how to get the value its at on the click:

const labels = [1, 2, 3, 4, 5]

const totalDuration = 5000;
const delayBetweenPoints = totalDuration / labels.length;
const previousY = (ctx) => ctx.index === 0 ? ctx.chart.scales.y.getPixelForValue(100) : ctx.chart.getDatasetMeta(ctx.datasetIndex).data[ctx.index - 1].getProps(['y'], true).y;

var options = {
  type: 'line',
  data: {
    labels,
    datasets: [{
      label: '# of Votes',
      data: [],
      borderWidth: 1,
      function: function(x) {
        return x * x * x
      },
      borderColor: 'red',
      backgroundColor: 'red'
    }]
  },
  options: {
    scales: {
      y: {
        max: 250
      }
    },
    animation: {
      x: {
        type: 'number',
        easing: 'linear',
        duration: delayBetweenPoints,
        from: NaN, // the point is initially skipped
        delay(ctx) {
          if (ctx.type !== 'data' || ctx.xStarted) {
            return 0;
          }
          ctx.xStarted = true;
          return ctx.index * delayBetweenPoints;
        }
      },
      y: {
        type: 'number',
        easing: 'linear',
        duration: delayBetweenPoints,
        from: previousY,
        delay(ctx) {
          if (ctx.type !== 'data' || ctx.yStarted) {
            return 0;
          }
          ctx.yStarted = true;
          return ctx.index * delayBetweenPoints;
        }
      }
    }
  },
  plugins: [{
    id: 'data',
    beforeInit: function(chart) {
      var data = chart.config.data;
      for (var i = 0; i < data.datasets.length; i++) {
        for (var j = 0; j < data.labels.length; j++) {
          var fct = data.datasets[i].function,
            x = data.labels[j],
            y = fct(x);
          data.datasets[i].data.push(y);
        }
      }
    }
  }]
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
var chart = new Chart(ctx, options);

<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.1/chart.js" integrity="sha512-HJ+fjW1Hyzl79N1FHXTVgXGost+3N5d1i3rr6URACJItm5CjhEVy2UWlNNmFPHgX94k1RMrGACdmGgVi0vptrw==" crossorigin="anonymous"></script>
</body>

这篇关于方程图的动画javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆