Skip to content

Commit 8999e2a

Browse files
committed
about to restructure how dimensions of the chart are calculated
1 parent e8b3fd1 commit 8999e2a

2 files changed

Lines changed: 188 additions & 34 deletions

File tree

learning_web_proj/d3_learning/pieChart.js

Lines changed: 160 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,93 @@
1+
/**
2+
* @module pieChart
3+
*
4+
* Used to report out on map polygon selections. Idea is in
5+
* the mapping component the user will select an area of interest,
6+
* The mapping widget will construct a series of data strucutres that
7+
* can be passes to this module to be visualized.
8+
*
9+
* In the end the visualization will show charts for selected polygons
10+
* as well as summary statistics about the polygon that was used to
11+
* select the polygons.
12+
*/
13+
114
var pieChart = (
215
function() {"use strict";
316

4-
var pieChrt, angleScale, svg, pi, i, radianOffSet, numColumns, svgWidth,
17+
var pieChrt, angleScale, svg, pi, i, radianOffSet, svgWidth,
518
svgHeight, minimumChartSize, spaceBetween, chartWidth, chartHeight,
6-
dataRadiusScale, minChrtWidth, minChrtHeight, percent2WidthDim,
7-
percent2HeightDim, ;
19+
dataRadiusScale, minChrtDims, percent2WidthDim,
20+
percent2HeightDim, numCols, numRows ;
821
pieChrt = {}; // namespace
922
pieChrt.chartDataSets = []; // populated with an array of data sets. Each element in this array represents a chart to be drawn
1023
pieChrt.chartConfigs = []; // populated with an array of config parameters
11-
numColumns = 2; // identifies how many charts to draw in the width provided for the svg
12-
minChrtWidth = 150;
13-
minChrtHeight = 125;
24+
minChrtDims = 150;
1425
spaceBetween = 5;
1526

1627

1728

29+
1830
/**
1931
* When this method is called we know how many charts need to get created. This method
2032
* will calculate the width and height of each chart and store this information in the
2133
* chartWidth and chartHeight variables
2234
*/
2335
function defineChartSize() {
24-
var numCols, whitespace;
36+
var whitespace;
2537
console.log("width is: " + svgWidth);
2638
// Can't forsee a situation where we would have more than 10 charts per column.
2739
// norm is likely 2 - 4.
28-
numCols = Math.floor(svgWidth / minChrtWidth);
40+
numCols = Math.floor(svgWidth / minChrtDims); // 2
2941
// space between each column and chart
3042
whitespace = spaceBetween * (numCols + 1);
31-
chartWidth = (svgWidth - whitespace) / numCols;
43+
console.log("whitespace: " + whitespace);
44+
chartWidth = Math.floor((svgWidth - whitespace) / numCols);
45+
46+
// numRows = (minChrtDims * Math.ceil(pieChrt.chartDataSets.length / numCols));
47+
numRows = Math.ceil(pieChrt.chartDataSets.length / numCols);
48+
console.log("numRows: " + numRows);
49+
console.log("numCols" + numCols);
3250

33-
if (numCols < pieChrt.chartDataSets.length) {
34-
svgHeight = minChrtHeight + (2 * spaceBetween);
51+
// svg height should be at whatever the chartWidth is * the number of rows
52+
// required
53+
if (numCols > pieChrt.chartDataSets.length) {
54+
//svgHeight = minChrtHeight + (2 * spaceBetween);
55+
svgHeight = chartWidth
56+
} else {
57+
//svgHeight = (minChrtHeight * Math.ceil(pieChrt.chartDataSets.length / numCols)) + (2 * spaceBetween);
58+
svgHeight = numRows * chartWidth;
59+
}
60+
//console.log("svg height is: " + svgHeight);
61+
// easiest to keep the charts a square, so...
62+
chartHeight = chartWidth + 0;
63+
}
64+
65+
/**
66+
* gets a count indicating what chart is being draw. Ie is it the first second
67+
* third etc. Based on that value and the number of rows and columns to be drawn
68+
* calculates offset coordinates as to where the chart should go.
69+
*/
70+
function getChartLocation(chartCnt) {
71+
var rowCnt, colCnt, offsets;
72+
// chartCnt is an array index so adding 1.
73+
chartCnt += 1
74+
rowCnt = Math.ceil(chartCnt / numCols);
75+
if (chartCnt <= numCols) {
76+
colCnt = chartCnt;
3577
} else {
36-
svgHeight = (minChrtHeight * Math.ceil(pieChrt.chartDataSets.length / numColumns)) + (2 * spaceBetween);
78+
colCnt = chartCnt%numCols;
79+
if (colCnt === 0) {
80+
colCnt = 1;
81+
} else {
82+
colCnt = colCnt + 1;
83+
}
3784
}
38-
chartHeight = minChrtHeight;
85+
offsets = {};
86+
offsets.x = colCnt * chartWidth;
87+
offsets.y = rowCnt * chartHeight;
88+
console.log("x: " + offsets.x);
89+
console.log("y: " + offsets.y);
90+
return offsets;
3991
}
4092

4193
/**
@@ -47,7 +99,8 @@ var pieChart = (
4799
function configureScaleFunctions(data) {
48100
var max, maxDomain;
49101
// Step 1, calculate the domain of the data.
50-
console.log("here");
102+
console.log("chartHeight: " + chartHeight);
103+
console.log("chartWidth: " + chartWidth);
51104
var max = d3.max(data, function(d, i) {
52105
return d.area;
53106
});
@@ -67,13 +120,12 @@ var pieChart = (
67120

68121
// 2c. radius scale, used to calculate minium and maxium raduses for arcs
69122
// input values are percentages.
70-
dataRadiusScale = d3.scale.linear().domain([0, data.length ]).range([percent2WidthDim(20), percent2WidthDim(70)]);
71-
123+
dataRadiusScale = d3.scale.linear().domain([0, data.length ]).range([percent2WidthDim(15), percent2HeightDim(70)]);
124+
//dataRadiusScale = d3.scale.linear().domain([0, data.length ]).range([20, 100]);
72125

73126
console.log('100 width:' + percent2WidthDim(100));
74127
console.log('100 height' + percent2HeightDim(100));
75-
console.log('100 radius ' + dataRadiusScale(100))
76-
128+
console.log('100 radius ' + dataRadiusScale(3))
77129
}
78130

79131
/**
@@ -118,19 +170,82 @@ var pieChart = (
118170
return arc;
119171
}
120172

173+
/**
174+
* creates an invisible arc upon which the labelling can be placed
175+
*
176+
*/
177+
function defineLabelArc(data) {
178+
var arc, d, i, val;
179+
arc = d3.svg.arc().innerRadius(function(d, i) {
180+
val = 0;
181+
if (i !== 0) {
182+
val = dataRadiusScale(data[i - 1].area);
183+
}
184+
return dataRadiusScale(i) + 1;
185+
}).outerRadius(function(d, i) {
186+
return dataRadiusScale(i + 1) - 1;
187+
}).startAngle(function(d, i) {
188+
return 0 - radianOffSet;
189+
}).endAngle(function(d, i) {
190+
return 0 + radianOffSet;
191+
});
192+
return arc;
193+
}
194+
195+
196+
/**
197+
* @param {Object[]} data
198+
* @param {String} data.label - The value that will be used to label the axis
199+
* @param {number} data.area - Contains the area that will be represented in the chart
200+
* @param {String} data.color - Hexidecimal number representing the color to be used to draw the value
201+
*
202+
* @param {Object} arc - a d3 svg arc that is used to define a path along which the data will be
203+
* shaded.
204+
* @param {Object} grpElem - A d3 grouped element that the arcs of data will be added to. The arc object
205+
* defines the path, this element is a grouped element that will get created by
206+
* d3. It will have the arcs that are used to visualize the data.
207+
* @param {Object} offsets
208+
* @param {number} offsets.x - The number of units from the x axis that coordinates should be
209+
* shifted in order to draw this element in the correct
210+
* location.
211+
* @param {number} offsets.y - Ditto as above but for Y.
212+
*/
213+
function drawDataArc(data, arc, grpElem, offsets) {
214+
grpElem.selectAll('path')
215+
.data(data)
216+
.enter()
217+
.append('path')
218+
.attr('d', arc)
219+
.attr('id', function(d, i) {
220+
return i;
221+
})
222+
.style('fill', function(d) {
223+
return d.color;
224+
})
225+
.attr("transform", 'translate('+ offsets.x +', '+ offsets.y +')'); // 300, 200
226+
//.attr("transform", 'translate('+ chartWidth +', '+ chartHeight +')'); // 300, 200
227+
}
228+
229+
function drawLabelDataArc(data, arc, dataLabelArc, offsets) {
230+
// start by drawing the hidden arc
231+
dataLabelArc.selectAll('path')
232+
.data(data)
233+
.enter()
234+
.append('path')
235+
.attr('d', arc)
236+
.attr('id', function(d, i) {
237+
return 'lab' + i;
238+
})
239+
.style("opacity", 0.5) // needs to be edited after debugging
240+
.attr('transform', 'translate('+ offsets.x + ',' + offsets.y + ')');
241+
}
242+
121243
function defineGroupedElements() {
122-
dataArc = svg.append('g');
123-
textArc = svg.append('g');
124-
majorTicsArc = svg.append('g');
125-
minorTicsArc = svg.append('g');
126-
labelsGroup = svg.append('g');
127-
128-
ticTextGroup = svg.append('g');
129-
ticTextArc = svg.append('g');
130-
ticTextPath = svg.append('g');
131-
132-
ticUnitsTextPathGroup = svg.append('g');
133-
ticUnitsTextGroup = svg.append('g');
244+
var groups = {};
245+
groups.dataArcs = svg.append('g');
246+
groups.textArc = svg.append('g');
247+
groups.dataLabelArc = svg.append('g');
248+
return groups;
134249
}
135250

136251
/**
@@ -141,9 +256,18 @@ var pieChart = (
141256
* @param {String} data.color - Hexidecimal number representing the color to be used to draw the value
142257
*
143258
*/
144-
function drawChart(data) {
145-
defineGroupedElements();
259+
function drawChart(dataPosition) {
260+
var data = pieChrt.chartDataSets[dataPosition];
261+
var offsets = getChartLocation(dataPosition);
262+
var groups = defineGroupedElements();
263+
264+
// drawing the data
146265
var dataArc = defineDataArc(data);
266+
drawDataArc(data, dataArc, groups.dataArcs, offsets);
267+
268+
// labelling the data
269+
var labelArc = defineLabelArc(data);
270+
drawLabelDataArc(data, labelArc, groups.dataLabelArc, offsets);
147271

148272

149273
}
@@ -202,12 +326,15 @@ var pieChart = (
202326
.append("svg")
203327
.attr("width", svgWidth)
204328
.attr("height", svgHeight);
329+
330+
console.log("svg width:" + svgWidth);
331+
console.log("svg height:" + svgHeight);
205332

206333

207334
// Iterating over the data used to draw each chart.
208335
for (i=0; i<pieChrt.chartDataSets.length; i++) {
209336
configureScaleFunctions(pieChrt.chartDataSets[i]);
210-
drawChart(pieChrt.chartDataSets[i]);
337+
drawChart(i);
211338
}
212339

213340

learning_web_proj/d3_learning/pieChart_MuleDeerTest_v5_7.html

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,34 @@
4040
units: 'm'
4141
}
4242

43+
var visualData = [{
44+
label : 'Preservation',
45+
area : 55234,
46+
color : '#B4FFB0'
47+
}, {
48+
label : 'Retained',
49+
area : 79691,
50+
color : '#A0E6E8'
51+
}, {
52+
label : 'Modified',
53+
area : 10752,
54+
color : '#BEBDFF'
55+
},
56+
{
57+
label : 'Max. Modified',
58+
area : 18093,
59+
color : '#E855DC'
60+
}];
61+
62+
var visualDataConfig = {
63+
label: 'Visual Quality Objective',
64+
units: 'm'
65+
}
66+
67+
4368
pieChart.addData(muleDeerData, muleDeerConfig);
44-
pieChart.createChart(400, 'chartsHere')
69+
pieChart.addData(visualData, visualDataConfig);
70+
71+
pieChart.createCharts(400, 'chartsHere')
4572
</script>
4673
</body>

0 commit comments

Comments
 (0)