可视化数据分析(三) 一个简单的散点图的实现

代码如下:


<!DOCTYPE>
<html>
<meta charset="utf-8">
<style>
    text{
        font: 10px sans-serif;
    }
    .axis path,
    .axis line{
            fill: none;
            stroke: black;
            shape-rendering: crispEdges;
    }
</style>
<body>
    <h1>The scatter diagram</h1>
    <script src="d3/d3.v3.js"></script>
    <script>
    //散点图的圆心的位置
    var center =[ [0.5,0.5],
                  [0.7,0.8],
                  [0.4,0.9],
                  [0.11,0.32],
                  [0.88,0.25],
                  [0.75,0.12],
                  [0.5,0.1],
                  [0.2,0.3],
                  [0.4,0.1],
                  [0.6,0.7]
                ];
    //设置坐标轴的横轴和纵轴的长度
    var xAxisWidth = 400;
    var yAxisWidth = 400;
    //生成svg图
    var width = 550;
    var height = 550;
    var svg = d3.select("body")
                .append("svg")
                .attr("width" , width )
                .attr("height", height );
    //设置外边距
    var padding = {
        top:30,
        right:30,
        bottom:30,
        left:30
    };
    </script>

    <script>
    function draw()
    {
        //设置比例尺
        var xScale = d3.scale.linear()
                       .domain([0,1.2*d3.max(center,function(d){
                               return d[0];
                               })])
                       .range([0,xAxisWidth]);

        var yScale = d3.scale.linear()
                       .domain([0,1.2*d3.max(center,function(d){
                               return d[1];
                               })])
                       .range([yAxisWidth,0]);
        //画坐标轴
        svg.selectAll("g").remove();

        var xAxis = d3.svg.axis()
                          .scale(xScale)
                          .orient("bottom");

        svg.append("g")
           .attr("class","axis")
           .attr("transform","translate(" + padding.left+ ","+(height-padding.bottom) + ")" )
           .call(xAxis);

        var yAxis = d3.svg.axis()
                      .scale(yScale)
                      .orient("left");

        svg.append("g")
           .attr("class","axis")
           .attr("transform","translate(" + padding.left + ","+(height-yAxisWidth-padding.top) + ")" )
           .call(yAxis);
        //描点
        var circle = svg.selectAll("circle")
                        .data(center)
                        .enter()
                        .append("circle")
                        .attr("fill","black")
                        .attr("cx",function(d){
                                return padding.left + xScale(d[0]);
                                })
                        .attr("cy",function(d){
                                return height-padding.bottom -yScale(d[1]);
                                })
                        .attr("r",5)
                        .transition()
                        .attr("fill","gray")
                        .duration(1000)
                        .delay(500);
    }
    </script>

    <script>
    draw();
    </script>
    <br>
    <button type="button" onclick="add()">添加一个点</button>
    <script>
    function add()
    {
        center.push([Math.random(),Math.random()]);
`       draw();
    }
    </script>
</body>

</html>

效果图如下:


这里写图片描述


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
app.title = '气泡图'; var data = [ [['2015/1/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/1/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/2/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/3/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/4/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/5/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/6/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/7/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/8/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/9/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/10/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/11/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2015/12/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2016/5/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2017/6/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2018/9/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2019/2/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2019/5/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"'], ['2019/7/1',10,'0','系列"游戏1"点"2015/01/01(2015/01/01,0.6)"']], [['2015/1/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/1/6',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/2/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/3/24',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/4/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/5/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/6/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/7/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/8/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/9/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/10/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/11/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/12/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2016/1/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2017/1/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2018/1/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2019/1/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2019/1/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2019/1/1',20,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"']], [['2015/1/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/2/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/3/23',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/4/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/5/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/6/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/7/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/8/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/9/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/10/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/11/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/12/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2016/1/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2016/2/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2016/3/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2016/4/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2017/5/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2018/6/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2019/7/1',30,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"']], [['2015/1/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/2/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/3/23',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/4/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/5/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/6/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/7/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/8/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/9/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/10/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/11/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2015/12/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2016/1/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2016/2/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2016/3/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2016/4/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2017/5/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2018/6/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"'], ['2019/7/1',40,'0','系列"游戏2"点"2015/01/01(2015/01/01,0.6)"']], [['2015/1/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/2/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/3/23',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/4/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/5/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/6/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/7/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/8/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/9/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/10/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/11/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2015/12/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2016/1/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2016/2/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2016/3/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2016/4/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2017/5/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2019/6/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"'], ['2019/7/1',50,'0','系列"游戏3"点"2015/01/01(2015/01/01,0.6)"']], [['2015/1/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/2/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/3/23',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/4/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/5/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/6/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/7/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/8/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/9/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/10/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/11/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/12/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2016/1/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2016/2/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2016/3/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2016/4/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2017/5/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2018/6/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2019/7/1',60,'0','系列"在游戏2范围内"点"2015/01/01(2015/01/01,0.6)"']], [['2015/1/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/2/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/3/23',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/4/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/5/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/6/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/7/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/8/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/9/14',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/10/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/11/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2015/12/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2016/1/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2016/2/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2016/3/16',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2016/4/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2017/5/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2018/6/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"'], ['2019/7/1',70,'0','系列"在游戏3范围内"点"2015/01/01(2015/01/01,0.6)"']] ]; option = { backgroundColor: new echarts.graphic.RadialGradient(0.3, 0.3, 0.8, [{ offset: 0, color: '#f7f8fa' }, { offset: 1, color: '#cdd0d5' }]), title: { text: '2015年装备重大游戏1与/游戏2情况统计', subtext:'', x:'center', y:'top', textAlign:'center' }, legend: { orient: 'horizontal', // 'vertical' x: 'center', // 'center' | 'left' | {number}, y: 'bottom', // 'center' | 'bottom' | {number} //backgroundColor: '#fff', // borderColor: 'rgba(178,34,34,0.8)', // borderWidth: 4, padding: 5, // [5, 10, 15, 20] itemGap: 20, data: ['游戏1', '游戏2', '游戏3', '游戏2', '游戏3', '在游戏2范围内', '在游戏3范围内'] }, xAxis: { type: 'time', name:'时间', splitLine: { show: true, lineStyle:{ color: ['#315070'], width: 1, type: 'solid' }   } }, yAxis: { type: 'value', name:'类型', axisLabel:{ formatter : function(params){ return ""; } }, splitLine: { show: true, lineStyle:{ color: ['#4c5055'], width: 1, show:false, type: 'solid' }   } }, series: [{ //图形 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow' name: '游戏1', data: data[0], type: 'scatter', symbol:"triangle", symbolSize:12, label: { emphasis: { show: true, formatter: function (param) { return param.data[3]; }, position: 'top' } }, itemStyle: { normal: { shadowBlur: 10, shadowColor: 'rgba(80, 56, 50, 0.5)', shadowOffsetY: 5, color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(76, 80, 85)' }, { offset: 1, color: 'rgb(85, 85, 72)' }]) } } }, { name: '游戏2', data: data[1], type: 'scatter', symbol:"rect", symbolSize: 12, label: { emphasis: { show: true, formatter: function (param) { return param.data[3]; }, position: 'top' } }, itemStyle: { normal: { shadowBlur: 10, shadowColor: 'rgba(250, 196, 122, 0.5)', shadowOffsetY: 5, color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(250, 139, 35)' }, { offset: 1, color: 'rgb(246, 144, 25)' }]) } } }, { name: '游戏3', data: data[2], type: 'scatter', symbol:"rect", symbolSize:12, label: { emphasis: { show: true, formatter: function (param) { return param.data[3]; }, position: 'top' } }, itemStyle: { normal: { shadowBlur: 10, shadowColor: 'rgba(25, 100, 150, 0.5)', shadowOffsetY: 5, color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(63, 167, 228)' }, { offset: 1, color: 'rgb(62, 157, 207)' }]) } } }, { name: '游戏2', data: data[3], type: 'scatter', symbol:"circle", symbolSize:12, label: { emphasis: { show: true, formatter: function (param) { return param.data[3]; }, position: 'top' } }, itemStyle: { normal: { shadowBlur: 10, shadowColor: 'rgba(250, 196, 122, 0.5)', shadowOffsetY: 5, color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(250, 139, 35)' }, { offset: 1, color: 'rgb(246, 144, 25)' }]) } } }, { name: '游戏3', data: data[4], type: 'scatter', symbol:"circle", symbolSize: 12, label: { emphasis: { show: true, formatter:15, position: 'top' } }, itemStyle: { normal: { shadowBlur: 10, shadowColor: 'rgba(25, 100, 150, 0.5)', shadowOffsetY: 5, color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(63, 167, 228)' }, { offset: 1, color: 'rgb(62, 157, 207)' }]) } } }, { name: '在游戏2范围内', data: data[5], type: 'scatter', symbol:"diamond", symbolSize: 12, label: { emphasis: { show: true, formatter: function (param) { return param.data[3]; }, position: 'top' } }, itemStyle: { normal: { shadowBlur: 10, shadowColor: 'rgba(250, 196, 122, 0.5)', shadowOffsetY: 5, color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(250, 139, 35)' }, { offset: 1, color: 'rgb(246, 144, 25)' }]) } } }, { name: '在游戏3范围内', data: data[6], type: 'scatter', symbol:"diamond", symbolSize: 12, label: { emphasis: { show: true, formatter: function (param) { return param.data[3]; }, position: 'top' } }, itemStyle: { normal: { shadowBlur: 10, shadowColor: 'rgba(25, 100, 150, 0.5)', shadowOffsetY: 5, color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: 'rgb(63, 167, 228)' }, { offset: 1, color: 'rgb(62, 157, 207)' }]) } } } ] };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值