mock.js假数据+G2

原文链接:http://www.cnblogs.com/CyLee/p/6072399.html

官网:http://mockjs.com/examples.html#DPD

方便使用

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>柱状图</title>
    <!-- 引入 G2 文件 -->
    <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.g2-3.2.8/dist/g2.min.js"></script>
    <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
    <script src="http://mockjs.com/dist/mock.js"></script>
  </head>
  <body>    
    <!-- 创建图表容器 -->
    <button id="tu1">折线图</button>
    <button id="tu2">饼状图</button>
    <div id="c1"></div>
    <div id="c2"></div>
    <script>
        //调用mock方法模拟数据
    Mock.mock(
        'http://mockjs', {
            "array":[
                {
                    "genre" : 'zxst',     //模拟名称
                    "sold|1-100":100,
                    "item": '事件一',
                    "count":0.17,        
                    "action":'浏览网站',
                    "pv|50000-100000":100000, 
                    "a|1-100":100,
                    "b|1-100":100, 
                    "color"    : "@color",    //模拟色值
                    "date"     : "@date('yyyy-MM-dd')",  //模拟时间
                    "url"      : "@url()",     //模拟url
                    "content"  : "@cparagraph()" //模拟文本
                },
                {
                    "genre" : 'zxr',     //模拟名称
                    "sold|1-100":100,          //模拟年龄(1-100)
                    "item": '事件二',
                    "count":0.12, 
                    "a|1-100":100,
                    "b|1-100":100, 
                    "action":'放入购物车',
                    "pv|30000-50000":50000,  
                    "color"    : "@color",    //模拟色值
                    "date"     : "@date('yyyy-MM-dd')",  //模拟时间
                    "url"      : "@url()",     //模拟url
                    "content"  : "@cparagraph()" //模拟文本
                },
                {
                    "genre" : 'zxqd',     //模拟名称
                    "sold|1-100":100,          //模拟年龄(1-100)
                    "item": '事件三',
                    "count":0.31,   
                    "a|1-100":100,
                    "b|1-100":100, 
                    "action":'生成订单',
                    "pv|10000-30000":30000,  
                    "color"    : "@color",    //模拟色值
                    "date"     : "@date('yyyy-MM-dd')",  //模拟时间
                    "url"      : "@url()",     //模拟url
                    "content"  : "@cparagraph()" //模拟文本
                },
                {
                    "genre" : 'zxdst',     //模拟名称
                    "sold|1-100":100,          //模拟年龄(1-100)
                    "item": '事件四',
                    "count":0.20,   
                    "a|1-100":100,
                    "b|1-100":100, 
                    "action":'支付订单',
                    "pv|1000-10000":10000,  
                    "color"    : "@color",    //模拟色值
                    "date"     : "@date('yyyy-MM-dd')",  //模拟时间
                    "url"      : "@url()",     //模拟url
                    "content"  : "@cparagraph()" //模拟文本
                },
                {
                    "genre" : 'zxgft',     //模拟名称
                    "sold|1-100":100,          //模拟年龄(1-100)
                    "item": '事件五',
                    "count":0.20,   
                    "a|1-100":100,
                    "b|1-100":100, 
                    "action":'完成交易',
                    "pv|100-1000":1000,  
                    "color"    : "@color",    //模拟色值
                    "date"     : "@date('yyyy-MM-dd')",  //模拟时间
                    "url"      : "@url()",     //模拟url
                    "content"  : "@cparagraph()" //模拟文本
                }
            ]
            
        }
    );
    $("#tu1").click(function(){
     //ajax请求
        $.ajax({
            url        : "http://mockjs",    //请求的url地址
            dataType   : "json",   //返回格式为json
            async      : true, //请求是否异步,默认为异步,这也是ajax重要特性
            data       : {},    //参数值
            type       : "GET",   //请求方式
            beforeSend : function() {
                //请求前的处理
            },
            success: function(res) {
                //请求成功时处理
                console.log(res.array);
                var data=res.array
   
      // G2 对数据源格式的要求,仅仅是 JSON 数组,数组的每个元素是一个标准 JSON 对象。
      // Step 1: 创建 Chart 对象
      
      const chart = new G2.Chart({
        container: 'c1', // 指定图表容器 ID
        width : 600, // 指定图表宽度
        height : 300 // 指定图表高度
      });
      // Step 2: 载入数据源
      chart.source(data);
      // Step 3:创建图形语法,绘制柱状图,由 date 和 sold 两个属性决定图形位置,date 映射至 x 轴,sold 映射至 y 轴
      chart.line().position('date*sold')
      chart.point().position('date*sold').size(4).shape('circle').style({
         stroke: '#fff',
         lineWidth: 1
        });
        
        chart.interval().position('date*sold')
      // Step 4: 渲染图表
      chart.render();
            },
            complete: function() {
                //请求完成的处理
            },
            error: function() {
                //请求出错处理
            }
        });
    })
    $("#tu2").click(function(){ 
    //ajax请求
   $.ajax({
       url        : "http://mockjs",    //请求的url地址
       dataType   : "json",   //返回格式为json
       async      : true, //请求是否异步,默认为异步,这也是ajax重要特性
       data       : {},    //参数值
       type       : "GET",   //请求方式
       beforeSend : function() {
           //请求前的处理
       },
       success: function(res) {
           //请求成功时处理
           console.log(res.array);
           var data=res.array

 // G2 对数据源格式的要求,仅仅是 JSON 数组,数组的每个元素是一个标准 JSON 对象。
 // Step 1: 创建 Chart 对象
 
 var chart = new G2.Chart({
  container: 'c2',
  forceFit: true,
  height: window.innerHeight
});
chart.source(data, {
  percent: {
    formatter: function formatter(val) {
      val = val * 100 + '%';
      return val;
    }
  }
});
chart.coord('theta', {
  radius: 0.75
});
chart.tooltip({
  showTitle: false,
  itemTpl: '<li><span style="background-color:{color};" class="g2-tooltip-marker"></span>{name}: {value}</li>'
});
chart.intervalStack().position('count').color('item').label('count', {
  formatter: function formatter(val, item) {
    return item.point.item + ': ' + val*100+'%';
  }
}).tooltip('item*count', function(item, count) {
    count = count * 100 + '%';
  return {
    name: item,
    value: count
  };
}).style({
  lineWidth: 1,
  stroke: '#fff'
});
chart.render();
       },
       complete: function() {
           //请求完成的处理
       },
       error: function() {
           //请求出错处理
       }
   });
})

</script>
  </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值