Esempio n. 1
0
 connection.query('SELECT duration FROM history WHERE start_station = ? AND end_station = ?', [start_name, end_name], function(err, results) {
   connection.release();
   var durations = [];
   for (var i = 0; i < results.length; i++) {
     durations.push(results[i].duration);
   }
   var any_results = durations.length > 0 ? true : false;
   var shortest_trip = d3_array.min(durations);
   var chart_values = [], chart_values_if_needed = [];
   for (var i = 0; i < durations.length; i++) {
     if (durations[i] < shortest_trip * 4) {
       chart_values.push(durations[i]);
     } else if (durations[i] < shortest_trip * 20) {
       chart_values_if_needed.push(durations[i]);
     }
   }
   var filler_results = chart_values.length * 2 < durations.length && chart_values_if_needed.length > 0 ? true: false;
   var average_duration = humanizeDuration(d3_array.sum(chart_values) / chart_values.length, {round: true, conjunction: ' and '});
   var send = {
       any_results: any_results,
       filler_results: filler_results,
       durations: durations,
       chart_values: chart_values,
       chart_values_if_needed: chart_values_if_needed,
       average_duration: average_duration
   }
   res.type('json');
   res.send(send);
 });
Esempio n. 2
0
 this._wrapRows = function() {
   newRows = [];
   let row = 1, rowWidth = 0;
   for (let i = 0; i < this._lineData.length; i++) {
     const d = this._lineData[i],
           w = d.width + this._padding * (d.width ? 2 : 1) + d.shapeWidth;
     if (sum(newRows.map(row => max(row, d => max([d.height, d.shapeHeight])))) > availableHeight) {
       newRows = [];
       break;
     }
     if (w > availableWidth) {
       newRows = [];
       this._wrapLines();
       break;
     }
     else if (rowWidth + w < availableWidth) {
       rowWidth += w;
     }
     else if (this._direction !== "column") {
       rowWidth = w;
       row++;
     }
     if (!newRows[row - 1]) newRows[row - 1] = [];
     newRows[row - 1].push(d);
     if (this._direction === "column") {
       rowWidth = 0;
       row++;
     }
   }
 };
Esempio n. 3
0
			.accumulator(values => {
				const total = sum(values, (v, i) => {
					return i < n
						? (i + 1) * v
						: (windowSize - i) * v;
				});
				return total / weight;
			});
Esempio n. 4
0
 const q = h.values.map((d, i) => {
   const value = sum(d.values, (x, i) => this._value(x, i));
   const r = value / maxValue * radius,
         radians = tau / totalAxis * i;
   return {
     x: r * Math.cos(radians),
     y: r * Math.sin(radians)
   };
 });
Esempio n. 5
0
			.accumulator(values => {
				const tr = last(values);
				const atr = isDefined(prevATR)
					? ((prevATR * (windowSize - 1)) + tr) / windowSize
					: sum(values) / windowSize;

				prevATR = atr;
				return atr;
			});
Esempio n. 6
0
tape('kde approximates the cdf', function(t) {
  var data = d3.range(0, 1000).map(gaussian.sample),
      kde = stats.randomKDE(data),
      domain = d3.range(-5, 5.1, 0.5),
      error = domain.map(function(x) {
        return Math.abs(kde.cdf(x) - gaussian.cdf(x));
      });

  t.ok((d3.sum(error) / domain.length) < 0.01);
  t.end();
});
Esempio n. 7
0
    const interceptor = (layout) => {
        const start = new Date();
        const finalLayout = strategy(layout);
        const time = new Date() - start;

        // record some statistics on this strategy
        if (!interceptor.time) {
            Object.defineProperty(interceptor, 'time', { enumerable: false, writable: true });
            Object.defineProperty(interceptor, 'hidden', { enumerable: false, writable: true });
            Object.defineProperty(interceptor, 'overlap', { enumerable: false, writable: true });
        }
        const visibleLabels = finalLayout.filter((d) => !d.hidden);
        interceptor.time = time;
        interceptor.hidden = finalLayout.length - visibleLabels.length;
        interceptor.overlap = sum(visibleLabels.map((label, index) => {
            return sum(visibleLabels.filter((_, i) => i !== index)
                .map((d) => layoutIntersect(d, label)));
        }));
        return finalLayout;
    };
  getDistributionType: function (data) {
    var histogram = data || this.get('data');
    var freqAccessor = function (a) { return a.freq; };
    var osc = d3.max(histogram, freqAccessor) - d3.min(histogram, freqAccessor);
    var mean = d3.mean(histogram, freqAccessor);
    // When the difference between the max and the min values is less than
    // 10 percent of the mean, it's a flat histogram (F)
    if (osc < mean * 0.1) return 'F';
    var sumFreqs = d3.sum(histogram, freqAccessor);
    var freqs = histogram.map(function (bin) {
      return 100 * bin.freq / sumFreqs;
    });

    // The ajus array represents relative growths
    var ajus = freqs.map(function (freq, index) {
      var next = freqs[index + 1];
      if (freq > next) return -1;
      if (Math.abs(freq - next) <= 0.05) return 0;
      return 1;
    });
    ajus.pop();
    var maxAjus = d3.max(ajus);
    var minAjus = d3.min(ajus);
    // If it never grows or shrinks, it returns flat
    if (minAjus === 0 && maxAjus === 0) return 'F';
    else if (maxAjus < 1) return 'L';
    else if (minAjus > -1) return 'J';
    else {
      var uniques = _.uniq(ajus);
      var A_TYPES = [[1, -1], [1, 0, -1], [1, -1, 0], [0, 1, -1]];
      var U_TYPES = [[-1, 1], [-1, 0, 1], [-1, 1, 0], [0, -1, 1]];
      if (A_TYPES.some(function (e) {
        return _.isEqual(e, uniques);
      })) return 'A';
      else if (U_TYPES.some(function (e) {
        return _.isEqual(e, uniques);
      })) return 'U';
      else return 'S';
    }
  },
