Home > Enterprise >  How to add trendline to line chart highcharts in JSON
How to add trendline to line chart highcharts in JSON

Time:09-01

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:

{
    "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 trend line to this line chart? is there any built in parameter to add trend line?

CodePudding user response:

First, load and initialize indicators and trendline modules. Then add new trendline series. For example:

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

Live demo: https://codesandbox.io/s/highcharts-react-demo-fork-wubgnd?file=/demo.jsx

API Reference: https://api.highcharts.com/highstock/series.trendline

  • Related