SlickGrid example 8:折线图

根据数据生成折线图,使用相当简单,也很容易。


主要方法:
数据:
var vals = [12,32,5,67,5,43,76,32,5];
生成折线图:
$("testid").empty().sparkline(vals, {width: "100%"});
然后折线图就出来了,是不是相当简单啊?

代码:
<!DOCTYPE HTML>
<html>
<head>
  <metahttp-equiv="Content-Type" content="text/html;charset=iso-8859-1">
  <title>SlickGrid example 10: Asyncpost render</title>
  <link rel="stylesheet"href="../slick.grid.css" type="text/css"/>
  <link rel="stylesheet"href="../css/smoothness/jquery-ui-1.8.16.custom.css"type="text/css"/>
  <link rel="stylesheet"href="examples.css" type="text/css"/>
  <style>
    .cell-title {
      font-weight: bold;
    }

    .cell-effort-driven{
      text-align: center;
    }

    .description * {
      font-size: 11pt;
    }
  </style>
</head>
<body>
<divstyle="width:600px;float:left;">
  <div class="grid-header"style="width:100%">
    <label>Scores:</label>
  </div>
  <div id="myGrid"style="width:100%;height:500px;"></div>
</div>

<div style="margin-left:650px;margin-top:40px;"class="description">
  <h2>Demonstrates:</h2>

  <p>
    With SlickGrid, you canstill have rich, complex cells rendered against the actual DOMnodes while still preserving
    the speed andresponsiveness.
    This is achieved throughasync background post-rendering.
    SlickGrid exposes a<u>asyncPostRender</u>property on a column which you can use to set your own functionthat will
    manipulate the cell DOMnode directly.
    The event is fired oneby one for all visible rows in the viewport on a timer so itdoesn't impact the UI
    responsiveness.
    You should still makesure that post-processing one row doesn't take too longthough.
    SlickGrid will figureout what and when needs to be updated for you.
  </p>

  <p>
    The example below is alist of 500 rows with a title and 5 integer cells followed bygraphical representation of
    these integers.
    The graph is drawn usinga CANVAS element in the background.
    The grid is editable, soyou can edit the numbers and see the changes reflected (almost)immediately in the graph.
    The graph cell behavesjust like an ordinary cell and can be resized/reordered.
    The graphs themselvesare created using the excellent <ahref="http://www.omnipotent.net/jquery.sparkline/"
                                                              target="_blank">jQuerySparklines</a> library.
  </p>
</div>

<scriptsrc="../lib/firebugx.js"></script>

<scriptsrc="../lib/jquery-1.7.min.js"></script>
<scriptsrc="../lib/jquery-ui-1.8.16.custom.min.js"></script>
<scriptsrc="../lib/jquery.event.drag-2.0.min.js"></script>
<scriptsrc="../lib/jquery.sparkline.min.js"></script>

<scriptsrc="../slick.core.js"></script>
<scriptsrc="../slick.editors.js"></script>
<scriptsrc="../slick.grid.js"></script>

<script>
  function requiredFieldValidator(value){
    if (value == null ||value == undefined || !value.length) {
      return {valid: false, msg: "This is a required field"};
    } else {
      return {valid: true, msg: null};
    }
  }

  function waitingFormatter(value) {
    return "wait...";
  }

  function renderSparkline(cellNode, row,dataContext, colDef) {
    var vals = [
      dataContext["n1"],
      dataContext["n2"],
      dataContext["n3"],
      dataContext["n4"],
      dataContext["n5"]
    ];

    $(cellNode).empty().sparkline(vals, {width: "100%"});
  }

  var grid;
  var data = [];
  var columns = [
    {id: "title", name:"Title", field: "title", sortable: false, width: 120, cssClass:"cell-title"},
    {id: "n1", name: "1",field: "n1", sortable: false, editor: Slick.Editors.Integer, width:40, validator: requiredFieldValidator},
    {id: "n2", name: "2",field: "n2", sortable: false, editor: Slick.Editors.Integer, width:40, validator: requiredFieldValidator},
    {id: "n3", name: "3",field: "n3", sortable: false, editor: Slick.Editors.Integer, width:40, validator: requiredFieldValidator},
    {id: "n4", name: "4",field: "n4", sortable: false, editor: Slick.Editors.Integer, width:40, validator: requiredFieldValidator},
    {id: "n5", name: "5",field: "n5", sortable: false, editor: Slick.Editors.Integer, width:40, validator: requiredFieldValidator},
    {id: "chart", name:"Chart", sortable: false, width: 60, formatter: waitingFormatter,rerenderOnResize: true, asyncPostRender: renderSparkline}
  ];

  var options = {
    editable: true,
    enableAddRow:false,
    enableCellNavigation:true,
    asyncEditorLoading:false,
    enableAsyncPostRender:true
  };


  $(function () {
    for (var i = 0; i< 500; i++) {
      var d= (data[i] = {});

      d["title"] = "Record " + i;
      d["n1"] = Math.round(Math.random() * 10);
      d["n2"] = Math.round(Math.random() * 10);
      d["n3"] = Math.round(Math.random() * 10);
      d["n4"] = Math.round(Math.random() * 10);
      d["n5"] = Math.round(Math.random() * 10);
    }

    grid = newSlick.Grid("#myGrid", data, columns, options);
  })
</script>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值