Chartjs-Make overlapping d3.js radar chart elements transparent

1đź‘Ť

âś…

Your description implies you will always have two charts (“before -> after”).

This solution uses SVG masks. While it would be doable with more charts, it gets more complicated. You would have to construct the <mask> elements dynamically.

/* Original radar chart design created by Nadieh Bremer - VisualCinnamon.com */
        
////////////////////////// Data ////////////////////////////// 

var data = [
    [//Before - red
    {axis:"First",value:0.49},
    {axis:"Banana",value:0.79},
    {axis:"Scratch",value:0.70},
    {axis:"Wheelie",value:0.09},
    {axis:"Pantalon",value:0.37},
    ],[//After - green
    {axis:"First",value:0.64},
    {axis:"Banana",value:0.44},
    {axis:"Scratch",value:0.37},
    {axis:"Wheelie",value:0.84},
    {axis:"Pantalon",value:0.81},
    ]
];
      
//////////////////////// Setup //////////////////////////////// 

var margin = {top: 50, right: 50, bottom: 50, left: 50},
    width = 300,
    height = 300;
var color = d3.scale.ordinal().range(["#CC333F","#53e87d"]);
var radarChartOptions = {
    w: width,                //Width of the circle
    h: height,                //Height of the circle
    margin: margin,         //The margins of the SVG
    opacityArea: 0.7,     //The opacity of the area of the blob
    color: color    //Color function
};

//////////////////////// RadarChart ///////////////////////////
  
RadarChart("radarChart", data, radarChartOptions);

function RadarChart(id, data, options) {

    var cfg = radarChartOptions;
    var allAxis = (data[0].map(function(i, j){return i.axis})),    //Names of each axis
        total = allAxis.length,                                //The number of different axes
        radius = Math.min(cfg.w/2, cfg.h/2),           //Radius of the outermost circle
        Format = d3.format('%'),                          //Percentage formatting
        angleSlice = Math.PI * 2 / total;              //The width in radians of each "slice"
    var rScale = d3.scale.linear()                  //Scale for the radius
       .range([0, radius]);
  
    var svg = d3.select('#' + id)     //Initiate the radar chart SVG
        .attr("width",  cfg.w + cfg.margin.left + cfg.margin.right)
        .attr("height", cfg.h + cfg.margin.top + cfg.margin.bottom)
        .attr("class", "radar"+id);

    svg.select('#bg').attr('r', radius);

    var dx = cfg.w/2 + cfg.margin.left,
        dy = cfg.h/2 + cfg.margin.top;

    svg.selectAll('mask')
        .attr('x', -dx)
        .attr('y', -dy);

    var source = svg.select('#radarSource')
    var wrapper = svg.select('#radarWrapper')
        .attr("transform", "translate(" + dx + "," + dy + ")");

    //The radial line function
    var radarLine = d3.svg.line.radial()
        .interpolate("cardinal-closed")
        .radius(function(d) { return rScale(d.value); })
        .angle(function(d,i) {    return i*angleSlice; });


    //Draw the blobs    
    source.selectAll("path")
        .data(data)
        .enter().append("path")
        .attr("id", function(d,i) {return 'area' + i;})
        .attr("d", function(d,i) { return radarLine(d); });

    //render and apply masks
    wrapper.selectAll("use")
        .data(data)
        .enter().append("use")
        .attr("xlink:href", function(d,i) {return '#area' + i;})
        .attr("mask", function(d,i) {return 'url(#mask' + i + ')';})
        .style("fill", function(d,i) { return cfg.color(i);})
        .style("fill-opacity", cfg.opacityArea);


}//RadarChart function
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<!-- Radar Chart -->
<svg id="radarChart" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <circle id="bg" />
    <g id="radarSource" />
    <mask id="mask0" maskUnits="userSpaceOnUse">
      <use xlink:href="#bg" fill="white" />
      <use xlink:href="#area1" fill="black" />
    </mask>
    <mask id="mask1" maskUnits="userSpaceOnUse">
      <use xlink:href="#bg" fill="white" />
      <use xlink:href="#area0" fill="black" />
    </mask>
  </defs>
  <g id="radarWrapper" />
</svg>

Leave a comment