function leftVisualization() {
const initialValue=top5_company_dispatches;
const svg2 = d3.create('svg')
.attr('width', width2)
.attr('height', height2)
.property('value', initialValue);
const x2 = d3.scaleLinear()
.domain([0, d3.max(top5_company_dispatches, d => +d['Unique Dispatched Vehicles'])])
.range([margin2.left, width2 - margin2.right]);
const y2 = d3.scaleLinear()
.domain([0, d3.max(top5_company_dispatches, d => +d['Total Dispatched Trips'])])
.range([height2 - margin2.bottom, margin2.top]);
const customColorScheme = [
"#1f77b4", "#ff7f0e", "#2ca02c", "#d62728",
"#9467bd", "#8c564b", "#e377c2", "#7f7f7f",
"#bcbd22", "#17becf", "#aec7e8", "#ffbb78"
];
const colorScale = d3.scaleOrdinal()
.domain(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'])
.range(customColorScheme);
const xAxis2 = d3.axisBottom(x2).tickFormat(d3.format(".0s"));
svg2.append('g')
.attr('transform', `translate(0, ${height2 - margin2.bottom})`)
.call(xAxis2);
svg2.append("text")
.attr("x", width2 / 2)
.attr("y", height2 - 20)
.attr("dy", "0.71em") // Adjust vertical alignment as needed
.attr("text-anchor", "middle")
.style("font-size", "12px")// Align text to the middle
.text("Unique Vehicles Dispatched");
// Create y-axis
const yAxis2 = d3.axisLeft(y2).tickFormat(d3.format(".0s"));
svg2.append('g')
// move y-axis to the left to account for left margin
.attr('transform', `translate(${margin2.left+15})`)
.call(yAxis2)
// add axis label
.append('text')
.attr('fill', 'black')
.attr('text-anchor', 'start')
.attr('dominant-baseline', 'hanging')
.attr('font-weight', 'bold')
.text('Total Dispatched Trips');
// Add legend
const legend = svg2.append('g')
.attr('class', 'legend')
.attr('transform', `translate(${margin2.left}, ${margin2.top})`); // Position the legend at the top-left corner
const colorLegend = legend.selectAll('.color-legend')
.data(customColorScheme)
.enter().append('g')
.attr('class', 'color-legend')
.attr('transform', (d, i) => `translate(50, ${i * 20})`);
// Add color rectangles
colorLegend.append('rect')
.attr('x', 0)
.attr('width', 15)
.attr('height', 15)
.attr('fill', d => d);
const monthNames = [
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
];
// Add legend text
colorLegend.append('text')
.attr('x', 20)
.attr('y', 10)
.text((d, i) => monthNames[i]);
// Create circles for each data point and color code based on the 'Month' attribute
const dots=svg2.selectAll('circle')
.data(top5_company_dispatches)
.enter()
.append('circle')
.attr('cx', d => x2(+d['Unique Dispatched Vehicles']))
.attr('cy', d => y2(+d['Total Dispatched Trips']))
.attr('r', 5)
.attr('fill', d => colorScale(d['Month']))
.attr('opacity', 0.7);
const container = d3.select('#container-id');
//creating brush
const brush = d3.brush()
// set the space that the brush can take up
.extent([[margin2.left, margin2.top], [width2 - margin2.right, height2 - margin2.bottom]])
// handle events
.on('brush', onBrush)
.on('end', onEnd);
svg2.append('g')
.call(brush);
function onBrush(event) {
// event.selection gives us the coordinates of the
// top left and bottom right of the brush box
const [[x_1, y_1], [x_2, y_2]] = event.selection;
console.log(x_1,y_1,x_2,y_2)
// Log filtered data to console
const brushedData = top5_company_dispatches.filter(d => {
const cx = x2(d['Unique Dispatched Vehicles']);
const cy = y2(d['Total Dispatched Trips']);
console.log('ppp',cx,cy)
return cx >= x_1 && cx <= x_2 && cy >= y_1 && cy <= y_2;
});
dots.attr('fill', d => brushedData.includes(d) ? colorScale(d['Month Name']) : 'gray');
console.log('Filtered Data:',brushedData );
svg2.property('value', brushedData).dispatch('input');
}
function onEnd(event) {
// if the brush is cleared
if (event.selection === null) {
// reset the color of all of the dots
dots.attr('fill', d => colorScale(d['Month Name']));
svg2.property('value', initialValue).dispatch('input');
}
}
return svg2.node();
}