Cara menggunakan html charts without javascript

Recently I was working on a project to improve our web app’s billing reports with pie charts that visualize customer usage. I wanted an implementation for the job with minimal foreseeable maintenance that kept our third-party dependencies low.

I thought a quick search on Stack Overflow would yield a copy/paste-able solution, but discovered there isn’t an agreed-upon approach.

Of the various approaches I saw, there are these three that caught my attention, and they all happen to use just HTML and CSS. Each approach has its own merits, and you’ll need to find the right approach for your use case.

Here’s a quick summary of what the implementation of the three approaches looks like:

Using CSS conic-gradient function


//HTML


// CSS
#shape {
 width: 300px;
 height: 300px;
 border-radius: 9999px;
 background: conic-gradient[
  LemonChiffon 0%, LemonChiffon 10%,
  LightGreen 10%, LightGreen 30%,
  WhiteSmoke 30%, WhiteSmoke 100%
 ];
}

Preview code snippet

Using SVG element


// HTML

 
  
  
 


// CSS
#shape svg {
 width: 300px;
 height: 300px;
 border-radius: 9999px;
 background-color: WhiteSmoke;
}

Preview code snippet

Using SVG


//HTML

 
  
  
 


// CSS
#shape svg {
 width: 300px;
 height: 300px;
 border-radius: 9999px;
 background-color: WhiteSmoke;
}

Preview code snippet

Each approach has its pros and cons, and offers different types of versatility:

The SVG element solution came out on top for me and for our use case: we currently don’t need chart animations/interactions and having the same implementation for both pie charts and donut charts is pretty useful.

The important thing to know about the solution is that it takes advantage of drawing overlapping circles [one for each segment of the pie chart] and that we use the `dash` feature of SVG to draw each segment. Dashes are normally used to define how to represent breaks in lines, but you can also use them in other SVG objects like circles, and by defining the dashes carefully we can have one dash per circle to generate each slice of the pie chart.

Here’s a quick overview of how the example above works.

We can implement this as a React component to make it easier for other contributors to reuse:


const CIRCLE_RADIUS = 50 / 2;
const STROKE_WIDTH = CIRCLE_RADIUS * 2;
const CIRCUMFERENCE = 2 * Math.PI * CIRCLE_RADIUS;
const DASH_OFFSET = CIRCUMFERENCE / 4;
 
const PieChart = [{
 backgroundColor = 'WhiteSmoke',
 strokeColorOptions = ['LemonChiffon', 'LightGreen'],
 values = [],
}] => {
 const outputStrokeColors = _.times[values.length].map[
   [i] => strokeColorOptions[i % strokeColorOptions.length]
 ];
 
 return [
   
    {values.map[[val, index] => {
     const strokeColor =
      outputStrokeColors[index % outputStrokeColors.length];
     const dashSize = [val / 100] * CIRCUMFERENCE;
     const gapSize = CIRCUMFERENCE - dashSize;
     // NOTE: if it's a full circle, then no need to provide a stroke dasharray
     const strokeDasharray =
      val === 100 ? 'none' : `${dashSize} ${gapSize}`;
     const accumulatedPriorPercentage = _.sum[values.slice[0, index]];
     const relativeOffset =
       [accumulatedPriorPercentage / 100] * CIRCUMFERENCE;
     const adjustedOffset = DASH_OFFSET - relativeOffset;
 
     return [
     
     ];
    }]}
   
 ];
};

There might be a point where requirements change and we’ll need to adopt another solution or revisit existing libraries. Until then, the lightweight and in-scope versatility of the SVG solution will serve us well.

Bài mới nhất

Chủ Đề