0

我正在尝试使用 HighCharts 沿 xAxis 显示日期(即按天)。我正在使用带有 Rails 的Lazy High Charts gem 来执行此操作。对于 HighCharts 的“数据”参数,我传入一个嵌套数组,其中包含 [[日期,收入],[日期,收入],[日期,收入]...等]。

带有收入的 yAxis 工作正常,但 xAxis 中的相应日期却没有。

这是控制器代码:

def graph_orders

        sales_and_date_array = []

        Order.revenue_to_array(sales_and_date_array)

        puts sales_and_date_array.inspect

        # http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/series/data-array-of-arrays-datetime/

        @chart = LazyHighCharts::HighChart.new('graph') do |f|
          f.title(:text => "Lono Sales")
          f.xAxis(
            type: 'datetime'
           )

          f.series(:name => "Lono Sales Revenue", :yAxis => 0, :data => sales_and_date_array)

          f.yAxis [
            {:title => {:text => "Revenue", :margin => 70} },
            {:title => {:text => "Revenue"}, :opposite => true},
          ]

          f.legend(:align => 'right', :verticalAlign => 'top', :y => 75, :x => -50, :layout => 'vertical',)
          f.chart({:defaultSeriesType=>"line"})
        end

    end

下面是检查“sales_and_date_array”的样子:

[["2014-06-12", 208.28], ["2014-06-11", 416.56], ["2014-06-11", 624.84], ["2014-06-11", 833.12], ["2014-06-10", 1041.4], ["2014-06-09", 1249.68], ["2014-06-08", 1457.96], ["2014-06-08", 1666.24], ["2014-06-07", 1874.52], ["2014-06-07", 2082.8], ["2014-06-07", 2291.08],....etc

以下是图形输出当前的样子:

在此处输入图像描述

有什么想法吗?

4

2 回答 2

2

确切的例子:

@chart = LazyHighCharts::HighChart.new('graph') do |f|
  f.title(:text => 'History')
  f.xAxis(:type => 'datetime',
          :title => {
            text: 'Date'
          })
  f.yAxis(:title => {
            text: 'Values'
          })
  f.series(:name => 'Value',
           :data => YourModel
                      .map { |i| [i.created_at.to_time.to_i * 1000,
                                  i.your_value] })

  f.chart({:defaultSeriesType => 'line'})
于 2015-11-26T09:25:41.883 回答
1

Highcharts 不接受该格式的日期。您可以传递 Date.UTC 对象,也可以以 javascript 纪元格式(以毫秒为单位)传递日期。

参考:

于 2014-06-14T12:13:52.657 回答