How to resize the Morris Charts?

心已入冬 提交于 2019-12-06 05:19:22

问题


// 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


回答1:


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.




回答2:


I had the same problem, I could solve it like this:

var donut = null;    

this.init = (function() {

    ...

    donut = Morris.Donut({
        element : 'id-div',
        data : [{ label:"no data", value:100 }],
        colors : [ '#E6A548', '#1CC09f', '#FB5651', '#1CC09f', '#63A3DF', '#999' ],
    });
});

this.find = (function() {
    $.ajax() ... {

        donut.setData(dados);
        donut.redraw();

    }
});



回答3:


This worked for me.

$("#morrisArea").css("height","400")




回答4:


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();


来源:https://stackoverflow.com/questions/37838269/how-to-resize-the-morris-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!