Published
Edited
Nov 4, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`checking how many companies | [ref](https://stackoverflow.com/questions/15125920/how-to-get-distinct-values-from-an-array-of-objects-in-javascript)`
Insert cell
companyList = stockValues.map(item => item.symbol)
.filter((value, index, self) => self.indexOf(value) === index)
Insert cell
processedData = stockValues.map( d => {return {
symbol: d.symbol,
date: new Date(d.date),
price: d.price}})
Insert cell
processedData.filter( d => d.price != undefined)
Insert cell
dataGroup = d3.groups(processedData, d => d.symbol)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
xmulti = d3.scaleTime()
.domain(d3.extent(processedData, d => d.date)) // d3.extend returns [min, max]
.range([margin.left, width - margin.right])
Insert cell
ymulti = d3.scaleLinear()
.domain([0, d3.max(processedData, d => d.price)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
d3.extent(processedData, d => d.date)
Insert cell
ymulti(dataGroup[0][1][0]['price'])
Insert cell
xmulti(dataGroup[0][1][0]['date'])
Insert cell
Insert cell
lineMultiValues = d3.line()
.defined(d => !isNaN(d.date) && !isNaN(d.price))
.x(d => xmulti(d.date))
.y(d => ymulti(d.price))
Insert cell
lineMultiValues(dataGroup[4][1])
Insert cell
dataGroup[0][1]
Insert cell
Insert cell
xmultiAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xmulti).ticks(width / 80).tickSizeOuter(0))
Insert cell
ymultiAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(ymulti))
.call(g => g.select(".domain").remove())
// .call(g => g.select(".tick:last-of-type text").clone()
// .attr("x", 3)
// .attr("text-anchor", "start")
// .attr("font-weight", "bold")
// .text("Total number of cases"))
Insert cell
xmultiAxis2 = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xmulti2).ticks(width / 80).tickSizeOuter(0))
Insert cell
dataGroup
Insert cell
md`### color`
Insert cell
colorScale = d3.scaleOrdinal().range(['blue', 'limegreen', 'black', 'orange', 'red', 'yellow'])
Insert cell
colorScale3 = d3.scaleOrdinal(dataGroup.map(d=> d[0]), d3.schemeCategory10)
Insert cell
checkCompany(parameters)
Insert cell
companyList
Insert cell
Insert cell
legendOrigin = [85, 35];
Insert cell
labelHeight = 12;
Insert cell
Insert cell
d3.min(processedData, d => d.date)
Insert cell
d3.max(processedData, d => d.date)
Insert cell
d3.max(processedData, d => d.date).getFullYear()
Insert cell
d3.max(processedData, d => d.date).getFullYear()
Insert cell
processedData
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function checkCompany(p) {
let companies = {
"MSFT" : p.MSFT,
"AMZN": p.AMZN,
"IBM": p.IBM,
"GOOG": p.GOOG,
"AAPL": p.AAPL}
const filteredByValue = Object.fromEntries(Object.entries(companies).filter(([key, value]) => value === true))
// companies.foreach( d => d
return Object.keys(filteredByValue)
}
Insert cell
checkCompany(parameters)
Insert cell
processedData2 = processedData.filter(d => d.date >= new Date(parameters.start) &&
d.date <= new Date(parameters.end))
.filter(d => checkCompany(parameters).includes(d.symbol))
Insert cell
Insert cell
dataGroup2 = d3.groups(processedData2, d => d.symbol)
Insert cell
dataGroup2.filter( d => d[0].includes(checkCompany(parameters)))

Insert cell
Insert cell
xmulti2 = d3.scaleTime()
.domain(d3.extent(processedData2, d => d.date)) // d3.extend returns [min, max]
.range([margin.left, width - margin.right])
Insert cell
ymulti2 = d3.scaleLinear()
.domain([0, d3.max(processedData2, d => d.price)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
ymultiAxis2 = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(ymulti2))
.call(g => g.select(".domain").remove())
Insert cell
lineMultiValues2 = d3.line()
.defined(d => !isNaN(d.date) && !isNaN(d.price))
.x(d => xmulti2(d.date))
.y(d => ymulti2(d.price))
Insert cell
companyList
Insert cell
colorScale2 = d3.scaleOrdinal()
.domain(companyList)
.range(["blue", "orange", "black", "yellow", "red"]);
Insert cell
Insert cell
multilinechart2 = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style('border', '1px dotted #999');

let legend = svg.selectAll(".legend")
.data(dataGroup2)
.enter();
legend
.append('text')
.text(d => d[0]) // symbol
.attr('x', legendOrigin[0] + labelHeight * 1.2)
.attr('y', (d,i) => legendOrigin[1] + labelHeight * i * 1.2 )
.style('font-family', 'sans-serif')
.style('font-size', `${labelHeight-1}px`);
legend
.append('rect')
.attr('x', legendOrigin[0])
.attr('y', (d,i) => legendOrigin[1] + labelHeight * 1.2 * i - 7)
.attr('width', labelHeight)
.attr('height', labelHeight/2)
.attr('fill', d => colorScale2(d[0]))
.attr('stroke', 'none')
.style('stroke-width', '1.5px');

svg.append("g")
.call(xmultiAxis2);

svg.append("g")
.call(ymultiAxis2);

svg.selectAll('.line')
.data(dataGroup2)
.join('path')
.attr("class", "line")
.attr("d", d => lineMultiValues2(d[1]))
.attr("stroke", d => colorScale2(d))
.style("fill", "none")
.style("stroke-width", 3);
return svg.node();
}
Insert cell
Insert cell
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