當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript d3.bisector函數代碼示例

本文整理匯總了TypeScript中d3.bisector函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript bisector函數的具體用法?TypeScript bisector怎麽用?TypeScript bisector使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了bisector函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: init

    init() {
        const {target, width, height, margin} = this;
        // const {xTicks, yTicks, yRange} = this;
        const {xTicks, yRange} = this;
        const [w, h] = this.dimensions();

        this.chart = d3.select(target)
            .attr('width', width)
            .attr('height', height)
            .append('g')
            .attr('transform', `translate(${margin.left}, ${margin.top})`);

        this.x = d3.time.scale()
            .range([0, w]);

        this.y = d3.scale.linear()
            .range(yRange);

        this.xAxis = d3.svg.axis()
            .orient('bottom')
            .scale(this.x)
            .ticks(xTicks)
            .tickPadding(8)
            .tickSize(-h);

        this.chart.append('g')
            .attr('class', 'x axis')
            .attr('transform', `translate(0, ${h})`)
            .call(this.xAxis);


        his.details = this.chart.append('g')
            .attr('class', 'details')
            .style('display', 'none')

        this.details.append('line')
            .attr('class', 'x')
            .attr('y1', 0)
            .attr('y1', h)

        this.details.append('text')
            .attr('class', 'time')

        this.details.append('g')
            .attr('class', 'data')

        this.overlay = this.chart.append('rect')
            .attr('class', 'overlay')
            .attr('width', w)
            .attr('height', h)
            .style('fill-opacity', 0)
            .on('mousemove', _ => this.onMouseOver())
            .on('mouseleave', _ => this.onMouseLeave());

        this.xBisect = d3.bisector(d => d.time).left;


    }
開發者ID:xsurge83,項目名稱:d3-charts,代碼行數:58,代碼來源:main.ts

示例2: getClosestItemIndexes2

export function getClosestItemIndexes2(array: Array<number>, value: number, accessor: any): { left: number, right: number } {
  let left: number = d3.bisector(accessor).left(array, value);
  left = Math.max(left - 1, 0);
  let right: number = Math.min(left + 1, array.length - 1);

  let item: number = accessor(array[left]);
  if (item >= value && item <= value) {
    right = left;
  }

  return { left, right };
}
開發者ID:Svjard,項目名稱:ng2-stockcharts,代碼行數:12,代碼來源:index.ts

示例3: financeEODScale

