UNPKG

@d3fc/d3fc-series

Version:

A collection of components for rendering data series to SVG and canvas, including line, bar, OHLC, candlestick and more

89 lines (76 loc) 3.01 kB
import xyBase from '../xyBase'; import { webglSeriesLine, webglAdjacentAttribute, webglScaleMapper, webglTypes } from '@d3fc/d3fc-webgl'; import { rebindAll, exclude, rebind } from '@d3fc/d3fc-rebind'; export default () => { const base = xyBase(); const crossValueAttribute = webglAdjacentAttribute(0, 1); const crossNextValueAttribute = crossValueAttribute.offset(1); const mainValueAttribute = webglAdjacentAttribute(0, 1); const mainNextValueAttribute = mainValueAttribute.offset(1); const definedAttribute = webglAdjacentAttribute(0, 1) .type(webglTypes.UNSIGNED_BYTE); const definedNextAttribute = definedAttribute.offset(1); const draw = webglSeriesLine() .crossValueAttribute(crossValueAttribute) .crossNextValueAttribute(crossNextValueAttribute) .mainValueAttribute(mainValueAttribute) .mainNextValueAttribute(mainNextValueAttribute) .definedAttribute(definedAttribute) .definedNextAttribute(definedNextAttribute); let equals = (previousData, data) => false; let scaleMapper = webglScaleMapper; let previousData = []; let previousXScale = null; let previousYScale = null; const line = (data) => { const xScale = scaleMapper(base.xScale()); const yScale = scaleMapper(base.yScale()); const dataChanged = !equals(previousData, data); if (dataChanged) { previousData = data; definedAttribute.value((d, i) => base.defined()(d, i)).data(data); } if (dataChanged || xScale.scale !== previousXScale) { previousXScale = xScale.scale; if (base.orient() === 'vertical') { crossValueAttribute.value((d, i) => xScale.scale(base.crossValue()(d, i))).data(data); } else { crossValueAttribute.value((d, i) => xScale.scale(base.mainValue()(d, i))).data(data); } } if (dataChanged || yScale.scale !== previousYScale) { previousYScale = yScale.scale; if (base.orient() === 'vertical') { mainValueAttribute.value((d, i) => yScale.scale(base.mainValue()(d, i))).data(data); } else { mainValueAttribute.value((d, i) => yScale.scale(base.crossValue()(d, i))).data(data); } } draw.xScale(xScale.webglScale) .yScale(yScale.webglScale) .decorate((program) => base.decorate()(program, data, 0)); draw(data.length); }; line.equals = (...args) => { if (!args.length) { return equals; } equals = args[0]; return line; }; line.scaleMapper = (...args) => { if (!args.length) { return scaleMapper; } scaleMapper = args[0]; return line; }; rebindAll(line, base, exclude('baseValue', 'bandwidth', 'align')); rebind(line, draw, 'context', 'lineWidth', 'pixelRatio'); return line; };