【问题标题】:Rotate chart "pie" to need value or angle旋转图表“饼图”以需要值或角度
【发布时间】:2019-02-25 19:00:21
【问题描述】:

我有一些图表有两种颜色(红色蓝色)。 Example .

我使用 Chartjs

最有效的方式: 我有价值观,例如:

  • 红色从 1 到 350,
  • 蓝色从 351 到 1000

警告:值(redblue)可能会发生变化。

好的,我们有了值,现在我不会旋转我的图表 X 计数并在值 256 上停止,(这是红色颜色)。

(从顶部开始 - Place of Start

问:我该怎么做?

例如,我发现一些代码随机旋转(旋转)我的图表:

            var spin = function () {
            var timeOut = function () {
                // called by setTimeout to update the chart after rotation been changed
                dynChart.update();
            }
            // generate random angle
            var newAngle = Math.random() * 1080 + currAngle;

            for (var angle = currAngle, interval = 100; angle < newAngle; angle += 1, interval += 5) {
                dynChart.options.rotation = angle;
                setTimeout(timeOut, interval);
            }
            //currAngle = dynChart.options.rotation;
        }

【问题讨论】:

    标签: javascript charts chart.js frontend


    【解决方案1】:

    这可以帮助你:

    var config = {
      type: 'pie',
      data: {
        datasets: [{
          data: [
            300,
            700
          ],
          backgroundColor: [
            window.chartColors.red,
            window.chartColors.blue,
          ],
          label: 'Dataset 1'
        }]
      },
      options: {
        responsive: true,
        rotation: 0,
        animation: {
          duration: 2000
        }
      }
    
    };
    var myPie;
    window.onload = function() {
      create();
    };
    
    function create() {
      var ctx = document.getElementById('chart-area').getContext('2d');
    
      config.data.datasets[0].data = [$("#redValue").val(), $("#blueValue").val()]
      config.options.rotation = -Math.PI / 2;
      config.options.animation.duration = 2000;
      myPie = new Chart(ctx, config);
      // console.log(myPie);
    }
    
    function rotate() {
      var fromDegree = 0;
      var toDegree = 360 * (parseFloat($("#stopValue").val())) /
        (parseFloat($("#redValue").val()) + parseFloat($("#blueValue").val()));
    
      rotatePie(0, 360, 8,
        function() {
          rotatePie(0, 360, 15,
            function() {
              rotatePie(fromDegree, toDegree, 30,
                function() {})
            })
        });
    }
    
    function rotatePie(fromDegree, toDegree, delay, callback) {
      var rotation = -Math.PI / 2 - fromDegree * 2 * Math.PI / 360;
      var stopRotation = -Math.PI / 2 - toDegree * 2 * Math.PI / 360;
    
      var intervalId = setInterval(frame, delay);
    
      function frame() {
        if (rotation <= stopRotation) {
          console.log('2');
          clearInterval(intervalId);
          callback();
        } else {
          rotation = rotation - Math.PI * 0.03;
          myPie.config.options.rotation = rotation;
          myPie.config.options.animation.duration = 0;
          myPie.update();
        }
      }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://www.chartjs.org/samples/latest/utils.js"></script>
    <script src="https://www.chartjs.org/dist/2.7.3/Chart.bundle.js"></script>
    
    <div id="canvas-holder" style="width:40%; margin: 20px">
      <canvas id="chart-area"></canvas>
    </div>
    
    Red Value: <input id="redValue" type="number" value="300" />
    <br/> Blue Value: <input id="blueValue" type="number" value="700" />
    <br/>
    <button type="button" onclick="create()">Create</button>
    
    <br/> <br/> Stop On: <input id="stopValue" type="number" value="350" />
    <br/><br/>
    
    <button type="button" onclick="rotate()">Rotate</button>

    【讨论】:

    • 如果我想将此图表旋转两次(如 360 度 + 360 度),高速且仅在 finalRotation 上停止下一个(速度较小),我该怎么做这个?
    • 我刚刚更改了代码。你可以得到这个想法并做得更​​好。
    • 发现了一个错误。按下“旋转”按钮后,如果我将鼠标移动到图表上,然后在图表上移动鼠标(上、下、左、右),它们的颜色会被移动。这个错误还是不声明功能? :d
    • 这是我的错 :) 我学到了一些新东西。检查编辑的代码,赚取积分不是一件容易的事;)
    • 是的,我也像你一样修好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多