shaiRos
User since Nov 28, 2024
3 Posts
Recent Posts
const rectSize = 15; // Size of the color boxconst spacing = 5; // Space between legend itemslet legend = canvas.append('g').attr('class','chart-legend').attr('transform', `translate(${0}, ${height + 3})`);// Sample data for the legendconst legendData = [{ label: 'low', color: '#1f77b4' },{ label: 'medium', color: '#ff7f0e' },{ label: 'high', color: '#2ca02c' },{ label: 'very high', color: '#2ca02c' }];legend.selectAll('g').data(legendData).join('g').attr('class', 'legend-item').each(function (d, i) {const legendItem = d3.select(this);console.log(d.label)console.log(d.label?.length)let labelWidth = (d.label.length * 5) + 7 + 15var bbox = legendItem.node().getBBox()var width = bbox.widthconsole.log(width)// Add rectangle for colorlegendItem.append('rect').attr('width', rectSize).attr('height', rectSize).attr('fill', d.color);// Add labellegendItem.append('text').attr('x', rectSize + spacing) // Position text to the right of the rectangle.attr('y', rectSize / 2) // Align text vertically with the rectangle.attr('dy', '.35em') // Adjust vertical alignment.attr('text-anchor', 'start') // Align text to the start.text(d.label).attr('fill', 'black'); // Optional: Text color// legendItem.attr('transform', `translate(${i * (rectSize + spacing + labelWidth)}, 0)`) // Position each item horizontally});const legendItems_spacing = 12var legendLength = 0legend.selectAll('.legend-item').each(function(d,i) {d3.select(this).attr('transform',function() {var bbox = d3.select(this).node().getBBox()var width = bbox.width// if (width == 0) width = (d.text.length * 5) +15 // not in dom yetlet startingPosition = 0if (i > 0) {startingPosition = legendLength}legendLength += width + legendItems_spacing// return `translate(${xx - (width + 30)},0$)`return `translate(${startingPosition},0)`})})
import React, { useEffect, useRef, useState } from 'react';import * as d3 from 'd3';export default function useD3(renderChartFn : (svg : d3.Selection<SVGElement,{},HTMLElement>) => any, dependencies : any[]) {const ref = useRef<SVGSVGElement>();useEffect(() => {let d3El = d3.select(ref.current)renderChartFn(d3.select(ref.current));return () => {};}, dependencies);return ref;}function MyChart(props : {w: number,h: number,data: Array<any>}) {const [currentData,setCurrentData] = useState(props.data)let svgRef = useD3((svg) => {if (props.data === currentData) returnsetCurrentData(props.data)// add d3 codesvg.selectAll("*").remove();const margin = { top: 0, right: 80, bottom: 20, left: 10 };const width = props.w - margin.left - margin.right;const height = props.h - margin.top - margin.bottom;const canvas = svg.attr('width', width + margin.left + margin.right).attr('height', height + margin.top + margin.bottom).append('g').attr('transform', `translate(${margin.left},${margin.top})`);// dataconst data = props.data},[props])return (<svg ref={svgRef} />)}
button::after {content: "";position: absolute;bottom: 0; /* Position underline at the bottom */left: 50%; /* Start the underline in the middle */transform: translateX(-50%); /* Center it horizontally */width: 0; /* Adjust width relative to the button's content */height: 4px;background-color: #007bff; /* Blue underline */border-radius: 2px; /* Optional: Rounded corners for the rectangle */}button.selected::after {width: 90%; /* Final width when the button is selected */transition: width 0.3s ease; /* Smooth animation for width */}
Post Statistics
Posts
No Posts Found
It looks like shaiRos has no public posts
Likes
Please Log In
You must be authenticated to view a user's likes
Profile Privacy
Multi-Factor Authentication
Multi-Factor Authentication (MFA) is an authentication method that requires you to provide two or more verification factors to gain access to your account. In addition to username and password, MFA requires you to verify your email on every login, which decreases the likelihood of someone stealing your account.
Change Password
Identity Color
Changes the color of your profile icon and cursor highlight in the live code editor. You and other users will be able to view this change.
Delete Account
Deleting your account is permanent. All data associated with your account will be lost.