图表_柱状图_动画效果

js对象规划

js代码太多,需要复用,相当于类Class。
如:内容组织类/图文组件类(基本类)/图表组件类

效果

图片.png

使用

 var data = [
        ['javascript',0.4,'#ff7676'],
        ['java',0.122,'#ff7676'],
        ['c',0.22,'#ff7676'],
        ['c#',0.62,'#ff7676'],
        ['php',0.32,'#ff7676']
    ] //数据
    var cfg = {sortType:'default'}; // 配置
    // 构造函数实例化
    var c = new ChartBar($('.cb'),data,cfg);
    c.init();
    c.updateArr(data).init();// 更新数据
    c.updateSortType(type).init;// 更新排序

代码

/**
 * 柱状图
 * @param parent jQuery dom 父元素(生成的柱状图的父元素)
 * @param data 镶套arr格式数据 [['javascript',0.4,'#ff7676'],['java',0.6]] (名称,占比,颜色(可忽略))
 * @param cfg 配置数据 参考代码中数据
 * @constructor
 * @mehod init 实例化对象之后需要init初始化,柱状图展示 (视图层)
 * @mehod updateArr 更新展示数据,格式同上 (数据层 需init)
 * @mehod updateSortType 调整实例的排序方式 max/min/default(数据层 需init)
 * @mehod returnMaxFloat 获得最大占比
 */
var ChartBar = function ( parent , data , cfg) {

    parent = ( parent instanceof jQuery)?parent:$('body');
    data = ( data instanceof Array)?data:[['数据失效',1,'#ff7676']];
    cfg = cfg || {};

    this.setting = $.extend({},{
        css:{
            backgroundColor:'#99c0ff', // 默认百分比背景颜色
        },
        maxPreWidth:200,
        sortType:'default',// default/max/min
    },cfg)
    this.parent = parent;
    this.cfg = cfg; // 传入的配置文件
    this.id = ('sz_chart_bar_' +Date.now()); // 实例化柱状图的id
    console.log(this.setting);
    this.updateArr(data);

    var $component = $('<ul id="'+this.id+'" class="sz_chart_bar">');
    $component.appendTo(this.parent);
    $component.on(this.id+'_load',function () {
        setTimeout(function () {
            $component.find('.bg').css('width','100%');
            $component.find('.sz_chart_bar_per').css('opacity','1')
        },10)
    })

}
ChartBar.prototype = {
    // ui结构构造 视图层 绘制图层
    init : function () {
        var $component = $('#'+this.id).empty();
        var setting = this.setting;
        var max = this.returnMaxFloat();
        max = (max == 0) ? 1 : max;

        $.each(this.sortArr,function(idx,item){
            var line = $('<li class="sz_chart_bar_item">');
            var name = $('<em class="sz_chart_bar_title">');
            var rate = $('<div class="sz_chart_bar_percent">');
            var per = $('<em class="sz_chart_bar_per">');
            var bgStyle = 'style="background-color:'+ ( item[2] || setting.css.backgroundColor) +'"';
            rate.html( '<div class="bg" '+bgStyle+'></div>' );
            rate.css('width', item[1]/max * setting.maxPreWidth + 'px' );
            name.text( item[0]);
            per.text( (new Number(item[1]*100)).toFixed(2) + '%');
            line.append( name ).append( rate ).append( per );
            $component.append(line);
        });
        $component.trigger(this.id+'_load');
        return this;
    },
    // 数据层 更新数据
    updateArr :function( data ){
        data = ( data instanceof Array)?data:[['数据失效',1,'#ff7676']];
        this.arr = data;
        this.sortArr = data;
        this.updateSortType();
        return this;
    },
    // 数据层 数据排序
    updateSortType:function ( sortType ) {
        var type ;
        if(sortType){
            this.setting.sortType = sortType;
        }
        type = this.setting.sortType;
        if(type == 'default'){
            this.sortArr = this.arr;
        }else{
            var oldArr = this.arr.concat();
            this.sortArr = oldArr.sort(function (a,b) {
                if( type != 'max' ){
                    return a[1] - b[1];
                }
                return b[1] - a[1];
            })
        }
        return this;
    },
    // 返回最大值
    returnMaxFloat :function(){
        var max = 0;
        this.sortArr.forEach(function (item) {
            if(item[1]>max){
                max = item[1]
            }
        })
        return max;
    }
}
.sz_chart_bar{
    margin: 0 auto;
}
.sz_chart_bar_item{
    display: block;
    vertical-align: middle;
}
.sz_chart_bar_title,.sz_chart_bar_percent,.sz_chart_bar_per{
    display: inline-block;
    position: relative;
    font-size: 12px;
    line-height: 32px;
    height: 32px;
    vertical-align: middle;
}
.sz_chart_bar_title{
    width: 80px;
    text-align: right;
    padding: 0 10px;
}
.sz_chart_bar_percent{
    width: 0;
    height: 15px;
}
.sz_chart_bar_percent .bg{
    width: 0;
    height: 15px;
    border-radius: 4px;
    transition: width 3s;
}
.sz_chart_bar_per{
    padding: 0 7px;
    opacity: 0;
    transition: opacity 0.5s 1.6s;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,977评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 不久前更新完的电视剧微微一笑很倾城,据说特别火。今天下午看了几集,便也爱上看了。 整部剧还是偶像风,画面小巧,演员...
    三更笙阅读 478评论 0 3
  • 工作、爱情顺其自然,时候未到,不轻易点头! 安排好考研时间,其他神马都不想啦
    林凡_阅读 160评论 0 0
  • 有的妹子生活中真的炒鸡好看啊,但拍出来的照片就是没有本人辣么好看(真的好气喔是不是)。先撇开摄像师这个外在因素不说...
    今日排行榜阅读 419评论 0 0