首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Vizframe图中实现线性回归线?

如何在Vizframe图中实现线性回归线?
EN

Stack Overflow用户
提问于 2017-03-21 22:05:20
回答 1查看 955关注 0票数 0

我有一个散点图Vizframe图表,其中我需要包括一个线性回归/趋势线。知道该怎么做吗?似乎这不是由电视“开箱即用”提供的东西吗?我找不到解决办法!

问题:

对于散点图Vizframe图上实现回归线的可行方法,有什么建议吗?

这是我的设置代码。当按下按钮时,散点图在对话框/模态中打开。

代码语言:javascript
复制
sap.ui.define([
        'jquery.sap.global',
        'vizConcept/controller/BaseController',
        'sap/ui/model/json/JSONModel',
        'vizConcept/model/viewControls',
        'sap/m/Button',
        'sap/m/Dialog',
    ],

    function (jQuery, BaseController, JSONModel, viewControls, Button, Dialog) {

        "use strict";

        var controls;

        var mainController = BaseController.extend("vizConcept.controller.Main", {

            onInit: function(oEvent) {

                // Access/expose the defined model(s) configured in the Component.js or Manifest.json within the controller.
                this.getView().setModel(this.getOwnerComponent().getModel("products"), "products");
                var oModel = this.getView().getModel("products");
                this.getView().setModel(oModel);

                var sUrl = "#" + this.getOwnerComponent().getRouter().getURL("page2");

        $(function() {
            var dataset = new sap.viz.ui5.data.FlattenedDataset({ 
                dimensions : [
                 {
                   axis : 1,
                   name : 'Award Date',
                   value : "{AwdDate}"
                 }
                ],
                measures : [
                            {
                            group: 1,
                                name : 'Award Date',
                                value : '{Hist}'
                            },
                            {
                            group: 2,
                                name : 'Current PPI',
                                value : '{Current}'
                            }
                ],
                data : {
                  path : "/ProductCollection"
                }   
              });


        var scatterViz = new sap.viz.ui5.Scatter({
                id : "idscatter",
                width : "1000px",
                height : "400px",
                title : {
                  text : 'Pricing Tool Scatter Plot Example'
                },
                xAxis : {
                  title : {
                    visible : true
                  }
                },
                yAxis : {
                  title : {
                    visible : true
                  }
                },
                dataset : dataset
              });

          scatterViz.setModel(sap.ui.getCore().getModel());  
          scatterViz.setModel(oModel);


      var dlg = new sap.m.Dialog({
        id: 'vizModal',    
        title: 'Scatter Plot Example Viz',
        width : "1800px",  
        height : "600px",  
        content : [scatterViz],
        beginButton: new Button({
                            text: 'Close',
                            press: function () {
                                dlg.close();
                            }
                        })  
      });

      (new sap.m.Button({
            text: 'open',
            type: 'Accept',
            press: function() {
              dlg.open();
              scatterViz.invalidate();
            }
          })).placeAt('content');
        });
            },    

        onToPage2 : function () {
            this.getOwnerComponent().getRouter().navTo("page2");
        },


        });

        return mainController;
    });

编辑

这是在视频帧图上输出的“产品”模型。我在manifest.json中定义了产品模型,但是那里的连接很好:

产品模型

代码语言:javascript
复制
          {
            "ProductCollection": [
                { 
                    "Item": "1",
                    "AwdDate": "20160715",
                    "Hist": 171.9,
                    "Current": 183

                },
                {
                    "Item": "2",
                    "AwdDate": "20160701",
                    "Hist" : 144.3,
                    "Current": 158.6
                },
                {
                    "Item": "3",
                    "AwdDate": "20150701",
                    "Hist": 160,
                    "Current": 165
                },
                { 
                    "Item": "1",
                    "AwdDate": "20160715",
                    "Hist": 201,
                    "Current": 167
                },
                {
                     "Item": "2",
                    "AwdDate": "20160801",
                    "Hist" : 175.3,
                    "Current": 178.2
                },
                {
                    "Item": "3",
                    "AwdDate": "20150721",
                    "Hist": 160,
                    "Current": 147
                },
                { 
                    "Item": "1",
                    "AwdDate": "20160715",
                    "Hist": 175.9,
                    "Current": 185.2
                },
                {
                    "Item": "2",
                    "AwdDate": "20161101",
                    "Hist" : 165.3,
                    "Current": 158.2
                },
                {
                    "Item": "3",
                    "AwdDate": "201700101",
                    "Hist": 160,
                    "Current": 165
                },
                {
                    "Item": "4",
                    "AwdDate": "201600401",
                    "Hist": 173,
                    "Current": 177
                }

            ]
        };    

