如何添加趋势线到高图表 [英] How to add trend line to high charts

查看:112
本文介绍了如何添加趋势线到高图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是高图表代码。

 <!DOCTYPE HTML& 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< title> Highcharts示例< / title>

< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js>< ; / script>
< script type =text / javascript>
$(function(){
$('#container')。highcharts({
title:{
text:'RNA',
x:-20 / / center
},
subtitle:{
text:'Outage Reasons',
x:-20
},
xAxis:{
类别:['18 -Jul-14','19 -Jul-14','20 -Jul-14','21 -Jul-14','22 -Jul-14','23 -Jul-14' ,
'24-Jul-14','25-Jul-14','26 -Jul-14','27 -Jul-14','28 -Jul-14','29- 14']
},
yAxis:{
title:{
text:'Outage'
},
plotLines:[{
value:0,
width:1,
color:'#808080'
}]
},
tooltip:{
valueSuffix:''
},
legend:{
layout:'vertical',
align:'right',
verticalAlign:'middle',
borderWidth:0
},
系列:[{
name:'RNA - CP(无线网络可用性 - 客户感知)',
数据:[99.75,99.77,99.78,99.84,99.82, 99.82,99.76,99.78,99.8,99.65,99.94,99.8]
}],
credit:{
enabled:false
}
}
});


< / script>
< / head>
< body>
< script src =highcharts.js>< / script>
< script src =exporting.js>< / script>

< div id =containerstyle =min-width:310px; height:400px; margin:0 auto>< / div&

< / body>
< / html>

我想为此图表添加趋势线,我在google中搜索并从此链接获取代码:把所有这些一起。


This is the high chart graph code.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
$(function () {
        $('#container').highcharts({
            title: {
                text: 'RNA',
                x: -20 //center
            },
            subtitle: {
                text: 'Outage Reasons',
                x: -20
            },
            xAxis: {
                categories: ['18-Jul-14', '19-Jul-14', '20-Jul-14', '21-Jul-14', '22-Jul-14', '23-Jul-14',
                    '24-Jul-14', '25-Jul-14', '26-Jul-14', '27-Jul-14', '28-Jul-14', '29-Jul-14']
            },
            yAxis: {
                title: {
                    text: 'Outage'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: ''
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [{
                name: 'RNA - CP ( Radio Network Availability - Customer Perceived)',
                data: [99.75, 99.77, 99.78, 99.84, 99.82, 99.82, 99.76, 99.78, 99.8, 99.65, 99.94, 99.8]
            }],
            credits: {
                enabled: false
            }
        });
    });


        </script>
    </head>
    <body>
<script src="highcharts.js"></script>
<script src="exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    </body>
</html>

I want to add trend line for this chart, i searched in google and got the code from this link : https://github.com/virtualstaticvoid/highcharts_trendline and i added the trend line code like this .

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

         <script type="text/javascript" src="regression.js"></script>

          <script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>


        <script type="text/javascript">
$(function () {
        $('#container').highcharts({
            title: {
                text: 'RNA',
                x: -20 //center
            },
            subtitle: {
                text: 'Outage Reasons',
                x: -20
            },
            xAxis: {
                categories: ['18-Jul-14', '19-Jul-14', '20-Jul-14', '21-Jul-14', '22-Jul-14', '23-Jul-14',
                    '24-Jul-14', '25-Jul-14', '26-Jul-14', '27-Jul-14', '28-Jul-14', '29-Jul-14']
            },
            yAxis: {
                title: {
                    text: 'Outage'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: ''
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [{
                name: 'RNA - CP ( Radio Network Availability - Customer Perceived)',
                data: [99.75, 99.77, 99.78, 99.84, 99.82, 99.82, 99.76, 99.78, 99.8, 99.65, 99.94, 99.8]
            }],
            credits: {
                enabled: false
            }
        });
    });


     var sourceData = [
          [18-Jul-14, 99.75], [19-Jul-14, 99.77],
          [20-Jul-14, 99.78], [21-Jul-14, 99.84],
          [22-Jul-14, 99.82], [23-Jul-14, 99.82],
          [24-Jul-14, 99.76], [25-Jul-14, 99.78],
          [26-Jul-14, 99.8], [27-Jul-14, 99.65],
          [28-Jul-14, 99.94], [29-Jul-14, 99.8]
      ];

      var chart_linear = new highcharts.Chart({
          chart: {
            renderTo: 'container'
          },
          plotOptions: {
            series: {
              enableMouseTracking: false
            }
          },
          series: [{
            type: 'scatter',
            data: sourceData
          },
          {
            type: 'line',
            marker: { enabled: false },
            /* function returns data for trend-line */
            data: (function() {
              return fitData(sourceData).data;
            })()
          }]
      });


        </script>
    </head>
    <body>

<script src="exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    </body>
</html>

and the trend line that acts according to the x-axis and y-axis values but on the x-axis values are date wise and the y-axis values are numbers ,please find the screen shot for the first code :

And this the trend line snapshot. please find.

please share any one of the code that i would like to add the two graphs in the same page , i have tried but the x-axis and y-axis values are different for the two graphs.

Thanks.

解决方案

Numerous problems.

1.) You didn't really integrate the regresssion code into your plot, you just cut/pasted from the example and are over-drawing your plot. You need to add the regression line as a second series to your plot:

        series: [{
            name: 'RNA - CP ( Radio Network Availability - Customer Perceived)',
            data: sourceData
        },{
            type: 'line',
            marker: { enabled: false },
            /* function returns data for trend-line */
            data: (function() {
              return fitData(sourceData).data;
            })()
        }]

2.) This is not valid javascript:

 var sourceData = [
      [18-Jul-14, 99.75], [19-Jul-14, 99.77],
      [20-Jul-14, 99.78], [21-Jul-14, 99.84],
      [22-Jul-14, 99.82], [23-Jul-14, 99.82],
      [24-Jul-14, 99.76], [25-Jul-14, 99.78],
      [26-Jul-14, 99.8], [27-Jul-14, 99.65],
      [28-Jul-14, 99.94], [29-Jul-14, 99.8]
  ];

Those are strings and they aren't quoted. Regardless, strings won't cut it for the regression, it needs numbers. Since your dates are really categories, just use:

 var sourceData = [
      [0, 99.75], [1, 99.77],
      [2, 99.78], [3, 99.84],
      [4, 99.82], [5, 99.82],
      [6, 99.76], [7, 99.78],
      [8, 99.8], [9, 99.65],
      [10, 99.94], [11, 99.8]
  ];

3.) Your series name is way too long for a right side legend (it squished the plot). In my example I moved it to the bottom.

Here's a example putting all this together.

这篇关于如何添加趋势线到高图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