3

我正在使用用 Highcharts 创建的图表。它们在我的本地开发环境中运行良好,但在 Heroku 上没有显示图表。div 只是空的:

<div id="dashboard_chart_75"></div>

我正在使用lazy_high_charts gem,这是我的application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require general.js.erb
//= require_tree .
//= require turbolinks
//= require highcharts/highcharts
//= require highcharts/highcharts-more
//= require social-share-button
//= require ckeditor-jquery

这是生成图表的代码:

spider_chart = LazyHighCharts::HighChart.new('graph') do |f|
  f.chart(polar: true, type: 'line', height: @height)
  f.pane(size: @size)
  f.colors(['#C41D21','#1c77bb'])
  f.xAxis(
    categories: categories.map{ |c| [@survey.id,c.id, c.name] },
    tickmarkPlacement: 'on',
    lineWidth: 0,
    labels: {
      style: {
        fontSize: '12px',
        weight: 'bold',
        width: '100%'
      },
      formatter: %|function() {return '<a href="' + window.location.hostname + '/dashboards/spiders?level=2&survey_id=' + this.value[0] + '&category_id=' + this.value[1] + '">' + this.value[2] + '</a>';}|.js_code }
      )
  f.yAxis(gridLineInterpolation: 'polygon', lineWidth: 0, min: 0, max: 10,tickInterval: 1, tickWidth: 20)
  f.tooltip(enabled: false)
  f.series(name: "Gemiddelde van gewenste score", data: norm_scores, pointPlacement: 'on')
  f.series(name: "Gemiddelde van huidige score", data: current_scores, pointPlacement: 'on')
  f.legend(enabled: @legend, align: 'center', verticalAlign: 'top', y: 10, layout: 'vertical')
end

在生产中,我在控制台中看到此错误:

Uncaught ReferenceError: Highcharts is not defined at window.onload

指的是这一行:

window.chart_spider_chart = new Highcharts.Chart(options);

这可能是什么原因?

4

2 回答 2

1

您似乎真的遇到了一个简单的资产版本控制问题,没有理由让 highcharts 在生产中表现不同。

尝试使用以下命令清除您的资产并强制预编译:

$ bundle exec rake assets:clobber
$ RAILS_ENV=production bundle exec rake assets:precompile

然后,添加并提交您的public/assets文件夹以推送到 Heroku。我在相信部署预编译会按预期工作时遇到了很多麻烦。有时编译资产会出现错误,您需要在预编译之前更改文件以感知更改。

如果这不起作用

您可能在生产中遇到 gem 的问题,可能它在 gemfile 的开发块内,或者甚至=// require_tree .可能在 highcharts lib 初始化之前插入该代码的任何其他要求,弄乱脚本的顺序会有所帮助(即使您粘贴的代码没有反映这一点)

于 2018-02-16T02:54:43.537 回答
0

尝试.sprockets-manifest在目录中查找文件,public如果找到则将其删除(这是一个隐藏文件)。如果您在本地编译资产并意外提交了该文件,则可能会阻止 Heroku 上的资产编译。

您还可以尝试通过将资产版本插入config/production.rb. IE换成config.assets.version = 1.0然后1.1push。

于 2018-02-16T08:36:07.703 回答