如何调整Morris图表的大小? [英] How to resize the Morris Charts?

查看:69
本文介绍了如何调整Morris图表的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在通过点击调整页面大小'菜单切换按钮',但是当我这样做时,莫里斯图表没有调整大小,我试图触发它,但它没有工作。
我在我的js $(window).trigger('resize'); 中添加了一行代码,但这个dint有效。有谁能够帮我?因为我不知道jQuery中的resize选项。

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.

谢谢
-Riot Zeast Captain

Thanks -Riot Zeast Captain

推荐答案

html:

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

创建一个变量并完成所需的所有Morris图表初始化但确保在初始化时不包含以下属性Morris图表重绘:true 调整大小:true

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({......});

这里jquery调整大小函数魔法,当div调整大小时触发不需要触发窗口调整大小。

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

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

每当DIV调整大小时,Morris Chart自动适合它的父DIV,即 col -sm-6

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

此解决方案根据父div大小为您提供图表大小。

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

您也不需要添加点击功能。

You don't need to add click function also.

这篇关于如何调整Morris图表的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