Esempio n. 9
0
 row.forEach(d => {
   if (i) {
     d.y = sum(newRows.slice(0, i), this._rowHeight.bind(this));
   }
 });
Esempio n. 10
0
 const maxValue = max(nestedGroupData.map(h => h.values.map(d => sum(d.values, (x, i) => this._value(x, i)))).flat());
Esempio n. 11
0
  /**
      @memberof Legend
      @desc Renders the current Legend to the page. If a *callback* is specified, it will be called once the legend is done drawing.
      @param {Function} [*callback* = undefined]
      @chainable
  */
  render(callback) {

    if (this._select === void 0) this.select(select("body").append("svg").attr("width", `${this._width}px`).attr("height", `${this._height}px`).node());

    // Shape <g> Group
    this._group = elem("g.d3plus-Legend", {parent: this._select});

    let availableHeight = this._height;
    this._titleHeight = 0;
    this._titleWidth = 0;
    if (this._title) {

      const f = this._titleConfig.fontFamily || this._titleClass.fontFamily()(),
            s = this._titleConfig.fontSize || this._titleClass.fontSize()();
      let lH = lH = this._titleConfig.lineHeight || this._titleClass.lineHeight();
      lH = lH ? lH() : s * 1.4;

      const res = textWrap()
        .fontFamily(f)
        .fontSize(s)
        .lineHeight(lH)
        .width(this._width)
        .height(this._height)
        (this._title);
      this._titleHeight = lH + res.lines.length + this._padding;
      this._titleWidth = max(res.widths);
      availableHeight -= this._titleHeight;
    }

    // Calculate Text Sizes
    this._lineData = this._data.map((d, i) => {

      const label = this._label(d, i);

      let res = {
        data: d,
        i,
        id: this._id(d, i),
        shapeWidth: this._fetchConfig("width", d, i),
        shapeHeight: this._fetchConfig("height", d, i),
        y: 0
      };

      if (!label) {
        res.sentence = false;
        res.words = [];
        res.height = 0;
        res.width = 0;
        return res;
      }

      const f = this._fetchConfig("fontFamily", d, i),
            lh = this._fetchConfig("lineHeight", d, i),
            s = this._fetchConfig("fontSize", d, i);

      const h = availableHeight - (this._data.length + 1) * this._padding,
            w = this._width;

      res = Object.assign(res, textWrap()
        .fontFamily(f)
        .fontSize(s)
        .lineHeight(lh)
        .width(w)
        .height(h)
        (label));

      res.width = Math.ceil(max(res.lines.map(t => textWidth(t, {"font-family": f, "font-size": s})))) + s * 0.75;
      res.height = Math.ceil(res.lines.length * (lh + 1));
      res.og = {height: res.height, width: res.width};
      res.f = f;
      res.s = s;
      res.lh = lh;

      return res;

    });

    let spaceNeeded;
    const availableWidth = this._width - this._padding * 2;
    spaceNeeded = this._rowWidth(this._lineData);

    if (this._direction === "column" || spaceNeeded > availableWidth) {
      let lines = 1, newRows = [];

      const maxLines = max(this._lineData.map(d => d.words.length));
      this._wrapLines = function() {

        lines++;

        if (lines > maxLines) return;

        const wrappable = lines === 1 ? this._lineData.slice()
          : this._lineData.filter(d => d.width + d.shapeWidth + this._padding * (d.width ? 2 : 1) > availableWidth && d.words.length >= lines)
              .sort((a, b) => b.sentence.length - a.sentence.length);

        if (wrappable.length && availableHeight > wrappable[0].height * lines) {

          let truncated = false;
          for (let x = 0; x < wrappable.length; x++) {
            const label = wrappable[x];
            const h = label.og.height * lines, w = label.og.width * (1.5 * (1 / lines));
            const res = textWrap().fontFamily(label.f).fontSize(label.s).lineHeight(label.lh).width(w).height(h)(label.sentence);
            if (!res.truncated) {
              label.width = Math.ceil(max(res.lines.map(t => textWidth(t, {"font-family": label.f, "font-size": label.s})))) + label.s;
              label.height = res.lines.length * (label.lh + 1);
            }
            else {
              truncated = true;
              break;
            }
          }
          if (!truncated) this._wrapRows();
        }
        else {
          newRows = [];
          return;
        }

      };

      this._wrapRows = function() {
        newRows = [];
        let row = 1, rowWidth = 0;
        for (let i = 0; i < this._lineData.length; i++) {
          const d = this._lineData[i],
                w = d.width + this._padding * (d.width ? 2 : 1) + d.shapeWidth;
          if (sum(newRows.map(row => max(row, d => max([d.height, d.shapeHeight])))) > availableHeight) {
            newRows = [];
            break;
          }
          if (w > availableWidth) {
            newRows = [];
            this._wrapLines();
            break;
          }
          else if (rowWidth + w < availableWidth) {
            rowWidth += w;
          }
          else if (this._direction !== "column") {
            rowWidth = w;
            row++;
          }
          if (!newRows[row - 1]) newRows[row - 1] = [];
          newRows[row - 1].push(d);
          if (this._direction === "column") {
            rowWidth = 0;
            row++;
          }
        }
      };

      this._wrapRows();

      if (!newRows.length || sum(newRows, this._rowHeight.bind(this)) + this._padding > availableHeight) {
        spaceNeeded = sum(this._lineData.map(d => d.shapeWidth + this._padding)) - this._padding;
        for (let i = 0; i < this._lineData.length; i++) {
          this._lineData[i].width = 0;
          this._lineData[i].height = 0;
        }
        this._wrapRows();
      }

      if (newRows.length && sum(newRows, this._rowHeight.bind(this)) + this._padding < availableHeight) {
        newRows.forEach((row, i) => {
          row.forEach(d => {
            if (i) {
              d.y = sum(newRows.slice(0, i), this._rowHeight.bind(this));
            }
          });
        });
        spaceNeeded = max(newRows, this._rowWidth.bind(this));
      }
    }

    const innerHeight = max(this._lineData, (d, i) => max([d.height, this._fetchConfig("height", d.data, i)]) + d.y) + this._titleHeight,
          innerWidth = max([spaceNeeded, this._titleWidth]);

    this._outerBounds.width = innerWidth;
    this._outerBounds.height = innerHeight;

    let xOffset = this._padding,
        yOffset = this._padding;
    if (this._align === "center") xOffset = (this._width - innerWidth) / 2;
    else if (this._align === "right") xOffset = this._width - this._padding - innerWidth;
    if (this._verticalAlign === "middle") yOffset = (this._height - innerHeight) / 2;
    else if (this._verticalAlign === "bottom") yOffset = this._height - this._padding - innerHeight;
    this._outerBounds.x = xOffset;
    this._outerBounds.y = yOffset;

    this._titleClass
      .data(this._title ? [{text: this._title}] : [])
      .duration(this._duration)
      .select(this._group.node())
      .textAnchor({left: "start", center: "middle", right: "end"}[this._align])
      .width(this._width - this._padding * 2)
      .x(this._padding)
      .y(this._outerBounds.y)
      .config(this._titleConfig)
      .render();

    this._shapes = [];
    const baseConfig = configPrep.bind(this)(this._shapeConfig, "legend"),
          config = {
            id: d => d.id,
            label: d => d.label,
            lineHeight: d => d.lH
          };

    const data = this._data.map((d, i) => {

      const obj = {
        __d3plus__: true,
        data: d, i,
        id: this._id(d, i),
        label: this._lineData[i].width ? this._label(d, i) : false,
        lH: this._fetchConfig("lineHeight", d, i),
        shape: this._shape(d, i)
      };

      return obj;

    });

    // Legend Shapes
    this._shapes = [];
    ["Circle", "Rect"].forEach(Shape => {

      this._shapes.push(new shapes[Shape]()
        .data(data.filter(d => d.shape === Shape))
        .duration(this._duration)
        .labelConfig({padding: 0})
        .select(this._group.node())
        .verticalAlign("top")
        .config(assign({}, baseConfig, config))
        .render());

    });

    if (callback) setTimeout(callback, this._duration + 100);

    return this;

  }
