首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FLOT中的趋势线

FLOT中的趋势线
EN

Stack Overflow用户
提问于 2014-08-08 17:21:54
回答 1查看 2.3K关注 0票数 1

我包括了jquery.flot.trendline.jsFrom Here

这是我的代码

代码语言:javascript
复制
$.plot($("#placeholder"), seriesdata, {
        series: {
     trendline: {
        show:true,
        lineWidth:2,
        fill:true,
        fillColor:false,
        steps:true
    },
    ...
});

我在图表上看不到趋势线。

EN

回答 1

Stack Overflow用户

发布于 2014-08-08 22:05:40

这个插件是行不通的。它需要修改flot源代码才能工作,在我看来做得不是很好。最简单的方法是自己添加趋势线作为附加系列。数学不难..。

代码语言:javascript
复制
  // calc slope and intercept
  // then use resulting y = mx + b to create trendline
  lineFit = function(points){
    sI = slopeAndIntercept(points);
    if (sI){
      // we have slope/intercept, get points on fit line
      var N = points.length;
      var rV = [];
      rV.push([points[0][0], sI.slope * points[0][0] + sI.intercept]);
      rV.push([points[N-1][0], sI.slope * points[N-1][0] + sI.intercept]);
      return rV;
    }
    return [];
  }

  // simple linear regression
  slopeAndIntercept = function(points){
    var rV = {},
        N = points.length,
        sumX = 0, 
        sumY = 0,
        sumXx = 0,
        sumYy = 0,
        sumXy = 0;

    // can't fit with 0 or 1 point
    if (N < 2){
      return rV;
    }    

    for (var i = 0; i < N; i++){
      var x = points[i][0],
          y = points[i][1];
      sumX += x;
      sumY += y;
      sumXx += (x*x);
      sumYy += (y*y);
      sumXy += (x*y);
    }

    // calc slope and intercept
    rV['slope'] = ((N * sumXy) - (sumX * sumY)) / (N * sumXx - (sumX*sumX));
    rV['intercept'] = (sumY - rV['slope'] * sumX) / N;
    rV['rSquared'] = Math.abs((rV['slope'] * (sumXy - (sumX * sumY) / N)) / (sumYy - ((sumY * sumY) / N)));

    return rV;
  }

然后,您可以这样调用:

代码语言:javascript
复制
  lineFitSeries = lineFit(someSeries);

并将lineFitSeries作为另一个序列添加到flot...

这是一个有效的example

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25200043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档