Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize the Morris Charts?

// Left Sidebar Toggle Menu JS
$("#menu-toggle").click(function(e) {
  e.preventDefault();
  $("#wrapper").toggleClass("toggled");
  $(window).trigger('resize');
});


//Morris Charts
jQuery.ready()
var data = [
  { y: '2014', a: 50, b: 90},
  { y: '2015', a: 65,  b: 75},
  { y: '2016', a: 55,  b: 50},
  { y: '2017', a: 75,  b: 60},
  { y: '2018', a: 80,  b: 65},
  { y: '2019', a: 90,  b: 70},
  { y: '2020', a: 100, b: 75},
  { y: '2021', a: 115, b: 75},
  { y: '2022', a: 120, b: 85},
  { y: '2023', a: 145, b: 85},
  { y: '2024', a: 160, b: 95}
],
    config = {
      data: data,
      xkey: 'y',
      ykeys: ['a', 'b'],
      labels: ['Total Income', 'Total Outcome'],
      fillOpacity: 0.6,
      hideHover: 'auto',
      behaveLikeLine: true,
      resize: true,
      pointFillColors:['#ffffff'],
      pointStrokeColors: ['black'],
      lineColors:['gray','red']
    };
config.element = 'area-chart';
Morris.Area(config);
config.element = 'line-chart';
Morris.Line(config);
config.element = 'bar-chart';
Morris.Bar(config);
config.element = 'stacked';
config.stacked = true;
Morris.Bar(config);
Morris.Donut({
  element: 'pie-chart',
  data: [
    {label: "Friends", value: 30},
    {label: "Allies", value: 15},
    {label: "Enemies", value: 45},
    {label: "Neutral", value: 10}
  ]
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://blackrockdigital.github.io/startbootstrap-simple-sidebar/js/bootstrap.min.js"></script>
<link href="http://blackrockdigital.github.io/startbootstrap-simple-sidebar/css/simple-sidebar.css" rel="stylesheet"/>
<script src='https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.2/raphael-min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.0/morris.js'></script>

<link href="https://blackrockdigital.github.io/startbootstrap-simple-sidebar/css/bootstrap.min.css" rel="stylesheet"/>

<link rel='stylesheet prefetch' href='http://cdn.oesmith.co.uk/morris-0.5.1.css'>
<div class="container-fluid" id="wrapper">
   <div id="sidebar-wrapper">
		<aside class="sidebar">
		<nav class="sidebar-nav">
		</nav>
	</aside>

	</div>

<div id="page-content-wrapper">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-8">
        <div class="panel panel-success">
          <div class="panel-heading">
            <h3 class="panel-title">Sales</h3>
          </div>
          <div class="panel-body">
            <div id="area-chart" ></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
  </div>

I'm resizing the page by clicking 'menu toggle button', but when am doing so the Morris charts are not resized, I tried to trigger it, but its not working. I added a line of code in my js $(window).trigger('resize'); but this dint worked. can any body help me? as I'm not aware of resize option in jQuery.

Thanks -Riot Zeast Captain

like image 606
Riot Zeast Captain Avatar asked Jun 15 '16 14:06

Riot Zeast Captain


2 Answers

html:

<div id="area-chart" class="col-sm-12"></div>

Make a variable and all Morris chart initialization you required but make sure not to include the following properties while initializing Morris chart redraw: true and resize: true

var bar = Morris.Bar({......});

Here the jquery resize function magic which trigger when ever div resizes no need to trigger window resize.

$('#area-chart').resize(function () {
  bar.redraw();
});

Whenever DIV resize the Morris Chart automatically fit to it's parent DIV which is col-sm-6.

This solution give you the size of chart according to the parent div size.

You don't need to add click function also.

like image 170
Azhar Avatar answered Oct 16 '22 05:10

Azhar


This Worked For Me

HTML

<div class="row">
      <div id="graph" class="col-xs-12"></div>
 </div>

JavaScript

var line = Morris.Line({
            element: 'graph',
            data: $.parseJSON(dataParam),
            xkey: 'x',
            ykeys: ['y'],
            labels: ['Usage Graph'],
            fillOpacity: 0.6,
            hideHover: 'auto',
            behaveLikeLine: true,
            resize: true,
            pointFillColors: ['#ffffff'],
            pointStrokeColors: ['black'],
            lineColors: 'black',
            parseTime: false,
            backgroundColor: '#FFFFFF',
            labelColor: 'black'
        });
        var parentDivWidth = $("#graph").parent("div").width();
        $("#graph").css("min-width", parentDivWidth);
        $("#graph > svg:nth-child(1)").css("min-width", parentDivWidth);
        line.redraw();
like image 1
abdul hameed Avatar answered Oct 16 '22 06:10

abdul hameed