0

I'm using jQuery Knob for displaying progress (read only mode). I want the circle size to adjust to the size of the layout / device my app is used on dynamically.

I already tried to use data-width="75%" but the circle disappears.

My second try was to define the size in the script:

$(".knob").knob({
    height: 75%; 

But that didn't work out either.

Could you guys help me out on this one? :)

thanks!

David Thomas
  • 249,100
  • 51
  • 377
  • 410
ManuKaracho
  • 1,180
  • 1
  • 14
  • 32
  • I've tried to tidy up a little, please check to make sure I've not altered your question accidentally (though I don't think I have). Also, could you post the full/relevant code showing the config options you've set? – David Thomas Jul 06 '13 at 19:38

4 Answers4

3
   <html>
    <head id="Head1" runat="server">
        <title></title>
        <style>
            body {height:100%; width:100%; }
        </style>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript" src="jquery.knob.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var knobWidthHeight,
                    windowObj;
                // store reference to div obj
                windowObj = $(window);
                // if the window is higher than it is wider
                if(windowObj.height() > windowObj.width()){
                    // use 75% width
                    knobWidthHeight = Math.round(windowObj.width()*0.75);
                } else {
                // else if the window is wider than it is higher
                    // use 75% height
                    knobWidthHeight = Math.round(windowObj.height()*0.75);
                }
                // change the data-width and data-height attributes of the input to either 75%
                // of the width or 75% of the height
                $('#knob').attr('data-width',knobWidthHeight).attr('data-height',knobWidthHeight);

                // draw the nob NOTE: must be called after the attributes are set.
                $(function() {

                    $("#knob").knob({
                        draw : function () {}
                    });

                });
            });
        </script>
    </head>
    <body>
        <!-- knob has to be an input | set default data-width and height, the jquery above will reset them before the knob is loaded
        knob width and height must always be the same as each other -->
        <input id="knob" data-width="500" data-height="500" data-displayInput=false value="35">
    </body>
</html>

For more examples on how to use the knob library, go to their demo page and view the source. It will show you all sorts of different ways to change the knob.

Adrian Trainor
  • 267
  • 1
  • 9
  • that looks nice! but it knob doesn't adjust to the window size in any case. I try to change the window size and reload the page, but the knob is always the same size – ManuKaracho Jul 06 '13 at 19:05
  • what size is it? a screenshot of your code and the result would be helpful. Also, check the console in Google Chrome – Adrian Trainor Jul 06 '13 at 19:09
  • i used your exact code. after it didn't adjust, i tried to tweak it a bit, but without success. Screen: http://jumplib.bplaced.net/Unbenannt.JPG – ManuKaracho Jul 06 '13 at 19:14
  • no problem, did you change the css for `#knobdiv` shown at the top, and place an input with the id `knob` inside the `knobdiv` div? Just check all the code, it could be a missing selector. Also did you try to console in Chrome or Firefox to see if there are any javascript errors? – Adrian Trainor Jul 06 '13 at 19:19
  • Ok, my bad, it wasn't working on mine either after I closed the console. Weird. I'll edit the code above. – Adrian Trainor Jul 06 '13 at 19:23
  • does that code work on your machine? on mine it only shows a default input box with value 35. – ManuKaracho Jul 06 '13 at 19:37
  • yea, it works fine here. Make sure you are calling the knob library, and check for js errors in console. – Adrian Trainor Jul 06 '13 at 20:14
  • knob library is called correctly. console tells me: Uncaught ReferenceError: knobDiv is not defined Line 23 – ManuKaracho Jul 06 '13 at 20:25
  • yea, I should have been more specific, I changed knobDiv to windowObj. Copy over all the javascript and it should work. – Adrian Trainor Jul 06 '13 at 20:44
  • sorry that i have to bother you again, but it still just shows the input box – ManuKaracho Jul 06 '13 at 21:09
  • is the Uncaught ReferenceError still there? If so, put up a screenshot of your javascript as it is now. – Adrian Trainor Jul 06 '13 at 21:12
  • My Bad!! It works now - you're a genius! thank you soooo much for your time!! :) have a nice evening – ManuKaracho Jul 06 '13 at 21:12
  • i wrote WindowObj instead of windowObj haha.. it has been too long in front of the computer for today. thank you so much again :) – ManuKaracho Jul 06 '13 at 21:13
  • would you mind testing your code on iexplore oder firefox? firefox gives me a small static knob (about 100px) and IE shows nothing but a white screen.. – ManuKaracho Jul 06 '13 at 22:09
  • Changed it above, both firefox and ie were working in a weird way. So I set the body's height and width to 100%, removed the #knobDiv altogether and it seems to be working fine now. Let me know how you get on – Adrian Trainor Jul 06 '13 at 22:23
