0

I am developing an application with gridstack and I want to put several graphics (lines and gauge) in different containers using the RGraph library.

I can not make the graph fill the width and height of the container. Also when I change the size I want the graphic to adapt to the container.

I have an example:

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>
PabloB
  • 19
  • 7

1 Answers1

0

You need to create a function to reset and redraw your line, then have that trigger when the window resizes and when the widget is updated (on the change event in gridstack). Make sure not to use extra styles, as they'll actually hinder your result. I've updated your example to work on window resize. It will not update when you resize the widget, but if you resize the window, you'll see the chart continues to fill up the full widget.

http://jsfiddle.net/kqada7zj/46/

dweiss
  • 832
  • 4
  • 9
  • Thank you very much for your response and contribution, I will continue testing according to the comments you made. – PabloB May 15 '18 at 22:23