d3.js 封装一个方法更新柱状图,运用数据模板

    <script>
        var width = 400;
        var height = 400;
        //创建画布
        var svg = d3.select(‘body‘)
        .append(‘svg‘)
        .attr(‘width‘,width)
        .attr(‘height‘,height)

        var padding = {top:20,left:20,right:20,bottom:20}
        var rectstep = 35; //矩形的宽带偏白
        var rectwidth = 30;//矩形的宽

        var dataset = [216,86,158,76,203]
        // 根据数据填充矩形
        var rect = svg.selectAll(‘rect‘)
                      .data(dataset)
                      .enter()
                      .append(‘rect‘)
                      .attr(‘fill‘,‘steelblue‘)
                      .attr(‘x‘,function(d,i){
                        return padding.left + i * rectstep;
                      })
                      .attr(‘y‘,function(d,i){
                          return height - padding.bottom - d;
                      })
                      .attr(‘width‘,rectwidth)
                      .attr(‘height‘,function(d,i){
                          return d;
                      })
        // 根据数据填充文本内容
        var text = svg.selectAll(‘text‘)
                      .data(dataset)
                      .enter()
                      .append(‘text‘)
                      .attr(‘fill‘,‘white‘)
                      .attr(‘x‘,function(d,i){
                        return padding.left + i * rectstep;
                      })
                      .attr(‘y‘,function(d,i){
                          return height - padding.bottom - d;
                      })
                      .attr(‘text-anchor‘,‘middle‘)
                      .attr(‘font-size‘,‘14px‘)
                      .attr(‘dx‘,rectwidth/2)
                      .attr(‘dy‘,‘1em‘)
                      .text(function(d,i){
                          return d;
                      })

        function draw(){
            // 获取矩形的update部分
            var updateRect = svg.selectAll(‘rect‘)
                                .data(dataset);

            // 获取矩形的enter部分
                var enterRect = updateRect.enter();
            // 获取矩形的exit部分
                var exitRect = updateRect.exit();
            // update部分处理办法
                updateRect.attr("fill","steelblue")
                          .attr("x",function(d,i){
                              return padding.left + i * rectstep;
                          })
                          .attr("y",function(d,i){
                              return height - padding.bottom - d;
                          })
                          .attr("width",rectwidth)
                          .attr("height",function(d,i){
                              return d;
                          })
            //enter部分处理办法
                enterRect.append("rect")
                          .attr("fill","steelblue")
                          .attr("x",function(d,i){
                              return padding.left + i * rectstep;
                          })
                          .attr("y",function(d,i){
                              return height - padding.bottom - d;
                          })
                          .attr("width",rectwidth)
                          .attr("height",function(d,i){
                              return d;
                          })
            // exit部分处理办法
                exitRect.remove();
            // 获取文本的update部分
            var updateText = svg.selectAll(‘text‘)
                                .data(dataset)

            // 获取文本的enter部分
                var enterText = updateText.enter();
            // 获取文本的exit部分
                var exitText =  updateText.exit();
            // 文本update部分的处理办法
                updateText.attr("fill","white")
                          .attr("font-size","14px")
                          .attr("text-anchor","middle")
                          .attr("x",function(d,i){
                              return padding.left + i * rectstep;
                          })
                          .attr("y",function(d,i){
                              return height- padding.bottom - d;
                          })
                          .attr("dx",rectwidth/2)
                          .attr("dy","1em")
                          .text(function(d,i){
                             return d;
                          });
            // 文本enter部分处理办法
                enterText.append("text")
                         .attr("fill","white")
                         .attr("font-size","14px")
                         .attr("text-anchor","middle")
                         .attr("x",function(d,i){
                             return padding.left + i * rectstep;
                         })
                         .attr("y",function(d,i){
                             return height- padding.bottom - d;
                         })
                         .attr("dx",rectwidth/2)
                         .attr("dy","1em")
                         .text(function(d,i){
                             return d;
                         });
            // 文本exit部分的处理办法
                exitText.remove();
        }
    </script>

    <button onclick="mysort()">排序</button>
    <button onclick="myadd()">增加数据</button>
    <script>
        function mysort() {
            dataset.sort(d3.ascending);
            draw();
        }

        function myadd() {
            dataset.push( Math.floor(Math.random()*100));
            draw();
        }
    </script>