export default function financeEODScale(indexAccessor = d => d.idx, dateAccessor = d => d.date, data = [0, 1], backingLinearScale = d3.scale.linear()) {

  var timeScaleSteps = [
    { step: 864e5, f: function(d) { return isDefined(dateAccessor(d)) && true; } },  // 1-day
    { step: 1728e5, f: function(d, i) { return isDefined(dateAccessor(d)) && (i % 2 === 0); } }, // 2-day
    { step: 8380e5, f: function(d, i, arr) {
      if (d.startOfMonth) return true;
      var list = [];
      if ((i - 2) >= 0) list.push(arr[i - 2]);
      if ((i - 1) >= 0) list.push(arr[i - 1]);
      list.push(arr[i]);
      if ((i + 1) <= arr.length - 1) list.push(arr[i + 1]);
      if ((i + 2) <= arr.length - 1) list.push(arr[i + 2]);
      var sm = list
            .map(function(each) { return each.startOfMonth; })
            .reduce(function(prev, curr) {
              return prev || curr;
            });
      return sm ? false : d.startOfWeek;
    } },  // 1-week
    { step: 3525e6, f: function(d) {return d.startOfMonth; } },  // 1-month
    { step: 7776e6, f: function(d) {return d.startOfQuarter; } },  // 3-month
    { step: 31536e6, f: function(d) {return d.startOfYear; } },  // 1-year
    { step: 91536e15, f: function(d) {return isDefined(dateAccessor(d)) && (d.startOfYear && dateAccessor(d).getFullYear() % 2 === 0); } }  // 2-year
  ];
  var timeScaleStepsBisector = d3.bisector(function(d) { return d.step; }).left;
  var bisectByIndex = d3.bisector(function(d) { return indexAccessor(d); }).left;
  var tickFormat = [
    [d3.time.format("%Y"), function(d) { return d.startOfYear; }],
    [d3.time.format("%b %Y"), function(d) { return d.startOfQuarter; }],
    [d3.time.format("%b"), function(d) { return d.startOfMonth; }],
    [d3.time.format("%d %b"), function(d) { return d.startOfWeek; }],
    [d3.time.format("%a %d "), function(/* d */) { return true; }]
  ];
  function formater(d) {
    var i = 0, format = tickFormat[i];
    while (!format[1](d)) format = tickFormat[++i];
    var tickDisplay = format[0](dateAccessor(d));
    // console.log(t;ickDisplay);
    return tickDisplay;
  }

  function scale(x) {
    return backingLinearScale(x);
  }
  scale.isPolyLinear = function() {
    return true;
  };
  scale.invert = function(x) {
    return backingLinearScale.invert(x);
  };
  scale.data = function(x) {
    if (!arguments.length) {
      return data;
    } else {
      data = x;
      // this.domain([data.first().index, data.last().index]);
      this.domain([indexAccessor(data[0]), indexAccessor(data[data.length - 1])]);
      return scale;
    }
  };
  scale.indexAccessor = function(x) {
    if (!arguments.length) return indexAccessor;
    indexAccessor = x;
    return scale;
  };
  scale.dateAccessor = function(x) {
    if (!arguments.length) return dateAccessor;
    dateAccessor = x;
    return scale;
  };
  scale.domain = function(x) {
    if (!arguments.length) return backingLinearScale.domain();
    // console.log("before = %s, after = %s", JSON.stringify(backingLinearScale.domain()), JSON.stringify(x))

    var d = [x[0], x[1]];
    // console.log(d);
    backingLinearScale.domain(d);
    return scale;
  };
  scale.range = function(x) {
    if (!arguments.length) return backingLinearScale.range();
    backingLinearScale.range(x);
    return scale;
  };
  scale.rangeRound = function(x) {
    return backingLinearScale.range(x);
  };
  scale.clamp = function(x) {
    if (!arguments.length) return backingLinearScale.clamp();
    backingLinearScale.clamp(x);
    return scale;
  };
  scale.interpolate = function(x) {
    if (!arguments.length) return backingLinearScale.interpolate();
    backingLinearScale.interpolate(x);
    return scale;
  };
  scale.ticks = function(m) {
    var start, end, count = 0;
//.........這裏部分代碼省略.........
開發者ID:Svjard,項目名稱:ng2-stockcharts,代碼行數:101,代碼來源:financeEODDiscontinuousScale.ts

示例4: Error

  { target: 55e4, level: 4 },
  { target: 11e5, level: 5 },
  { target: 21e5, level: 6 },
  { target: 43e5, level: 7 },
  { target: 49e5, level: 8 }, // not tested
  { target: 50e5, level: 9 }, // not tested
  { target: 58e6, level: 10 }, // not tested
  { target: 58e6, level: 11 },
  { target: 11e7, level: 12 },
  { target: 78e7, level: 13 },
  { target: 16e8, level: 14 },
  { target: 62e8, level: 15 },
  { target: 10e20, level: 16 },
];

var tickLevelBisector = d3.bisector(function(d) { return d.target; }).left;

export default function financeDiscontinuousScale(index,
    interval,
    backingLinearScale = d3.scale.linear()) {

  if (isNotDefined(index) || isNotDefined(interval))
    throw new Error("Use the discontinuousTimeScaleProvider to create financeDiscontinuousScale");

  function scale(x) {
    return backingLinearScale(x);
  }
  scale.invert = function(x) {
    var inverted = backingLinearScale.invert(x);
    return Math.round(inverted * 10000) / 10000;
  };
開發者ID:Svjard,項目名稱:ng2-stockcharts,代碼行數:31,代碼來源:financeDiscontinuousScale.ts

示例5: financeIntradayScale

export default function financeIntradayScale(indexAccessor = d => d.idx, dateAccessor = d => d.date, data = [0, 1], backingLinearScale = d3.scale.linear()) {

  var timeScaleSteps = [
    { step: 40000, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfMinute); } }, // 1 min
    { step: 192000, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOf5Minutes); } }, // 5 min
    { step: 400000, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfQuarterHour); } },
    { step: 6684000, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfHalfHour); } },
    { step: 108e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfHour); } },
    { step: 216e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfEighthDay); } },
    { step: 432e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfQuarterDay); } }, // 12 hours
    { step: 864e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay || d.startOfHalfDay); } },  // 1-day
    { step: 2592e5, f: function(d) { return isDefined(dateAccessor(d)) && d.startOfDay; } },  // 3-day, doesnt work with 2h scale
    { step: 6048e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay && dateAccessor(d).getDate() % 3 === 0); } },  // 7-day
    { step: 12096e5, f: function(d) { return isDefined(dateAccessor(d)) && (d.startOfDay && dateAccessor(d).getDate() % 3 === 0); } }  // 14-day
  ];
  var timeScaleStepsBisector = d3.bisector(function(d) { return d.step; }).left;
  var bisectByIndex = d3.bisector(function(d) { return indexAccessor(d); }).left;
  var tickFormat = [
    [d3.time.format.utc("%a %d"), function(d) { return d.startOfDay; }],
    [d3.time.format.utc("%_I %p"), function(d) { return d.startOfHour && !d.startOfDay; }],
    [d3.time.format.utc("%I:%M %p"), d3.functor(true)] // accumulator fallback for first entry
  ];
  function formater(d) {
    var i = 0, format = tickFormat[i];
    while (!format[1](d)) format = tickFormat[++i];
    var tickDisplay = format[0](dateAccessor(d));
    // console.log(t;ickDisplay);
    return tickDisplay;
  }

  function scale(x) {
    return backingLinearScale(x);
  }
  scale.isPolyLinear = function() {
    return true;
  };
  scale.invert = function(x) {
    return backingLinearScale.invert(x);
  };
  scale.data = function(x) {
    if (!arguments.length) {
      return data;
    } else {
      data = x;
      // this.domain([data.first().index, data.last().index]);
      this.domain([indexAccessor(data[0]), indexAccessor(data[data.length - 1])]);
      return scale;
    }
  };
  scale.indexAccessor = function(x) {
    if (!arguments.length) return indexAccessor;
    indexAccessor = x;
    return scale;
  };
  scale.dateAccessor = function(x) {
    if (!arguments.length) return dateAccessor;
    dateAccessor = x;
    return scale;
  };
  scale.domain = function(x) {
    if (!arguments.length) return backingLinearScale.domain();
    // console.log("before = %s, after = %s", JSON.stringify(backingLinearScale.domain()), JSON.stringify(x))

    var d = [x[0], x[1]];
    // console.log(d);
    backingLinearScale.domain(d);
    return scale;
  };
  scale.range = function(x) {
    if (!arguments.length) return backingLinearScale.range();
    backingLinearScale.range(x);
    return scale;
  };
  scale.rangeRound = function(x) {
    return backingLinearScale.range(x);
  };
  scale.clamp = function(x) {
    if (!arguments.length) return backingLinearScale.clamp();
    backingLinearScale.clamp(x);
    return scale;
  };
  scale.interpolate = function(x) {
    if (!arguments.length) return backingLinearScale.interpolate();
    backingLinearScale.interpolate(x);
    return scale;
  };
  scale.ticks = function(m) {
    /* var start, end, count = 0;
    data.forEach(function(d) {
      if (isDefined(dateAccessor(d))) {
        if (isNotDefined(start)) start = d;
        end = d;
        count++;
      }
    });
    console.log(count / data.length);
    m = (count / data.length) * m;
    */

    var start = head(data);
//.........這裏部分代碼省略.........
開發者ID:Svjard,項目名稱:ng2-stockcharts,代碼行數:101,代碼來源:financeIntradayDiscontinuousScale.ts


注:本文中的d3.bisector函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。