Search code examples
htmlcsscanvasjs

How to have one div inside another div


Hi I am using canvasjs for making a chart. It is very simple to make a particular chart but here is the problem I want to have two different charts such that a pie chart would be inside of a doughnut chart.

I am not able to make one other inside. I have used the

display : inline-block 

for the two div id but no effect. Could someone please suggest me how we can make that happen.

This is my code-

<!DOCTYPE HTML>
<html>
<head>
<script src="http://canvasjs.com/assets/script/canvasjs.min.js"></script> 
<link type='text/css' rel="stylesheet" href='style.css' />
<script type="text/javascript">

window.onload = function () {
    var chart = new CanvasJS.Chart("chartContainer", {
        title:{
            text: "My First Chart in CanvasJS"              
        },
        data: [              
        {
            // Change type to "doughnut", "line", "splineArea", etc.
            type: "doughnut",
            dataPoints: [
                { label: "apple",  y: 10  },
                { label: "orange", y: 15  },
                { label: "banana", y: 25  },
                { label: "mango",  y: 30  },
                { label: "grape",  y: 28  }
            ]
        }
        ]
    });
    chart.render();
    var chart = new CanvasJS.Chart("chartContainerpie", {
        data: [              
        {
            // Change type to "doughnut", "line", "splineArea", etc.
            type: "pie",
            dataPoints: [
                { label: "apple",  y: 10  },
                { label: "orange", y: 15  },
                { label: "banana", y: 25  },
                { label: "mango",  y: 30  },
                { label: "grape",  y: 28  }
            ]
        }
        ]
    });
    chart.render();
}
</script>
</head>
<body>
<div>
<div id="chartContainerpie" style="height: 300px; width: 100%;></div>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
</div>
</body>
</html>

The CSS only contains the line for display block.

I want something like this


Solution

  • This is probably not the perfect answer but it will point you in right direction:

    The idea is to change the background color of canvas to transparent and overlap the two canvas using position css

    CSS

    #chartContainerpie{
      position: absolute;
      top: 130px;
      left: 0px;
    }
    #chartContainer{
      position: absolute;
      top: 0px;
      left: 0px;
    }
    

    JS

    var chart = new CanvasJS.Chart("chartContainer", {
      title: {
        text: "My First Chart in CanvasJS"
      },
      backgroundColor: "transparent",
      data: [{
        // Change type to "doughnut", "line", "splineArea", etc.
        type: "doughnut",
        dataPoints: [{
          label: "apple",
          y: 10
        }, {
          label: "orange",
          y: 15
        }, {
          label: "banana",
          y: 25
        }, {
          label: "mango",
          y: 30
        }, {
          label: "grape",
          y: 28
        }]
      }]
    });
    chart.render();
    var chart = new CanvasJS.Chart("chartContainerpie", {
        backgroundColor: "transparent",
      data: [{
        // Change type to "doughnut", "line", "splineArea", etc.
        indexLabelPlacement: "inside",
        indexLabelFontColor: "white",
        indexLabelFontSize: "14px",
        type: "pie",
        dataPoints: [{
          label: "apple",
          y: 10
        }, {
          label: "orange",
          y: 15
        }, {
          label: "banana",
          y: 25
        }, {
          label: "mango",
          y: 30
        }, {
          label: "grape",
          y: 28
        }]
      }]
    });
    chart.render();
    

    Here is fiddle