编辑2这里是我为这里提供的解决方案的尝试。但是,在控制器的onInit()函数中包含了这一点之后,什么也不会出现。

代码语言:javascript
复制
var regressionData = [];
for (var i = 0; i < 10; i++) {
regressionData[i] = [oData.ProductCollection[i].Current, oData.ProductCollection[i].Hist];
            };

regression('linear', regressionData);  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-25 09:58:40

不幸的是,您有一些固有的限制,即图表。即:

  • 您不能在图表中添加“斜”参考线(=趋势线)。您只能通过参考线特性添加垂直/水平的。查看这方面的(非直观的)文档:即图表文档 (查看plotArea.referenceLine.line;它只是一个数字=将是一条水平/垂直线,取决于您的图表类型或方向)。
  • 不能将散点图与线状图组合在一起。如果您查看相同的即图表文档,您将看到在组合“章节”的左边,没有“线散射”组合。

此外,您的X值(AwDate)是字符串的ABAP样式日期.更恰当的做法是使用日期/时间图来正确显示它们。无论如何,当您使用日期时,回归更有意义。否则,您的数据是“绝对的”,唯一可以进行回归的方法是对它们进行排序,并在X轴上考虑它们的等距--如果您有非等距数据(如您的示例中的数据),则不会像通常所希望的那样。

我的建议是:

  • 使用线状图而不是散点图(这样也可以显示趋势线)。
  • 将ABAP样式的字符串转换为日期,这样就可以使用timeseries图表.在示例数据( btw:"201600401". )中有一些“错误”日期
  • 做任何你想要的回归,并把它作为一个单独的“趋势”系列。您将需要为其他系列中的每个点在“趋势”系列上计算一个点(基本上,您必须向ProductCollection中的每一行添加一个“趋势”属性)。如果您使用的是OData模型,那么您需要切换到JSON客户端模型,或者使用格式化程序进行一些丑陋的变通。

按照要求,我在这里做了一个示例实现:https://jsfiddle.net/93mx0yvt/23/。我从这里借用的回归算法:https://dracoblue.net/dev/linear-least-squares-in-javascript/。守则的要点是:

代码语言:javascript
复制
// format for parsing the ABAP-style dates
var oFormat = DateFormat.getDateInstance({
  pattern: "yyyyMMdd"
});

// maps an ProductCollection entry to a new object
// which has the parsed date (and only the needed attributes)
var fnMapData = function(oEntry) {
  return {
    date: oFormat.parse(oEntry.AwdDate),
    current: oEntry.Current,
    historical: oEntry.Hist
  };
};

var fnProcessData = function(oD) {
  var aEntries = oD.ProductCollection.map(fnMapData),
    aXs = aEntries.map(function(oE) { // get the Xs
      // we take the millis to be able to do arithmetics
      return oE.date.getTime(); 
    }),
    aYs = aEntries.map(function(oE) { // get the Ys (hist)
      return oE.historical;
    }),
    //changed the function to only return only result Ys
    aRs = findLineByLeastSquares(aXs, aYs);

  //save the Ys into the result
  for (var i = 0; i < aEntries.length; ++i) {
    aEntries[i].trend = aRs[i];
  }
  return {
    data: aEntries
  };
};

然后,您可以使用fnProcessData函数在JSONModel中返回的数据,然后在此基础上构建一个简单的多序列日期/时间线图。

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

https://stackoverflow.com/questions/42939004

复制
相关文章

相似问题

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