RGraph + gridstack调整为div

时间:2018-05-07 22:55:35

标签: javascript charts gridstack rgraph

我正在使用gridstack开发一个应用程序,我想使用RGraph库将几个图形(行和标尺)放在不同的容器中。

我无法使图形填充容器的宽度和高度。 另外,当我改变尺寸时,我希望图形适应容器。

我有一个例子:

Code JSFiddle

$(document).ready(function ()
    {
 var data = [[4,5,8,11,15], [1,3,2,5,9]];
var tips = ['a','b','c','d','e','yf','yh','tt','hh','ll'];

var line = new RGraph.Line('cvs', data)
    .set('tooltips', tips)
    .set('gutter.left', 50)
    .draw();
    });
 #canvas{
width: 100% !important;
 max-width: 100% !important;
height: 70% !important;
  align-content:center;
  vertical-align:middle;
/*position:static;*/
}

#cvs {
    width: 100%;
    height: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Serialization demo</title>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
   
    <link rel="stylesheet" href="./gridstack.css"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.3.0/gridstack.min.css" />
<script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.3.0/gridstack.min.js'></script>
<script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.3.0/gridstack.jQueryUI.min.js'></script>


<script src="http://www.rgraph.net/libraries/RGraph.common.core.js"></script>
<script src="http://www.rgraph.net/libraries/RGraph.common.dynamic.js"></script>
<script src="http://www.rgraph.net/libraries/RGraph.common.tooltips.js"></script
src="http://www.rgraph.net/libraries/RGraph.common.resizing.js"></script>
<script src="http://www.rgraph.net/libraries/RGraph.line.js"></script>



    <style type="text/css">
        .grid-stack {
            background: lightgoldenrodyellow;
        }

        .grid-stack-item-content {
            color: #2c3e50;
            text-align: center;
            background-color: #18bc9c;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
 

        <div>
            <a class="btn btn-default" id="save-grid" href="#">Save Grid</a>
            <a class="btn btn-default" id="load-grid" href="#">Load Grid</a>
            <a class="btn btn-default" id="clear-grid" href="#">Clear Grid</a>
        </div>

        <br/>

        <div class="grid-stack" id="grid-stack">
          <div class="chart-container" id="tile1" >
            <div class="grid-stack-item-content">
              <span class="pull-left">Tile 1</span>
              <div class="pull-left" style="clear:both">Content</div>
            </div>
          </div>
          <div class="chart-container" id="tile2">
            <div class="grid-stack-item-content">
              <span class="pull-left">Tile 2</span>
              <div class="pull-left" style="clear:both">              
<canvas id="cvs" width="600" height="250" style="width: 100%; float: left">[No canvas support]
</canvas>
          
              
              </div>
            </div>
          </div>
</div>
 <hr/>

        <textarea id="saved-data" cols="100" rows="20" readonly="readonly"></textarea>
    </div>


    <script type="text/javascript">
        $(function () {
            var options = {
            };
            $('.grid-stack').gridstack(options);

            new function () {
                this.serializedData = [
                    {id: "tile1", x: 0, y: 0, w: 4, h:2},
                    {id: "tile2", x: 4, y: 0, w: 5, h: 4},
                ];

                this.grid = $('.grid-stack').data('gridstack');

                this.loadGrid = function () {
                    this.grid.removeAll();
                    var items = GridStackUI.Utils.sort(this.serializedData);
                    items.forEach(node=>{
                        var containerElt = $("#" + node.id);
                        containerElt.attr("data-gs-id", node.id);
                        containerElt.attr("data-gs-width", node.w);
                        containerElt.attr("data-gs-height", node.h);
                        containerElt.attr("data-gs-x", node.x);
                        containerElt.attr("data-gs-y", node.y);
                        this.grid.makeWidget(containerElt)
                    });
                    return false;
                }.bind(this);

                this.saveGrid = function () {
                    this.serializedData = _.map($('.grid-stack > .grid-stack-item:visible'), (el)=> {
                        el = $(el);
                        var node = el.data('_gridstack_node');
                        return {
                            id: node.id,
                            x: node.x,
                            y: node.y,
                            width: node.width,
                            height: node.height
                        };
                    }, this);
                    $('#saved-data').val(JSON.stringify(this.serializedData, null, '    '));
                    return false;
                }.bind(this);

                this.getSerializedData = function () {
                  var result = _.map($('.grid-stack > .grid-stack-item:visible'), (el)=> {
                      el = $(el);
                      var node = el.data('_gridstack_node');
                      return {
                          id: node.id,
                          x: node.x,
                          y: node.y,
                          w: node.width,
                          h: node.height
                      };
                  }, this);
                  return result;
                }.bind(this);

                this.clearGrid = function () {
                    this.grid.removeAll();
                    return false;
                }.bind(this);

                $('#save-grid').click(this.saveGrid);
                $('#load-grid').click(this.loadGrid);
                $('#clear-grid').click(this.clearGrid);
                
                $('#grid-stack').on('change', (event, items) => {
                  var result = this.getSerializedData();
                  var json = JSON.stringify(result, null, '    ');
                  $('#saved-data').val(json);
                });

               this.loadGrid();
            };
        });
    </script>
</body>
</html>

0 个答案:

没有答案