Esempio n. 12
0
 negVals = groupedData.map(arr => sum(arr.filter(d => d < 0)));
Esempio n. 13
0
 posVals = groupedData.map(arr => sum(arr.filter(d => d > 0)));
Esempio n. 14
0
function collectionArea(collection) {
  return sum(collection, function(d) { return polygonArea(d); });
}
Esempio n. 15
0
 cellTrans = (d,i) => {
     const width = sum(shapeSize.slice(0, i + 1 ), d => d.width);
     return `translate(${(width + i*shapePadding)},0)`; };
Esempio n. 16
0
 .reduce((obj, d) => {
   if (!obj[d.key]) obj[d.key] = 0;
   obj[d.key] += sum(d.values, dd => dd[opp]);
   return obj;
 }, {});
Esempio n. 17
0
 _rowWidth(row) {
   return sum(row.map((d, i) => {
     const p = this._padding * (i === row.length - 1 ? 0 : d.width ? 2 : 1);
     return d.shapeWidth + d.width + p;
   }));
 }
Esempio n. 18
0
		var triaverage = slidingWindow().windowSize(windowSize).sourcePath(sourcePath).accumulator(function (values) {
			var total = sum(values, function (v, i) {
				return i < n ? (i + 1) * v : (windowSize - i) * v;
			});
			return total / weight;
		});
