viewof cPlotDensity = {
const selectType = vl
.selectPoint("selected_pt")
.on("click")
.fields("Offense")
.toggle(true);
const selectBrush = vl.
selectInterval()
.resolve("intersect");
const sPlotFaceted = vl
.markCircle({ stroke: "black", strokeWidth: 1 })
.width(400)
.params(selectBrush,selectType)
.title({
text: "NonViolent Crime Totals by Neighbourhoods Summer 2016",
subtitle: headerString
})
.data(crimeDensityTotal)
.transform(
vl.filter({ field: "NEIGHBOURHOOD", oneOf: mapSelections }),
vl.joinaggregate({ op: "count", field: "datum.Offense", as: "Ctotals" })
)
.encode(
vl.facet({ field: "Offense Type", type: "ordinal" }),
vl
.size()
.fieldQ("population")
.legend({ orient: "right" })
.scale({ domain: [0, 50000] }),
vl.tooltip([
{ field: "Offense", type: "nominal" },
{
field: "Offense",
type: "quantitative",
aggregate: "count",
title: "Total"
},
{ field: "NEIGHBOURHOOD", type: "nominal" },
{
field: "population",
type: "quantitative",
title: "Population"
},
{
field: "density",
type: "quantitative",
title: "Density",
format: ".3f"
},
{
title: "Overall Crime rate (crimes/person)",
type: "quantitative",
field: "Ctotals",
format: ".2f"
}
]),
vl.opacity()
.value(0.1)
.if(selectType, vl.value(.9)),
vl
.y()
.fieldQ("sqm")
.title("Square M per person")
.scale({ domain: [0, 500] }),
vl
.x()
.fieldQ("Offense")
.aggregate("count")
.scale({ type: "log" })
.title("Total Crimes"),
vl
.color()
.scale({ range: OffenseCatColors })
.legend({ orient: "right" })
.if( selectBrush, vl.fieldN("Offense Category")).value("lightgrey")
);
const crimeTotals = vl
.markBar()
.width(250)
.data(crimeDensityTotal)
.params(selectType)
.title("Crime Total Offenses Summer 2016")
.transform(
vl.filter({ field: "NEIGHBOURHOOD", oneOf: mapSelections }),
//vl.filter(selectType),
vl.filter(selectBrush)
)
.encode(
vl.y().fieldN("Offense").scale({ domain: OffenseTypes }),
vl.x().fieldQ("*").aggregate("count"),
//.scale({ domain: [0, 4400] })
vl.color().fieldN("Offense Category").scale({ range: OffenseCatColors }),
vl.opacity().if(selectType, vl.value(0.99)).value(0.1), // if point selected, use opacity 0.7, otherwise 0.1
);
const crimeRatios = vl
.markBar()
.width(250)
.data(crimeDensityTotal)
// .params(selectType)
.title("Crime Totals by Category and Neighbourhood")
.transform(
vl.filter({ field: "NEIGHBOURHOOD", oneOf: mapSelections }),
vl.filter(selectType),
vl.filter(selectBrush)//
//vl.calculate(vl.count({ field: "*", aggregate: "count" })).as("cpRatio")
)
.encode(
vl.y().fieldN("NEIGHBOURHOOD"),
vl.x().fieldQ("Offense").aggregate("count"),
//.scale({ domain: [0, 4400] })
vl.color().fieldN("Offense Category").scale({ range: OffenseCatColors })
// .if(selectedBar, vl.fieldN("Offense Category"))
// .value("grey"), // if point selected, use default color, otherwise grey
//vl.opacity().if(selectType, vl.value(0.99)).value(0.4) // if point selected, use opacity 0.7, otherwise 0.1
);
return vl
.vconcat(sPlotFaceted, vl.hconcat(crimeTotals, crimeRatios))
.render();
}