Dial: Dial With a Surrounding Image
This example shows how to create a Dial
widget using an image
that surrounds (or is larger than) the Dial.
Creating a Dial and Surrounding It With a Larger Image
Some cases may require a Dial
that has an image surrounding it such as tick marks, units, or other
visual enhancements. These images can be larger than the ring of the dial and therefore may not fit as a background image.
To provide for this use case, an extra image object will need to be added to the DOM.
In this example we'll simulate the climate control on an car dashboard. The image we'll add contains two curved wedges of color, blue and red, that wrap around the dial, signifying the temperature of air conditioning or heat.
The Markup
The only markup requirement is a div to contain the Dial
.
<div id="demo"></div>
<div id="demo"></div>
The JavaScript
The same JavaScript can be used as in the basic Dial example, with a bit of extra code to add the image object.
Some commonly used configuration attributes are shown below.
This example also shows how to modify the visible UI strings after the Dial
renders.
YUI({ filter: 'raw' }).use("dial", function(Y) { var dial = new Y.Dial({ min: -90, max: 90, stepsPerRev: 200, value: 0, diameter: 100 }); dial.render("#demo"); //Setting visible strings after Dial renders. //See other example for setting strings before rendering the Dial. dial._setLabelString('Climate:'); dial._setTooltipString('Drag for cool or heat.'); dial._setResetString('Off'); });
YUI({ filter: 'raw' }).use("dial", function(Y) { var dial = new Y.Dial({ min: -90, max: 90, stepsPerRev: 200, value: 0, diameter: 100 }); dial.render("#demo"); //Setting visible strings after Dial renders. //See other example for setting strings before rendering the Dial. dial._setLabelString('Climate:'); dial._setTooltipString('Drag for cool or heat.'); dial._setResetString('Off'); });
Inserting the Image
After rendering the Dial
, we create and insert the image object.
//Create an image node. var im = Y.Node.create('<img src="assets/images/cold_hot.png"/>'); //Position it absolutely to the correct spot depending on it's size. im.setStyles({'position':'absolute', 'top':'-3px', 'left':'-9px'}); //Insert it in the DOM. //The north-mark is the first object inside the ring. //depending on the image, you may need to insert it before the yui3-dial-label Y.one('.yui3-dial-north-mark').insert(im, 'before');
//Create an image node. var im = Y.Node.create('<img src="assets/images/cold_hot.png"/>'); //Position it absolutely to the correct spot depending on it's size. im.setStyles({'position':'absolute', 'top':'-3px', 'left':'-9px'}); //Insert it in the DOM. //The north-mark is the first object inside the ring. //depending on the image, you may need to insert it before the yui3-dial-label Y.one('.yui3-dial-north-mark').insert(im, 'before');
The CSS
Here, we are just cleaning out the visible styles of the ring and the center button. This is optional.
/* Adding some margin for the image */ .yui3-dial{ margin:0 0 20px 20px; } /* Remove visible styles of the ring */ .yui3-skin-sam .yui3-dial-content .yui3-dial-ring{ box-shadow: none; -moz-border-radius: none; -webkit-border-radius: none; -moz-box-shadow: none; background:none; } /* Remove visible style of the center button */ .yui3-skin-sam .yui3-dial-content .yui3-dial-center-button { box-shadow: none; -moz-border-radius: none; -webkit-border-radius: none; -moz-box-shadow: none; background:none; } /* Hide all VML ovals in IE.*/ .yui3-skin-sam .yui3-dial-ring-vml v\:oval { visibility:hidden; } /* Show the marker and the handle ovals */ .yui3-skin-sam .yui3-dial-ring-vml .yui3-dial-marker v\:oval, .yui3-skin-sam .yui3-dial-ring-vml .yui3-dial-handle v\:oval { visibility:visible; } /* Fill center button and ring so their backgrounds accept events in IE */ .yui3-skin-sam .yui3-dial-content .yui3-dial-center-button-vml, .yui3-skin-sam .yui3-dial-content .yui3-dial-ring-vml{ background:url(assets/images/empty.png); }
/* Adding some margin for the image */ .yui3-dial{ margin:0 0 20px 20px; } /* Remove visible styles of the ring */ .yui3-skin-sam .yui3-dial-content .yui3-dial-ring{ box-shadow: none; -moz-border-radius: none; -webkit-border-radius: none; -moz-box-shadow: none; background:none; } /* Remove visible style of the center button */ .yui3-skin-sam .yui3-dial-content .yui3-dial-center-button { box-shadow: none; -moz-border-radius: none; -webkit-border-radius: none; -moz-box-shadow: none; background:none; } /* Hide all VML ovals in IE.*/ .yui3-skin-sam .yui3-dial-ring-vml v\:oval { visibility:hidden; } /* Show the marker and the handle ovals */ .yui3-skin-sam .yui3-dial-ring-vml .yui3-dial-marker v\:oval, .yui3-skin-sam .yui3-dial-ring-vml .yui3-dial-handle v\:oval { visibility:visible; } /* Fill center button and ring so their backgrounds accept events in IE */ .yui3-skin-sam .yui3-dial-content .yui3-dial-center-button-vml, .yui3-skin-sam .yui3-dial-content .yui3-dial-ring-vml{ background:url(assets/images/empty.png); }