cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <body>

</body>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="js/pieChart.js" type="text/javascript"></script>
</html>


            
          
!
            
              body{
  background-color: #ffffff;
  display:flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.PieChart {
  height: auto;
  width:auto;
  display: inline-block;
}

            
          
!
            
              //#2c9f51
//#20421f
/*jslint esversion:6 */
function Pie(product) {
  const chartbaseCircleColor = '#f2f9fc';
  const pieBaseColor = 'rgba(162, 180, 181,0.1)';

  // let pieCanvas =  document.getElementById('pieCanvas');
  let pieCanvas =  document.createElement('canvas');
  pieCanvas.width = 300;
  pieCanvas.height = 300;
  document.body.appendChild(pieCanvas);
  //TODO:

  let width = pieCanvas.width;
  let height = pieCanvas.height;
    //opts for circles
  let progress = 1;

//chartOptions
  let chartOptions = {
      center: {
        x: width/2,
        y: height/2
      },
      outterCircleRadius: pieCanvas.width/Math.PI,
  }

 //Ratio
  chartOptions.ratioOutterCircle = chartOptions.outterCircleRadius/150;
  chartOptions.innerCircleRadius = Math.floor(45*chartOptions.ratioOutterCircle);
  chartOptions.radius = Math.floor(30*chartOptions.ratioOutterCircle);
  chartOptions.centeroffset = Math.floor(5*chartOptions.ratioOutterCircle);
  chartOptions.labelBorderRadius = Math.floor(154*chartOptions.ratioOutterCircle);

 let pieSections = [
       {score: 59, category: 'Nutrition' ,iconLink:'https://www.greenchoicenow.com/newslleter/icons/nutri.png'},
        {score: 100, category: 'Health Safety',iconLink:'https://www.greenchoicenow.com/newslleter/icons/health.png'},
        {score: 95, category: 'Animal Welfare',iconLink:'https://www.greenchoicenow.com/newslleter/icons/animal_ware.png'},
        {score: 80, category: 'Corporate Sustainability',iconLink:'https://www.greenchoicenow.com/newslleter/icons/comp_sust.png'},
        {score: 68, category: 'Transparency',iconLink:'https://www.greenchoicenow.com/newslleter/icons/trans.png'},
        {score: 100, category: 'Freshness' ,iconLink:'https://www.greenchoicenow.com/newslleter/icons/fresh.png'},
        
  ];


  let grerenScore = 84;//pieSections.reduce((sum,value) => Math.floor(sum + value.score/6),1);
  let maxPieScore = pieSections.reduce((max = 0,value) => {
        return  value.score > max ? value.score: max;
  },1);

  // pieCanvas.style.boxShadow = "1px 1px 20px #777777";
  pieCanvas.style.backgroundColor = '#ffffff';


  let ctx = pieCanvas.getContext('2d');

  let timer, n = 0;

    /*--- start ---*/
    //drawBaseCircle(ctx,chartOptions,chartbaseCircleColor)
    drawPies(ctx,chartOptions,pieSections,null,progress);
    drawPiesBase(ctx,chartOptions,chartbaseCircleColor);
    drawBlock(ctx,chartOptions);

    function pieChartFrame(progress,indexPass,draw) {
      ctx.clearRect(0, 0, width, height);
      //circlesBase in chart
      //drawBaseCircle(ctx,chartOptions,chartbaseCircleColor);
      //base for each Pie Section
      drawPiesBase(ctx,chartOptions,chartbaseCircleColor);
      //draw each Pie Section
      drawPies(ctx,chartOptions,pieSections,progress,indexPass,draw);
      //draw GreenScore
      scoreFrame(ctx,chartOptions,progress,grerenScore,maxPieScore);
      // draw drawConcentricCircle
      //drawConcentricCircle(ctx,chartOptions);
      // draw Lines in middle of each pie
      //drawLines(ctx,chartOptions);
      // draw outter block
      drawBlock( ctx,chartOptions);
      //drawLableBorder(ctx,chartOptions,pieSections)
      //drawIcon(ctx,chartOptions,pieSections);
    }

    function drawPieChart(timestamp) {
      let step = 3;
      pieChartFrame(progress);
      progress += step;

      if (progress < (100 - step)) {
        window.requestAnimationFrame(drawPieChart);
      } else {
        pieChartFrame(100);
        showScoreTip(pieCanvas);
        mouseInEffect(pieCanvas);
        //pieCanvas.addEventListener('mousemove',mouseHoverHandler);
      }
    }


    this.pieSections = pieSections;

    setTimeout(() => {
      window.requestAnimationFrame(drawPieChart);
    },90);

    function showScoreTip(canvas){
      let tip = document.createElement('div');
      tip.style.background = 'rgba(32, 66, 31, .7)';
      tip.style.color = '#fff';
      tip.style.borderRadius = '2px';
      tip.style.padding = '5px 10px';
      tip.style.position = 'absolute';
      tip.style.zIndex = '999';
      tip.style.display = 'none';

      document.body.appendChild(tip);

      canvas.addEventListener('mousemove', (e) => {
        let crd = {x:e.offsetX,
                  y:e.offsetY};
        let count = 0;
        pieSections.forEach(pieSection => {
            if(pieSection.isInPath(crd,pieSection,pieChartFrame)) {
              //document.getElementById("xycoordinates").innerHTML = "Coordinates: (" + crd.x + "," + crd.y + ") scorePoint: (" + pieSection.score  + ")";
              let fontSize = 1*chartOptions.ratioOutterCircle;
              tip.innerHTML = `<font size='+0.5'>${pieSection.category}: </font><font size='+1.3'>${pieSection.score}%</font>`;
              tip.style.left = e.pageX + 15 +'px';
              tip.style.top = e.pageY  + 10 + 'px';
              tip.style.display = 'block';
            } else {
              count++;
            }
            count === 6 && (tip.style.display = 'none');
          });
      });

        canvas.addEventListener('mouseleave', () => {
        tip.style.display = 'none';
      });
  }

  function mouseInEffect(canvas) {

    canvas.addEventListener('mousemove', (e) => {
      let crd = {x:e.offsetX,
                y:e.offsetY};
     let count = 0;
      pieSections.forEach(pieSection => {

          if(pieSection.isInPath(crd,pieSection,pieChartFrame)) {
            pieCanvas.style.cursor = 'pointer';
            pieSection.mouseIn = true;
            pieChartFrame(100,pieSection.index,false);
            //pieChartFrame(100,pieSection.index,true);

            ctx.save();
            //let fillStyle = `hsl(${pieSection.pieColorHsl.h},${pieSection.pieColorHsl.s + 3}%,${pieSection.pieColorHsl.l + 20}%)`
            let fillStyle = addGradient(ctx,chartOptions,crd,pieSection.pieColorHsl);
            //chartOptions.centeroffset = 0;
            pieSection.isInPath(crd,pieSection,pieChartFrame)

            ctx.shadowColor = 'rgb(132, 154, 155)';
            ctx.shadowBlur = 5;
            ctx.shadowOffsetX = 3;
            ctx.shadowOffsetY = 5;
            ctx.fillStyle = fillStyle;
            ctx.fill();
            ctx.restore();
            drawLableBorderMoouseIn(ctx,chartOptions,pieSection,pieSection.pieColor);

          } else {
            count ++;
          }

          if(count === 6 ) {
            pieCanvas.style.cursor = 'auto';
          }

        });
      });
  }

}

function appendIcon(ctx,chartOptions,pieSections) {

    ctx.save();
    let radius = chartOptions.outterCircleRadius +  Math.floor(30*chartOptions.ratioOutterCircle);
    for(let i = 0; i < pieSections.length; i++) {
      let angle = pieSections[i].angle - 30;

      //could be use again
      let icon = new Image();
      let iconCrd =   rot(chartOptions.center.x , chartOptions.center.y, angle , radius);
      let iconRect = {
        width: Math.floor(30*chartOptions.ratioOutterCircle),
        height: Math.floor(30*chartOptions.ratioOutterCircle)
      };

      let iconRectOffset = {
        offsetX: iconRect.width/2,
        offsetY: iconRect.height/2
      };

      let path = `./${pieSections[i].category}.png`;
      icon.src = path;
      //console.log(icon,iconCrd.x - iconRectOffset.offsetX, iconRect.y - iconRectOffset.offsetY,iconRect.width,iconRect.height)
      icon.onload = () => {
      ctx.drawImage(icon, iconCrd.x - iconRectOffset.offsetX, iconCrd.y - iconRectOffset.offsetY, iconRect.width, iconRect.height);
      }
    }
    ctx.restore();
 }


function drawIcon(ctx,chartOptions,pieSections) {


    let radius = chartOptions.outterCircleRadius +  Math.floor(35*chartOptions.ratioOutterCircle);
    for(let i = 0; i < pieSections.length; i++) {
      ctx.save();
      let angle = pieSections[i].angle - 30;

      //could be use again
      let icon = new Image();
      let iconCrd =   rot(chartOptions.center.x , chartOptions.center.y, angle , radius);
      let iconRect = {
        width: Math.floor(45*chartOptions.ratioOutterCircle),
        height: Math.floor(45*chartOptions.ratioOutterCircle)
      };

      let iconRectOffset = {
        offsetX: iconRect.width/2,
        offsetY: iconRect.height/2
      };

      icon.src = pieSections[i].iconLink;
      //console.log(icon,iconCrd.x - iconRectOffset.offsetX, iconRect.y - iconRectOffset.offsetY,iconRect.width,iconRect.height)
      ctx.imageSmoothingEnabled = true;
      ctx.mozImageSmoothingEnabled = true;
      ctx.webkitImageSmoothingEnabled = true;
      ctx.msImageSmoothingEnabled = true;
      ctx.drawImage(icon, iconCrd.x - iconRectOffset.offsetX, iconCrd.y - iconRectOffset.offsetY, iconRect.width, iconRect.height);
      // let imgd = ctx.getImageData(iconCrd.x - iconRectOffset.offsetX, iconCrd.y - iconRectOffset.offsetY, iconRect.width, iconRect.height);
      // let pix = imgd.data;
      // ctx.globalCompositeOperation = "source-atop";
      // ctx.globalAlpha = 0.3;
      // ctx.fillStyle = pieSections[i].pieColor;
      // ctx.fillRect(iconCrd.x - iconRectOffset.offsetX, iconCrd.y - iconRectOffset.offsetY, iconRect.width, iconRect.height);
      // ctx.globalCompositeOperation = "source-over";
      // ctx.globalAlpha = 1.0;
      // ctx.restore();
    }

 }

function drawScore(ctx,chartOptions,pieSections,color) {
  ctx.save();
  for(let i = 0; i < 6; i ++) {
    let angle = pieSections[i].angle - 30;
    let scoreCrd = getScoreCrd(chartOptions.center.x, chartOptions.center.y, chartOptions.innerCircleRadius, angle,pieSections[i].score * chartOptions.ratioOutterCircle );

    //score tips Rectangle w/h
    let scoreRec = {
      high:30,
      width:40
    }

    let scoreRecOffset = {
      offsetX: scoreRec.width/2,
      offsetY: scoreRec.high/2
    }
    /*Tiprec fillstyle*/
    ctx.fillStyle = 'rgba(30,30,30,0.7)';
    ctx.fillRect(scoreCrd.x - scoreRecOffset.offsetX, scoreCrd.y - scoreRecOffset.offsetY, scoreRec.width, scoreRec.high);
    /*Tipscore fillstyle*/
    ctx.fillStyle = '#fcfdff';
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
    ctx.font = "100 20px helvetica";
    ctx.fillText(`${pieSections[i].score}`, scoreCrd.x, scoreCrd.y);
  }
  ctx.restore();
}

// get the middle crd for each pies
function getScoreCrd(x,y,innerCircleRadius,angle,score) {
    let h = score / 2 + innerCircleRadius;    //innerCircleRadius
    var rad = angle * (Math.PI / 180);
    let crd = {  x: x + h * Math.cos(rad),
                y: y - h * Math.sin(rad)
              };
    return crd;
}


function rot(x,y,angle,offset) {

    var rad = angle * (Math.PI / 180);
    let point = {
                    x: x + offset * Math.cos(rad),
                    y: y - offset * Math.sin(rad)
                  };
    return point;
}

function drawPies(ctx,chartOptions,pieSections,n,indexPass,draw) {
  ctx.save();
  let pieCount = pieSections.length;
  ctx.setLineDash([]);
  ctx.lineJoin = 'round';
  ctx.lineCap = 'round';
  ctx.lineWidth = 1;

  for(let i = 0; i < 6; i ++) {
      if(typeof indexPass === 'number') {
        if(draw) {
          if(indexPass !== i) continue;
        } else {
          if(indexPass === i) continue;
        }
      }


    //default h = 0
    let h = 0;
    // hight of Pie
    if(pieSections[i].score > n ) {
       h = n;
     } else {
       h = pieSections[i].score;
     }

    let pieHeight = Math.floor(h * chartOptions.ratioOutterCircle);

    let hsl = calColor(h);
    let color = `hsl(${hsl.h},${hsl.s}%,${hsl.l}%)`;
    ctx.fillStyle = color;

    //TODO refect here
    let angle = pieSections[i].angle || (- 60 * i + 60);
    //new center of pie
    let offset = pieSections[i].centerCrd || rot(chartOptions.center.x, chartOptions.center.y, angle-30, chartOptions.centeroffset);
    ctx.beginPath();
    let radius = pieSections[i].radius || chartOptions.innerCircleRadius + pieHeight;

    let point1 =  pieSections[i].baseLine ? pieSections[i].baseLine.point1 : rot(offset.x , offset.y, angle, chartOptions.innerCircleRadius);
    ctx.moveTo(point1.x, point1.y);

    let point2 =  rot(offset.x, offset.y, angle, radius);
    ctx.lineTo(point2.x, point2.y);
    ctx.arc(offset.x, offset.y, radius , (- angle/360) * Math.PI * 2, ((- angle  + 60)/360) * Math.PI * 2, false);

    //let point3 = rot(offset.x, offset.y, angle - 60 , radius);
    //ctx.lineTo(point3.x, point3.y);

    let point4 = pieSections[i].baseLine ? pieSections[i].baseLine.point4 : rot(offset.x, offset.y, angle - 60  , chartOptions.innerCircleRadius);
    ctx.lineTo(point4.x, point4.y);
    ctx.arc(offset.x, offset.y, chartOptions.innerCircleRadius ,((- angle + 60)/360) * Math.PI * 2, (- angle/360) * Math.PI * 2, true);

    ctx.closePath();

    ctx.shadowColor = 'rgb(132, 154, 155)';
    ctx.shadowBlur = 2;
    ctx.shadowOffsetX = 1;
    ctx.shadowOffsetY = 2;
    ctx.fill();


    if( h === pieSections[i].score && !pieSections[i].radius) {
      pieSections[i].pieColorHsl = hsl;
      pieSections[i].pieColor = color;
      pieSections[i].radius = radius;
      pieSections[i].baseLine.point2 = point2;
      pieSections[i].baseLine.point3 = rot(pieSections[i].centerCrd.x, pieSections[i].centerCrd.y, pieSections[i].angle - 60 , chartOptions.innerCircleRadius + pieHeight);
      //pieSections[i].mouseIn = false;

      pieSections[i].isInPath = (crd,pieSection,pieChartFrame) => {
        ctx.save();
        ctx.beginPath();
        let radius = pieSection.radius;
        let point1 =  pieSection.baseLine.point1 ;
        ctx.moveTo(point1.x, point1.y);
        let point2 = pieSection.baseLine.point2;
        ctx.lineTo(point2.x, point2.y);
        ctx.arc(pieSection.centerCrd.x, pieSection.centerCrd.y, pieSection.radius , (- pieSection.angle/360) * Math.PI * 2, ((- pieSection.angle  + 60)/360) * Math.PI * 2, false);
        // let point3 = pieSection.baseLine.point3;
        // ctx.lineTo(point3.x, point3.y);
        let point4 = pieSection.baseLine.point4;
        ctx.lineTo(point4.x, point4.y);
        ctx.arc(pieSection.centerCrd.x, pieSection.centerCrd.y, chartOptions.innerCircleRadius ,((- pieSection.angle + 60)/360) * Math.PI * 2, (- pieSection.angle/360) * Math.PI * 2, true);

        let isIn = ctx.isPointInPath(crd.x, crd.y);

        if(pieSection.mouseIn && !isIn ) {
          pieSection.mouseIn = false;
          //rerender whole chart
          pieChartFrame(100);
        }
        ctx.closePath();
        ctx.restore();
        return isIn;
      }
    }


    if(!pieSections[i].centerCrd) {
        //offset
        pieSections[i].centerCrd = {
          x:offset.x,
          y:offset.y,
        };
        pieSections[i].baseLine = {
          point1:point1,
          point4:point4
        };
        pieSections[i].angle = angle;
        pieSections[i].index = i;
    }
  }


  ctx.restore();
}



function drawPiesBase(ctx,chartOptions,fillStyle) {
    ctx.save();
  //draw line
  let pieCount = 6;
  for(let i = 0; i < pieCount; i ++) {

    let angle =   - 60 * i + 60;
    let h = 100;
    let  pieHeight = Math.floor(h * chartOptions.ratioOutterCircle);

    //TODO ??????????????????????????
    ctx.fillStyle = fillStyle;
    let offset = rot(chartOptions.center.x, chartOptions.center.y, angle-30, chartOptions.centeroffset);
    ctx.beginPath();
    let radius = chartOptions.innerCircleRadius + pieHeight;
    //could be use again
    let point1 = rot(offset.x , offset.y, angle   , chartOptions.innerCircleRadius);
    ctx.moveTo(point1.x, point1.y);

    let point2 = rot(offset.x, offset.y, angle , radius);
    ctx.lineTo(point2.x, point2.y);

    ctx.arc(offset.x, offset.y, radius , (- angle/360) * Math.PI * 2, ((- angle  + 60)/360) * Math.PI * 2, false);

     let point4 = rot(offset.x, offset.y, angle - 60  , chartOptions.innerCircleRadius);
     ctx.lineTo(point4.x, point4.y);
     ctx.arc(offset.x, offset.y, chartOptions.innerCircleRadius ,((- angle + 60)/360) * Math.PI * 2, (- angle/360) * Math.PI * 2, true);

    ctx.closePath();
    ctx.fill();
  }
    ctx.restore();
}

function scoreFrame(ctx,chartOptions,progress,greenScore,maxPieScore) {
   ctx.save();
   // 100 sacle
   //let score = Math.floor(greenScore * n / 100);

   if(maxPieScore > progress) {
       score =  Math.floor(greenScore * progress / maxPieScore);
    } else {
      score = greenScore;
    }

   //fontColor
   ctx.fillStyle = '#20421f'; //'rgb(66, 70, 76)';
   ctx.textAlign = 'center';
   ctx.textBaseline = 'middle';
   ctx.font = `600 ${Math.floor(14 * chartOptions.ratioOutterCircle)}px helvetica`;
   ctx.fillText('GreenScore', chartOptions.center.x, chartOptions.center.y + Math.floor(20 * chartOptions.ratioOutterCircle));
   ctx.shadowColor = 'rgba(140,140,140,1)';
   ctx.shadowBlur = Math.floor(2 * chartOptions.ratioOutterCircle);
   ctx.shadowOffsetX = Math.floor(1 * chartOptions.ratioOutterCircle);
   ctx.shadowOffsetY = Math.floor(1 * chartOptions.ratioOutterCircle);


   let hsl = calColor(score)
   ctx.fillStyle = `hsl(${hsl.h},${hsl.s}%,${hsl.l}%)`;

   ctx.font = `${Math.floor(48 * chartOptions.ratioOutterCircle)}px helvetica`;
   ctx.fillText(score.toFixed(0), chartOptions.center.x, chartOptions.center.y - Math.floor(10 * chartOptions.ratioOutterCircle));
   ctx.restore();
}

function drawBaseCircle(ctx,chartOptions,chartbaseCircleColor,globalAlpha) {
  ctx.save();
  ctx.globalAlpha = globalAlpha || 1;
  ctx.beginPath();
  ctx.fillStyle = chartbaseCircleColor;
  ctx.arc(chartOptions.center.x,chartOptions.center.y, chartOptions.outterCircleRadius,0,Math.PI*2,true);
  ctx.closePath();
  ctx.fill();
  ctx.restore();
}

function drawBlock(ctx,chartOptions,chartbaseCircleColor,globalAlpha) {
//innercircle
  ctx.save();
  ctx.globalAlpha = globalAlpha || 1;
  ctx.beginPath();
  ctx.strokeStyle = 'rgba(132, 154, 155,0.8)';
  let shdowInset = 5;
  ctx.arc(chartOptions.center.x,chartOptions.center.y, chartOptions.outterCircleRadius + shdowInset,0,Math.PI*2,true);
  ctx.lineWidth = shdowInset + shdowInset;
  ctx.closePath();
  ctx.shadowColor = 'rgba(132, 154, 155,0.4)';
  ctx.shadowBlur = Math.floor(14*chartOptions.ratioOutterCircle);
  ctx.shadowOffsetX = Math.floor(5*chartOptions.ratioOutterCircle);
  ctx.shadowOffsetY = Math.floor(3*chartOptions.ratioOutterCircle);
  ctx.stroke();
  ctx.restore();

  //outercircleBlock
  ctx.save();
  ctx.beginPath();
  ctx.strokeStyle = 'white';
  //???????????????????????????????????????????????
  let outterCircleRadius = 1.2  *  150 * chartOptions.ratioOutterCircle;
  let outterCircleWidth = 0.4 * 150 * chartOptions.ratioOutterCircle;
  ctx.arc(chartOptions.center.x,chartOptions.center.y,outterCircleRadius,0,Math.PI*2,true);
  ctx.lineWidth = outterCircleWidth;
  ctx.closePath();
  ctx.stroke();
  ctx.restore();



  //shinny refelection
  // ctx.beginPath();
  // ctx.arc(chartOptions.center.x,chartOptions.center.y, chartOptions.outterCircleRadius - 50 , - Math.PI / 6 , - Math.PI /3, true);
  // ctx.lineWidth = 30;
  // ctx.lineCap = 'round';
  // ctx.strokeStyle = 'rgba(20,20,255,0.7)';
  // ctx.stroke()
  // ctx.closePath();
  // ;
  // //ctx.fill();
  // ctx.restore();
}



function drawLableBorder(ctx,chartOptions,pieSections,toggle) {
  ctx.save();
  for(let i = 0; i < 6; i ++) {
      let angle =  pieSections[i].angle;
      ctx.strokeStyle = pieSections[i].pieColor;
      ctx.beginPath()
      ctx.arc(pieSections[i].centerCrd.x,pieSections[i].centerCrd.y, chartOptions.labelBorderRadius, (- angle/360) * Math.PI * 2, ((- angle  + 60)/360) * Math.PI * 2, false);

      if(toggle === i) {
        let color = pieSections.color;
        ctx.strokeStyle = color;
      } else {
        ctx.strokeStyle = 'rgba(132, 154, 155,0.2)';
      }

      ctx.lineWidth = chartOptions.labelBorderRadius - chartOptions.outterCircleRadius;
      // ctx.shadowColor = 'rgb(132, 154, 155)';
      // ctx.shadowBlur = 2;
      // ctx.shadowOffsetX = 1;
      // ctx.shadowOffsetY = 2;

      ctx.stroke()
      ctx.closePath();

  }
  ctx.restore();
}

function drawLableBorderMoouseIn(ctx,chartOptions,pieSection,bordercolor) {
  ctx.save();

      let angle =  pieSection.angle;
      ctx.strokeStyle = pieSection.pieColor;
      ctx.beginPath()
      ctx.arc(pieSection.centerCrd.x,pieSection.centerCrd.y, chartOptions.labelBorderRadius, (- angle/360) * Math.PI * 2, ((- angle  + 60)/360) * Math.PI * 2, false);
      ctx.strokeStyle = bordercolor;
      ctx.lineWidth = chartOptions.labelBorderRadius - chartOptions.outterCircleRadius;
      ctx.stroke()
      ctx.closePath();

  ctx.restore();
}



function calColor(greenScore) {
  let  s = 86;
  let l = 50;
   if(greenScore < 30) {
      let grad = greenScore / 30;
      return {h:15, s:s, l:l};
  } else if(greenScore >= 30   && greenScore < 60) {
              let grad = (greenScore - 30)/20;
              return {h:Math.floor(15 + 40*grad),s:s,l:l};
  } else if(greenScore >= 50  && greenScore < 75) {
              let grad = (greenScore - 50)/25;
              return {h:Math.floor(55 + 35*grad), s:s, l:l};
  } else if(greenScore >= 75   && greenScore <= 100) {
              let grad = (greenScore - 75)/25;
              return {h:Math.floor(90 + 40*grad), s:s, l:l};
  }
};


function isInPath (ctx,crd,x,y,r){
    ctx.arc(x,y, r, 0, Math.PI * 2);
    let  isInPath = ctx.isPointInPath(crd.x, crd.y);
    ctx.closePath();
    return isInPath;
}

/*--- not used ---*/
function addGradient(ctx,chartOptions,crd,hsl) {
  let gradient = ctx.createRadialGradient(crd.x,crd.y,0,crd.x,crd.y,chartOptions.outterCircleRadius);
  let light = `hsl(${hsl.h},${hsl.s + 4}%,${hsl.l + 20}%)`
  let normal = `hsl(${hsl.h},${hsl.s}%,${hsl.l}%)`
  let dark = `hsl(${hsl.h - 10},${hsl.s-3}%,${hsl.l-5}%)`
  gradient.addColorStop(.1,light);
  gradient.addColorStop(.5,normal);
  gradient.addColorStop(1,dark);
  return gradient;
}



function drawLines(ctx,chartOptions) {
  ctx.save();
  //ctx.strokeStyle = '#f2f9fc';
  ctx.strokeStyle = '#777777';
  ctx.setLineDash([1,1]);
  ctx.lineWidth =2;
  for(let i = 0; i < 6; i ++){
    ctx.translate(chartOptions.center.x, chartOptions.center.y);
    ctx.rotate(1/3 * Math.PI);
    ctx.translate(-chartOptions.center.x, -chartOptions.center.y);
    ctx.beginPath();
    ctx.moveTo(chartOptions.center.x,0);
    ctx.lineTo(chartOptions.center.x,chartOptions.center.y - chartOptions.innerCircleRadius);
    ctx.stroke();
  }
  ctx.restore();
}

function drawConcentricCircle(ctx,chartOptions) {
  ctx.save();
    ctx.setLineDash([1,1]);
    ctx.lineWidth = 0.3;
    //ctx.strokeStyle = '#f2f9fc';
    ctx.strokeStyle = '#777777';
    for(let i = 1 ; i <= 2; i++) {
    ctx.beginPath();
    ctx.arc(chartOptions.center.x,chartOptions.center.y, 50 + chartOptions.center.radius*i, 0, Math.PI*2, true);
    ctx.stroke();
  }
  ctx.restore();
}


$(document).ready(Pie);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console