Esempio n. 19
0
 interceptor.overlap = sum(visibleLabels.map((label, index) => {
     return sum(visibleLabels.filter((_, i) => i !== index)
         .map((d) => layoutIntersect(d, label)));
 }));
function helper(props, moreProps) {
	var x1Value = props.x1Value,
	    x2Value = props.x2Value,
	    type = props.type;
	var xScale = moreProps.xScale,
	    yScale = moreProps.chartConfig.yScale,
	    fullData = moreProps.fullData;
	var xAccessor = moreProps.xAccessor;

	/*
 http://www.metastock.com/Customer/Resources/TAAZ/?p=65
 y = a + bx
 n = length of array
 b = (n * sum(x*y) - sum(xs) * sum(ys)) / (n * sum(xSquareds) - (sum(xs) ^ 2))
 a = (sum of closes)
 */

	var _getClosestItemIndexe = getClosestItemIndexes(fullData, x1Value, xAccessor),
	    left = _getClosestItemIndexe.left;

	var _getClosestItemIndexe2 = getClosestItemIndexes(fullData, x2Value, xAccessor),
	    right = _getClosestItemIndexe2.right;

	var startIndex = Math.min(left, right);
	var endIndex = Math.max(left, right) + 1;

	var array = fullData.slice(startIndex, endIndex);

	var xs = array.map(function (d) {
		return xAccessor(d).valueOf();
	});
	var ys = array.map(function (d) {
		return d.close;
	});
	var n = array.length;

	var combine = zipper().combine(function (x, y) {
		return x * y;
	});

	var xys = combine(xs, ys);
	var xSquareds = xs.map(function (x) {
		return Math.pow(x, 2);
	});

	var b = (n * sum(xys) - sum(xs) * sum(ys)) / (n * sum(xSquareds) - Math.pow(sum(xs), 2));
	var a = (sum(ys) - b * sum(xs)) / n;

	var newy1 = a + b * x1Value;
	var newy2 = a + b * x2Value;

	var x1 = xScale(x1Value);
	var y1 = yScale(newy1);
	var x2 = xScale(x2Value);
	var y2 = yScale(newy2);

	var stdDev = type === "SD" ? deviation(array, function (d) {
		return d.close;
	}) : 0;

	var dy = yScale(newy1 - stdDev) - y1;

	return {
		x1: x1, y1: y1, x2: x2, y2: y2, dy: dy
	};
}
Esempio n. 21
0
 cellTrans = (d,i) => {
     const height = sum(shapeSize.slice(0, i + 1 ), d => d.height);
     return `translate(0, ${(height + i*shapePadding)})`};
Esempio n. 22
0
            let maxValueByDate = d3Array.max(dataByDateFormatted, function(d){
                let vals = keys.map((key) => d[key]);

                return d3Array.sum(vals);
            });