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

Gauge will not become responsive #73

Open
russellharrower opened this issue May 29, 2015 · 14 comments
Open

Gauge will not become responsive #73

russellharrower opened this issue May 29, 2015 · 14 comments

Comments

@russellharrower
Copy link

Hi, I am having a little issue, with the gauge not wanting to be responsive, I am using Bootstrap and I have add the following to the CSS

canvas {
width: 100%;
height: auto;
}

and also this to the JS part

scaleOverride : true,

Now I don't know if your script support this function, but what happens is that the gauge completely gets removed from the page.

If you can help that be great

@llagit
Copy link

llagit commented Jan 26, 2016

I have the same problem: is there any solution?? thankyou...

@rodo008
Copy link

rodo008 commented May 21, 2016

Yes, i found a solution:

Add style width 100%

   <canvas style="width:100%;" class="canvas-preview" ></canvas>
  <div class="preview-textfield" style="font-size: 41px;"></div>

Remove the "style" attribute after you render the control:

 var gauges = document.getElementsByClassName("canvas-preview");
                        for (var i = 0; i < gauges.length; i++) {
                            var el = gauges[i];
                            var gauge = new Gauge(el).setOptions(opts);
                            gauge.setTextField(el.nextElementSibling);
                            gauge.animationSpeed = 32;

                            var max = parseFloat(el.attributes["data-max"].value);
                            var value = parseFloat(el.attributes["data-val"].value);

                            el.removeAttribute("style");

                            if (value > max) {
                                max = value;
                            }

                            gauge.maxValue = max;
                            gauge.set(value);
                        }

It's a little bit hack

@EmilMoe
Copy link

EmilMoe commented Jun 20, 2016

This way it becomes very ugly

@rodo008
Copy link

rodo008 commented Jun 20, 2016

you can suggered any solution then? greetings.

@EmilMoe
Copy link

EmilMoe commented Jun 20, 2016

Sorry for my short answer, yes I will. But I'm still struggling how. My problem is it gets pixelated. I was trying to ask the same question on Stackoverflow but they so often waste time by telling people they ask wrong. But I take your punch back as a fair one, my comment was too short. I will when I have something.

@rodo008
Copy link

rodo008 commented Jun 20, 2016

no problem my friend, I'd searched anywhere without success. you are skype account? for chat and show you how It worked. Maybe we can get any solution together.

Grettings

@EmilMoe
Copy link

EmilMoe commented Jun 20, 2016

I have managed to make it 100% responsive now, only with a little bit of lag allowing it to be redrawn. My solution is made in Vue.js, but I will try to explain it without Vue.

I hope this can help you :-)

The concept

Every time the screen resizes the canvas needs to be redrawn and sizes must be set as HTML attributes for it to be drawn properly.

This solution dont' take into account if you resize elements with Javascript, in that case you need to redraw it manually.

The code

I have not refactored my code here, so it's a bit dirty:

put the canvas into a div

<div id="gaugeContainer">
  <canvas id="gauge"></canvas>
</div>

Have a function to draw it

function draw() {
                var width  = document.getElementById('gaugeContainer').offsetWidth;
                var height = width / 2;
                document.getElementById('gauge').setAttribute('width', width +'px');
                document.getElementById('gauge').setAttribute('height', height +'px');
                var options = {}; 
                var gauge = new Gauge(document.getElementById('gauge')).setOptions(options);
                document.getElementById('gauge').maxValue = 100;
                document.getElementById('gauge').set(60);
}

And a function to redraw it

var timeoutHandle = null;

function redraw() {
                document.getElementById('gauge').getContext('2d').save();
                document.getElementById('gauge').getContext('2d').setTransform(1, 0, 0, 1, 0, 0);
                document.getElementById('gauge').getContext('2d').clearRect(0, 0, document.getElementById('gauge').getContext('2d').canvas.width, document.getElementById('gauge').getContext('2d').canvas.height);
                document.getElementById('gauge').getContext('2d').restore();

                if (timeoutHandle)
                    window.clearTimeout(timeoutHandle);

               timeoutHandle = window.setTimeout(draw, 250);
}

On page load, make sure to draw it and add an event listener for window resize.

            draw();
            window.addEventListener('resize', redraw);

Finally have this CSS that prevents it from messing up your grid system

    canvas {
        max-width: 100%;
    }

@haniegol
Copy link

hi,
The solution was greate ,
But i did not know what is Gauge that you use it in "new Gauge..."

thank you

@adadgio
Copy link

adadgio commented Mar 7, 2018

Hey guys,

Just a suggestion/fix, which works for me without any additional coding:

canvas#da-gauge {
width: 100% !important;
height: auto !important;
}

@harry0725
Copy link

@adadgio 's answer works for me!

@rvillablanca
Copy link

Thanks @adadgio

@albertogomezikusi
Copy link

I have managed to make it 100% responsive now, only with a little bit of lag allowing it to be redrawn. My solution is made in Vue.js, but I will try to explain it without Vue.

I hope this can help you :-)

The concept

Every time the screen resizes the canvas needs to be redrawn and sizes must be set as HTML attributes for it to be drawn properly.

This solution dont' take into account if you resize elements with Javascript, in that case you need to redraw it manually.

The code

I have not refactored my code here, so it's a bit dirty:

put the canvas into a div

<div id="gaugeContainer">
  <canvas id="gauge"></canvas>
</div>

Have a function to draw it

function draw() {
                var width  = document.getElementById('gaugeContainer').offsetWidth;
                var height = width / 2;
                document.getElementById('gauge').setAttribute('width', width +'px');
                document.getElementById('gauge').setAttribute('height', height +'px');
                var options = {}; 
                var gauge = new Gauge(document.getElementById('gauge')).setOptions(options);
                document.getElementById('gauge').maxValue = 100;
                document.getElementById('gauge').set(60);
}

And a function to redraw it

var timeoutHandle = null;

function redraw() {
                document.getElementById('gauge').getContext('2d').save();
                document.getElementById('gauge').getContext('2d').setTransform(1, 0, 0, 1, 0, 0);
                document.getElementById('gauge').getContext('2d').clearRect(0, 0, document.getElementById('gauge').getContext('2d').canvas.width, document.getElementById('gauge').getContext('2d').canvas.height);
                document.getElementById('gauge').getContext('2d').restore();

                if (timeoutHandle)
                    window.clearTimeout(timeoutHandle);

               timeoutHandle = window.setTimeout(draw, 250);
}

On page load, make sure to draw it and add an event listener for window resize.

            draw();
            window.addEventListener('resize', redraw);

Finally have this CSS that prevents it from messing up your grid system

    canvas {
        max-width: 100%;
    }

I have a question, Are you not filling the HTML page with a lot of Gauges everytime you resize the window instead of destroying first and create it again?

@jose2007kj
Copy link

thanks @adadgio it fixed the issue for me

@Vemb
Copy link

Vemb commented Aug 15, 2022

The gauge becomes blurry when the canvas element is scaled with CSS. Is there a better alternative?

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