Merge pull request #477 from autodidacticon/master

free positioned x coords, custom x labels
This commit is contained in:
Jelte Fennema 2015-01-17 19:20:44 +01:00
commit 570f7561d5
4 changed files with 34 additions and 3 deletions

View file

@ -47,6 +47,9 @@ With node installed, install [grunt](https://github.com/cowboy/grunt) using
`npm install -g grunt-cli`, and then the rest of the test/build dependencies `npm install -g grunt-cli`, and then the rest of the test/build dependencies
with `npm install` in the morris.js project folder. with `npm install` in the morris.js project folder.
Additionally, [bower](http://bower.io/) is required for for retrieving additional test dependencies.
Install it with `npm install -g bower` and then `bower install` in the morris project folder.
Once you're all set up, you can compile, minify and run the tests using `grunt`. Once you're all set up, you can compile, minify and run the tests using `grunt`.
Note: I'm experimenting with using perceptual diffs to catch rendering Note: I'm experimenting with using perceptual diffs to catch rendering

View file

@ -96,6 +96,7 @@ class Morris.Grid extends Morris.EventEmitter
gridDefaults: gridDefaults:
dateFormat: null dateFormat: null
axes: true axes: true
freePosition: false
grid: true grid: true
gridLineColor: '#aaa' gridLineColor: '#aaa'
gridStrokeWidth: 0.5 gridStrokeWidth: 0.5
@ -164,6 +165,10 @@ class Morris.Grid extends Morris.EventEmitter
ret.label = @options.dateFormat ret.x ret.label = @options.dateFormat ret.x
else if typeof ret.label is 'number' else if typeof ret.label is 'number'
ret.label = new Date(ret.label).toString() ret.label = new Date(ret.label).toString()
else if @options.freePosition
ret.x = parseFloat(row[@options.xkey])
if @options.xLabelFormat
ret.label = @options.xLabelFormat ret
else else
ret.x = index ret.x = index
if @options.xLabelFormat if @options.xLabelFormat
@ -188,7 +193,7 @@ class Morris.Grid extends Morris.EventEmitter
yval yval
ret ret
if @options.parseTime if @options.parseTime or @options.freePosition
@data = @data.sort (a, b) -> (a.x > b.x) - (b.x > a.x) @data = @data.sort (a, b) -> (a.x > b.x) - (b.x > a.x)
# calculate horizontal range of the graph # calculate horizontal range of the graph

View file

@ -193,6 +193,8 @@ class Morris.Line extends Morris.Grid
labels = [[@data[0].label, @data[0].x]] labels = [[@data[0].label, @data[0].x]]
else else
labels = Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelFormat) labels = Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelFormat)
else if @options.customLabels
labels = ([row.label, row.x] for row in @options.customLabels)
else else
labels = ([row.label, row.x] for row in @data) labels = ([row.label, row.x] for row in @data)
labels.reverse() labels.reverse()

View file

@ -64,8 +64,29 @@ describe 'Morris.Line', ->
labels: ['dontcare'] labels: ['dontcare']
dateFormat: (d) -> dateFormat: (d) ->
x = new Date(d) x = new Date(d)
"#{x.getYear()}/#{x.getMonth()+1}/#{x.getDay()}" "#{x.getYear()+1900}/#{x.getMonth()+1}/#{x.getDay()+1}"
chart.data.map((x) -> x.label).should == ['2012/1/1', '2012/1/2'] chart.data.map((x) -> x.label).should.eql(['2012/1/1', '2012/1/2'])
it 'should use user-defined labels', ->
chart = Morris.Line
element: 'graph'
data: [{x:1,y:2}],
xkey: 'x',
ykeys: ['y'],
labels: ['dontcare']
customLabels: [{x:3,label:'label'}]
chart.options.customLabels.map((x) -> x.label).should.eql(['label'])
it 'should use relative x-coordinates', ->
chart = Morris.Line
element: 'graph'
data: [{x:1,y:2}, {x:1.2,y:2}],
xkey: 'x',
ykeys: ['y'],
labels: ['dontcare']
parseTime: false
freePosition: true
[chart.data[1].x - chart.data[0].x].should.not.equal(1)
describe 'rendering lines', -> describe 'rendering lines', ->
beforeEach -> beforeEach ->