Created
September 4, 2013 22:28
-
-
Save timelyportfolio/6443665 to your computer and use it in GitHub Desktop.
rCharts Dimple Facet Example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype HTML> | |
<meta charset = 'utf-8'> | |
<html> | |
<head> | |
<script src='http://d3js.org/d3.v3.min.js' type='text/javascript'></script> | |
<script src='http://dimplejs.org/dist/dimple.v1.min.js' type='text/javascript'></script> | |
<style> | |
.rChart { | |
display: block; | |
margin-left: auto; | |
margin-right: auto; | |
width: 800px; | |
height: 400px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id='chart23bc3e645036' class='rChart dimple'></div> | |
<script type="text/javascript"> | |
(function(){ | |
var opts = { | |
"dom": "chart23bc3e645036", | |
"width": 800, | |
"height": 400, | |
"x": "mpg", | |
"y": "wt", | |
"groups": "model", | |
"type": "bubble", | |
"facet": { | |
"x": "vs", | |
"y": "am" | |
}, | |
"id": "chart23bc3e645036" | |
}, | |
data = [{"model":"Mazda RX4","mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.62,"qsec":16.46,"vs":0,"am":1,"gear":4,"carb":4,"dummy":1},{"model":"Mazda RX4 Wag","mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.875,"qsec":17.02,"vs":0,"am":1,"gear":4,"carb":4,"dummy":1},{"model":"Datsun 710","mpg":22.8,"cyl":4,"disp":108,"hp":93,"drat":3.85,"wt":2.32,"qsec":18.61,"vs":1,"am":1,"gear":4,"carb":1,"dummy":1},{"model":"Hornet 4 Drive","mpg":21.4,"cyl":6,"disp":258,"hp":110,"drat":3.08,"wt":3.215,"qsec":19.44,"vs":1,"am":0,"gear":3,"carb":1,"dummy":1},{"model":"Hornet Sportabout","mpg":18.7,"cyl":8,"disp":360,"hp":175,"drat":3.15,"wt":3.44,"qsec":17.02,"vs":0,"am":0,"gear":3,"carb":2,"dummy":1},{"model":"Valiant","mpg":18.1,"cyl":6,"disp":225,"hp":105,"drat":2.76,"wt":3.46,"qsec":20.22,"vs":1,"am":0,"gear":3,"carb":1,"dummy":1},{"model":"Duster 360","mpg":14.3,"cyl":8,"disp":360,"hp":245,"drat":3.21,"wt":3.57,"qsec":15.84,"vs":0,"am":0,"gear":3,"carb":4,"dummy":1},{"model":"Merc 240D","mpg":24.4,"cyl":4,"disp":146.7,"hp":62,"drat":3.69,"wt":3.19,"qsec":20,"vs":1,"am":0,"gear":4,"carb":2,"dummy":1},{"model":"Merc 230","mpg":22.8,"cyl":4,"disp":140.8,"hp":95,"drat":3.92,"wt":3.15,"qsec":22.9,"vs":1,"am":0,"gear":4,"carb":2,"dummy":1},{"model":"Merc 280","mpg":19.2,"cyl":6,"disp":167.6,"hp":123,"drat":3.92,"wt":3.44,"qsec":18.3,"vs":1,"am":0,"gear":4,"carb":4,"dummy":1},{"model":"Merc 280C","mpg":17.8,"cyl":6,"disp":167.6,"hp":123,"drat":3.92,"wt":3.44,"qsec":18.9,"vs":1,"am":0,"gear":4,"carb":4,"dummy":1},{"model":"Merc 450SE","mpg":16.4,"cyl":8,"disp":275.8,"hp":180,"drat":3.07,"wt":4.07,"qsec":17.4,"vs":0,"am":0,"gear":3,"carb":3,"dummy":1},{"model":"Merc 450SL","mpg":17.3,"cyl":8,"disp":275.8,"hp":180,"drat":3.07,"wt":3.73,"qsec":17.6,"vs":0,"am":0,"gear":3,"carb":3,"dummy":1},{"model":"Merc 450SLC","mpg":15.2,"cyl":8,"disp":275.8,"hp":180,"drat":3.07,"wt":3.78,"qsec":18,"vs":0,"am":0,"gear":3,"carb":3,"dummy":1},{"model":"Cadillac Fleetwood","mpg":10.4,"cyl":8,"disp":472,"hp":205,"drat":2.93,"wt":5.25,"qsec":17.98,"vs":0,"am":0,"gear":3,"carb":4,"dummy":1},{"model":"Lincoln Continental","mpg":10.4,"cyl":8,"disp":460,"hp":215,"drat":3,"wt":5.424,"qsec":17.82,"vs":0,"am":0,"gear":3,"carb":4,"dummy":1},{"model":"Chrysler Imperial","mpg":14.7,"cyl":8,"disp":440,"hp":230,"drat":3.23,"wt":5.345,"qsec":17.42,"vs":0,"am":0,"gear":3,"carb":4,"dummy":1},{"model":"Fiat 128","mpg":32.4,"cyl":4,"disp":78.7,"hp":66,"drat":4.08,"wt":2.2,"qsec":19.47,"vs":1,"am":1,"gear":4,"carb":1,"dummy":1},{"model":"Honda Civic","mpg":30.4,"cyl":4,"disp":75.7,"hp":52,"drat":4.93,"wt":1.615,"qsec":18.52,"vs":1,"am":1,"gear":4,"carb":2,"dummy":1},{"model":"Toyota Corolla","mpg":33.9,"cyl":4,"disp":71.1,"hp":65,"drat":4.22,"wt":1.835,"qsec":19.9,"vs":1,"am":1,"gear":4,"carb":1,"dummy":1},{"model":"Toyota Corona","mpg":21.5,"cyl":4,"disp":120.1,"hp":97,"drat":3.7,"wt":2.465,"qsec":20.01,"vs":1,"am":0,"gear":3,"carb":1,"dummy":1},{"model":"Dodge Challenger","mpg":15.5,"cyl":8,"disp":318,"hp":150,"drat":2.76,"wt":3.52,"qsec":16.87,"vs":0,"am":0,"gear":3,"carb":2,"dummy":1},{"model":"AMC Javelin","mpg":15.2,"cyl":8,"disp":304,"hp":150,"drat":3.15,"wt":3.435,"qsec":17.3,"vs":0,"am":0,"gear":3,"carb":2,"dummy":1},{"model":"Camaro Z28","mpg":13.3,"cyl":8,"disp":350,"hp":245,"drat":3.73,"wt":3.84,"qsec":15.41,"vs":0,"am":0,"gear":3,"carb":4,"dummy":1},{"model":"Pontiac Firebird","mpg":19.2,"cyl":8,"disp":400,"hp":175,"drat":3.08,"wt":3.845,"qsec":17.05,"vs":0,"am":0,"gear":3,"carb":2,"dummy":1},{"model":"Fiat X1-9","mpg":27.3,"cyl":4,"disp":79,"hp":66,"drat":4.08,"wt":1.935,"qsec":18.9,"vs":1,"am":1,"gear":4,"carb":1,"dummy":1},{"model":"Porsche 914-2","mpg":26,"cyl":4,"disp":120.3,"hp":91,"drat":4.43,"wt":2.14,"qsec":16.7,"vs":0,"am":1,"gear":5,"carb":2,"dummy":1},{"model":"Lotus Europa","mpg":30.4,"cyl":4,"disp":95.1,"hp":113,"drat":3.77,"wt":1.513,"qsec":16.9,"vs":1,"am":1,"gear":5,"carb":2,"dummy":1},{"model":"Ford Pantera L","mpg":15.8,"cyl":8,"disp":351,"hp":264,"drat":4.22,"wt":3.17,"qsec":14.5,"vs":0,"am":1,"gear":5,"carb":4,"dummy":1},{"model":"Ferrari Dino","mpg":19.7,"cyl":6,"disp":145,"hp":175,"drat":3.62,"wt":2.77,"qsec":15.5,"vs":0,"am":1,"gear":5,"carb":6,"dummy":1},{"model":"Maserati Bora","mpg":15,"cyl":8,"disp":301,"hp":335,"drat":3.54,"wt":3.57,"qsec":14.6,"vs":0,"am":1,"gear":5,"carb":8,"dummy":1},{"model":"Volvo 142E","mpg":21.4,"cyl":4,"disp":121,"hp":109,"drat":4.11,"wt":2.78,"qsec":18.6,"vs":1,"am":1,"gear":4,"carb":2,"dummy":1}], | |
xAxis = { | |
"type": "addMeasureAxis", | |
"showPercent": false, | |
"overrideMax": 34 | |
}, | |
yAxis = { | |
"type": "addMeasureAxis", | |
"showPercent": false, | |
"overrideMax": 6 | |
}, | |
zAxis = [], | |
colorAxis = [], | |
legend = []; | |
var svg = dimple.newSvg("#" + opts.id, opts.width, opts.height); | |
//data = dimple.filterData(data, "Owner", ["Aperture", "Black Mesa"]) | |
var myChart = new dimple.chart(svg, data); | |
if (opts.bounds) { | |
myChart.setBounds(opts.bounds.x, opts.bounds.y, opts.bounds.width, opts.bounds.height);//myChart.setBounds(80, 30, 480, 330); | |
} | |
//if facet not provided for x or y make Dummy variable | |
opts.facet.x = opts.facet.x ? opts.facet.x : "Dummy" | |
opts.facet.y = opts.facet.y ? opts.facet.y : "Dummy" | |
if(opts.facet.x === "Dummy" || opts.facet.y === "Dummy") { | |
data.forEach(function(d){ | |
d.Dummy = 1; | |
}) | |
} | |
var x = myChart.addCategoryAxis("x", opts.facet.x); | |
var y = myChart.addCategoryAxis("y", opts.facet.y); | |
// Add the bar series to create the matrix shapes | |
var s = myChart.addSeries("Hide", dimple.plot.bar); | |
// Hide this series | |
myChart.assignColor("Hide", "#fff", "#fff", 0); | |
// The bar gap here will define the gaps between the charts | |
s.barGap = 0.2; | |
// Remove the click event from the master chart | |
s.addEventHandler("mouseover", function (e) {}); | |
// Draw the main chart | |
myChart.draw(); | |
// Remove the axis shapes from the main chart | |
x.shapes.selectAll("path,line").remove(); | |
x.titleShape.remove(); | |
y.shapes.selectAll("path,line").remove(); | |
y.titleShape.remove(); | |
if(opts.facet.x === "Dummy") x.shapes.selectAll("text").remove(); | |
if(opts.facet.y === "Dummy") y.shapes.selectAll("text").remove(); | |
// Iterate the shapes from the parent chart | |
s.shapes.each(function (d) { | |
// Filter the data set for the quarter and the price tier | |
// of the current shape | |
var filteredData = dimple.filterData(data, opts.facet.x, String(d.xField[0])); | |
filteredData = dimple.filterData(filteredData, opts.facet.y, String(d.yField[0])); | |
// Draw a new chart which will go in the current shape | |
var subChart = new dimple.chart(svg, filteredData); | |
// Get the shape from the main chart on which this chart is based | |
var shape = d3.select(this); | |
// Position the chart inside the shape | |
subChart.setBounds( | |
parseFloat(shape.attr("x")), | |
parseFloat(shape.attr("y")) - parseFloat(shape.attr("height")), | |
parseFloat(shape.attr("width")), | |
parseFloat(shape.attr("height"))); | |
//dimple allows use of custom CSS with noFormats | |
if(opts.noFormats) { myChart.noFormats = opts.noFormats; }; | |
//for markimekko and addAxis also have third parameter measure | |
//so need to evaluate if measure provided | |
//x axis | |
var subx; | |
if(xAxis.measure) { | |
subx = subChart[xAxis.type]("x",opts.x,xAxis.measure); | |
} else { | |
subx = subChart[xAxis.type]("x", opts.x); | |
}; | |
if(!(xAxis.type === "addPctAxis")) subx.showPercent = xAxis.showPercent; | |
if (xAxis.orderRule) subx.addOrderRule(xAxis.orderRule); | |
if (xAxis.grouporderRule) subx.addGroupOrderRule(xAxis.grouporderRule); | |
if (xAxis.overrideMin) subx.overrideMin = xAxis.overrideMin; | |
if (xAxis.overrideMax) subx.overrideMax = xAxis.overrideMax; | |
if (xAxis.overrideMax) subx.overrideMax = xAxis.overrideMax; | |
if (xAxis.inputFormat) subx.dateParseFormat = xAxis.inputFormat; | |
if (xAxis.outputFormat) subx.tickFormat = xAxis.outputFormat; | |
//y axis | |
var suby; | |
if(yAxis.measure) { | |
suby = subChart[yAxis.type]("y",opts.y,yAxis.measure); | |
} else { | |
suby = subChart[yAxis.type]("y", opts.y); | |
}; | |
if(!(yAxis.type === "addPctAxis")) suby.showPercent = yAxis.showPercent; | |
if (yAxis.orderRule) suby.addOrderRule(yAxis.orderRule); | |
if (yAxis.grouporderRule) suby.addGroupOrderRule(yAxis.grouporderRule); | |
if (yAxis.overrideMin) suby.overrideMin = yAxis.overrideMin; | |
if (yAxis.overrideMax) suby.overrideMax = yAxis.overrideMax; | |
if (yAxis.inputFormat) suby.dateParseFormat = yAxis.inputFormat; | |
if (yAxis.outputFormat) suby.tickFormat = yAxis.outputFormat; | |
//z for bubbles | |
var subz; | |
if (!(typeof(zAxis) === 'undefined') && zAxis.type){ | |
if(zAxis.measure) { | |
subz = subChart[zAxis.type]("z",opts.z,zAxis.measure); | |
} else { | |
sub = subChart[zAxis.type]("z", opts.z); | |
}; | |
if(!(zAxis.type === "addPctAxis")) subz.showPercent = zAxis.showPercent; | |
if (zAxis.orderRule) subz.addOrderRule(zAxis.orderRule); | |
if (zAxis.overrideMin) subz.overrideMin = zAxis.overrideMin; | |
if (zAxis.overrideMax) subz.overrideMax = zAxis.overrideMax; | |
} | |
if(d3.keys(colorAxis).length > 0) { | |
subChart[colorAxis.type](colorAxis.colorSeries,colorAxis.palette) ; | |
} | |
//here need think I need to evaluate group and if missing do null | |
//as the first argument | |
//if provided need to use groups from opts | |
var seriesVariables = []; | |
if(!(opts.facet.x==="Dummy")) seriesVariables.push(opts.facet.x) | |
if(!(opts.facet.y==="Dummy")) seriesVariables.push(opts.facet.y) | |
if(opts.hasOwnProperty("groups")) { | |
seriesVariables.push(opts.groups) | |
var s = subChart.addSeries( seriesVariables, dimple.plot[opts.type] ); | |
//series offers an aggregate method that we will also need to check if available | |
//options available are avg, count, max, min, sum | |
if (!(typeof(opts.aggregate) === 'undefined')) { | |
s.aggregate = eval(opts.aggregate); | |
} | |
if (!(typeof(opts.lineWeight) === 'undefined')) { | |
s.lineWeight = eval(opts.lineWeight); | |
} | |
if (!(typeof(opts.barGap) === 'undefined')) { | |
s.barGap = eval(opts.barGap); | |
} | |
} else var s = myChart.addSeries( seriesVariables, dimple.plot[opts.type] ); | |
//unsure if this is best but if legend is provided (not empty) then evaluate | |
if(d3.keys(legend).length > 0) { | |
var l =myChart.addLegend(); | |
d3.keys(legend).forEach(function(d){ | |
l[d] = legend[d]; | |
}); | |
} | |
//quick way to get this going but need to make this cleaner | |
if(opts.storyboard) { | |
subChart.setStoryboard(opts.storyboard); | |
}; | |
subChart.draw(); | |
}) | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment