Skip to content

Commit

Permalink
Custom dateFormat for string x-values. #90
Browse files Browse the repository at this point in the history
  • Loading branch information
oesmith committed Oct 19, 2012
1 parent 42099ea commit 1d4c7f0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
26 changes: 15 additions & 11 deletions lib/morris.line.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,20 @@ class Morris.Line
parseTime: true
preUnits: ''
postUnits: ''
dateFormat: (x) -> new Date(x).toString()
dateFormat: null
xLabels: 'auto'
xLabelFormat: null

# Update the data series and redraw the chart.
#
setData: (data, redraw = true) ->
# shallow copy & sort data
# shallow copy & sort data (if required)
@options.data = data.slice(0)
if @options.parseTime
@options.data.sort (a, b) =>
(a[@options.xkey] < b[@options.xkey]) - (b[@options.xkey] < a[@options.xkey])
else
@options.data.reverse()
# extract labels
@columnLabels = $.map @options.data, (d) => d[@options.xkey]

# extract series data
@series = []
Expand All @@ -118,19 +116,25 @@ class Morris.Line
else null
@series.push(series_data)

# extract labels
@columnLabels = $.map @options.data, (d) => d[@options.xkey]

# translate x labels into nominal dates
# note: currently using decimal years to specify dates
if @options.parseTime
@xvals = $.map @columnLabels, (x) -> Morris.parseDate x
else
@xvals = [(@columnLabels.length-1)..0]
# translate column labels, if they're timestamps

# format column labels
if @options.parseTime
@columnLabels = $.map @columnLabels, (d) =>
if typeof d is 'number'
@options.dateFormat(d)
else
d
if @options.dateFormat
@columnLabels = $.map @xvals, (d) => @options.dateFormat(d)
else
@columnLabels = $.map @columnLabels, (d) =>
# default formatter for numeric timestamp labels
if typeof d is 'number' then new Date(d).toString() else d

# calculate horizontal range of the graph
@xmin = Math.min.apply null, @xvals
@xmax = Math.max.apply null, @xvals
if @xmin is @xmax
Expand Down
28 changes: 16 additions & 12 deletions morris.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d4c7f0

Please sign in to comment.