Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Network with 100% height set does not render correctly #1832

Open
ghost opened this issue Apr 29, 2016 · 59 comments
Open

Network with 100% height set does not render correctly #1832

ghost opened this issue Apr 29, 2016 · 59 comments

Comments

@ghost
Copy link

ghost commented Apr 29, 2016

I'm trying to use Network where the height/width needs to be dynamic for a responsive layout.

If I set the Network to a fixed height/width all works as expected, except resizing (obviously) won't change the canvas to fit.

But if I set height/width to 100%, the canvas correctly sizes to the width (including autoResize set to true) but I always end up with a 150px height canvas, whereas the height of the container div could comfortably be > 800px.

I've created a demo plunker to show the problem.

@abcbaby
Copy link

abcbaby commented May 3, 2016

I've made a tweak to your code; Basically, you can't use %, you need to "px". Here is the new code example from your fork, look at your script.js:

http://plnkr.co/edit/B73VN9Kbew9KfCzYxUKs?p=preview

@ghost
Copy link
Author

ghost commented May 3, 2016

Problem is this isn't dynamic - my understanding is that to get the network autoResize to work, it needs to be in % not px - otherwise it won't think to resize (as it's still rendering at the fixed width). I assume on resize I could try to recalculate, but I need it to fill 100% of the container, rather than window size.

I'm using this in bootstrap where it's inside a 9 col div (where the full 12 col set are nested inside a 9 col div). I know from prior experience that trying to render svg/canvas to fill 100% of a container is not the easiest thing though.

patrickmcelwee added a commit to patrickmcelwee/ml-visjs-graph-ng that referenced this issue Aug 2, 2016
Had to hardcode it in the options to 600px because of issue as described
in this issue: almende/vis#1832
denislobanov added a commit to denislobanov/grakn that referenced this issue Sep 27, 2016
Currently there is a bug in vis.js which means its size is set to a
hardcoded value of 150px, even though we set it to 100% in a reactive
framework (bootstrap).
    * See almende/vis#1832

As a workaround canvas size is set manually after attaching the vue
component & on window resize.
fppt pushed a commit to typedb/typedb that referenced this issue Sep 28, 2016
Currently there is a bug in vis.js which means its size is set to a
hardcoded value of 150px, even though we set it to 100% in a reactive
framework (bootstrap).
    * See almende/vis#1832

As a workaround canvas size is set manually after attaching the vue
component & on window resize.
@mojoaxel
Copy link
Member

I'm closing this issue due to inactivity.
If you think this issue is still relevant please feel free to reopen!

@pjeutr
Copy link

pjeutr commented Oct 4, 2017

I still see this happening with 4.20.1
Also using it within a bootstrap grid and see the canvas size set to height="150" initially
I can overwrite it in options with something like 500px, but 100% is not working

@pjeutr
Copy link

pjeutr commented Oct 4, 2017

http://plnkr.co/edit/2FGTOd8v2GbmwGgeYAVu?p=preview
shows the problem. I also use GraphJs which scales fine.
Part of the problem for me of getting unreadable results is that visjs takes full width but only 150px height. If there would be a definable aspect ratio or something like height = .8 * width. It would look a lot better for me.

@wimrijnders
Copy link
Contributor

@pjeutr Thanks for demo, that's useful. Makes it much easier to examine the issue.

@wimrijnders wimrijnders reopened this Oct 4, 2017
@wimrijnders
Copy link
Contributor

@pjeutr the funny thing is, I can't reproduce it on my computer. The layout looks just fine to me, even if I resize the browser window (linux+ firefox/chrome).

The plunker demo does display the autoscaling problem, though. Any way to force it?

@pjeutr
Copy link

pjeutr commented Oct 5, 2017

@wimrijnders Thanks for looking into this. Just to be clear this is what I see on osx ( firefox / safari / chrome ) The red border is the size of the VisJS canvas.
screen shot 2017-10-06 at 00 32 56
What I'm looking for is something like this. In this case achieved by height:"350px" because
height:"100%" is not working for me.
screen shot 2017-10-06 at 00 39 54

@wimrijnders
Copy link
Contributor

Yeah, my bad. I neglected to copy the CSS from plunker. I see it now as well.

Sorry to correct you, but you are using Chart.js in the second panel, not GraphJs. it had me confused for a while.

@pjeutr
Copy link

pjeutr commented Oct 6, 2017

Sorry! Must have been because I use Graph as model name. Never heard of GraphJS before.

@wimrijnders
Copy link
Contributor

wimrijnders commented Oct 6, 2017

OK, I can see it happening, so: Confirmed.

It's actually useful that you added the other module; since they are doing something right, I might be able to peek in their code and find out how they fix it. I'll keep you up to date.

@wimrijnders
Copy link
Contributor

wimrijnders commented Oct 6, 2017

Here is where the magic happens. I think. Will verify.

For comparison, this is what Network does.

@wimrijnders
Copy link
Contributor

wimrijnders commented Oct 6, 2017

This is essentially the problem. We need to ensure that the defaults for the canvas size, 300x150 px, do not get used. Canvas.js deals with it, Network must deal with it also.

@pjeutr
Copy link

pjeutr commented Oct 6, 2017

Wow, w3c? Good find!
I grepped for 150 in de vis code and couldn't find it 😄

@wimrijnders
Copy link
Contributor

Hehe, now we know where that 150 comes from. This headache will be solved soon.

@abcbaby
Copy link

abcbaby commented Oct 6, 2017

I had this issue awhile back and here is how it resolved it. By default vis.js network height is 100% which is correct. However the default height for canvas, like @wimrijnders says, is 150px. Since the canvas is inside the 'mynetwork' div (default height: auto) tag, it's can only expand as big as the canvas, which is 150px height. In order to adjust to 100% of what YOU want, you have to calculate and adjust the height of the 'mynetwork' div tag, then the canvas can fully resize to whatever you want.

Here is a fork of your @pjeutr code, where i adjust the height of the div tag. You can take a look at: http://plnkr.co/edit/Kkz8SZDgv93yPRpRxckZ?p=preview

Hope this helps!

@wimrijnders
Copy link
Contributor

@dragonzone Great, thanks. But where is the fix exactly in the demo? I can't seem to find it.

@wimrijnders
Copy link
Contributor

wimrijnders commented Oct 9, 2017

The flicker is not too bad (not always, heartbeat frequency)

Aha. This is a giveaway of what's happening. Autoresize is refreshing about once per second, so you really are seeing this happening. There must be better solutions. I have a one-liner idea for this...

...also stops the resizing

Meaning, that the networks are still visible on screen but it doesn't resize along? This is something that can be focused on.

@pjeutr
Copy link

pjeutr commented Oct 9, 2017

Correct, networks are still visible. But doesn't resize.
Throw in the one-liner and it's fixed!?

Testing MS browser behaviour is great using vm's. MS provides images themselves
https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/

@wimrijnders
Copy link
Contributor

wimrijnders commented Oct 9, 2017

OK, my initial hunch was incorrect. I've been doing some online snooping with respect to Safari compatibility, it appears to be OK from Safari 6.1 onwards.

The only thing I can think of is that the canvas rendering is slower on safari, causing the redraw to go over a frame. I'm looking for a benchmark to test this. This looks hopeful, but I can't get it to run.


Update: Running now. I think the page got the hint after I furiously kept on tapping F5.

Would you mind running that test on Safari? To see if the canvas rendering time could be an issue.

These are my scores:

  • The 'itty bitty' machine: CanvasMark Score: 6308 (Firefox 52 on Linux)
  • The i7 workstation: CanvasMark Score: 13833 (Chrome 59 on Linux)
  • The Raspberry Pi: just kidding, that is the equivalent of torturing a kitten

Network runs without flickering on the first two. I don't dare to try it on the third.

@pjeutr
Copy link

pjeutr commented Oct 9, 2017

Seems all OK. This is me:
MB pro i5

  • CanvasMark Score: 8658 (Firefox 55 on Mac)
  • CanvasMark Score: 8695 (Safari 11 on Mac)
  • CanvasMark Score: 13038 (Chrome 60 on Mac)
  • CanvasMark Score: 10912 (Chrome 63 on Mac)

iPhone SE

  • CanvasMark Score: 11641 (Safari 11 on iOS)
    also shows flickering running the demo

Rasberry Pi (the first) + Raspbian ( kitties like to be challenged! )

  • CanvasMark Score: 34416 (Chrome 60 on Linux)
    Not joking. I suppose something is wrong with the test. If the hardware is to slow

@wimrijnders
Copy link
Contributor

wimrijnders commented Oct 10, 2017

So I finally read the instructions on the benchmark page:

The results can only be compared to other browsers running on the same machine - as each machine with different CPU or graphics will produce difference results.

So it's not an absolute measure. The only thing that can be looked at is your results for the different browsers on the MB.

I was hoping for a clear indication that Safari is slower with the rendering. It's clearly slower than chrome, but comparable to firefox, of which you said there is no flicker problem. So I'm inclined to conclude that this test is moot.


For completeness:

  • i7 workstation: CanvasMark Score: 13954 (Firefox 52 on Linux)
  • (previous, for comparison): CanvasMark Score: 13833 (Chrome 59 on Linux)
  • the 'kitten' (RPi 3): CanvasMark Score: 7711 (Chrome 51 on Linux)

Seeing that the i7 was +-60FPS and the RPi +-12FPS, it's safe to say that comparing values between machines is meaningless. Apologies if I have wasted your time.

@wimrijnders
Copy link
Contributor

wimrijnders commented Oct 10, 2017

IMHO, the flickering comes from here:

Canvas.js:

 setOptions(options) {
    if (options !== undefined) {
      let fields = ['width','height','autoResize'];
      util.selectiveDeepExtend(fields,this.options, options);
    }

    if (this.options.autoResize === true) {
      // automatically adapt to a changing size of the browser.
      this._cleanUp();
      this.resizeTimer = setInterval(() => {  // <=== Timer resizing every second!!
        let changed = this.setSize();
        if (changed === true) {
          this.body.emitter.emit("_requestRedraw");
        }
      }, 1000);                               // <=== End timer
      this.resizeFunction = this._onResize.bind(this);
      util.addEventListener(window,'resize',this.resizeFunction);
    }
  }

This thing looks dated to me, more so since addEventListener has nearly universal support, and it's easy to make a fall-back. I'm going to prepare an interim build for you with this disabled, see if it fares better.


New demo. The only thing changed from previous is the reference to vis.js (added version postfix).
This should bury the flickering, can you verify?

@pjeutr
Copy link

pjeutr commented Oct 10, 2017

Yup, it works. Also tried it locally by removing the timer. That was the culprit.
Not sure why it was there, but it looks brute force.

@wimrijnders
Copy link
Contributor

Yay! That means we're done with the fix.

One thing to do for me: add unit tests.
Also, reminder: This will not make it into next release due to code freeze. I might try whining a bit, you never know....

(Anything I missed? Just checking)

@pjeutr
Copy link

pjeutr commented Oct 10, 2017

Nope, thanks!
I downloaded your version. I'll be fine for the next decade 😄

@wimrijnders
Copy link
Contributor

Hehe. Decade is like an epoch in the javascript world. I wouldn't count on it.

OK, until next time! I believe you'll get a notification here when the PR has been published.

@wimrijnders
Copy link
Contributor

@pjeutr I'm rounding off the PR here.

Question: Would you mind if I added the last demo you gave me to the examples? This to showcase that the dynamic sizing works. It's pretty complete and visually enticing.

TIA!

@pjeutr
Copy link

pjeutr commented Oct 23, 2017 via email

@wimrijnders
Copy link
Contributor

Thanks for that! I will attribute you in the source code.

@glaggia-larus
Copy link

Hi, is there a schedule for when this change will be released?

Was this tested also with bootstrap grid layout (row + col-xx-xx) ?

@pjeutr
Copy link

pjeutr commented Nov 11, 2017

Hi Wim

I just noticed the edge labels are missing, see also the plnkr demo.
When using the original min.vis.js they come back.

@glaggia-larus
Check the demo, we tested there with col-md-6, it's easy to change the test to your usecase

@vletoux
Copy link

vletoux commented Dec 2, 2017

After 1,5 days of investigation, I found this thread.
+1 for the 150px size.

I manage to do a single test for me using this gist:
https://gist.github.com/anonymous/dc7d6446628650b9153f5f76d8ba017b

If I remove the doctype html (html5) it fit nicely
With HTML5:
image
Without HTML5:
image

There is no example for a fullscreen mode. All examples I looked at are boxed.
=> please add a fullscreen example in the documentation
(example of question: should I add a workaround to compute the size in javascript ?)

Also:
=> what was the last working version of vis.js for 100% height ?

@yaitskov
Copy link

Ops. Html5 makes it working, but it affects view of many other page elements, so I have to validate and fix all pages. I agreed up on a workaround where parent div using fixed position, because anyway graph view should be as big as possible and it would occupy the entire screen. Sparse buttons would be located inside the view with transparent background.

@mcarf
Copy link

mcarf commented Jul 19, 2018

@wimrijnders Hi, what is the current state of this issue? I can give a hand if the branch or the PR is published, thanks

@davidhbigelow
Copy link

I have been struggling with sizing network canvas based on the height of the available space... and when a user resizes the browser window.

After a bit of testing and playing around -- I figured this out, seems to work. If you have alternative approaches, please post a suggestion or alternative example code.

I hope this helps others as a starting point for their own projects. Note: I am using jQuery to help streamline the resize functions.

IMPORTANT - NON-HMTL5 DOCTYPE

     <!DOCTYPE>

THE HEAD SECTION

  <head>
  
    <!-- JQUERY -->
    <script
            src="https://code.jquery.com/jquery-3.3.1.min.js"
            integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
            crossorigin="anonymous"></script>

    <style>

        html, body
        {
            height: 100%;
        }

        #header
        {
            width: 100%;
            background: orange;
            vertical-align: middle;
            text-align: center;
            line-height: 100px;
        }

    </style>

</head>

SAMPLE BODY CONTENT


    <div id="header"> Sample header region </div>
    <div id="container">
        <div id="mynetwork" style="border: 1px solid red;">
        </div>
    </div>

THE SCRIPT (bottom of the HTML BODY)

<script>

    function resizeCanvas () {
        $('#mynetwork').height($('body').height()-$('#header').height()-20)
    }

    $(document).ready(function () {
        $(window).resize(function(){
            resizeCanvas();
        });
    });

    resizeCanvas();

</script>

EXAMPLE - PAGE LOAD
firstload

EXAMPLE - AFTER RESIZE
afterresize

@carlpaten
Copy link

I'm also affected ;_;

@davidhbigelow
Copy link

@LilRed -- did my example NOT help you? It seems to work fine for me across all browsers.

@carlpaten
Copy link

I'm using vis to make Grafana panel plug-ins, so I don't have control over the doctype.

@davidhbigelow
Copy link

I'm using vis to make Grafana panel plug-ins, so I don't have control over the doctype.

Oh - yea -- that was the only work around I found.... :(

@dherault
Copy link

dherault commented Mar 3, 2019

Another workaround:

network.once('afterDrawing', () => {
  container.style.height = '100vh'
})

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

No branches or pull requests