🎥 Make videos programmatically with React
Elements: Add backgrounds to chart elements (#10544)
Igor Samokhovets566c7f5d0c15
diff --git a/packages/docs/elements/data/horizontal-bar-chart/horizontal-bar-chart.tsx b/packages/docs/elements/data/horizontal-bar-chart/horizontal-bar-chart.tsxindex f6fba02870..9ab4c9884f 100644--- a/packages/docs/elements/data/horizontal-bar-chart/horizontal-bar-chart.tsx+++ b/packages/docs/elements/data/horizontal-bar-chart/horizontal-bar-chart.tsx@@ -84,46 +84,61 @@ const Bar: React.FC<{export const HorizontalBarChart: React.FC = () => {return (<Interactive.Div- name="Chart"+ name="Background"style={{- fontFamily,- fontVariantNumeric: 'tabular-nums',- height: '100%',+ alignItems: 'center',+ backgroundColor: '#f5f6f7',+ boxSizing: 'border-box',display: 'flex',- flexDirection: 'column',+ height: '100%',justifyContent: 'center',- gap: 42,+ padding: 56,+ width: '100%',}}>- <Interactive.H1- name="Title"+ <Interactive.Div+ name="Chart"style={{- fontSize: 76,- fontWeight: 800,- letterSpacing: -3.8,- lineHeight: 0.95,- margin: 0,- color: '#111827',+ display: 'flex',+ flexDirection: 'column',+ fontFamily,+ fontVariantNumeric: 'tabular-nums',+ gap: 42,+ height: '100%',+ justifyContent: 'center',+ width: '100%',}}>- Team member pull-ups- </Interactive.H1>- {data.map(({highlighted, label, value}, index) => (- <div- key={label}+ <Interactive.H1+ name="Title"style={{- height: HEIGHT,+ fontSize: 76,+ fontWeight: 800,+ letterSpacing: -3.8,+ lineHeight: 0.95,+ margin: 0,+ color: '#111827',}}>- <Interactive.Div- from={8 + index * 6}- style={{width: `${(value / maxValue) * 100}%`, height: '100%'}}- showInTimeline={false}+ Team member pull-ups+ </Interactive.H1>+ {data.map(({highlighted, label, value}, index) => (+ <div+ key={label}+ style={{+ height: HEIGHT,+ }}>- <Bar highlighted={highlighted} label={label} value={value} />- </Interactive.Div>- </div>- ))}+ <Interactive.Div+ from={8 + index * 6}+ style={{width: `${(value / maxValue) * 100}%`, height: '100%'}}+ showInTimeline={false}+ >+ <Bar highlighted={highlighted} label={label} value={value} />+ </Interactive.Div>+ </div>+ ))}+ </Interactive.Div></Interactive.Div>);};diff --git a/packages/docs/elements/data/line-chart/line-chart.tsx b/packages/docs/elements/data/line-chart/line-chart.tsxindex 15d149f237..3b8be315d5 100644--- a/packages/docs/elements/data/line-chart/line-chart.tsx+++ b/packages/docs/elements/data/line-chart/line-chart.tsx@@ -62,215 +62,231 @@ export const LineChart: React.FC = () => {return (<Interactive.Div- name="Chart"+ name="Background"style={{- color: '#111827',+ alignItems: 'center',+ backgroundColor: '#f5f6f7',+ boxSizing: 'border-box',display: 'flex',- flexDirection: 'column',- fontFamily,- fontVariantNumeric: 'tabular-nums',- gap: 84,height: '100%',justifyContent: 'center',- translate: '30px 0px',+ padding: 160,+ width: '100%',}}>- <Interactive.H1- name="Title"- style={{- color: '#111827',- fontSize: 76,- fontWeight: 800,- letterSpacing: -3.8,- lineHeight: 0.95,- margin: 0,- translate: '0 -32px',- }}- >- Monthly active users- </Interactive.H1><Interactive.Div- name="Plot area"+ name="Chart"style={{- height: 520,- marginBottom: 64,- position: 'relative',+ color: '#111827',+ display: 'flex',+ flexDirection: 'column',+ fontFamily,+ fontVariantNumeric: 'tabular-nums',+ gap: 84,+ height: '100%',+ justifyContent: 'center',+ translate: '30px 0px',+ width: '100%',}}>- <div+ <Interactive.H1+ name="Title"style={{- color: '#4b5563',- fontSize: 40,- fontWeight: 700,- height: '100%',- position: 'absolute',- right: `calc(${100 - (CHART_SIDE_PADDING / CHART_WIDTH) * 100}% + 64px)`,- top: 0,- width: 94,+ color: '#111827',+ fontSize: 76,+ fontWeight: 800,+ letterSpacing: -3.8,+ lineHeight: 0.95,+ margin: 0,+ translate: '0 -32px',}}>- {Y_AXIS_VALUES.map((value) => (- <Interactive.Div- key={value}- name="Y-axis label"- style={{- position: 'absolute',- right: 0,- textAlign: 'right',- top: `${((MAX_VALUE - value) / (MAX_VALUE - MIN_VALUE)) * 100}%`,- translate: '0 -50%',- whiteSpace: 'nowrap',- width: '100%',- }}- >- {value}K- </Interactive.Div>- ))}- </div>- <svg- viewBox={`0 0 ${CHART_WIDTH} ${CHART_HEIGHT}`}- preserveAspectRatio="none"+ Monthly active users+ </Interactive.H1>+ <Interactive.Div+ name="Plot area"style={{- height: '100%',- overflow: 'visible',- position: 'absolute',- width: '100%',+ height: 520,+ marginBottom: 64,+ position: 'relative',}}>- <Interactive.G- name="Grid lines"- fill="none"- stroke="#d1d5db"- strokeWidth={2}- >- {Y_AXIS_VALUES.map((value) => {- const y =- ((MAX_VALUE - value) / (MAX_VALUE - MIN_VALUE)) * CHART_HEIGHT;-- return (- <line- key={value}- x1={CHART_SIDE_PADDING}- x2={CHART_WIDTH - CHART_SIDE_PADDING}- y1={y}- y2={y}- />- );- })}- </Interactive.G>- <Interactive.Path- name="Area fill"- d={areaPath}- fill="#2858e8"+ <divstyle={{- opacity: interpolate(frame, [48, 68], [0, 0.1], {- extrapolateLeft: 'clamp',- extrapolateRight: 'clamp',- }),+ color: '#4b5563',+ fontSize: 40,+ fontWeight: 700,+ height: '100%',+ position: 'absolute',+ right: `calc(${100 - (CHART_SIDE_PADDING / CHART_WIDTH) * 100}% + 64px)`,+ top: 0,+ width: 94,}}- />- <Interactive.Path- name="Trend line"- d={linePath}- fill="none"- pathLength={1}- stroke="#2858e8"- strokeDasharray="1 1"- strokeDashoffset={interpolate(frame, [14, 58], [1, 0], {- easing: Easing.bezier(0, 0, 0.58, 1),- extrapolateLeft: 'clamp',- extrapolateRight: 'clamp',- })}- strokeLinecap="round"- strokeLinejoin="round"- strokeWidth={12}- />- <Interactive.G- name="Data points"- fill="#ffffff"- stroke="#2858e8"- strokeWidth={8}>- {points.map(({label, x, y}, index) => (- <circle- key={label}- cx={x}- cy={y}- r={interpolate(- frame,- [- index === 0 ? 7 : 14 + index * 7,- index === 0 ? 15 : 22 + index * 7,- ],- [0, 11],- {- easing: Easing.bezier(0.34, 1.56, 0.64, 1),- extrapolateLeft: 'clamp',- extrapolateRight: 'clamp',- },- )}- />- ))}- </Interactive.G>- </svg>- <div- style={{- color: '#4b5563',- fontSize: 40,- fontWeight: 700,- height: 48,- left: 0,- position: 'absolute',- right: 0,- top: 'calc(100% + 64px)',- }}- >- {data.map(({label}, index) =>- index % 2 === 0 ? (+ {Y_AXIS_VALUES.map((value) => (<Interactive.Div- key={label}- name="X-axis label"+ key={value}+ name="Y-axis label"style={{- left: `${((CHART_SIDE_PADDING + (index / (data.length - 1)) * (CHART_WIDTH - CHART_SIDE_PADDING * 2)) / CHART_WIDTH) * 100}%`,position: 'absolute',- top: 0,- translate: '-50% 0',+ right: 0,+ textAlign: 'right',+ top: `${((MAX_VALUE - value) / (MAX_VALUE - MIN_VALUE)) * 100}%`,+ translate: '0 -50%',+ whiteSpace: 'nowrap',+ width: '100%',}}>- {label}+ {value}K</Interactive.Div>- ) : null,- )}- </div>- <div- style={{- left: `${(latestPoint.x / CHART_WIDTH) * 100}%`,- position: 'absolute',- top: `${(latestPoint.y / CHART_HEIGHT) * 100}%`,- translate: '-50% -140%',- }}- >- <Interactive.Div- name="Latest value"+ ))}+ </div>+ <svg+ viewBox={`0 0 ${CHART_WIDTH} ${CHART_HEIGHT}`}+ preserveAspectRatio="none"+ style={{+ height: '100%',+ overflow: 'visible',+ position: 'absolute',+ width: '100%',+ }}+ >+ <Interactive.G+ name="Grid lines"+ fill="none"+ stroke="#d1d5db"+ strokeWidth={2}+ >+ {Y_AXIS_VALUES.map((value) => {+ const y =+ ((MAX_VALUE - value) / (MAX_VALUE - MIN_VALUE)) *+ CHART_HEIGHT;++ return (+ <line+ key={value}+ x1={CHART_SIDE_PADDING}+ x2={CHART_WIDTH - CHART_SIDE_PADDING}+ y1={y}+ y2={y}+ />+ );+ })}+ </Interactive.G>+ <Interactive.Path+ name="Area fill"+ d={areaPath}+ fill="#2858e8"+ style={{+ opacity: interpolate(frame, [48, 68], [0, 0.1], {+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ }),+ }}+ />+ <Interactive.Path+ name="Trend line"+ d={linePath}+ fill="none"+ pathLength={1}+ stroke="#2858e8"+ strokeDasharray="1 1"+ strokeDashoffset={interpolate(frame, [14, 58], [1, 0], {+ easing: Easing.bezier(0, 0, 0.58, 1),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ })}+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth={12}+ />+ <Interactive.G+ name="Data points"+ fill="#ffffff"+ stroke="#2858e8"+ strokeWidth={8}+ >+ {points.map(({label, x, y}, index) => (+ <circle+ key={label}+ cx={x}+ cy={y}+ r={interpolate(+ frame,+ [+ index === 0 ? 7 : 14 + index * 7,+ index === 0 ? 15 : 22 + index * 7,+ ],+ [0, 11],+ {+ easing: Easing.bezier(0.34, 1.56, 0.64, 1),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ },+ )}+ />+ ))}+ </Interactive.G>+ </svg>+ <div+ style={{+ color: '#4b5563',+ fontSize: 40,+ fontWeight: 700,+ height: 48,+ left: 0,+ position: 'absolute',+ right: 0,+ top: 'calc(100% + 64px)',+ }}+ >+ {data.map(({label}, index) =>+ index % 2 === 0 ? (+ <Interactive.Div+ key={label}+ name="X-axis label"+ style={{+ left: `${((CHART_SIDE_PADDING + (index / (data.length - 1)) * (CHART_WIDTH - CHART_SIDE_PADDING * 2)) / CHART_WIDTH) * 100}%`,+ position: 'absolute',+ top: 0,+ translate: '-50% 0',+ }}+ >+ {label}+ </Interactive.Div>+ ) : null,+ )}+ </div>+ <divstyle={{- backgroundColor: '#2858e8',- borderRadius: 12,- color: '#ffffff',- fontSize: 44,- fontWeight: 800,- letterSpacing: -1.5,- lineHeight: 1,- padding: '18px 24px',- scale: `${latestValueProgress}`,- transformOrigin: 'bottom center',- visibility: frame <= 58 ? 'hidden' : 'visible',- whiteSpace: 'nowrap',- willChange: 'transform',+ left: `${(latestPoint.x / CHART_WIDTH) * 100}%`,+ position: 'absolute',+ top: `${(latestPoint.y / CHART_HEIGHT) * 100}%`,+ translate: '-50% -140%',}}>- {latestPoint.value}K- </Interactive.Div>- </div>+ <Interactive.Div+ name="Latest value"+ style={{+ backgroundColor: '#2858e8',+ borderRadius: 12,+ color: '#ffffff',+ fontSize: 44,+ fontWeight: 800,+ letterSpacing: -1.5,+ lineHeight: 1,+ padding: '18px 24px',+ scale: `${latestValueProgress}`,+ transformOrigin: 'bottom center',+ visibility: frame <= 58 ? 'hidden' : 'visible',+ whiteSpace: 'nowrap',+ willChange: 'transform',+ }}+ >+ {latestPoint.value}K+ </Interactive.Div>+ </div>+ </Interactive.Div></Interactive.Div></Interactive.Div>);diff --git a/packages/docs/elements/data/pie-chart/pie-chart.tsx b/packages/docs/elements/data/pie-chart/pie-chart.tsxindex 99333513aa..6ce8464c4d 100644--- a/packages/docs/elements/data/pie-chart/pie-chart.tsx+++ b/packages/docs/elements/data/pie-chart/pie-chart.tsx@@ -145,108 +145,123 @@ export const PieChart: React.FC = () => {return (<Interactive.Div- name="Chart"+ name="Background"style={{+ alignItems: 'center',+ backgroundColor: '#f5f6f7',+ boxSizing: 'border-box',display: 'flex',- flexDirection: 'column',- fontFamily,- fontVariantNumeric: 'tabular-nums',- gap: 42,height: '100%',justifyContent: 'center',+ padding: 56,+ width: '100%',}}>- <Interactive.H1- name="Title"- style={{- color: '#111827',- fontSize: 76,- fontWeight: 800,- letterSpacing: -3.8,- lineHeight: 0.95,- margin: 0,- }}- >- How we spend a workday- </Interactive.H1>- <div+ <Interactive.Div+ name="Chart"style={{- alignItems: 'center',display: 'flex',- flex: 1,- gap: 72,- minHeight: 0,+ flexDirection: 'column',+ fontFamily,+ fontVariantNumeric: 'tabular-nums',+ gap: 42,+ height: '100%',+ justifyContent: 'center',+ width: '100%',}}>- <Interactive.Svg- name="Pie chart"- viewBox="0 0 600 600"+ <Interactive.H1+ name="Title"style={{- flex: '0 0 600px',- height: 600,- overflow: 'visible',- scale: interpolate(frame, [8, 40], [0.96, 1], {- easing: Easing.inOut(Easing.cubic),- extrapolateLeft: 'clamp',- extrapolateRight: 'clamp',- output: 'perceptual-scale',- }),- width: 600,+ color: '#111827',+ fontSize: 76,+ fontWeight: 800,+ letterSpacing: -3.8,+ lineHeight: 0.95,+ margin: 0,}}>- <Interactive.Circle- name="Chart background"- cx={300}- cy={300}- fill="#e5e7eb"- opacity={interpolate(frame, [0, 8], [0, 1], {- easing: Easing.inOut(Easing.cubic),- extrapolateLeft: 'clamp',- extrapolateRight: 'clamp',- })}- r={284}- />- {data.map(({color, label, value}) => {- const startAngle = angle;- const endAngle = angle + (value / total) * 360;- angle = endAngle;-- return (- <Interactive.Path- key={label}- name={`${label} slice`}- d={getPieSlicePath({- center: 300,- endAngle: Math.min(endAngle, revealAngle),- radius: 284,- startAngle,- })}- fill={color}- />- );- })}- </Interactive.Svg>+ How we spend a workday+ </Interactive.H1><divstyle={{+ alignItems: 'center',display: 'flex',flex: 1,- flexDirection: 'column',- gap: 18,- justifyContent: 'center',+ gap: 72,+ minHeight: 0,}}>- {data.map(({color, foreground, label, value}) => (- <Interactive.Div key={label} from={8} showInTimeline={false}>- <LegendItem- color={color}- foreground={foreground}- label={label}- value={value}- />- </Interactive.Div>- ))}+ <Interactive.Svg+ name="Pie chart"+ viewBox="0 0 600 600"+ style={{+ flex: '0 0 600px',+ height: 600,+ overflow: 'visible',+ scale: interpolate(frame, [8, 40], [0.96, 1], {+ easing: Easing.inOut(Easing.cubic),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ output: 'perceptual-scale',+ }),+ width: 600,+ }}+ >+ <Interactive.Circle+ name="Chart background"+ cx={300}+ cy={300}+ fill="#e5e7eb"+ opacity={interpolate(frame, [0, 8], [0, 1], {+ easing: Easing.inOut(Easing.cubic),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ })}+ r={284}+ />+ {data.map(({color, label, value}) => {+ const startAngle = angle;+ const endAngle = angle + (value / total) * 360;+ angle = endAngle;++ return (+ <Interactive.Path+ key={label}+ name={`${label} slice`}+ d={getPieSlicePath({+ center: 300,+ endAngle: Math.min(endAngle, revealAngle),+ radius: 284,+ startAngle,+ })}+ fill={color}+ />+ );+ })}+ </Interactive.Svg>+ <div+ style={{+ display: 'flex',+ flex: 1,+ flexDirection: 'column',+ gap: 18,+ justifyContent: 'center',+ }}+ >+ {data.map(({color, foreground, label, value}) => (+ <Interactive.Div key={label} from={8} showInTimeline={false}>+ <LegendItem+ color={color}+ foreground={foreground}+ label={label}+ value={value}+ />+ </Interactive.Div>+ ))}+ </div></div>- </div>+ </Interactive.Div></Interactive.Div>);};diff --git a/packages/docs/elements/data/vertical-bar-chart/vertical-bar-chart.tsx b/packages/docs/elements/data/vertical-bar-chart/vertical-bar-chart.tsxindex 50919ac9fa..4c31670eb1 100644--- a/packages/docs/elements/data/vertical-bar-chart/vertical-bar-chart.tsx+++ b/packages/docs/elements/data/vertical-bar-chart/vertical-bar-chart.tsx@@ -166,71 +166,86 @@ const Bar: React.FC<{export const VerticalBarChart: React.FC = () => {return (<Interactive.Div- name="Chart"+ name="Background"style={{+ alignItems: 'center',+ backgroundColor: '#f5f6f7',+ boxSizing: 'border-box',display: 'flex',- flexDirection: 'column',- fontFamily,- fontVariantNumeric: 'tabular-nums',- gap: 42,height: '100%',justifyContent: 'center',+ padding: 56,+ width: '100%',}}>- <Interactive.H1- name="Title"+ <Interactive.Div+ name="Chart"style={{- color: '#111827',- fontSize: 76,- fontWeight: 800,- letterSpacing: -3.8,- lineHeight: 0.95,- margin: 0,- }}- >- Browser tabs open- </Interactive.H1>- <div- style={{- alignSelf: 'center',display: 'flex',- flex: 1,- justifyContent: 'space-between',- minHeight: 0,- position: 'relative',- width: 1080,+ flexDirection: 'column',+ fontFamily,+ fontVariantNumeric: 'tabular-nums',+ gap: 42,+ height: '100%',+ justifyContent: 'center',+ width: '100%',}}>- <Interactive.Div- name="Baseline"+ <Interactive.H1+ name="Title"style={{- backgroundColor: '#c5cad2',- bottom: 68,- height: 3,- left: '50%',- position: 'absolute',- translate: '-50% 50%',- width: '100%',- zIndex: 1,+ color: '#111827',+ fontSize: 76,+ fontWeight: 800,+ letterSpacing: -3.8,+ lineHeight: 0.95,+ margin: 0,}}- />- {data.map(({highlighted, label, value}, index) => (- <div- key={label}+ >+ Browser tabs open+ </Interactive.H1>+ <div+ style={{+ alignSelf: 'center',+ display: 'flex',+ flex: 1,+ justifyContent: 'space-between',+ minHeight: 0,+ position: 'relative',+ width: 1080,+ }}+ >+ <Interactive.Div+ name="Baseline"style={{- flex: '0 0 280px',- height: '100%',+ backgroundColor: '#c5cad2',+ bottom: 68,+ height: 3,+ left: '50%',+ position: 'absolute',+ translate: '-50% 50%',+ width: '100%',+ zIndex: 1,}}- >- <Bar- animationDelay={index * 24}- highlighted={highlighted}- label={label}- value={value}- />- </div>- ))}- </div>+ />+ {data.map(({highlighted, label, value}, index) => (+ <div+ key={label}+ style={{+ flex: '0 0 280px',+ height: '100%',+ }}+ >+ <Bar+ animationDelay={index * 24}+ highlighted={highlighted}+ label={label}+ value={value}+ />+ </div>+ ))}+ </div>+ </Interactive.Div></Interactive.Div>);};diff --git a/packages/docs/src/components/Elements/element-definitions.ts b/packages/docs/src/components/Elements/element-definitions.tsindex 393569c5f7..4f682997e6 100644--- a/packages/docs/src/components/Elements/element-definitions.ts+++ b/packages/docs/src/components/Elements/element-definitions.ts@@ -316,12 +316,11 @@ const elementImplementations = {'data/horizontal-bar-chart': {component: HorizontalBarChart,contributors: [],- description:- 'A bold bar chart card with three directly labeled data points.',+ description: 'A bold bar chart with three directly labeled data points.',dependencies: [{name: '@remotion/google-fonts', version: null}],durationInFrames: 120,- elementHeight: 864,- elementWidth: 1560,+ elementHeight: null,+ elementWidth: null,fps: 30,height: 1080,posterFrame: 70,@@ -331,7 +330,7 @@ const elementImplementations = {videoUrl:'https://remotion.media/elements/data-horizontal-bar-chart-preview.mp4',},- previewPadding: 56,+ previewPadding: 0,installationMode: 'wrapped',width: 1920,},@@ -341,8 +340,8 @@ const elementImplementations = {description: 'A bold animated line chart with a directly labeled trend.',dependencies: [{name: '@remotion/google-fonts', version: null}],durationInFrames: 120,- elementHeight: 864,- elementWidth: 1560,+ elementHeight: null,+ elementWidth: null,fps: 30,height: 1080,posterFrame: 70,@@ -350,7 +349,7 @@ const elementImplementations = {posterUrl: 'https://remotion.media/elements/data-line-chart-preview.png',videoUrl: 'https://remotion.media/elements/data-line-chart-preview.mp4',},- previewPadding: 160,+ previewPadding: 0,installationMode: 'wrapped',width: 1920,},@@ -361,8 +360,8 @@ const elementImplementations = {'A bold animated pie chart with four directly labeled data points.',dependencies: [{name: '@remotion/google-fonts', version: null}],durationInFrames: 120,- elementHeight: 864,- elementWidth: 1560,+ elementHeight: null,+ elementWidth: null,fps: 30,height: 1080,posterFrame: 70,@@ -370,7 +369,7 @@ const elementImplementations = {posterUrl: 'https://remotion.media/elements/data-pie-chart-preview.png',videoUrl: 'https://remotion.media/elements/data-pie-chart-preview.mp4',},- previewPadding: 56,+ previewPadding: 0,installationMode: 'wrapped',width: 1920,},@@ -408,8 +407,8 @@ const elementImplementations = {'A bold vertical bar chart with three directly labeled data points.',dependencies: [{name: '@remotion/google-fonts', version: null}],durationInFrames: 120,- elementHeight: 864,- elementWidth: 1560,+ elementHeight: null,+ elementWidth: null,fps: 30,height: 1080,posterFrame: 115,@@ -419,7 +418,7 @@ const elementImplementations = {videoUrl:'https://remotion.media/elements/data-vertical-bar-chart-preview.mp4',},- previewPadding: 56,+ previewPadding: 0,installationMode: 'wrapped',width: 1920,},