Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic resizing #111

Open
iamjoshua opened this issue Nov 10, 2012 · 43 comments
Open

dynamic resizing #111

iamjoshua opened this issue Nov 10, 2012 · 43 comments
Milestone

Comments

@iamjoshua
Copy link

Is it possible to have a chart resize dynamically with parent div? i.e. 100% height/width

@oesmith
Copy link
Contributor

oesmith commented Nov 10, 2012

A lot of people have asked for this.. I'll stick it on my todo list.

@DanielGenser
Copy link

This would be incredible.

@ferdinandsalis
Copy link

I'd love to see that too. What about only redrawing the svg when the resize has ended so the cpu doesn't get taxed too much. I have done that before like this,

    function Resize() {
      var lazyTrigger = _.debounce(_triggerLazyResize, 800);
      $(window).on('resize', lazyTrigger);
      _bindResizeStart();
    }

    var _bindResizeStart = function() {
      $(window).one('resize', function() {
        $(window).trigger('resize:start');
      });
    };

    var _triggerLazyResize = function() {
      $(window).trigger('resize:end');
      _bindResizeStart();
    };

I would then remove the svg on resize:start and redraw it on resize:end. During the resize one could show a fancy preloader. Overall I think its a pretty nice effect. However I had some problems in older browsers (if you can point me in a direction I would be very grateful).

What do you think.

@oesmith
Copy link
Contributor

oesmith commented Jan 25, 2013

SVG-only fixes are an issue because old-IE uses VML instead.

This is a valuable feature though -- adding to the TODO list for v0.5.

@alexcurtis
Copy link

This would be a great addition. Especially in conjunction with Backbone + Responsive layouts.

@ljl
Copy link

ljl commented Apr 10, 2013

This would be very useful.

@Kevlys
Copy link

Kevlys commented Apr 24, 2013

This would be a great feature.

@d0z0
Copy link

d0z0 commented Apr 25, 2013

+1

4 similar comments
@pusewicz
Copy link

pusewicz commented May 2, 2013

👍

@marccantwell
Copy link

+1

@wojons
Copy link

wojons commented Jul 3, 2013

+1

@dboze
Copy link

dboze commented Aug 19, 2013

+1

@elvista
Copy link

elvista commented Sep 19, 2013

+2

@khoffma4
Copy link

+1

2 similar comments
@kazzkiq
Copy link

kazzkiq commented Oct 10, 2013

👍

@quadcodes
Copy link

+1

This was referenced Oct 25, 2013
@Teddy95
Copy link

Teddy95 commented Oct 25, 2013

Great, i hope v0.5 comes out soon! :)

@franckbrunet
Copy link

I vote for this one too!

@cnicodeme
Copy link

I'm on board for this feature too !

@oesmith oesmith closed this as completed in 88e3fe6 Nov 9, 2013
@oesmith oesmith reopened this Nov 9, 2013
@oesmith
Copy link
Contributor

oesmith commented Nov 9, 2013

OK, resizing is in master now. Please everyone give it a test drive with their charts and let me know how it works.

To enable resizing, pass resize: true with your chart options. It should work with all chart types.

If it's all good, it'll ship with v0.5.0, which will be the next release!

@kazzkiq
Copy link

kazzkiq commented Nov 9, 2013

It worked flawlessly in Chrome/Safari. Thanks!

@jeremywrowe
Copy link

Just as a heads up, when resizing in Firefox the timeout ends up in an infinite loop and the browser crashes / hangs

@oesmith
Copy link
Contributor

oesmith commented Nov 11, 2013

@jeremywrowe Which version of Firefox are you testing with? can you create a jsbin that demonstrates the crash bug?

The example in examples/resize.html works OK for me on Firefox 25 on OS X.

@jeremywrowe
Copy link

@oesmith I am on firefox 25.0. It seems to be a byproduct of having multiple charts rendering on the same page. I have a line, stacked bar, and an area chart. I can get you a jsbin later today when I have a spare second :)

@cnicodeme
Copy link

Tested under Linux Fedora with :

  • Chrome v30.0.1599.114 : Does seems to work. Parent div width is initially set at 475px, graph at 476px (svg width from Morris), after resizing, parent div width is at 571px, svg graph still at 476px.
  • Firefox v24 : the same.

(I added the resize: true)

BUT, I was moving the width in javascript using padding/width of others elements. It appears that resizing is made when the browser trigger a resize event (after calling $(window).trigger('resize'), the graph was correctly updated.

You should precise this in the documentation containing resize option :)

Note: After calling resize, everything works as expected for Firefox AND Chrome

@guilbep
Copy link

guilbep commented Nov 21, 2013

@oesmith I'm setting data to [] to display nothing with morris.setData(data); unfortunately it throws error on me: Uncaught TypeError: Cannot read property 'length' of undefined in chrome at least (31) .. But I have some similar issue when I'm using the morris.redraw message when my data is []. Works good despite that 👍 Thanks!

@mikerockett
Copy link

Doesn't seem to work for me. I have my charts laid out in a table with multiple columns. Here's the markup:

<table class="table tableBordered" width="100%">
                        <tbody>
                            <tr>
                                <td width="50%">
                                    <strong>Pledge-count for the last 30 days</strong><br>
                                    <div id="dashboardPledgesMonthCount" style="height: 160px"></div>
                                </td>
                                <td width="50%">
                                    <strong>Amounts pledged from day-to-day</strong><br>
                                    <div id="dashboardPledgesMonthAmounts" style="height: 160px"></div>
                                </td>
                            </tr>

And these are the callers:

[...]

Morris.Bar({
    barColors: ['#cdcdcd'],
    data: countData,
    element: 'dashboardPledgesMonthCount',
    grid: false,
    gridTextFamily: 'Noto Sans',
    hideHover: 'auto',
    labels: ['Pledges Made'],
    resize: true,
    xkey: 'date',
    ykeys: ['Pledges Made'],
});

[...]

Morris.Line({
    data: amountData,
    element: 'dashboardPledgesMonthAmounts',
    grid: false,
    gridTextFamily: 'Noto Sans',
    hideHover: 'auto',
    labels: ['Amount Pledged'],
    lineColors: ['#242d3c', '#cdcdcd'],
    parseTime: false,
    preUnits: 'R ',
    resize: true,
    xkey: 'date',
    xLabels: 'date',
    ykeys: ['Amount Pledged'],
});

Result after resizing:

morrisresize

@mikerockett
Copy link

Just picked up that if it is loaded with a small width, it will resize up (grow wider), but it won't resize down (thinner).

@ghost
Copy link

ghost commented Mar 16, 2014

Are there any plans to fix the issues with the resize ability of the plugin? I've set resize: true on all my charts but it doesn't appear to be working as intended?

@dumplingsol
Copy link

I did it like this:

$(document).ready(function() {
    barChart();

    $(window).resize(function() {
        window.m.redraw();
    });
});

function barChart() {

    window.m = Morris.Bar({

        element: 'stats-bar',
        data: [
            { y: '0.10', a: 93, b: 50, c: 23, d: 42, e: 10, f: 23 },
            { y: '0.20', a: 75,  b: 65 },
            { y: '0.30', a: 50,  b: 40 },
            { y: '0.40', a: 75,  b: 65 },
            { y: '0.50', a: 50,  b: 40 },
            { y: '0.60', a: 75,  b: 65 }
        ],
        xkey: 'y',
        ykeys: ['a', 'b', 'c', 'd', 'e', 'f'],
        labels: ['C', 'C#', 'C++', 'Java', 'Python 2', 'Python 3'],
        gridTextColor: '#898992',
        barColors: ['#4F89DB', '#333A92', '#5D57BA', '#2F1360', '#620F8B', '#872FB0'],
        stacked: true,
        hideHover: 'auto',
        resize: true,
        redraw: true

    });
}

Also to avoid getting a too wide svg, see to it that you dont forget the CSS:

#chart > svg {width: 100% !important;}

View result:
http://jsbin.com/nejoleqi/3

@astocks-cp
Copy link

Thanks jonathanolsen! This worked perfectly!

@morrow95
Copy link

morrow95 commented Apr 2, 2015

Also running into this problem. Resizing on window change works perfectly fine. My issue is resizing when the charts parent container resizes. For instance, I am using BS3 and showing my charts in portlets. Say the portlet is 400px on load... now the dimensions of the portlet change to say 800px. The chart stays exactly the same as it was because the window has not resized, but instead its parent container did (the portlet).

I tried a similar chart plugin, Flot, which works exactly as expected in this case. It resizes to fit the size of its container rather than on window resize... problem is I like Morris charts much more than Flot.

I thought the original poster of this thread was talking about the same thing as I am, but the solution provided is only for window resize. Anyone else manage to get something like this working?

@jaminellis
Copy link
Contributor

SVG is notoriously tricky on the resizing front. I'll have a dig and see if there is a way to scale from Raphael.

@guilbep
Copy link

guilbep commented Apr 7, 2015

Just for your information. I stopped using the redraw thingy. Because It was making Firefox halt.. badly. :p

@jhaenchen
Copy link

image

I'm using jQuery to show and hide the graphs. As you can see, the graph momentarily has no width before realizing its parent element has grown and resizing to fit it. Is there any way to change this behavior? It takes more than a second to adjust, and in the meantime is totally unusable.

@jhaenchen
Copy link

To clarify, you can achieve this result by using jQuery .show() and .hide() on the graph.

@johnny-vikrant
Copy link

Graph re-sizes when i minimize my window but stays in minimized sized even when i maximized the window. To make it fit to window i've to reload the browser every time. Any solutions?

@rpujakesuma
Copy link

Thanks @JonathanOlsen. +1

@yong0011
Copy link

yong0011 commented Jul 7, 2016

Thanks a lot @JonathanOlsen +1
that's perfect.

@winstonfale
Copy link

TNXXXXXXXXXXXXXXXXXXXXXXXX ALOT 💃

@azhrhussain
Copy link

azhrhussain commented Jul 28, 2016

I achieve dynamic resizing using JQuery div resize.
Let suppose I have a div with 100% width and morris bar append in it
html:

<div id="myBar"></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

$('#myBar').resize(function () { bar.redraw(); });

Whenever DIV resize the Morris Chart automatically fit to it's parent DIV.

@danegraphics
Copy link

To prevent it changing the size of the parent div, set the svg size in css like so:
#chartDivID svg { height: 200px; width: 200px; }

@duckduckdroid
Copy link

duckduckdroid commented Aug 12, 2019

Hey humans,

NOT sure if this is relevant, but I'm using materialize css and I found that when the chart resizes, the svg height keeps growing :/ (could be PBKAC). However if this is an actual issue I found that the following jquery keeps the svg from growing++

$(document).ready(function() {
    $("#chartID").find("svg").css("maxHeight", $("#chartID").css("height"))
})

Peace ☮️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests