简体   繁体   English

当我将数据源用作 JSON - Highcharts.js 时如何在折线图中添加回归线

[英]How to add a regression line to line chart when i use data source as JSON - Highcharts.js react

im currently struggling to create a trend line to a line chart.我目前正在努力为折线图创建趋势线。 found some old solutions and those things didn't work for me.找到了一些旧的解决方案,这些东西对我不起作用。

Current code(Json file):当前代码(Json 文件):

[
{
    "key": "003",
    "title": "Detections",
    "type": "chart",
    "chartData": {
        "chart": {
            "type": "line",
            "renderTo": "container"
        },
        "title": {
            "text": ""
        },
        "subtitle": {
            "text": ""
        },
    
        
        
        "xAxis": {
            "categories": ["Jan 7", "Jan 14", "Jan 21", "Jan 28",
        "Feb 4","Feb 11","Feb 18","Feb 25",
        "Mar 4","Mar 11","Mar 18","Mar 28",
        "Apr 1","Apr 8","Apr 15","Apr 22","Apr 29",
        "May 6","May 13","May 20","May 27"
        
        
        ]
        },

        
        "colors": [ "#f89c1b"],

        "yAxis": {
            "title": {
                "text": "Number of Exits"
            }
        },
        "plotOptions": {
            "line": {
                "dataLabels": {
                    "enabled": true
                },
                "enableMouseTracking": false
            }
        },
        "series": [{
            "name": "Week",
            "data": [60,12,29,48,
            24,31,15,37,
            32,16,22,29,
            21,13,9,14,15,
            10,12,13,7]
        }
    ]
        

    },
    "index": 2,
    "defaultWidth": "100%"
}
]

Current chart:当前图表:

enter image description here在此处输入图像描述

How do i add a regression line to this?我如何为此添加回归线?

i found this link and it shows to add regression line in JS.But is there a way to do this using JSON?我找到了这个链接,它显示在 JS 中添加回归线。但是有没有办法使用 JSON 来做到这一点?

Similar as for other indicators, you need to load and initialize proper modules (indicators and regressions in this case):与其他指标类似,您需要加载和初始化适当的模块(在本例中为指标和回归):

// Import Highcharts
import Highcharts from "highcharts/highstock";
import indicators from "highcharts/indicators/indicators";
import regressions from "highcharts/indicators/regressions";

indicators(Highcharts);
regressions(Highcharts);

And add new trendline series:并添加新的趋势线系列:

series: [
  {
    id: "mainSeries",
    name: "Week",
    data: [...]
  },
  {
    type: "linearRegression",
    linkedTo: "mainSeries"
  }
]

Live demo: https://codesandbox.io/s/highcharts-react-demo-fork-vjni21?file=/demo.jsx现场演示: https://codesandbox.io/s/highcharts-react-demo-fork-vjni21?file=/demo.jsx

API Reference: https://api.highcharts.com/highstock/series.linearregression API 参考: https://api.highcharts.com/highstock/series.linearregression

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM