1 // ******************************************
3 // Each scenario has a score
4 // We use a gauge to indicate the trust level
5 // ******************************************
6 var gauge = function(container) {
16 pointerTailLength : 5,
17 pointerHeadLengthPercent : 0.8,
28 labelFormat : d3.format(',g'),
31 arcColorFn : d3.interpolateHsl(d3.rgb('#ff0000'), d3.rgb('#00ff00'))
35 var range = undefined;
37 var pointerHeadLength = undefined;
42 var scale = undefined;
43 var ticks = undefined;
44 var tickData = undefined;
45 var pointer = undefined;
47 var donut = d3.layout.pie();
49 function deg2rad(deg) {
50 return deg * Math.PI / 180;
53 function newAngle(d) {
55 var newAngle = config.minAngle + (ratio * range);
59 function configure() {
60 range = config.maxAngle - config.minAngle;
62 pointerHeadLength = Math.round(r * config.pointerHeadLengthPercent);
64 // a linear scale that maps domain values to a percent from 0..1
65 scale = d3.scale.linear()
67 .domain([config.minValue, config.maxValue]);
69 ticks = scale.ticks(config.majorTicks);
70 tickData = d3.range(config.majorTicks).map(function() {return 1/config.majorTicks;});
73 .innerRadius(r - config.ringWidth - config.ringInset)
74 .outerRadius(r - config.ringInset)
75 .startAngle(function(d, i) {
77 return deg2rad(config.minAngle + (ratio * range));
79 .endAngle(function(d, i) {
80 var ratio = d * (i+1);
81 return deg2rad(config.minAngle + (ratio * range));
84 that.configure = configure;
86 function centerTranslation() {
87 return 'translate('+r +','+ r +')';
90 function isRendered() {
91 return (svg !== undefined);
93 that.isRendered = isRendered;
95 function render(newValue) {
96 svg = d3.select(container)
98 .attr('class', 'gauge')
99 .attr('width', config.clipWidth)
100 .attr('height', config.clipHeight);
102 var centerTx = centerTranslation();
104 var arcs = svg.append('g')
105 .attr('class', 'arc')
106 .attr('transform', centerTx);
108 arcs.selectAll('path')
110 .enter().append('path')
111 .attr('fill', function(d, i) {
112 return config.arcColorFn(d * i);
116 var lg = svg.append('g')
117 .attr('class', 'label')
118 .attr('transform', centerTx);
121 .enter().append('text')
122 .attr('transform', function(d) {
123 var ratio = scale(d);
124 var newAngle = config.minAngle + (ratio * range);
125 return 'rotate(' +newAngle +') translate(0,' +(config.labelInset - r) +')';
127 .text(config.labelFormat);
129 var lineData = [ [config.pointerWidth / 2, 0],
130 [0, -pointerHeadLength],
131 [-(config.pointerWidth / 2), 0],
132 [0, config.pointerTailLength],
133 [config.pointerWidth / 2, 0] ];
134 var pointerLine = d3.svg.line().interpolate('monotone');
135 var pg = svg.append('g').data([lineData])
136 .attr('class', 'pointer')
137 .attr('transform', centerTx);
139 pointer = pg.append('path')
140 .attr('d', pointerLine/*function(d) { return pointerLine(d) +'Z';}*/ )
141 .attr('transform', 'rotate(' +config.minAngle +')');
143 update(newValue === undefined ? 0 : newValue);
145 that.render = render;
147 function update(newValue, newConfiguration) {
148 if ( newConfiguration !== undefined) {
149 configure(newConfiguration);
151 var ratio = scale(newValue);
152 var newAngle = config.minAngle + (ratio * range);
154 .duration(config.transitionMs)
156 .attr('transform', 'rotate(' +newAngle +')');
158 that.update = update;