jQuery UI Custom Widget
by ramyaaviji
HTML
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<div class="chart"></div>
<ul class="controls">
<li> <span>Show Legend</span>
<input type="checkbox" data-prop="legend" checked="checked" />
</li>
<li>
<input type="button" value="Set Random Size" data-prop="size" />
</li>
<li>
<input type="button" value="Generate Random Markers" data-prop="markers" />
</li>
<li>
<input type="button" value="Generate Random Bars" data-prop="bars" />
</li>
<li>
<input type="button" value="Generate Random Ticks" data-prop="ticks" />
</li>
</ul>
CSS
.bullet-chart {
font-size: 16px;
-webkit-user-select: none;
}
.bullet-chart .chart-container {
height: 30px;
background: #EEE;
position: relative;
margin-left: 2.1em;
}
/* Bar */
.bullet-chart .bar {
background: #777;
position: absolute;
/* Vertical centering */
height: 10px;
top: 50%;
margin-top: -5px;
}
/* Marker */
.bullet-chart .marker {
background: #555;
width: 2px;
height: 20px;
position: absolute;
left: 250px;
top: 50%;
margin-top: -10px;
}
.bullet-chart .red {
background: #cd6668;
}
.bullet-chart .green {
background: #72cd84;
}
.bullet-chart .blue {
background: #64a0cb;
}
.bullet-chart .purple {
background: #a77cdb;
}
.bullet-chart .orange {
background: #ffb95e;
}
.bullet-chart .cyan {
background: #a0e2ff;
}
/* Ticks */
.bullet-chart .tick-bar {
position: relative;
top: 30px;
border-top: 1px solid #AAA;
}
.bullet-chart .tick-bar .tick {
position: absolute;
width: 1px;
height: 6px;
top: -1px;
background: #AAA;
}
.bullet-chart .tick-bar .tick-label {
text-align: center;
position: absolute;
font-size: 0.6em;
width: 2.1em;
margin-left: -1.05em;
top: 5px;
color: #AAA;
}
/* Legend */
.bullet-chart .legend {
margin: 0 0 1em 2.1em;
border: 1px solid #EEE;
padding: 0.5em 0 0 0.5em;
max-width: 300px;
}
.bullet-chart .legend .legend-item {
display: inline-block;
margin: 0 0.5em 0.5em 0;
background: #FFE;
padding: 0 5px;
border-radius: 2px;
cursor: pointer;
}
.legend-item .legend-symbol {
position: static;
margin: 0px;
display: inline-block;
vertical-align: middle;
}
.legend-item .legend-symbol.marker {
height: 12px;
width: 2px;
}
.legend-item .legend-symbol.bar {
height: 12px;
width: 7px;
}
.legend-item .legend-label {
font-size: 0.6em;
vertical-align: middle;
margin-left: 5px;
}
.fade {
opacity: 0.5;
}
/*...
JavaScript
(function ($) {
/***********************************************************
* Base BulletChart widget
***********************************************************
*/
$.widget('nt.bulletchart', {
options: {
// percentage: 0 - 100
size: 100,
// [{ title: 'Sample Bar', value: 75, css: '' }],
bars: [],
// [{ title: 'Sample Marker', value: 50, css: '' }],
markers: [],
// ticks -- percent values
ticks: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
},
widgetEventPrefix: 'bulletchart:',
_create: function () {
this.element.addClass('bullet-chart');
// chart container
this._container = $('<div class="chart-container"></div>')
.appendTo(this.element);
this._setOptions({
'size': this.options.size,
'ticks': this.options.ticks,
'bars': this.options.bars,
'markers': this.options.markers
});
},
_destroy: function () {
this.element.removeClass('bullet-chart');
this.element.empty();
this._super();
},
_setOption: function (key, value) {
var self = this,
prev = this.options[key],
fnMap = {
'bars': function () {
createBars(value, self);
},
'markers': function () {
createMarkers(value, self);
},
'ticks': function () {
createTickBar(value, self);
},
'size': function () {
self.element.find('.chart-container')
.css('width', value + '%');
}
};
...