How to create circular progress(pie chart) like indicator

后端 未结 6 1624
情话喂你
情话喂你 2020-12-04 09:09

I have to show progress graphs exactly in following way where percentage would be in center of circular graph
\"cir

6条回答
  •  臣服心动
    2020-12-04 10:04

    If you are not targeting old browsers, you can easily do that by drawing on a canvas element. This gives you the freedom to do whatever you need with the chart.

    That means this solution's only requirement is jQuery and any browser that supports the canvas element...IE9+ Here's a code snippet that demonstrates it...

    //input
    var dimens = 256;
    var color = "rgba(54, 162, 235, 0.9)";
    var padding = 12;
    var width = 10;
    var value = 80;
    var maxValue = 100;
    var countFontRatio = 0.25; //ratio in relation to the dimens value
    
    $(function() {
      $(".chart-total").each(function(idx, element) {
    
        var _render = function() {
    
          var startingPoint = -0.5;
          var pointValue = startingPoint;
          var currentPoint = startingPoint;
          var timer;
          var _ctx;
    
          var $canvas = $(element).find("canvas");
          var canvas = $canvas.get(0);
    
          pointValue = (value / (maxValue / 20) * 0.1) - 0.5;
    
          canvas.height = dimens;
          canvas.width = dimens;
    
          if (!countFontRatio)
            $canvas.parent().find(".legend-val").css("font-size", dimens / value.toString().length);
          else
            $canvas.parent().find(".legend-val").css("font-size", dimens * countFontRatio);
    
          _ctx = canvas.getContext("2d");
    
    
    
          var _draw = function() {
    
            _ctx.clearRect(0, 0, dimens, dimens);
            _ctx.beginPath();
            _ctx.arc(dimens / 2, dimens / 2, (dimens / 2) - padding, startingPoint * Math.PI, 1.5 * Math.PI);
            _ctx.strokeStyle = "#ffffd";
            _ctx.lineWidth = 2;
            _ctx.lineCap = "square";
            _ctx.stroke();
    
            _ctx.beginPath();
            _ctx.arc(dimens / 2, dimens / 2, (dimens / 2) - padding, startingPoint * Math.PI, currentPoint * Math.PI);
            _ctx.strokeStyle = color;
            _ctx.lineWidth = width;
            _ctx.lineCap = "round";
            _ctx.stroke();
    
            currentPoint += 0.1;
    
            if (currentPoint > pointValue) {
              clearInterval(timer)
            }
    
          };
    
          timer = setInterval(_draw, 100);
        };
    
        _render();
    
        $(window).resize(function() {
          _render();
        });
    
      });
    })
    body {
      font-family: 'Open Sans', sans-serif;
      color: #757575;
    }
    
    .chart-total {
      position: relative;
      margin: 0 auto;
    }
    
    .chart-total-legend {
      position: absolute;
      top: 50%;
      left: 50%;
      -ms-transform: translateY(-50%) translateX(-50%);
      -o-transform: translateY(-50%) translateX(-50%);
      -moz-transform: translateY(-50%) translateX(-50%);
      -moz-transform: translateY(-50%) translateX(-50%);
      transform: translateY(-50%) translateX(-50%);
    }
    
    .legend-val {
      font-size: 4em;
      display: block;
      text-align: center;
      font-weight: 300;
      font-family: 'Roboto', sans-serif;
    }
    
    .legend-desc {
      display: block;
      margin-top: 5px;
      text-align: center;
    }
    
    
    
    
    3,933 Total Progress

提交回复
热议问题