Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1246951
    yhshin1020
    Participant

    Hey,

    I’m trying to implement this snippet: http://codepen.io/ejsado/pen/cLrlm/

    I’ve added the JS code like such:

    (function($){
    	$(document).ready(function(){
    function sliceSize(dataNum, dataTotal) {
      return (dataNum / dataTotal) * 360;
    }
    function addSlice(sliceSize, pieElement, offset, sliceID, color) {
      $(pieElement).append("<div class='slice "+sliceID+"'><span></span></div>");
      var offset = offset - 1;
      var sizeRotation = -179 + sliceSize;
      $("."+sliceID).css({
        "transform": "rotate("+offset+"deg) translate3d(0,0,0)"
      });
      $("."+sliceID+" span").css({
        "transform"       : "rotate("+sizeRotation+"deg) translate3d(0,0,0)",
        "background-color": color
      });
    }
    function iterateSlices(sliceSize, pieElement, offset, dataCount, sliceCount, color) {
      var sliceID = "s"+dataCount+"-"+sliceCount;
      var maxSize = 179;
      if(sliceSize<=maxSize) {
        addSlice(sliceSize, pieElement, offset, sliceID, color);
      } else {
        addSlice(maxSize, pieElement, offset, sliceID, color);
        iterateSlices(sliceSize-maxSize, pieElement, offset+maxSize, dataCount, sliceCount+1, color);
      }
    }
    function createPie(dataElement, pieElement) {
      var listData = [];
      $(dataElement+" span").each(function() {
        listData.push(Number($(this).html()));
      });
      var listTotal = 0;
      for(var i=0; i<listData.length; i++) {
        listTotal += listData[i];
      }
      var offset = 0;
      var color = [
        "cornflowerblue", 
        "olivedrab", 
        "orange", 
        "tomato", 
        "crimson", 
        "purple", 
        "turquoise", 
        "forestgreen", 
        "navy", 
        "gray"
      ];
      for(var i=0; i<listData.length; i++) {
        var size = sliceSize(listData[i], listTotal);
        iterateSlices(size, pieElement, offset, i, 0, color[i]);
        $(dataElement+" li:nth-child("+(i+1)+")").css("border-color", color[i]);
        offset += size;
      }
    }
    createPie(".pieID.legend", ".pieID.pie");
    	});
    })(jQuery);

    but its giving me a JS error and the effect is not working on my page.

    Did I add the JS code wrongly?

    Any help is appreciated.

    #1246952
    yhshin1020
    Participant
    This reply has been marked as private.
    #1246957
    yhshin1020
    Participant
    This reply has been marked as private.
    #1246972
    Thai
    Moderator

    Glad you’ve sorted it out.

    If you need anything else, please let us know 🙂

    #1246984
    yhshin1020
    Participant

    Is there a way to get the pie chart on the page aligned to the center?

    Thanks.

    #1247022
    Darshana
    Moderator

    Hi there,

    Assign the class “center-text” to the Column that resides your pie-chart.

    Hope that helps.

  • <script> jQuery(function($){ $("#no-reply-1246951 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>