Commit 36ffbdbc authored by amarcic's avatar amarcic
Browse files

added highlightedObjects to global state; timeline: implemented highlighting

parent 4932c684
......@@ -38,6 +38,7 @@ const initialInput = {
selectedMarker: undefined,
timelineSort: "period",
highlightedTimelineObject: undefined,
highlightedObjects: ["1080824"],
areaA: 1,
areaB: 0,
bigTileArea: "",
......
......@@ -12,6 +12,7 @@ export const Timeline = (props) => {
const [dimensions, setDimensions] = useState({width: 0, height: 0, margin: {top: 0, right: 0, left: 0, bottom: 0}});
const { timelineObjectsData } = props;
const [input, dispatch] = props.reducer;
const filteredTimelineData = timelineObjectsData&&timelineObjectsData
.filter( datapoint =>
datapoint.periodSpans?.[0]!==undefined||datapoint.periodSpans?.length>1);
......@@ -33,7 +34,11 @@ export const Timeline = (props) => {
</Grid>
<Grid id="timelineContainer" className={classes.dashboardTileContent} item container direction="column" spacing={2}>
<Grid item>
<TimelineChart filteredTimelineData={filteredTimelineData} dimensions={dimensions/*getDimensions("timelineContainer")*/} />
<TimelineChart
reducer={[input, dispatch]}
filteredTimelineData={filteredTimelineData}
dimensions={dimensions}
/>
</Grid>
</Grid>
</>
......
......@@ -9,15 +9,21 @@ export const TimelineChart = (props) => {
const classes = useStyles();
const svgRef = useRef();
console.log("dimensions", props.dimensions)
//todo: make highlighted global state?
let highlighted = {
/*const [highlighted, setHighlighted] = useState({
objects: ["1080824", "1086070"],
periods: [],
locations: []
};
});*/
const [input, dispatch] = props.reducer;
//const [highlightedObjects, setHighlightedObjects] = useState(["1080824"]);
//let highlightedObjects = input.highlightedObjects;
const svgRef = useRef();
console.log("dimensions", props.dimensions)
const xDomain = getTimeRangeOfTimelineData(props.filteredTimelineData,"period");
const dataUnsorted = newGroupByPeriods(props.filteredTimelineData);
const data = dataUnsorted && new Map([...dataUnsorted.entries()]
......@@ -46,7 +52,7 @@ export const TimelineChart = (props) => {
//draw timeline everytime filteredTimelineData changes
useEffect( () => {
drawTimeline(timelineData, props.dimensions)
}, [props.filteredTimelineData, props.dimensions] );
}, [props.filteredTimelineData, props.dimensions, input] );
//todo: check if still depending on outer scope (like height, width, margin)
const drawTimeline = (timelineConfig, dimensions) => {
......@@ -54,6 +60,7 @@ export const TimelineChart = (props) => {
const { data, svgRef, xDomain } = timelineConfig;
const { width, height, margin } = dimensions;
const svg = select(svgRef.current);
//todo: change name to be more describing: limit of bar height for rendering labels in different ways
const labelRenderLimit = 8;
//empty canvas in case no data is found by query
......@@ -92,7 +99,7 @@ export const TimelineChart = (props) => {
.range(["#5AE6BA","#4BC8A3","#3EAA8C","#318D75","#25725F"]);
//function to add labels to the bars (when bandwith is heigh enough for readable labels)
//todo: remove outer dependency on selectionLabels
//todo: remove outer dependency on selectionLabels?
const addLabels = (bandwidth, renderLimit) => {
if (bandwidth > renderLimit) {
selectionLabels
......@@ -179,11 +186,17 @@ export const TimelineChart = (props) => {
.attr("x", value => xScale(value.periodSpan?.[0]))
.attr("y", (value, index) => yScale(periodIds[index]))
.attr("width", value => Math.abs(xScale(value.periodSpan?.[0])-xScale(value.periodSpan?.[1]))||0)
.attr("class", value =>
value.items.map( item =>
item.id ).some( id =>
input.highlightedObjects.indexOf(id) >-1 )
? "bar highlighted"
: "bar")
.attr("fill", value => colorScale(value.items.length))
.attr("stroke", value => highlighted.objects.some( id =>
/*.attr("stroke", value => highlighted.objects.some( id =>
value.items.map( item => item.id).indexOf(id) > -1)
? "black"
: "red")
: "red")*/
//display tooltip when mouse enters bar on chart
selectionEnteringAndUpdating
......@@ -194,9 +207,6 @@ export const TimelineChart = (props) => {
select("#mouse-target")
.attr("id", null)
})
highlighted.objects = value.items.map( item => item.id);
//console.log("high: ", highlighted)
svg
.selectAll(".tooltip")
.data([value])
......@@ -218,6 +228,15 @@ export const TimelineChart = (props) => {
.remove()
} )
selectionEnteringAndUpdating
.on("click", (event, value) => {
//select(event.currentTarget)
dispatch({
type: "UPDATE_INPUT",
payload: {field: "highlightedObjects", value: value.items.map( item => item.id)}
});
})
addLabels(yScale.bandwidth(), labelRenderLimit);
//apply zoom
......@@ -228,145 +247,6 @@ export const TimelineChart = (props) => {
}
/*
useEffect(() => {
svg.attr()
//remove previously rendered timeline bars in the case there is no data from the current search
//if (!timelineObjectsData||!byPeriodData||byPeriodData.size===0) {
if (!data||data.size===0) {
svg.select(".bars")
.selectAll(".bar").remove()
svg.select(".bars")
.selectAll(".label").remove()
svg.select(".background").selectAll("rect").remove();
} else {
const periodIds = [...data.keys()];
//scale for the x axis
const xScale = scaleLinear()
.domain(xDomain)
.range([0,width])
//scale for the y axis
const yScale = scaleBand()
.domain(periodIds)
.range([height,0])
.padding(0.2)
//todo: replace this temporary cleanup of the background svg group
svg.select(".background").selectAll("rect").remove();
//select and position svg element for the timeline
const timelineCanvas = svg.select(".bars")
.attr("transform",`translate(${margin.left}, ${margin.top})`)
//bind svg groups for bars and labels to the values of the data (map grouped by periods)
const barGroups = timelineCanvas
.selectAll(".barGroup")
.data([...data.values()], (data,index) => data.periodName+data.periodSpan[0]);
//todo: evaluate if generating the key form period name + period beginning sufficiently unique?
//console.log("bar groups: ", barGroups);
//instructions for joining the synced data and group elements: add svg g, rect and text elements on enter
//returns a selection of new and updating groups
const newAndUpdatedGroups = barGroups
.join(
enter => {
let group = enter
.append("g");
group.attr("class","barGroup");
group
.append("rect")
.attr("class", "bar")
.attr("height", yScale.bandwidth())
.attr("fill", "#69b3a2");
group
.append("text")
.attr("class","label");
return group;
}
)
//position all new and updating groups
newAndUpdatedGroups
.attr("transform", (value, index) => `translate(${xScale(value.periodSpan?.[0])},${yScale(periodIds[index])})`)
//extend the svg rect elements as chart bars according to the data and return as "bar"
const bar = newAndUpdatedGroups.selectAll(".bar")
.transition()
.attr("width", value => Math.abs(xScale(value.periodSpan?.[0])-xScale(value.periodSpan?.[1]))||0);
//add period names as text labels to the groups and return as "labels"
const labels = newAndUpdatedGroups.selectAll(".label")
.text(value => value.periodName);
console.log("new and updated after labels: ", newAndUpdatedGroups);
//attach click event and sort function to sort button;
select(".sortButton").on("click", () =>
svg.selectAll(".barGroup").sort( (a,b) => ascending(parseInt(a.periodSpan?.[0]),parseInt(b.periodSpan?.[0])) )
.attr("transform", (value, index) => `translate(${xScale(value.periodSpan?.[0])},${yScale(periodIds[index])})`)
);
//on click show detailed view of dated objects for a period
newAndUpdatedGroups.on("click", (event, value) => {
//const element = svg.selectAll(".bar").nodes(),
// index = element.indexOf(event.currentTarget);
const itemDatingMin = min(value.items, item => item.spanDated?.[0]),
itemDatingMax = max(value.items, item => item.spanDated?.[1]);
const xScaleDetail = scaleLinear()
.domain([itemDatingMin,itemDatingMax])
.range([0,width])
const yScaleDetail = scaleLinear()
.domain([0, value.items.length])
.range([height,0]);
//remove bars and labels
svg.selectAll(".bar").remove();
svg.selectAll(".label").remove();
svg.select(".background").selectAll("rect").remove();
//attach x axis
svg.select(".xAxis")
.attr("transform", `translate(0,${height})`)
.call(axisBottom(xScaleDetail))
//join svg rect elements with array of item dating
svg.select(".bars")
.selectAll("rect").data(value.items).join(
enter => enter.append("rect")
).attr("class","bar")
.attr("x", value => xScaleDetail(value.spanDated?.[0]))
.attr("y", (value,index) => yScaleDetail(index))
.attr("height", 5)
.transition()
.attr("width", value => Math.abs(xScaleDetail(value.spanDated?.[0])-xScaleDetail(value.spanDated?.[1]))+1||0)
//paint on the background a rect representing the temporal extension of the period
svg.select(".background")
.selectAll("rect").data([value]).join("rect")
.attr("width", Math.abs(xScaleDetail(value.periodSpan?.[0])-xScaleDetail(value.periodSpan?.[1]))||0)
.attr("height", height)
.attr("x", xScaleDetail(value.periodSpan[0]))
.transition()
.attr("opacity", 0.3)
})
}
}, [data])
*/
return (
<div className="timeline">
<svg ref={svgRef}>
......
......@@ -20,6 +20,9 @@
background-color: #1c9489 !important;
color: #fff !important;
}
.highlighted {
stroke: pink;
}
#mouse-target {
stroke: blue
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment