Public
Edited
Apr 2, 2023
Insert cell
Insert cell
Insert cell
Insert cell
dogZip = FileAttachment("Actor2_REF_only2017.zip").zip()
Insert cell
fileNames = dogZip.filenames
Insert cell
Insert cell
Insert cell
Insert cell
n_articles_data_orig = d3.csvParse(await readMe, function(d) {
return {
MonthYear: parser(d.SQLDATE),
NumArticles: d.NumArticles,
Actor1Code: d.Actor1Code,
Actor1Type1Code: d.Actor1Type1Code,
Actor1CountryCode: d.Actor1CountryCode,
EventRootCode: d.EventRootCode
};
});
Insert cell
Insert cell
Insert cell
n_articles_data_roll = d3.rollup(n_articles_data_orig, v => d3.sum(v, d => d.NumArticles), d => d.MonthYear)
Insert cell
Insert cell

n_articles_data_roll_actor = d3.rollup(n_articles_data_orig, v => v.length, d => d.Actor1Code)
Insert cell
Insert cell
n_articles_data_roll_country = d3.rollup(n_articles_data_orig, v => v.length, d => d.Actor1CountryCode)
Insert cell
Insert cell
n_articles_data_roll_eventrootcode = d3.rollup(n_articles_data_orig, v => v.length, d => d.Actor1CountryCode, g => g.EventRootCode)
Insert cell
Insert cell
n_articles_data_roll_actor1type1code = d3.rollup(n_articles_data_orig, v => v.length, d => d.Actor1Type1Code)
Insert cell
Insert cell
function unroll(rollup, keys, label = "value", p = {}) {
return Array.from(rollup, ([key, value]) =>
value instanceof Map
? unroll(value, keys.slice(1), label, Object.assign({}, { ...p, [keys[0]]: key } ))
: Object.assign({}, { ...p, [keys[0]]: key, [label] : value })
).flat();
}
Insert cell
n_articles_data = unroll(n_articles_data_roll, "MonthYear", "NumArticles")
Insert cell
n_articles_data_actor1 = unroll(n_articles_data_roll_actor, "Actor1Code", "frequency")
Insert cell
n_articles_data_country1 = unroll(n_articles_data_roll_country, "Actor1CountryCode", "frequency")
Insert cell
n_articles_data_eventrootcode = unroll(n_articles_data_roll_eventrootcode, "Actor1CountryCode", "frequency")
Insert cell
n_articles_data_actor1type1code = unroll(n_articles_data_roll_actor, "Actor1Type1Code", "frequency")
Insert cell
Insert cell
n_articles_data_actor1_sorted = n_articles_data_actor1.sort(function (a, b, c) {
var n_articles_data_actor1_sorted = a.A - b.frequency
return n_articles_data_actor1_sorted
});
Insert cell
n_articles_data_country1_sorted = n_articles_data_country1.sort(function (a, b) {
var n_articles_data_country1_sorted = b.frequency - a.frequency
return n_articles_data_country1_sorted
});
Insert cell
n_articles_data_n_articles_data_actor1type1code_sorted = n_articles_data_actor1type1code.sort(function (a, b) {
var n_articles_data_n_articles_data_actor1type1code_sorted = b.frequency - a.frequency
return n_articles_data_n_articles_data_actor1type1code_sorted
});
Insert cell
Insert cell
filter_null = (arry) => {
var results = arry.filter(obj => obj.A != "");
return results
};
Insert cell
filter_null(n_articles_data_actor1_sorted)
Insert cell
n_articles_data_actor1_filtered = filter_null(n_articles_data_country1_sorted)
Insert cell
n_articles_data_eventrootcode_filtered = filter_null(n_articles_data_eventrootcode)
Insert cell
n_articles_data_actor1type1code_filtered = filter_null(n_articles_data_n_articles_data_actor1type1code_sorted)
Insert cell
Insert cell
n_articles_data_actor1_sorted_chart = filter_null(n_articles_data_actor1_sorted).slice(0,20)
Insert cell
n_articles_data_country1_sorted_chart = filter_null(n_articles_data_country1_sorted).slice(0,20)
Insert cell
n_articles_data_actor1type1code_sorted_chart = filter_null(n_articles_data_actor1type1code_filtered).slice(0,20)
Insert cell
Insert cell
Insert cell
Insert cell
countrydata = n_articles_data_actor1_filtered.filter(d => !cameoRC.has(d.A))
Insert cell
Insert cell
countrydata_eventrootcode = n_articles_data_eventrootcode_filtered.filter(d => !cameoRC.has(d.A))
Insert cell
geoCountryData = countrydata.map(d => Object({...d, 'id': cameoCC_id_2[d.A]}))
Insert cell
Insert cell
geoCountryData_eventrootcode = countrydata_eventrootcode.map(d => Object({...d, 'id': cameoCC_id_2[d.A]}))
Insert cell
selected_event_codes
Insert cell
geoCountryData_selected = {
let combined_array = [];
let results = [];
for (let i = 0; i <= selected_event_codes.length; i++) {
results = geoCountryData_eventrootcode.filter(obj => obj.c == selected_event_codes[i])
combined_array = combined_array.concat(results);
return combined_array;
}} //NumArticles_brush
Insert cell
world = await (await fetch('https://vega.github.io/vega-datasets/data/world-110m.json')).json()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
NumArticles_brush = {

// The selection chart
const brush = vl.selectInterval().encodings('x').resolve('intersect');
// The context chart
const context = vl.markLine()
.data(n_articles_data)
.encode(
vl.y().fieldQ("NumArticles").axis({grid: false, titleFont:"serif", titleFontSize:12, labelFontSize:12, labelFont:"serif", titlePadding:20, title:"Frequency"}),
vl.x().fieldT("M").axis({grid: false, titleFont:"serif", titleFontSize:15, labelAngle:-90, labelFontSize:12, labelFont:"serif", titlePadding:20, title:"Time"}))
.width(250)
.height(125)

// Adding the brush layer to the context chart.
const context_layer = vl.layer(
context.select(brush).encode(vl.color().value('#66b2b2')))
// .height(height/5).width(chart_width/2);
return context_layer //, context_layer.data(n_articles_data) //context_layer,
.render()
}
Insert cell
selected = Generators.observe((notify) => {
const selected = (selection, predicates) => {
const within = n_articles_data.filter(d => {
for (const [key, [min, max]] of Object.entries(predicates))
if (isNaN(+d[key]) || d[key] < min || d[key] > max)
return false;
return true;
})
notify(within);
}
selectable.addSignalListener(signal, selected);
return () => selectable.removeEventListener(signal, selected);
})
Insert cell
Insert cell
embed = require("vega-embed@5")
Insert cell
import {toc} from "@harrystevens/toc"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more