예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
배열의 항목마다 사각형 하나를 그립니다. y와 height는 데이터로 계산하는 함수여서, update()를 호출하면 막대가 움직입니다.
import 'nuclo' const labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] let data = [40, 72, 55, 90, 30, 64] const rand = () => Math.round(10 + Math.random() * 90) const barY = (value: number) => 140 - value * 1.3 // Every SVG builder ends in "Svg". Attributes that depend on // data are functions, so update() moves the bars. A CSS // transition on y and height animates the move. export const BarChart = () => div( svgSvg( { viewBox: '0 0 320 160', width: '320', height: '160' }, lineSvg({ x1: 0, y1: 140.5, x2: 320, y2: 140.5, stroke: '#ccc' }), ...labels.map((label, i) => gSvg( rectSvg({ x: i * 52 + 14, width: 36, rx: 5, fill: '#ff3f00', style: { transition: 'y 0.4s, height 0.4s' }, y: () => barY(data[i]), height: () => data[i] * 1.3, }), textSvg( { x: i * 52 + 32, y: 156, 'text-anchor': 'middle' }, label, ), // A text's y does not transition; a CSS transform does. gSvg( { style: () => ({ transition: 'transform 0.4s', transform: `translateY(${barY(data[i]) - 6}px)`, }), }, textSvg( { x: i * 52 + 32, y: 0, 'text-anchor': 'middle' }, () => data[i], ), ), ), ), ), button('Randomize', { onClick: () => { data = data.map(rand); update() }, }), button('Sort', { onClick: () => { data = [...data].sort((a, b) => a - b); update() }, }), )