原文地址:https://www.cnblogs.com/webmc/p/11076230.html

时间: 2024-07-29 13:40:10

d3.js 封装一个方法更新柱状图,运用数据模板的相关文章

[3] 用D3.js做一个简单的图表吧!

本人的个人博客为: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处,谢谢. 前面说了几节,都是对文字进行处理,这一节中将用 D3.js 做一个简单的柱形图. 做柱形图有很多种方法,比如用 HTML 的 div 标签,或用 svg . 推荐用 SVG 来做各种图形.SVG 意为可缩放矢量图形(Scalable Vector Graphics),SVG 使用 XML 格式定义图像,不清楚什么是SVG的朋友请先在 w3cschools 学习下

js封装的方法

1.JS封装就是尽量把使用的方式简单化,内部逻辑和使用解耦.通俗的说就是使用的时候只需要知道参数和返回值,其他条件尽量不要使用人员进行设置. 2.JS封装的方法有函数方式.对象的方式.闭包的方式. 举例 1)函数方式function kk(a,b){   内部对a,b怎么处理就不需要关心了} 2)对象方式function kk(a,b){   this.x = a;   this.y = b;}var k = new kk(1,2);//通过面向对象的方式alert(k.x);3)闭包方式fun

分享一个远程更新目标库数据的存储过程

本文给大家分享一个远程更新目标库数据的存储过程,适用于更新列名一致,主键为Int类型,可远程链接的数据库.USE [Table]--切换到源表,就是数据最新的那个表GO/****** Object: StoredProcedure [dbo].[proc_DataUpdate] Script Date: 2018/5/4 15:08:56 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO(http://www.0831jlyy.com)--

关于tween.js 封装的方法

今天做的是匀速情况下div的运动.首先开始之前先了解运动的原理 A------------>>BA移动到B 这段距离是总距离 用一个变量保存下来:var dA移动到B 移动的总次数  用一个变量保存下来:var cA移动到B 每次移动的距离   用一个变量保存下来:var s function move(obj,name,target,dur,fn){ var timer; //控制定时器 var c=parseInt(dur/30); //移动的总步数 var start=parseFloa

【 D3.js 入门系列 --- 2 】 如何使用数据和选择元素

接着上一讲的内容,这次讨论如何选择元素和使用数据.    现在页面中有三行文字,代码为: [html] view plain copy <p>Hello World 1</p> <p>Hello World 2</p> <p>Hello World 3</p> 定义一个集合set,里面有三个元素: [html] view plain copy var set = ["I like dog","I like

D3.js 做一个简单的图表!

柱形图是一种最简单的可视化图标,主要有矩形.文字标签.坐标轴组成.本文为简单起见,只绘制矩形的部分,用以讲解如何使用 D3 在 SVG 画布中绘图. 一. 画布是什么 前几章的处理对象都是 HTML 的文字,没有涉及图形的制作. 要绘图,首要需要的是一块绘图的“画布”. HTML 5 提供两种强有力的“画布”:SVG 和 Canvas. 1.1. SVG 是什么 SVG,指可缩放矢量图形(Scalable Vector Graphics),是用于描述二维矢量图形的一种图形格式,是由万维网联盟制定

基于D3.js 绘制一个折柱混合图

测试题目:使用 D3 绘制一个折柱混合图,示例数据如下: data = [ ["时间", "销售额", "增长率(%)"], ["一月", 27506, 20.8], ["二月", 24399, 5.4], ["三月", 23120, 22], ["四月", 22053, 0.4], ["五月", 21221, 3.1], ["六月&qu

原生JS封装ajax方法

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 6 <title>Examples</title> 10 <script> 11 12 //将对象序列

js通过一个方法实现对象的深浅拷贝。

众所周知,对象的深浅拷贝是工作中肯定会遇到的问题.所以,今天考虑写个小的功能来记录一下 //type:boolean,true-deep,true为深拷贝, function extendCopy(type,item){ if(typeof type != "boolean" || typeof item !='object'){ return } var newObj = item.constructor ==="Array" ?[]:{}; if(type){