random colors when selected

average
Matt Huntington 2 years ago
parent 5e3d4c5720
commit a32f6a438a

@ -1,11 +1,3 @@
circle {
r:5;
fill: black;
}
circle.highlighted{
fill:red;
r:10;
}
#container { #container {
overflow: visible; overflow: visible;
margin-bottom: 50px; margin-bottom: 50px;

@ -1,4 +1,3 @@
// TODO - different colors for each highted dot, matched in table row backgrounds
// TODO - filter by course // TODO - filter by course
// TODO - change y axis to dropped // TODO - change y axis to dropped
const WIDTH = 800; const WIDTH = 800;
@ -9,6 +8,13 @@ let instances;
let xScale, yScale; let xScale, yScale;
let highlighted = [] let highlighted = []
const randomColor = ()=>{
const red = Math.floor(Math.random()*128) + 64;
const green = Math.floor(Math.random()*128) + 64;
const blue = Math.floor(Math.random()*128) + 64;
return `rgb(${red}, ${green}, ${blue})`
}
const renderTable = () => { const renderTable = () => {
let trs = d3.select('tbody') let trs = d3.select('tbody')
.selectAll('tr') .selectAll('tr')
@ -16,7 +22,10 @@ const renderTable = () => {
trs.exit().remove() trs.exit().remove()
trs = trs.enter().append('tr') trs = trs
.enter()
.append('tr')
.style('background-color', datum => datum.color)
trs.selectAll('td') trs.selectAll('td')
.data(d => [ .data(d => [
d.instance_id, d.instance_id,
@ -36,6 +45,7 @@ const renderTable = () => {
.text('Deselect') .text('Deselect')
.on('click', (event, datum) =>{ .on('click', (event, datum) =>{
event.preventDefault(); event.preventDefault();
delete datum.color
highlighted = highlighted.filter(i => i.instance_id != datum.instance_id) highlighted = highlighted.filter(i => i.instance_id != datum.instance_id)
renderPoints() renderPoints()
renderTable() renderTable()
@ -57,13 +67,16 @@ const renderPoints = () => {
.attr('cx', (datum, index) => xScale(parseTime(datum.graduation_date)) ); .attr('cx', (datum, index) => xScale(parseTime(datum.graduation_date)) );
d3.selectAll('circle') d3.selectAll('circle')
.classed('highlighted', (datum, index)=> highlighted.find(i => i.instance_id === datum.instance_id) !== undefined) .attr('fill', datum => datum.color? datum.color : 'black')
.attr('r', datum => datum.color? 10 : 5)
d3.selectAll('circle').on('click', (event, datum) => { d3.selectAll('circle').on('click', (event, datum) => {
const found = highlighted.find(i => i.instance_id === datum.instance_id) const found = highlighted.find(i => i.instance_id === datum.instance_id)
if(found === undefined){ if(found === undefined){
datum.color = randomColor()
highlighted.push(datum) highlighted.push(datum)
} else { } else {
delete datum.color
highlighted = highlighted.filter(i => i.instance_id != datum.instance_id) highlighted = highlighted.filter(i => i.instance_id != datum.instance_id)
} }
renderPoints() renderPoints()
@ -114,6 +127,7 @@ const createFormSubmissionHandler = () => {
const instanceID = parseInt(d3.select('input[type="text"]').property('value')) const instanceID = parseInt(d3.select('input[type="text"]').property('value'))
const found = instances.find(i => i.instance_id === instanceID) const found = instances.find(i => i.instance_id === instanceID)
if(found !== undefined && highlighted.find(instance => instance.instance_id === instanceID) === undefined){ if(found !== undefined && highlighted.find(instance => instance.instance_id === instanceID) === undefined){
found.color = randomColor()
highlighted.push(found); highlighted.push(found);
} }
renderTable(); renderTable();

Loading…
Cancel
Save