Dashed Circular Bar with Percentage

前端 未结 1 1779
半阙折子戏
半阙折子戏 2021-01-28 13:35

I am trying to make a circular progress bar with dashed line. I programmatically create stroke-dasharray and stroke-dashoffset to draw a circle with pe

1条回答
  •  南方客
    南方客 (楼主)
    2021-01-28 14:00

    Not sure if this is somewhat in the range of what you are looking for. ( full snippet / demo below)

    Im not an expert on this subject, so there might be another option (like two half circles with different styling) - but what is basically done here is to lay another circle on top of the solid circle, and make sure it has the same stroke color as the page. This will then mask over gaps of the circle behind, (basically hide parts of the circle).

              
    

    css:

    .circle-dashes {
      stroke: #FFF;
      fill: none;
    }
    

    and remove

      stroke-linecap: round;
      stroke-linejoin: round;
    

    A few minor tweaks to fit your need, and hopefully you got it!

    If you take a look at the app with another background color, the changes might be more obvious.

    class CircularProgressBar extends React.Component {
      constructor(props) {
        super(props);
        this.state = {};
      }
    
      render() {
        // Size of the enclosing square
        const sqSize = this.props.sqSize;
        // SVG centers the stroke width on the radius, subtract out so circle fits in square
        const radius = (this.props.sqSize - this.props.strokeWidth) / 2;
        // Enclose cicle in a circumscribing square
        const viewBox = `0 0 ${sqSize} ${sqSize}`;
        // Arc length at 100% coverage is the circle circumference
        const dashArray = radius * Math.PI * 2;
        // Scale 100% coverage overlay with the actual percent
        const dashOffset = dashArray - dashArray * this.props.percentage / 100;
    
        return (
          
              
              
                
                
          
        );
      }
    }
    
    CircularProgressBar.defaultProps = {
      sqSize: 200,
      percentage: 25,
      strokeWidth: 10
    };
    
    class App extends React.Component {
      constructor(props) {
        super(props);
    
        this.state = {
          percentage: 25
        };
    
        this.handleChangeEvent = this.handleChangeEvent.bind(this);
      }
    
      handleChangeEvent(event) {
        this.setState({
          percentage: event.target.value
        });
      }
    
      render() {
        return (
          
    ); } } ReactDOM.render( , document.getElementById('app') );
    #app {
      margin-top: 40px;
      margin-left: 50px;
    }
    
    #progressInput {
      margin: 20px auto;
      width: 30%;
    }
    
    .circle-background,
    .circle-progress {
      fill: none;
    }
    
    .circle-background {
      stroke: #ffffd;
    }
    
    .circle-dashes {
      stroke: #fff;
      fill: none;
    }
    
    .circle-progress {
      stroke: #F99123;
    }
    
    
    

    0 讨论(0)
提交回复
热议问题