如何在另一个div中有一个div

时间:2017-01-04 07:04:23

标签: html css canvasjs

您好我使用canvasjs制作图表。制作特定图表非常简单,但问题是我想要有两个不同的图表,这样饼图就会出现在圆环图中。

我无法在里面做另一个。我用过

display : inline-block 

表示two div id但没有效果。有人可以建议我如何实现这一目标。

这是我的代码 -

<!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>

CSS仅包含显示块的行。

I want something like this

1 个答案:

答案 0 :(得分:0)

这可能不是一个完美的答案,但它会指出你正确的方向:

我们的想法是将画布的背景颜色更改为透明,并使用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();

这是fiddle