示例#1
0
文件: macd.js 项目: ScottLogic/d3fc
export default function() {

    let value = identity;

    const fastEMA = exponentialMovingAverage()
        .period(12);
    const slowEMA = exponentialMovingAverage()
        .period(26);
    const signalEMA = exponentialMovingAverage()
        .period(9);

    const macd = data => {

        fastEMA.value(value);
        slowEMA.value(value);

        const diff = zip(fastEMA(data), slowEMA(data))
            .map(d => (d[0] !== undefined && d[1] !== undefined) ? d[0] - d[1] : undefined);

        const averageDiff = signalEMA(diff);

        return zip(diff, averageDiff)
            .map(d =>
                ({
                    macd: d[0],
                    signal: d[1],
                    divergence: d[0] !== undefined && d[1] !== undefined ? d[0] - d[1] : undefined
                })
            );
    };

    macd.value = (...args) => {
        if (!args.length) {
            return value;
        }
        value = args[0];
        return macd;
    };

    rebindAll(macd, fastEMA, includeMap({'period': 'fastPeriod'}));
    rebindAll(macd, slowEMA, includeMap({'period': 'slowPeriod'}));
    rebindAll(macd, signalEMA, includeMap({'period': 'signalPeriod'}));

    return macd;
}
示例#2
0
export default () => {

    let xValue = (d) => d.x;
    let yValue = (d) => d.y;
    let colorValue = (d) => d.color;
    let yBandwidth = () => 5;
    let xBandwidth = () => 5;
    let colorInterpolate = interpolateViridis;

    const heatmap = createBase({
        decorate: () => {},
        defined: (d, i) => defined(xValue, yValue, colorValue)(d, i),
        xScale: scaleIdentity(),
        yScale: scaleIdentity()
    });

    heatmap.pathGenerator = shapeBar()
        .x(0)
        .y(0);

    heatmap.colorScale = (data) => {
        const colorValues = data.map(colorValue);
        // a scale that maps the color values onto a unit range, [0, 1]
        return scaleLinear()
          .domain([min(colorValues), max(colorValues)]);
    };

    heatmap.values = (d, i) => ({
        x: heatmap.xScale()(xValue(d, i)),
        y: heatmap.yScale()(yValue(d, i)),
        colorValue: colorValue(d, i),
        width: xBandwidth(d, i),
        height: yBandwidth(d, i)
    });

    heatmap.xValue = (...args) => {
        if (!args.length) {
            return xValue;
        }
        xValue = functor(args[0]);
        return heatmap;
    };
    heatmap.yValue = (...args) => {
        if (!args.length) {
            return yValue;
        }
        yValue = functor(args[0]);
        return heatmap;
    };
    heatmap.colorValue = (...args) => {
        if (!args.length) {
            return colorValue;
        }
        colorValue = functor(args[0]);
        return heatmap;
    };
    heatmap.colorInterpolate = (...args) => {
        if (!args.length) {
            return colorInterpolate;
        }
        colorInterpolate = args[0];
        return heatmap;
    };
    heatmap.xBandwidth = (...args) => {
        if (!args.length) {
            return xBandwidth;
        }
        xBandwidth = functor(args[0]);
        return heatmap;
    };
    heatmap.yBandwidth = (...args) => {
        if (!args.length) {
            return yBandwidth;
        }
        yBandwidth = functor(args[0]);
        return heatmap;
    };

    rebindAll(heatmap, heatmap.pathGenerator, includeMap({
        'horizontalAlign': 'xAlign',
        'verticalAlign': 'yAlign'
    }));

    return heatmap;
};