码迷,mamicode.com
首页 > 其他好文 > 详细

[D3] 2. Basics of SVG

时间:2015-06-03 23:17:59      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

1. svg should use ‘fill‘ prop instead ‘background-color‘
2. svg width & height no need ‘px‘
3. attr(function(data_val, index){})
4. create svg, d3.select(‘selector‘).append(‘svg‘).attr(‘width‘, xxx).attr(‘height‘, xx)
5. svg should use ‘rect‘ instead of ‘div‘

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../ventor/d3.min.js"></script>
    <style type="text/css">

        body
        {
            padding-top: 50px;
            padding-left: 100px;

        }

        #chartArea {
            width: 400px;
            height: 300px;
            background-color: #CCC;
        }

        .bar
        {
            display: inline-block;
            width: 20px;
            height: 75px; /* Gets overriden by D3-assigned height below */
            margin-right: 2px;
            fill: teal; /* SVG doesn‘t have background prop, use fill instead*/
            z-index:99;
        }

    </style>
</head>
<body>
<section id="chartArea"></section>
<script>
    var dataset = [3,5,7,9,4,6];
    var svg = d3.select(#chartArea).append(svg)
            .attr(width, 400)
            .attr(height, 300); //svg deosn‘t need ‘px‘

    svg.selectAll(div)
            .data(dataset)
            .enter()
            .append(rect)// svg doesn‘t have div, use rect instead
            .attr(class, "bar")
            .attr(width, 20)
            .attr(x, function(each_data, index){
                return index * 22;
            })
            .attr(y, function(each_data){
                return 300-each_data*10;
            })
            .attr(height, function(each_data, i){
                return each_data * 10;  // svg doesn‘t have ‘style‘
            });
</script>

<!--
    1. svg should use ‘fill‘ prop instead ‘background-color‘
    2. svg width & height no need ‘px‘
    3. attr(function(data_val, index){})
    4. create svg, d3.select(‘selector‘).append(‘svg‘).attr(‘width‘, xxx).attr(‘height‘, xx)
    5. svg should use ‘rect‘ instead of ‘div‘
    -->
</body>
</html>

 

[D3] 2. Basics of SVG

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4550414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!