无法在Coffeescript上的Rickshaw Graph上显示月份名称

时间:2014-02-13 21:33:54

标签: javascript coffeescript sinatra rickshaw dashing

我正在使用名为Dashing的基于Sinatra的框架开发项目。我的项目的一部分是使用RickShaw Graph创建图形。我的问题是我无法在X轴上显示月份名称和日期。我正在使用coffeescript来呈现这些值。以下是图表的代码:

class Dashing.Graph extends Dashing.Widget



@accessor 'points', Dashing.AnimatedValue

  @accessor 'current', ->
    return @get('displayedValue') if @get('displayedValue')
    points = @get('points')
    if points
      points[points.length - 1].y

#ready is triggered when ever the page is loaded.
  ready: ->
    container = $(@node).parent()
    # Gross hacks. Let's fix this.
    width = (Dashing.widget_base_dimensions[0] * container.data("sizex")) + Dashing.widget_margins[0] * 2 * (container.data("sizex") - 1)
    height = (Dashing.widget_base_dimensions[1] * container.data("sizey"))
    @graph = new Rickshaw.Graph(
      element: @node
      width: width
      height: height
      renderer: @get("graphtype")
      series: [
        {
        color: "#fff",
        data: [{x:0, y:0}]
        }
      ]
    )

    @graph.series[0].data = @get('points') if @get('points')
    time = new Rickshaw.Fixtures.Time()
    days = time.unit("day")

    x_axis = new Rickshaw.Graph.Axis.Time(
      graph: @graph
      timeUnit: days
    )
    y_axis = new Rickshaw.Graph.Axis.Y(graph: @graph, tickFormat: Rickshaw.Fixtures.Number.formatKMBT)
    @graph.render()

根据我对Rickshaw Graph API的理解,这里有:

https://github.com/shutterstock/rickshaw/blob/master/src/js/Rickshaw.Fixtures.Time.js

它表示您可以指定单位名称。所以对于这个例子我只是出于测试原因而使用“day”,但这似乎并没有起作用。 任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

您可以指定一个timeFixture来驱动x轴上的刻度应该如何标记。

var xAxis = new Rickshaw.Graph.Axis.Time( {
  graph: graph,
  timeFixture: new Rickshaw.Fixtures.Time.Local()
} );

灯具负责显示的时间范围,并触发日期/时间格式的适当详细程度,例如:放大几年到几个小时。

您也可以创建自己的'时间装置'并将其设置在那里,看看Rickshaw.Fixtures.Time或Rickshaw.Fixtures.Time.Local

或者,指定固定间距,即“您始终要显示的单位:

var timeFixture = new Rickshaw.Fixtures.Time();
var unitHour = timeFixture.unit('hour');
var xAxis = new Rickshaw.Graph.Axis.Time( {
  graph: graph,
  timeUnit: unitHour,
  timeFixture: timeFixture
} );