2017-01-10 44 views
0

我想通过在我的Rails应用程序上使用Chartkick宝石和谷歌图表来绘制线性函数(如y = ax + b)。通过使用Chartkick宝石和Google图表绘制线性函数

'a'和'b'将作为参数给出。

问题是我试图使用谷歌图表出来的斜线'a'和'b'来实现无限线图,我不知道如何以适当的方式实现这一点。

我想出了用两点绘制折线图(-b/a,0)和(0,b)的方案,并给它添加了趋势线选项。

<!-- y = 0.55x floatify(2,@datas[4][2]) + 1.47 floatify(2,@datas[3][2]) --> 
<%= line_chart [ 
       {name: "targets", 
       data: [ 
        ["#{-(@datas[3][2].to_f/@datas[4][2].to_f).round(2)}",0], 
        [0,"#{@datas[3][2].to_f}"] 
       ] 
       }, 
      ], 
      library: { 
       title: "#{@simplereg.variable_name}", 
       subtitle: "#{@simplereg.data_name}", 
       legend: "right", 
       vAxis: {title: 'average'}, 
       trendlines: { 
        0: { 
         type: 'exponential', 
         visibleInLegend: true 
        } 
       } 
      } 
%> 

但是,与谷歌图表文档所述不同,rails不能读取'0'值作为趋势线的选项。 - >https://developers.google.com/chart/interactive/docs/gallery/trendlines 我不知道如何解决此代码的工作。

请帮助我...我不介意抛弃此代码并以完全不同的方式编写。

提前感谢。

回答

0

使用类似的基本上不是个符号0以下

<%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %> 
 

 

 
<%= line_chart [ 
 
        {name: "targets", 
 
        data: [ 
 
         ["-2/3",0], 
 
         [0,"2"] 
 
        ] 
 
        }, 
 
       ], 
 
       library: { 
 
        title: "ABC", 
 
        subtitle: "XYZ", 
 
        legend: "right", 
 
        vAxis: {title: 'average'}, 
 
        trendlines: { 
 
         "0"=> { 
 
     type: 'exponential', 
 
     visibleInLegend: true 
 
    } 
 
    } 
 
    } 
 
%>

:用 “0”=>

+0

太谢谢你了! –