import _ from 'lodash'; import React, { forwardRef } from 'react'; import { Group } from '../../../../components/Group'; import { Path } from '../../../../components/Path'; import { Mark, PlotContext, plotMarkReified } from '../Plot'; import { curveCatmullRom, line as d3Line } from 'd3-shape'; import { withBluefishFn, BBox } from '../../../../bluefish'; import { NewBBox } from '../../../../NewBBox'; import { PaperScope } from 'paper/dist/paper-core'; export const line = (data: T[], { x, y, color }: { x: string; y: string; color: string }): Mark => ({ data, encodings: { x, y, color }, scale: ( channels: { X: number[]; Y: number[]; COLOR: string[] }, scales: { xScale: (x: number) => number; yScale: (y: number) => number; colorScale: (color: string) => string }, _dimensions: any, ) => { const { X, Y, COLOR } = channels; const { xScale, yScale, colorScale } = scales; const indices = _.range(X.length); return { // TODO: fix this! stroke: colorScale(COLOR[0]), points: indices.map((i) => ({ x: xScale(X[i]), y: yScale(Y[i]), })), }; }, render: (data: { stroke: string; points: { x: number; y: number }[] }) => { return ( [x, y])) ?? ''} fill={'none'} stroke={data.stroke} strokeWidth={1.5} strokeLinecap={'round'} strokeLinejoin={'round'} strokeMiterlimit={1} /> ); }, }); export const Line: React.FC<{ data?: any[]; encodings: { x: string; y: string; color: string } }> = forwardRef( (props, ref) => { const context = React.useContext(PlotContext); const { encodings } = props; const data = props.data ?? context.data; const { x, y, color } = encodings; const mark = line(data, { x, y, color }); return {plotMarkReified(mark, context.scales, context.dimensions)}; }, ); export type RectScaleProps = React.SVGProps & { yScale: (d: any) => (y: number) => number; }; export const RectScale = withBluefishFn( (props: RectScaleProps) => { const { x, y, width, height } = props; return (_, constraints) => { const scaledY = y !== undefined ? props.yScale(constraints.height)(+y) : undefined; const scaledHeight = height !== undefined ? props.yScale(constraints.height)(+height) : undefined; return { left: x !== undefined ? +x : undefined, top: scaledY, width: width !== undefined ? +width : undefined, height: scaledHeight, }; }; }, forwardRef((props: RectScaleProps & { $bbox?: Partial }, ref: any) => { const { $bbox, yScale, ...rest } = props; return ( // translate and scale based on $bbox.coord ); }), ); RectScale.displayName = 'RectScale'; export type PathProps = React.SVGProps & Partial; export const PathScale = withBluefishFn( ({ d }: PathProps) => { const canvas = document.createElement('canvas'); const paperScope = new PaperScope(); paperScope.setup(canvas); const path = new paperScope.Path(d!); const bounds = path.bounds; return () => { return { left: bounds.left, top: bounds.top, width: bounds.width, height: bounds.height, }; }; }, (props: PathProps & { $bbox?: Partial }) => { const { $bbox, ...rest } = props; return ( ); }, );