0

I don't think jQuery Knob accepts percentages as values. Try getting the width and height of the window and calculating 75% of this, then set that pixel value. Something like:

var windowObj = $(window);
var newHeight = Math.round((windowObj.height() / 4) * 3);
var newWidth = Math.round((windowObj.width() / 4) * 3);

Now just replace the percentage values with the newHeight and newWidth pixel values.

Adrian Trainor
  • 267
  • 1
  • 9
  • the knob is placed inside a div container. can i get the width of the div container as well? – ManuKaracho Jul 06 '13 at 16:30
  • Yea, just replace the `$(window)` with `$('#divName')` and set the div container's height and width to `75%` in css. That should work as long as the div you're talking about is not wrapped inside another. – Adrian Trainor Jul 06 '13 at 16:39
  • I tried: `$(document).ready(function() { $(window).resize(function() { var mydiv = document.getElementById("knobdiv"); var curr_width = mydiv.style.width; var knobwidth = curr_width * 0.75; knob.trigger('configure', { 'width': 500, 'height': 500 }); knob.draw(); }); });` – ManuKaracho Jul 06 '13 at 17:00
  • but that doenst work out for me, and I dont know why. Do you have any idea? – ManuKaracho Jul 06 '13 at 17:00
  • I'm just setting up knob to try this on my local machine, but at the minute i can't see any height and width options set into configure. Is there any more javascript or css I might need to test this? – Adrian Trainor Jul 06 '13 at 17:17
  • thanks! i stumbled upon this: http://www.highonphp.com/jquery-visual-timer-using-knob maybe this will give you a hint in the right direction on how to change configuration details. Im struggling with it right now – ManuKaracho Jul 06 '13 at 17:44
  • when i try: `window.onresize = function() { $('.knob').trigger('configure',{"fgColor": '#FF0000'}); }` it changes the color immediately. But it doesn't work with the size.. – ManuKaracho Jul 06 '13 at 18:13
  • I think I have it, I'll put it in a new answer – Adrian Trainor Jul 06 '13 at 18:30
0

I had this issue too and I came with a simpler solution with some javascript code.

First, to target some resolutions (as responsive design) you must add this code to a js file like custom.js:

$(window).bind("resize", widthFunctions);

    function widthFunctions( e ) {
       var winHeight = $(window).height();
       var winWidth = $(window).width();

       if (winWidth < 980 && winWidth > 750) {

          $(".pinkCircle").knob({
         'min':0,
         'max':10000,
         'readOnly': true,
         'width': ... ,
         'height': ... ,
         'fgColor': '#e42b75',
         'dynamicDraw': true,
         'thickness': 0.2,
         'tickColorizeValues': true,
         'skin':'tron'
          })

       }
    }

winWidth is the width you want to target, as you do with media queries. Then you can add the settings you want to. Note that this class (.pinkCircle) was added to <input>.

Then, you must add with media queries the same width that you add with js, like:

@media only screen and (max-width: 768px) {
    .divStats > div[onTablet="span4"] {
        width: ... ; // Here you could add a width for 3 circles display inline for example
    }
}

I'm using bootstrap so I add here the width of span4 which is '31.914893617021278%', note this is an example only, all width and code could be different from yours, but this is all around from width.

Greetings.

Luis Serrano
  • 131
  • 1
  • 4
  • 10
0

You can use data-width on the input. Here is the first commit for the responsive implementation.