Access "BC" Dates from csv file with d3

Hi,

I want to build a scatter plot, displaying all recorded wars in history with d3 version 5. Which is a table from wikipedia, converted into a csv file.
The xAxis displays the dates of the beginning. The yAxis the victims. The radius of the plot the duration of the war.
This is an example:

War,Deathrange,Geometricmean(Note 1),Date,Combatants,Location,Notes
Conquests of Cyrus the Great,"100,000+","100,000",549 BC–530 BC,Persian Empire vs. various states,Middle East,"Number given is the sum of all deaths in battle recorded by writers during this time period, does not take into account civilian deaths, the actual number may be much greater."
Greco–Persian Wars,"73,800+","73,800",499 BC–449 BC,Greece vs. Persian Empire,Greece,

I don’t understad, how to get the first date. In this case 549 BC

I tried the following:

let startDate = [];

dataset.forEach(function(d) {
if (d.Date.includes(" “)) {
let end = d.Date.indexOf(” “);
startDate.push(d.Date.substring(0, end))
} else if (d.Date.includes(”–")) {
let end = d.Date.indexOf("–");
startDate.push(d.Date.substring(0, end));
}
console.log(startDate)
})

And when I console log “startDate” I get all my dates. (But flawed. I need to subsitute the “BC” by a minus sign.)

Is there a more optimized solution? I don’t understand how to parse the strings into a date object directly.

The dates are ranging from “BC” to our times. Like

Peasants vs. Eastern Han China,China,– Part of Three Kingdoms War
Wars of the Sixteen Kingdoms,"150,000+[citation needed]","150,000",304–439,Northern Chinese States,Northern China,"Number given is the sum of all deaths in battle recorded in this time period in battles between armies of the Sixteen Kingdoms, does not take into account civilian deaths, the actual number may be much greater."
Hunnic Invasions,"165,000+[citation needed]","165,000",395–453,Roman Empire vs. Hunnic

Thank you for time and help!

In data visualization it is often necessary to clean and normalize our data. Your dataset is small so you could import it into a spreadsheet first and change it so that you can import your data into d3 with having to catch exceptions inside your visualization code. Everyone uses different tools, but we familiarize ourselves with and reformat data manually or programmatically before using d3. Outside of FCC anyways.

1 Like