dc.js getting a total for dimensions - dc.js

Fairly new to both javascript and dc so hopefully someone can point out a glaring mistake in here if one is obvious.
So the data I'm working with is in json format. It details the greenhouse gas emissions in kilotons from road transport between the years 1990 and 2016:
[
{
"1990": 69593.92909,
"1991": 68925.18017,
"1992": 69568.94507,
"1993": 69195.74371,
"1994": 66808.01905,
"1995": 64783.39396,
"1996": 66260.2817,
"1997": 65966.62162,
"1998": 64867.48598,
"1999": 65179.13673,
"2000": 64344.87089,
"2001": 63143.60193,
"2002": 62912.45843,
"2003": 60191.471,
"2004": 58955.982,
"2005": 56986.74396,
"2006": 54508.46013,
"2007": 52975.47679,
"2008": 49592.26223,
"2009": 46777.43559,
"2010": 43660.39487,
"2011": 41442.9064,
"2012": 39430.83139,
"2013": 37416.43375,
"2014": 36589.0465,
"2015": 35781.24439,
"2016": 35349.0576,
"Source": "Cars - Petrol"
},
{
"1990": 3148.096056,
"1991": 3559.279418,
"1992": 4501.543401,
"1993": 5763.746164,
"1994": 7551.451743,
"1995": 8839.059026,
"1996": 10165.98148,
"1997": 11134.32811,
"1998": 11490.2288,
"1999": 12563.88218,
"2000": 13115.085,
"2001": 14155.22918,
"2002": 15869.5177,
"2003": 17287.36108,
"2004": 18968.56143,
"2005": 20870.68665,
"2006": 22828.90392,
"2007": 24638.45712,
"2008": 25697.25635,
"2009": 26184.72538,
"2010": 27064.5961,
"2011": 28505.71289,
"2012": 30195.3133,
"2013": 31042.51841,
"2014": 32126.20481,
"2015": 33455.19565,
"2016": 35354.85215,
"Source": "Cars - Diesel"
},
{
"1990": 7320.552186,
"1991": 7093.082751,
"1992": 6637.212661,
"1993": 6243.982502,
"1994": 5764.001438,
"1995": 5117.0386,
"1996": 4848.336446,
"1997": 4508.602099,
"1998": 4226.296125,
"1999": 3582.015194,
"2000": 3104.684909,
"2001": 2647.70305,
"2002": 2270.02413,
"2003": 1949.951969,
"2004": 1731.534116,
"2005": 1498.096445,
"2006": 1489.266001,
"2007": 1377.155724,
"2008": 1239.89787,
"2009": 1086.068122,
"2010": 978.3437778,
"2011": 901.571409,
"2012": 823.0520605,
"2013": 768.0300208,
"2014": 727.6683437,
"2015": 683.7916969,
"2016": 649.8161786,
"Source": "LGVs - Petrol"
},
{
"1990": 4380.373876,
"1991": 5001.759915,
"1992": 5589.825615,
"1993": 6187.067679,
"1994": 7192.852571,
"1995": 7853.696941,
"1996": 8756.823494,
"1997": 9670.809673,
"1998": 10210.54947,
"1999": 10788.81777,
"2000": 11249.3556,
"2001": 11733.90808,
"2002": 12320.65248,
"2003": 13067.11867,
"2004": 13756.55654,
"2005": 14428.76742,
"2006": 14858.27928,
"2007": 15573.87491,
"2008": 14821.01165,
"2009": 14722.3269,
"2010": 15169.77151,
"2011": 15348.7601,
"2012": 15590.40271,
"2013": 15857.02735,
"2014": 16626.16906,
"2015": 17523.01445,
"2016": 18567.29094,
"Source": "LGVs - Diesel"
},
{
"1990": 5339.157087,
"1991": 5467.466971,
"1992": 5392.960065,
"1993": 5377.750567,
"1994": 5440.415776,
"1995": 5508.010587,
"1996": 5568.965398,
"1997": 5566.064477,
"1998": 5432.401344,
"1999": 5241.387095,
"2000": 4912.219975,
"2001": 4801.21838,
"2002": 4773.64816,
"2003": 4883.157328,
"2004": 4681.323432,
"2005": 4673.478741,
"2006": 4728.510847,
"2007": 4870.561051,
"2008": 4246.393859,
"2009": 4237.468976,
"2010": 4331.09672,
"2011": 3995.306943,
"2012": 3819.356001,
"2013": 3872.42702,
"2014": 3858.306896,
"2015": 3733.342863,
"2016": 3491.547872,
"Source": "Buses and Coaches"
},
{
"1990": 20549.84591,
"1991": 19538.54867,
"1992": 19457.03083,
"1993": 19730.14005,
"1994": 20340.94429,
"1995": 20004.24193,
"1996": 20573.1718,
"1997": 20611.02009,
"1998": 20393.29457,
"1999": 19989.60494,
"2000": 19665.41352,
"2001": 19642.46875,
"2002": 20113.67776,
"2003": 20299.40457,
"2004": 20701.27394,
"2005": 20956.01743,
"2006": 20975.16199,
"2007": 21128.61243,
"2008": 19650.07428,
"2009": 18115.9976,
"2010": 18661.96818,
"2011": 18236.36235,
"2012": 18286.93244,
"2013": 18381.82839,
"2014": 18909.17826,
"2015": 19931.43374,
"2016": 20360.34154,
"Source": "HGV"
},
{
"1990": 727.5617999,
"1991": 710.4634975,
"1992": 609.4520657,
"1993": 519.774719,
"1994": 509.2231228,
"1995": 503.1006123,
"1996": 525.6073905,
"1997": 568.1948702,
"1998": 585.0268566,
"1999": 646.6997447,
"2000": 641.3058103,
"2001": 652.2754823,
"2002": 681.564717,
"2003": 741.4856181,
"2004": 679.5842972,
"2005": 710.8353572,
"2006": 662.6894662,
"2007": 704.5256474,
"2008": 649.562263,
"2009": 636.8283738,
"2010": 561.0698798,
"2011": 557.3736257,
"2012": 531.1360981,
"2013": 504.6618709,
"2014": 519.1426045,
"2015": 524.409001,
"2016": 544.3244057,
"Source": "Motorcycles - >50 cc"
},
{
"1990": 68.18440893,
"1991": 61.90839807,
"1992": 51.38951192,
"1993": 41.13160339,
"1994": 39.39956559,
"1995": 34.87192636,
"1996": 32.59435156,
"1997": 30.99510927,
"1998": 30.56930476,
"1999": 33.9429331,
"2000": 35.40654479,
"2001": 36.54028425,
"2002": 35.25401917,
"2003": 36.87676555,
"2004": 30.94174336,
"2005": 30.78605235,
"2006": 25.91880501,
"2007": 27.52053808,
"2008": 22.41888214,
"2009": 21.07530165,
"2010": 18.32310824,
"2011": 17.17460528,
"2012": 15.6090557,
"2013": 13.14104882,
"2014": 12.30089753,
"2015": 11.32364855,
"2016": 10.97048206,
"Source": "Mopeds - <50cc"
},
{
"1990": 0,
"1991": 0,
"1992": 0,
"1993": 0,
"1994": 0,
"1995": 0,
"1996": 2.970725709,
"1997": 5.941451418,
"1998": 11.97029772,
"1999": 23.93656381,
"2000": 65.61233439,
"2001": 160.465194,
"2002": 258.3156443,
"2003": 311.1751708,
"2004": 332.7632485,
"2005": 358.2895797,
"2006": 374.6397872,
"2007": 353.3743875,
"2008": 369.0592288,
"2009": 316.9182002,
"2010": 314.7224273,
"2011": 290.7193995,
"2012": 274.9473235,
"2013": 277.6184974,
"2014": 259.730796,
"2015": 241.8100066,
"2016": 209.0667968,
"Source": "All LPG Vehicles"
}
]
I am able to render a chart with the different sources of emissions (e.g. Cars - Petrol, Cars - Diesel etc.) on the x axis and the numerical value for a year on the y axis. So for example, the emissions data for the year 1990 would be rendered:
function makeGraph(error, ggData) {
if (error) throw error;
var ndx = crossfilter(ggData); //load the data into a crossfilter
var sourceDim = ndx.dimension(dc.pluck("Source"));
var sourceDimGroup = sourceDim.group();
y1990Emissions(ndx);
dc.renderAll();
//generate x axis source and y axis kilotonnes for year 1990
function y1990Emissions(ndx) {
var y1990 = sourceDim.group().reduceSum(dc.pluck("1990")); //the y axis
dc.barChart("#y1990-pollution")
.width(700)
.height(500)
.margins({top:10, right:50, bottom: 100, left:50})
.dimension(sourceDim)
.group(y1990)
.x(d3.scale.ordinal())
.xUnits(dc.units.ordinal);
};
};
However, I'm at a loss as to how I can generate a bar chart for the TOTAL emissions data from all sources along the y axis, with the different years along the x axis.
Something that looks like this
I feel like I need to use a counter variable, and so far I have this
function totalEmissionsPerYear(ndx) {
var total1990 = 0;
var total1990Dim = ndx.dimension(function(d) {
return total1990 += d["1990"];
});
But I'm not really sure where to take it from there.
Thanks!

Related

D3 Applying colour from a linear scale to my data

I have an SVG hex map that I've joined to my dataset using a foreach function.
Dataset:
var data = [
{ "id": "Scotland ", "value": 5000 },
{ "id": "Wales ", "value": 3000 },
{ "id": "Ireland ", "value": 750 },
{ "id": "North West ", "value": 1250 },
{ "id": "North East ", "value": 4500 },
{ "id": "East Midlands ", "value": 2000 },
{ "id": "South East ", "value": 350 },
{ "id": "South West ", "value": 6000 },
{ "id": "East of England ", "value": 4000 },
{ "id": "West Midlands ", "value": 2500 },
{ "id": "Northern Ireland ", "value": 1000},
{ "id": "Isle of Man ", "value": 3000 },
{ "id": "Yorkshire and the Humber ", "value": 1500 },
{ "id": "Greater London ", "value": 5000 }
];
SVG Sample:
<g role="menuitem" aria-label="South East " transform="translate(445.5384524232801,-18.74937119481314)" cursor="pointer">
<path cs="100,100" d="M5.5,3.5 L0.5,6.5 L-4.5,3.5 L-4.5,-2.5 L0.5,-5.5 L5.5,-2.5 Z" fill="#ffffff" stroke="#ffffff" fill-opacity="1" stroke-width="0.01" stroke-opacity="1" transform="translate(0,0) scale(1)" class="amcharts-map-image"></path>
</g>
<g role="menuitem" aria-label="South East " transform="translate(409.5336486154187,-18.74937119481314)" cursor="pointer">
<path cs="100,100" d="M5.5,3.5 L0.5,6.5 L-4.5,3.5 L-4.5,-2.5 L0.5,-5.5 L5.5,-2.5 Z" fill="#ffffff" stroke="#ffffff" fill-opacity="1" stroke-width="0.01" stroke-opacity="1" transform="translate(0,0) scale(1)" class="amcharts-map-image"></path>
</g>
<g role="menuitem" aria-label="Greater London " transform="translate(457.534160443724,22.32951986709005)">
<path cs="100,100" d="M5.5,3.5 L0.5,6.5 L-4.5,3.5 L-4.5,-2.5 L0.5,-5.5 L5.5,-2.5 Z" fill="#ffffff" stroke="#ffffff" fill-opacity="1" stroke-width="0.01" stroke-opacity="1" transform="translate(0,0) scale(1)" class="amcharts-map-image"></path>
</g>
The function iterates through my dataset and maps the IDs to the respective IDs in the SVG. The IDs within the SVG are contained with the aria-label attribute of g (eg "Scotland", "South East", "Wales"...etc
Function
const ids = data.map(d => d.id);
ids.forEach(id =>
d3.selectAll(`g[aria-label="${id}"]`)
My data contains a Value against each ID/Region. What I'm trying to do is use a colour scale like this:
var colorScale = d3
.scaleLinear()
.domain(d3.extent(data, (d) => d.value))
.range(["#5C4D87", "#EC4E6B"])
.interpolate(d3.interpolateHcl)
...and apply that colour to my SVG based on the value assigned to the Region.
This is what I've tried so far:
const svgUrl = "https://raw.githubusercontent.com/gangrel11/samplefiles/main/amCharts.pixelMap.svg";
d3.xml(svgUrl).then(render);
function render(svg) {
d3.select("body").node().append(svg.documentElement)
const ids = data.map(d => d.id);
ids.forEach(id =>
d3.selectAll(`g[aria-label="${id}"]`)
.select('path')
.style("fill", function(d) {
return colorScale(d)
})
);
Iterate over your initial data not the map of ids.
data.forEach(d => {
d3.selectAll(`g[aria-label="${d.id}"]`)
.selectAll('path')
.style("fill",colorScale(d.value))
})

Use D3 to create a contour/polar graph

Using D3, I want to create a polar graph like this figure.
Following the volcano example, I can get the contour correctly rendered.
How do I translate the contours onto a circle to create a polar graph?
Update:
The initial data I'm using is a 2d array, where the first dimension (array[n]) represents period and the second dimension (array[n][0]) represents force. See below for a sample of data and the code I'm using to draw the contour graph.
function draw(spectrumData: Swell["spectrum"]): void {
if (!spectrumData) {
console.warn("spectrumData is undefined");
return;
}
const { spectrum, width, height } = transformSpectrum(spectrumData);
const sizeFactor = 13;
const svg = d3
.select("#spectrum-contour")
.attr("width", width * sizeFactor)
.attr("height", height * sizeFactor);
// Ret the contour size
var contour = d3Contour.contours().size([width, height]);
// Returns "rgb(x, y, z)"
const interpolator = (t: number): string => {
const i0 = d3Hsv.interpolateHsvLong(
d3Hsv.hsv(240, 0.77, 0.73),
d3Hsv.hsv(114, 0.77, 0.73)
);
const i1 = d3Hsv.interpolateHsvLong(
d3Hsv.hsv(114, 0.77, 0.73),
d3Hsv.hsv(0, 0.77, 0.73)
);
return t < 0.2 ? i0(t * 2) : i1((t - 0.2) * 2);
};
const colorScale = d3Scale
.scaleSequential(interpolator)
// extent returns [min, max]
.domain(d3.extent(spectrum));
const path: any = d3Geo.geoPath(
d3Geo.geoIdentity().scale((width * sizeFactor) / width)
);
// draw the contours
svg
.selectAll("path")
.data(contour(spectrum))
.enter()
.append("path")
.attr("d", (feature) => path(feature))
.attr("fill", (d) => colorScale(d.value));
}
// Transform the 2D spectrum array to a 1D array
// and replace negative values with zeros.
function transformSpectrum(data: Swell["spectrum"]): TransformedSpectrum {
const array1d = ([] as number[]).concat.apply([], data);
return {
width: data[0].length,
height: data.length,
spectrum: array1d.map((i) => (i >= 0 ? i : 0)),
};
}
"spectrum": [
[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
[ 0.0002, 0.0003, 0.0003, 0.0003, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0003, 0.0003, 0.0003, 0.0003, 0.0002, 0.0002, 0.0002, 0.0002, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0002, 0.0002, 0.0002, 0.0003, 0.0003, 0.0003, 0.0004, 0.0004, 0.0005, 0.0005, 0.0006, 0.0006, 0.0007, 0.0007, 0.0007 ],
[ 0.0001, 0.0001, 0.0002, 0.0002, 0.0003, 0.0003, 0.0004, 0.0004, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0005, 0.0005, 0.0005, 0.0006, 0.0006, 0.0007, 0.0007, 0.0007, 0.0008, 0.0008 ],
[ 0.0003, 0.0003, 0.0002, 0.0002, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0002, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0009, 0.001, 0.0011, 0.0013, 0.0014, 0.0015, 0.0016, 0.0017, 0.0018, 0.0018, 0.0018, 0.0019, 0.0018, 0.0018, 0.0018, 0.0017, 0.0016, 0.0015, 0.0014, 0.0013, 0.0011, 0.001, 0.0008, 0.0007 ],
[ 0.0007, 0.0007, 0.0006, 0.0006, 0.0006, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0006, 0.0006, 0.0007, 0.0008, 0.0009, 0.001, 0.0011, 0.0012, 0.0013, 0.0015, 0.0016, 0.0016, 0.0017, 0.0018, 0.0018, 0.0019, 0.0019, 0.0018, 0.0018, 0.0018, 0.0017, 0.0016, 0.0015, 0.0014, 0.0012, 0.0011, 0.001, 0.0008 ],
[ 0.0007, 0.0006, 0.0005, 0.0004, 0.0004, 0.0003, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008, 0.001, 0.0012, 0.0013, 0.0015, 0.0017, 0.0018, 0.002, 0.0021, 0.0022, 0.0023, 0.0023, 0.0023, 0.0024, 0.0023, 0.0023, 0.0022, 0.0021, 0.002, 0.0019, 0.0017, 0.0016, 0.0014 ],
[ 0.0001, 0.0001, 0.0002, 0.0002, 0.0003, 0.0004, 0.0006, 0.0007, 0.0009, 0.0011, 0.0013, 0.0016, 0.0018, 0.0021, 0.0023, 0.0026, 0.0029, 0.0032, 0.0034, 0.0037, 0.0039, 0.0041, 0.0042, 0.0044, 0.0045, 0.0045, 0.0046, 0.0046, 0.0045, 0.0044, 0.0043, 0.0042, 0.004, 0.0038, 0.0036, 0.0034, 0.0031, 0.0029, 0.0026, 0.0024 ],
[ 0.0014, 0.001, 0.0006, 0.0003, 0.0, -0.0002, -0.0003, -0.0003, -0.0001, 0.0003, 0.0008, 0.0015, 0.0024, 0.0035, 0.0047, 0.0061, 0.0076, 0.0092, 0.0109, 0.0126, 0.0143, 0.016, 0.0175, 0.019, 0.0203, 0.0214, 0.0224, 0.023, 0.0235, 0.0236, 0.0235, 0.0231, 0.0224, 0.0215, 0.0204, 0.019, 0.0175, 0.0158, 0.014, 0.0122 ],
[ 0.0036, 0.0023, 0.0009, -0.0005, -0.0019, -0.0033, -0.0045, -0.0054, -0.006, -0.0062, -0.0061, -0.0054, -0.0043, -0.0027, -0.0005, 0.0021, 0.0052, 0.0086, 0.0124, 0.0165, 0.0207, 0.0251, 0.0294, 0.0336, 0.0376, 0.0413, 0.0447, 0.0475, 0.0498, 0.0516, 0.0526, 0.0531, 0.0528, 0.0519, 0.0503, 0.0481, 0.0453, 0.0421, 0.0385, 0.0345 ],
[ 0.0031, 0.0022, 0.0013, 0.0003, -0.0008, -0.0018, -0.0027, -0.0035, -0.0041, -0.0045, -0.0046, -0.0044, -0.0039, -0.0029, -0.0016, 0.0, 0.002, 0.0043, 0.0069, 0.0098, 0.0128, 0.0159, 0.0191, 0.0222, 0.0253, 0.0282, 0.0309, 0.0333, 0.0353, 0.0369, 0.038, 0.0387, 0.0389, 0.0386, 0.0378, 0.0366, 0.0349, 0.0328, 0.0304, 0.0277 ],
[ 0.001, -0.0001, -0.0012, -0.0022, -0.0032, -0.0039, -0.0045, -0.0047, -0.0047, -0.0044, -0.0038, -0.0027, -0.0014, 0.0003, 0.0022, 0.0044, 0.0069, 0.0095, 0.0122, 0.015, 0.0177, 0.0204, 0.0229, 0.0252, 0.0272, 0.0289, 0.0302, 0.0311, 0.0316, 0.0316, 0.0312, 0.0303, 0.029, 0.0274, 0.0254, 0.0231, 0.0205, 0.0178, 0.015, 0.0121 ],
[ -0.0002, -0.0006, -0.001, -0.0014, -0.0017, -0.0019, -0.002, -0.0019, -0.0018, -0.0015, -0.0011, -0.0005, 0.0001, 0.0009, 0.0019, 0.0029, 0.0039, 0.0051, 0.0062, 0.0073, 0.0085, 0.0095, 0.0105, 0.0114, 0.0121, 0.0127, 0.0131, 0.0134, 0.0135, 0.0134, 0.0131, 0.0126, 0.012, 0.0113, 0.0104, 0.0094, 0.0084, 0.0072, 0.0061, 0.005 ],
[ 0.0003, 0.0, -0.0004, -0.0007, -0.001, -0.0013, -0.0015, -0.0016, -0.0016, -0.0015, -0.0012, -0.0009, -0.0004, 0.0002, 0.0009, 0.0017, 0.0026, 0.0035, 0.0045, 0.0056, 0.0067, 0.0077, 0.0087, 0.0096, 0.0105, 0.0112, 0.0119, 0.0123, 0.0127, 0.0128, 0.0128, 0.0127, 0.0124, 0.0119, 0.0113, 0.0105, 0.0097, 0.0088, 0.0078, 0.0067 ],
[ -0.0003, -0.0004, -0.0005, -0.0005, -0.0005, -0.0005, -0.0004, -0.0003, -0.0002, 0.0, 0.0002, 0.0005, 0.0008, 0.0011, 0.0014, 0.0018, 0.0021, 0.0025, 0.0028, 0.0032, 0.0035, 0.0038, 0.004, 0.0042, 0.0044, 0.0045, 0.0046, 0.0046, 0.0046, 0.0045, 0.0044, 0.0042, 0.004, 0.0037, 0.0035, 0.0032, 0.0028, 0.0025, 0.0022, 0.0019 ],
[ -0.0004, -0.0006, -0.0008, -0.0009, -0.001, -0.0011, -0.0011, -0.001, -0.0009, -0.0007, -0.0004, -0.0001, 0.0003, 0.0008, 0.0013, 0.0019, 0.0024, 0.003, 0.0036, 0.0042, 0.0048, 0.0053, 0.0057, 0.0062, 0.0065, 0.0067, 0.0069, 0.007, 0.007, 0.0069, 0.0067, 0.0064, 0.0061, 0.0057, 0.0052, 0.0047, 0.0041, 0.0036, 0.003, 0.0024 ],
[ -0.0001, -0.0002, -0.0004, -0.0005, -0.0006, -0.0007, -0.0007, -0.0007, -0.0007, -0.0006, -0.0005, -0.0003, -0.0001, 0.0002, 0.0005, 0.0009, 0.0013, 0.0017, 0.0021, 0.0026, 0.003, 0.0034, 0.0038, 0.0042, 0.0045, 0.0048, 0.005, 0.0051, 0.0052, 0.0053, 0.0052, 0.0051, 0.005, 0.0048, 0.0045, 0.0042, 0.0038, 0.0034, 0.003, 0.0026 ],
[ -0.0003, -0.0003, -0.0003, -0.0003, -0.0003, -0.0002, -0.0002, -0.0001, 0.0, 0.0001, 0.0002, 0.0004, 0.0005, 0.0007, 0.0009, 0.0011, 0.0013, 0.0015, 0.0017, 0.0018, 0.002, 0.0021, 0.0023, 0.0024, 0.0025, 0.0025, 0.0025, 0.0026, 0.0025, 0.0025, 0.0024, 0.0023, 0.0022, 0.0021, 0.002, 0.0018, 0.0017, 0.0015, 0.0014, 0.0012 ],
[ -0.0002, -0.0002, -0.0002, -0.0001, -0.0001, -0.0001, 0.0, 0.0, 0.0001, 0.0002, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008, 0.0008, 0.0009, 0.001, 0.0011, 0.0011, 0.0011, 0.0012, 0.0012, 0.0012, 0.0012, 0.0012, 0.0011, 0.0011, 0.0011, 0.001, 0.001, 0.0009, 0.0008, 0.0008, 0.0007, 0.0006, 0.0006, 0.0005 ],
[ -0.0001, -0.0001, -0.0002, -0.0001, -0.0001, -0.0001, -0.0001, 0.0, 0.0, 0.0001, 0.0002, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008, 0.0009, 0.001, 0.0011, 0.0011, 0.0012, 0.0012, 0.0013, 0.0013, 0.0013, 0.0013, 0.0012, 0.0012, 0.0012, 0.0011, 0.0011, 0.001, 0.0009, 0.0008, 0.0008, 0.0007, 0.0006, 0.0005 ],
[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0001, 0.0001, 0.0002, 0.0002, 0.0003, 0.0003, 0.0004, 0.0005, 0.0006, 0.0006, 0.0007, 0.0008, 0.0009, 0.001, 0.0011, 0.0011, 0.0012, 0.0013, 0.0013, 0.0014, 0.0014, 0.0014, 0.0014, 0.0015, 0.0014, 0.0014, 0.0014, 0.0014, 0.0013, 0.0013, 0.0012, 0.0012, 0.0011, 0.001, 0.0009 ],
[ -0.0002, -0.0002, -0.0002, -0.0002, -0.0002, -0.0001, -0.0001, -0.0001, 0.0, 0.0001, 0.0002, 0.0002, 0.0003, 0.0004, 0.0005, 0.0007, 0.0008, 0.0009, 0.001, 0.0011, 0.0011, 0.0012, 0.0013, 0.0013, 0.0014, 0.0014, 0.0014, 0.0014, 0.0013, 0.0013, 0.0012, 0.0012, 0.0011, 0.001, 0.0009, 0.0008, 0.0007, 0.0006, 0.0005, 0.0005 ],
[ 0.0002, 0.0002, 0.0002, 0.0001, 0.0001, 0.0, 0.0, 0.0, -0.0001, -0.0001, -0.0001, -0.0001, -0.0001, -0.0001, -0.0001, -0.0001, 0.0, 0.0001, 0.0001, 0.0002, 0.0004, 0.0005, 0.0006, 0.0007, 0.0009, 0.001, 0.0011, 0.0012, 0.0013, 0.0014, 0.0015, 0.0015, 0.0016, 0.0016, 0.0016, 0.0016, 0.0015, 0.0015, 0.0014, 0.0013 ],
[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0002, 0.0002, 0.0002, 0.0002, 0.0003, 0.0003, 0.0004, 0.0004, 0.0005, 0.0005, 0.0005, 0.0006, 0.0006, 0.0007, 0.0007, 0.0007, 0.0008, 0.0008, 0.0008, 0.0008, 0.0008, 0.0008, 0.0008, 0.0008, 0.0007, 0.0007 ],
[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0001, 0.0001, 0.0001, 0.0001, 0.0002, 0.0002, 0.0002, 0.0003, 0.0003, 0.0003, 0.0003, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0004, 0.0003, 0.0003, 0.0003, 0.0003, 0.0002 ]
],
Update 2
Using the suggestion below I was able to translate the coordinates with the following:
const path: any = d3Geo.geoPath(
d3Geo.geoTransform({
point: function (x, y) {
const [x2, y2] = polarPoint(x, y);
this.stream.point(x2, y2);
},
})
);
Given your polar plot innerRadius, outerRadius, domainRange (range of arguments), and valueRange (range of values), the polar coordinates of each point (arg, value) are calculated the following way:
const polarPoint = (arg, value) => {
const angle = (arg - domainRange.min) /
(domainRange.max - domainRange.min) * Math.PI * 2;
const radius = (value - valueRange.min) /
(valueRange.max - valueRange.min) * (outerRadius - innerRadius) + innerRadius;
const x = radius * Math.sin(angle);
const y = -radius * Math.cos(angle);
return {x, y};
}
See the snippet as illustration:
const domainRange = {min: 0, max: 100}; // Range of arguments
const valueRange = {min: 0, max: 1000}; // Range of values
const plotWidth = 200; // Width of the cartesian plot
const plotHeight = 100; // Height of the cartesian plot
const innerRadius = 20; // Inner radius of the polar plot
const outerRadius = 100; // Outer radius of the polar plot
const values = [[0, 100],[10,400],[20,700],[30,750],[40,900],[50,600],[60,400],[70,350],[80,300],[90,250],[100,100]]; // pairs of arguments/values
const cartesianPoint = (arg, value) => {
const x = (arg - domainRange.min) / (domainRange.max - domainRange.min) * plotWidth;
const y = -(value - valueRange.min) / (valueRange.max - valueRange.min) * plotHeight;
return {x, y};
};
const polarPoint = (arg, value) => {
const angle = (arg - domainRange.min) / (domainRange.max - domainRange.min) * Math.PI * 2;
const radius = (value - valueRange.min) / (valueRange.max - valueRange.min) * (outerRadius - innerRadius) + innerRadius;
const x = radius * Math.sin(angle);
const y = -radius * Math.cos(angle);
return {x, y};
}
const svg = d3.select('svg');
const cartesianPlot = svg.append('g')
.attr('transform', 'translate(20, 150)');
cartesianPlot.append('rect')
.attr('x', 0)
.attr('y', -plotHeight)
.attr('width', plotWidth)
.attr('height', plotHeight)
.style('fill', '#eee');
let prev = null;
values.forEach(v => {
const point = cartesianPoint(v[0], v[1]);
cartesianPlot.append('circle')
.attr('cx', point.x)
.attr('cy', point.y)
.attr('r', 3)
.style('fill', 'red');
if (prev)
cartesianPlot.append('line')
.attr('x1', prev.x)
.attr('y1', prev.y)
.attr('x2', point.x)
.attr('y2', point.y)
.style('stroke', 'blue')
prev = point;
})
const polarPlot = svg.append('g')
.attr('transform', 'translate(350, 120)');
polarPlot.append('circle')
.attr('r', outerRadius)
.style('fill', '#eee')
polarPlot.append('circle')
.attr('r', innerRadius)
.style('fill', '#fff')
prev = null;
values.forEach(v => {
const point = polarPoint(v[0], v[1]);
polarPlot.append('circle')
.attr('cx', point.x)
.attr('cy', point.y)
.attr('r', 3)
.style('fill', 'red');
if (prev)
polarPlot.append('line')
.attr('x1', prev.x)
.attr('y1', prev.y)
.attr('x2', point.x)
.attr('y2', point.y)
.style('stroke', 'blue')
prev = point;
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="250" />

D3JS: Event triggering not working

I have made this list of countries, and I want its corresponding circle to have some interactivity, but the function declared for the mouseover and the mouseout are not working. Everything seems to be fine and I can't find the issue. Can you help me figure out what's happening?
here's my code
var mapData = [
{"code":"AU" , "name":"Australia", "value":3, "color":"#BC204B"},
{"code":"BE" , "name":"Belgium", "value":1, "color":"#BC204B"},
{"code":"BR" , "name":"Brazil", "value":1, "color":"#BC204B"},
{"code":"CA" , "name":"Canada", "value":4, "color":"#BC204B"},
{"code":"FI" , "name":"Finland", "value":1, "color":"#BC204B"},
{"code":"FR" , "name":"France", "value":1, "color":"#BC204B"},
{"code":"DE" , "name":"Germany", "value":1, "color":"#BC204B"},
{"code":"IS" , "name":"Iceland", "value":1, "color":"#BC204B"},
{"code":"IN" , "name":"India", "value":2, "color":"#BC204B"},
{"code":"IE" , "name":"Ireland", "value":1, "color":"#BC204B"},
{"code":"JP" , "name":"Japan", "value":2, "color":"#BC204B"},
{"code":"KP" , "name":"Korea, Dem. Rep.", "value":1, "color":"#BC204B"},
{"code":"LU" , "name":"Luxembourg", "value":1, "color":"#BC204B"},
{"code":"NL" , "name":"Netherlands", "value":3, "color":"#BC204B"},
{"code":"NZ" , "name":"New Zealand", "value":2, "color":"#BC204B"},
{"code":"NO" , "name":"Norway", "value":2, "color":"#BC204B"},
{"code":"PY" , "name":"Paraguay", "value":1, "color":"#BC204B"},
{"code":"SA" , "name":"Saudi Arabia", "value":2, "color":"#BC204B"},
{"code":"SG" , "name":"Singapore", "value":1, "color":"#BC204B"},
{"code":"ES" , "name":"Spain", "value":1, "color":"#BC204B"},
{"code":"CH" , "name":"Switzerland", "value":2, "color":"#BC204B"},
{"code":"AE" , "name":"United Arab Emirates", "value":2, "color":"#BC204B"},
{"code":"GB" , "name":"United Kingdom", "value":2, "color":"#BC204B"},
{"code":"US" , "name":"United States", "value":4, "color":"#BC204B"}
];
var width = "512"
var height = "400"
var radius = 5
var svg = d3.select("body")
.append("svg")
.attr("width",width)
.attr("height",height)
.style("background","#dedede")
.append("g")
.attr("transform", function (d, i) {
return "translate(" + width/20 + "," + (height -170) + ")";
});
var g = svg.selectAll("g")
.data(mapData)
.enter()
.append("g")
.attr("transform", function (d, i) {
return "translate(" + i * 20 + "," + 0+ ")";
});
var circles = g.append("circle")
.attr("cx",0)
.attr("cy",0)
.attr("r",radius)
.attr("fill","#00A3A1")
.on("mouseover", handleMouseOver)
.on("mouseout", handleMouseOut);
var labels = g.append("text")
.attr("class","countriesText")
.style("fill", "black")
.attr("x", 0)
.attr("y", 0)
.attr("dy", ".35em")
.attr("text-anchor", "start")
.text(function(d,i){
return mapData[i].name;
})
.attr("transform", "translate(0,20) scale(1) rotate(90)")
function handleMouseOver(d, i) {
d3.select(this).attr({
fill: "orange",
r: radius * 2
});
};
function handleMouseOut(d, i) {
d3.select(this).attr({
fill: "#00a3a1",
r: radius
})
};
*{
font-family: arial
-webkit-font-smoothing: antialiased;
}
.countriesText{
font-family: arial;
text-rendering: optimizeLegibility;
}
thanks

Issue with d3 Multi Line Chart

I didn't get line throw this code,
i get perfect axis both side but line is still missing
while calling drawline function i got this errorr "Error: attribute d: Expected number, "M0,NaNL116.66666666…"."
i couldn't find out any solutions.
<script src="http://d3js.org/d3.v4.min.js"></script>
//Data from Webservice
var jsonData = [{ "YEAR": "2017", "JAN": "0", "FEB": "21", "MAR": "65", "APR": "25", "MAY": "33", "JUN": "30", "JUL": "17", "AUG": "26", "SEP": "0", "OCT": "5", "NOV": "33", "DEC": "12" }, { "YEAR": "2018", "JAN": "10", "FEB": "0", "MAR": "0", "APR": "0", "MAY": "0", "JUN": "0", "JUL": "0", "AUG": "0", "SEP": "0", "OCT": "0", "NOV": "0", "DEC": "0" }]; `
var x = d3.scaleBand().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var tests = months.map(function (m) {`
return {
month: m,
values: jsonData.map(function (d) {
return {
year: d.YEAR,
quote: +d[m]
};
})
};
});
x.domain(tests.map(function (d) { return d.month; }));
y.domain([d3.min(tests, function (c) {
return d3.min(c.values, function (v) {
return v.quote;
});
}),
d3.max(tests, function (c) { return d3.max(c.values, ` `function (v) { return v.quote; }); })]);
// Create Line
var drawLine = d3.line()
.curve(d3.curveBasis)
.x(function (d) {
debugger;
return x(d.month);
})
.y(function (d) {
debugger;
return y(d.values);
});
var draw = svg.selectAll(".month")
.data(tests)
.enter().append("g")
.attr("class", "month")
.datum(tests)
.append("path")
.attr("class", "line")
.attr("d", drawLine)
.style("stroke", function (d) {
return color(d.year);
})
`
Looks like a lot of stuff is missing from your code.
I made a few changes in the code and it works now.
//Data from Webservice
var jsonData = [{ "YEAR": "2017", "JAN": "0", "FEB": "21", "MAR": "65", "APR": "25", "MAY": "33", "JUN": "30", "JUL": "17", "AUG": "26", "SEP": "0", "OCT": "5", "NOV": "33", "DEC": "12" }, { "YEAR": "2018", "JAN": "10", "FEB": "0", "MAR": "0", "APR": "0", "MAY": "0", "JUN": "0", "JUL": "0", "AUG": "0", "SEP": "0", "OCT": "0", "NOV": "0", "DEC": "0" }];
var width = 400;
var height = 400;
var svg = d3.select('#lineChart');
var x = d3.scaleBand().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var months = [];
d3.keys(jsonData[0]).filter(function(key) {
if (key !== "YEAR") {
months.push(key)
}
});
var years = [];
for (var i = 0; i < jsonData.length; i++) {
years.push(jsonData[i]['YEAR']);
}
var tests = months.map(function(m) {
return {
month: m,
values: jsonData.map(function(d) {
return {
year: d.YEAR,
quote: parseInt(+d[m])
};
})
};
});
x.domain(tests.map(function(d) {
return d.month;
}));
var extent = [d3.min(tests, function(c) {
return d3.min(c.values, function(v) {
return v.quote;
});
}),
d3.max(tests, function(c) {
return d3.max(c.values, function(v) {
return v.quote;
});
})
]
y.domain(extent);
// Create Line
var drawLine = d3.line()
.curve(d3.curveBasis)
.x(function(d) {
return x(d.month);
})
.y(function(d) {
return y((d.values[0]['quote']));
});
var draw = svg.append('g').append("path")
.datum(tests)
.attr("class", "line")
.attr("d", drawLine)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5);

How do you render a 3D object (where the vertices and normals are stored in a header file) using OpenGL ES for iPhone development?

I created a simple 3D cube object using blender and used Jeff Lamarche's blender export to store it in a header file and added it into my project. However I am having trouble rendering it.
header file:
//If not using MC3D, change 1 to 0 to add needed types
#if 0
#import "MC3DTypes.h"
#else
struct texCoord
{
GLfloat u;
GLfloat v;
};
typedef struct texCoord texCoord;
typedef texCoord* texCoordPtr;
typedef struct vec2 vec2;
typedef vec2* vec2Ptr;
struct vec3
{
GLfloat x;
GLfloat y;
GLfloat z;
};
typedef struct vec3 vec3;
typedef vec3* vec3Ptr;
struct vec4
{
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat w;
};
typedef struct vec4 vec4;
typedef vec4* vec4Ptr;
#endif
struct vertexData
{
vec3 vertex;
vec3 normal;
};
typedef struct vertexData vertexData;
typedef vertexData* vertexDataPtr;
static const vertexData MeshVertexData[] = {
{/*v:*/{1.000000, -1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, -0.577349} },
{/*v:*/{1.000000, -1.000000, 1.000000}, /*n:*/{0.577349, -0.577349, -0.577349} },
{/*v:*/{-1.000000, -1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, -0.577349} },
{/*v:*/{1.000000, 1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, 0.577349} },
{/*v:*/{-1.000000, 1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, 0.577349} },
{/*v:*/{0.999999, 1.000000, 1.000001}, /*n:*/{0.577349, -0.577349, 0.577349} },
{/*v:*/{1.000000, -1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, -0.577349} },
{/*v:*/{1.000000, 1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, 0.577349} },
{/*v:*/{1.000000, -1.000000, 1.000000}, /*n:*/{0.577349, -0.577349, -0.577349} },
{/*v:*/{1.000000, -1.000000, 1.000000}, /*n:*/{0.577349, -0.577349, -0.577349} },
{/*v:*/{0.999999, 1.000000, 1.000001}, /*n:*/{0.577349, -0.577349, 0.577349} },
{/*v:*/{-1.000000, -1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, -0.577349} },
{/*v:*/{-1.000000, -1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, -0.577349} },
{/*v:*/{-1.000000, 1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, 0.577349} },
{/*v:*/{-1.000000, 1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, 0.577349} },
{/*v:*/{1.000000, 1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, 0.577349} },
{/*v:*/{1.000000, -1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, -0.577349} },
{/*v:*/{-1.000000, 1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, 0.577349} },
{/*v:*/{-1.000000, -1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, -0.577349} },
{/*v:*/{1.000000, -1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, -0.577349} },
{/*v:*/{-1.000000, -1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, -0.577349} },
{/*v:*/{1.000000, 1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, 0.577349} },
{/*v:*/{0.999999, 1.000000, 1.000001}, /*n:*/{0.577349, -0.577349, 0.577349} },
{/*v:*/{1.000000, -1.000000, 1.000000}, /*n:*/{0.577349, -0.577349, -0.577349} },
{/*v:*/{1.000000, -1.000000, -1.000000}, /*n:*/{0.577349, 0.577349, -0.577349} },
{/*v:*/{-1.000000, -1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, -0.577349} },
{/*v:*/{-1.000000, 1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, 0.577349} },
{/*v:*/{-1.000000, 1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, 0.577349} },
{/*v:*/{-1.000000, 1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, 0.577349} },
{/*v:*/{0.999999, 1.000000, 1.000001}, /*n:*/{0.577349, -0.577349, 0.577349} },
{/*v:*/{-1.000000, -1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, -0.577349} },
{/*v:*/{-1.000000, -1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, -0.577349} },
{/*v:*/{-1.000000, 1.000000, -1.000000}, /*n:*/{-0.577349, 0.577349, 0.577349} },
{/*v:*/{0.999999, 1.000000, 1.000001}, /*n:*/{0.577349, -0.577349, 0.577349} },
{/*v:*/{-1.000000, 1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, 0.577349} },
{/*v:*/{-1.000000, -1.000000, 1.000000}, /*n:*/{-0.577349, -0.577349, -0.577349} },
};
// Example OpenGL ES 1.1 Drawing Code:
// glEnableClientState(GL_VERTEX_ARRAY);
// glEnableClientState(GL_NORMAL_ARRAY);
// glVertexPointer(3, GL_FLOAT, sizeof(VertexData3D), &MeshVertexData[0].vertex);
// glNormalPointer(GL_FLOAT, sizeof(VertexData3D), &MeshVertexData[0].normal);
// glDrawArrays(GL_TRIANGLES, 0, kMeshNumberOfVertices);
// glDisableClientState(GL_VERTEX_ARRAY);
// glDisableClientState(GL_NORMAL_ARRAY);
And here is the code I am using:
//
// ES1Renderer.m
// glTestingGround
//
// Created by David Jacobs on 3/8/10.
// Copyright Stanford University 2010. All rights reserved.
//
#import "ES1Renderer.h"
#import "banana.h"
#import "PVRTexture.h"
#import "cubefr.h"
#define BUFFER_OFFSET(x)((char *)NULL+(x))
#implementation ES1Renderer
// Create an ES 1.1 context
- (id) init
{
if (self = [super init])
{
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context])
{
[self release];
return nil;
}
// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
glGenFramebuffersOES(1, &defaultFramebuffer);
glGenRenderbuffersOES(1, &colorRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
glEnable(GL_CULL_FACE);
glEnable(GL_CCW);
// ###################### Texture Demo #################################
/*NSString * path = [[NSBundle mainBundle] pathForResource: #"banana" ofType:#"pvrtc"];
texture = [[PVRTexture alloc] initWithContentsOfFile:path];
glBindTexture(GL_TEXTURE_2D, texture.name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/
// #####################################################################
}
return self;
}
- (void) render
{
// Replace the implementation of this method to do your own custom drawing
// This application only creates a single context which is already set current at this point.
// This call is redundant, but needed if dealing with multiple contexts.
[EAGLContext setCurrentContext:context];
// This application only creates a single default framebuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple framebuffers.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
//obj2opengl.pl banana.obj
//To include the converted object all you have to do is
// include generated arrays
// ---------------------------------------------------------------
// Determines the Normalized Device Coordinate -> Window Coordinate Transform
// ---------------------------------------------------------------
glViewport(0, 0, backingWidth, backingHeight);
// ---------------------------------------------------------------
// Determines the Eye Coordinate -> Clip Coordinate Transform
// ---------------------------------------------------------------
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-2, 2, -3, 3, 5, 20);
// ---------------------------------------------------------------
// Determines the Object Coordinate -> Eye Coordinate Transform
// ---------------------------------------------------------------
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -8);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// ####################### Geometry Demo #########################
/*
#define TETRA_FRONT 0.0, 0.0, 1.0
#define TETRA_RIGHT 0.943, 0.0, -0.333
#define TETRA_TOP -0.471, 0.816, -0.333
#define TETRA_LEFT -0.471, -0.816, -0.333
#define v1 1.000000, -1.000000, -1.000000
#define v2 1.000000, -1.000000, 1.000000
#define v3 -1.000000, -1.000000, 1.000000
#define v4 -1.000000, -1.000000, -1.000000
#define v5 1.000000, 1.000000, -1.000000
#define v6 0.999999, 1.000000, 1.000001
#define v7 -1.000000, 1.000000, 1.000000
#define v8 -1.000000, 1.000000, -1.000000
static const GLfloat tetraVertices[] = {
TETRA_TOP,
TETRA_RIGHT,
TETRA_LEFT,
TETRA_FRONT,
TETRA_TOP,
TETRA_RIGHT,
};
static const GLfloat newVertices[] = {
v1,
v2,
v3,
v4,
v5,
v6,
v7,
v8
};
#define RED 255, 0, 0,255
#define GREEN 0,255, 0,255
#define BLUE 0, 0,255,255
#define YELLOW 255,255, 0,255
static const GLubyte tetraColors[] = {
RED,
GREEN,
BLUE,
YELLOW,
RED,
GREEN,
};
static const GLubyte newColors[] = {
RED,
RED,
RED,
RED,
RED,
RED,
RED,
RED
};
static float t = 0.0;
t += 1/30.f;
//glVertexPointer(3, GL_FLOAT, 0, tetraVertices);
glVertexPointer(3, GL_FLOAT, 0, newVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, newColors);
//glColorPointer(4, GL_UNSIGNED_BYTE,0, tetraColors);
glEnableClientState(GL_COLOR_ARRAY);
glPushMatrix();
glTranslatef(0, 0, -3);
glRotatef(30*t, 0, 1, 0);
glScalef(3, 3, 3);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
glPopMatrix();
glPushMatrix();
glTranslatef(2, 2, 0);
glRotatef(-30*t, 1, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
glPopMatrix();
glPushMatrix();
glTranslatef(2, -2, 0);
glRotatef(-60*t, 1, 0, 1);
glScalef(2, 2, 2);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
glPopMatrix();
*/
//glDisableClientState(GL_VERTEX_ARRAY);
//glDisableClientState(GL_NORMAL_ARRAY);
// set input data to arrays
//glVertexPointer(3, GL_FLOAT, 0, MeshVertexData);
//glEnableClientState(GL_VERTEX_ARRAY);
//glNormalPointer(GL_FLOAT, 0,indexes);
//glEnableClientState(GL_NORMAL_ARRAY);
//glTexCoordPointer(2, GL_FLOAT, 0, bananaTexCoords);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(MeshVertexData), &MeshVertexData[0].vertex);
glNormalPointer(GL_FLOAT, sizeof(MeshVertexData), &MeshVertexData[0].normal);
//glTexCoordPointer(2, GL_FLOAT, sizeof(TexturedVertexData3D), &VertexData[0].texCoord);
static float t = 0.0;
t += 1/30.f;
glPushMatrix();
glTranslatef(0, 0, 0);
glRotatef(30*t, 0, 1, 0);
glScalef(1, 1, 1);
//glDrawArrays(GL_TRIANGLES, 0, vertex_count);
glDrawArrays(GL_TRIANGLES, 0, sizeof(&MeshVertexData[0].vertex));
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glLoadIdentity();
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//glDisable(GL_TEXTURE_2D);
// ###############################################################
// Enable lighting
// This application only creates a single color renderbuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple renderbuffers.
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
- (BOOL) resizeFromLayer:(CAEAGLLayer *)layer
{
// Allocate color buffer backing based on the current layer size
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer];
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
{
NSLog(#"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
return NO;
}
return YES;
}
- (void) dealloc
{
// Tear down GL
if (defaultFramebuffer)
{
glDeleteFramebuffersOES(1, &defaultFramebuffer);
defaultFramebuffer = 0;
}
if (colorRenderbuffer)
{
glDeleteRenderbuffersOES(1, &colorRenderbuffer);
colorRenderbuffer = 0;
}
// Tear down context
if ([EAGLContext currentContext] == context)
[EAGLContext setCurrentContext:nil];
[context release];
context = nil;
[super dealloc];
}
#end
Any help is greatly appreciated. Also any tutorials on OpenGL ES would be great.
Thanks in advance
It's not enough to have the vertex and normal data from Blender, you need the projection and model-view matrices and camera and light positions too. A better approach is to export to a file format that includes that, such as Collada, or POD (if you want to keep it simple). The PowerVR SDK includes tools to export from Blender and even a basic game engine that will parse POD. You also get texture coordinates and animations this way. Check out my first article here:
http://montgomery1.com/opengl/

Resources