Skip to content

Commit

Permalink
adds specific "margin" property to .axes( )
Browse files Browse the repository at this point in the history
  • Loading branch information
davelandry committed Jul 6, 2016
1 parent fd87481 commit 21b5e34
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 43 deletions.
11 changes: 11 additions & 0 deletions src/viz/methods/axes.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
process = require "../../core/methods/process/margin.coffee"
rendering = require "../../core/methods/rendering.coffee"

module.exports =
Expand All @@ -7,6 +8,16 @@ module.exports =
stroke:
color: "#ccc"
width: 1
margin:
accepted: [Number, Object, String]
process: (value) ->

value = @value if value is undefined
userValue = value
process value, this
userValue

value: 10
mirror:
accepted: [Boolean]
deprecates: ["mirror_axis", "mirror_axes"]
Expand Down
4 changes: 2 additions & 2 deletions src/viz/types/area.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ area = (vars) ->
d.d3plus = {} unless d.d3plus

d.d3plus.x = discrete.scale.viz fetchValue vars, d, discrete.value
d.d3plus.x += vars.axes.margin.left
d.d3plus.x += vars.axes.margin.viz.left

d.d3plus.y = opposite.scale.viz fetchValue vars, d, opposite.value
d.d3plus.y += vars.axes.margin.top
d.d3plus.y += vars.axes.margin.viz.top

if d.d3plus.merged instanceof Array
point.d3plus.merged = [] unless point.d3plus.merged
Expand Down
6 changes: 3 additions & 3 deletions src/viz/types/bar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ bar = (vars) ->

discreteVal = fetchValue(vars, d, vars[discrete].value)
d.d3plus[discrete] = vars[discrete].scale.viz discreteVal
d.d3plus[discrete] += vars.axes.margin[cMargin] + mod
d.d3plus[discrete] += vars.axes.margin.viz[cMargin] + mod

length = base - value

d.d3plus[opposite] = base - length/2
d.d3plus[opposite] += vars.axes.margin[oMargin] unless vars.axes.stacked
d.d3plus[opposite] += vars.axes.margin.viz[oMargin] unless vars.axes.stacked

delete d.d3plus.r
d.d3plus[w] = newSize
Expand All @@ -145,7 +145,7 @@ bar = (vars) ->

d.d3plus.init[opposite] = oppMethod.scale.viz zero
d.d3plus.init[opposite] -= d.d3plus[opposite]
d.d3plus.init[opposite] += vars.axes.margin[oMargin]
d.d3plus.init[opposite] += vars.axes.margin.viz[oMargin]

d.d3plus.init[w] = d.d3plus[w]

Expand Down
4 changes: 2 additions & 2 deletions src/viz/types/box.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ box = (vars) ->

discrete = vars.axes.discrete
opposite = vars.axes.opposite
disMargin = if discrete is "x" then vars.axes.margin.left else vars.axes.margin.top
oppMargin = if opposite is "x" then vars.axes.margin.left else vars.axes.margin.top
disMargin = if discrete is "x" then vars.axes.margin.viz.left else vars.axes.margin.viz.top
oppMargin = if opposite is "x" then vars.axes.margin.viz.left else vars.axes.margin.viz.top
h = if discrete is "x" then "height" else "width"
w = if discrete is "x" then "width" else "height"
space = vars.axes[w] / vars[discrete].ticks.values.length
Expand Down
54 changes: 26 additions & 28 deletions src/viz/types/helpers/graph/includes/plot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ uniques = require "../../../../../util/uniques.coffee"
module.exports = (vars, opts) ->

# Reset margins
vars.axes.margin = resetMargins vars
vars.axes.margin.viz =
top: vars.axes.margin.top
right: vars.axes.margin.right
bottom: vars.axes.margin.bottom
left: vars.axes.margin.left
vars.axes.height = vars.height.viz
vars.axes.width = vars.width.viz
axes = if vars.width.viz > vars.height.viz then ["y", "y2", "x", "x2"] else ["x", "x2", "y", "y2"]
Expand Down Expand Up @@ -116,28 +120,20 @@ module.exports = (vars, opts) ->
new Date(d).getTime()

# Calculate padding for tick labels
labelPadding vars unless vars.small
if vars.small
vars.axes.width -= (vars.axes.margin.viz.left + vars.axes.margin.viz.right)
vars.axes.height -= (vars.axes.margin.viz.top + vars.axes.margin.viz.bottom)
for axis in axes
vars[axis].label.height = 0
else
labelPadding vars unless vars.small

# Create SVG Axes
for axis in axes
vars[axis].axis.svg = createAxis(vars, axis)

return

resetMargins = (vars) ->
if vars.small
# return
top: 0
right: 0
bottom: 0
left: 0
else
# return
top: 10
right: 10
bottom: 10
left: 10

labelPadding = (vars) ->

xDomain = vars.x.scale.viz.domain()
Expand Down Expand Up @@ -182,8 +178,9 @@ labelPadding = (vars) ->
if vars[axis].ticks.labels.value
vars[axis].ticks.hidden = false
yAxisWidth = d3.max fontSizes(yText,yAttrs), (d) -> d.width
yAxisWidth = Math.ceil yAxisWidth + vars.labels.padding
vars.axes.margin[margin] += yAxisWidth
if yAxisWidth
yAxisWidth = Math.ceil yAxisWidth + vars.labels.padding
vars.axes.margin.viz[margin] += yAxisWidth

else
vars[axis].ticks.hidden = true
Expand All @@ -200,10 +197,10 @@ labelPadding = (vars) ->
else
vars[axis].label.height = 0
if vars[axis].label.value
vars.axes.margin[margin] += vars[axis].label.height
vars.axes.margin[margin] += vars[axis].label.padding * 2
vars.axes.margin.viz[margin] += vars[axis].label.height
vars.axes.margin.viz[margin] += vars[axis].label.padding * 2

vars.axes.width -= (vars.axes.margin.left + vars.axes.margin.right)
vars.axes.width -= (vars.axes.margin.viz.left + vars.axes.margin.viz.right)
vars.x.scale.viz.range buckets([0, vars.axes.width], xDomain.length)
vars.x2.scale.viz.range buckets([0, vars.axes.width], x2Domain.length) if x2Domain

Expand Down Expand Up @@ -290,14 +287,15 @@ labelPadding = (vars) ->
xAxisHeight = Math.ceil xAxisHeight
vars[axis].ticks.maxHeight = xAxisHeight
vars[axis].ticks.maxWidth = xAxisWidth
vars.axes.margin[margin] += xAxisHeight + vars.labels.padding
if xAxisHeight
vars.axes.margin.viz[margin] += xAxisHeight + vars.labels.padding
lastTick = vars[axis].ticks.visible[vars[axis].ticks.visible.length - 1]
rightLabel = vars[axis].scale.viz lastTick
rightLabel += xAxisWidth/2 + vars.axes.margin.left
rightLabel += xAxisWidth/2 + vars.axes.margin.viz.left
if rightLabel > vars.width.value
rightMod = rightLabel - vars.width.value + vars.axes.margin.right
rightMod = rightLabel - vars.width.value + vars.axes.margin.viz.right
vars.axes.width -= rightMod
vars.axes.margin.right += rightMod
vars.axes.margin.viz.right += rightMod
else
vars[axis].ticks.hidden = true

Expand All @@ -313,10 +311,10 @@ labelPadding = (vars) ->
else
vars[axis].label.height = 0
if vars[axis].label.value
vars.axes.margin[margin] += vars[axis].label.height
vars.axes.margin[margin] += vars[axis].label.padding * 2
vars.axes.margin.viz[margin] += vars[axis].label.height
vars.axes.margin.viz[margin] += vars[axis].label.padding * 2

vars.axes.height -= (vars.axes.margin.top + vars.axes.margin.bottom)
vars.axes.height -= (vars.axes.margin.viz.top + vars.axes.margin.viz.bottom)

vars.x.scale.viz.range buckets([0, vars.axes.width], xDomain.length)
vars.x2.scale.viz.range buckets([0, vars.axes.width], x2Domain.length) if x2Domain
Expand Down
6 changes: 3 additions & 3 deletions src/viz/types/helpers/graph/includes/svg.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports = (vars) ->
.attr "font-weight", vars[axis].lines.font.weight

# Draw Plane Group
planeTrans = "translate(" + vars.axes.margin.left + "," + vars.axes.margin.top + ")"
planeTrans = "translate(" + vars.axes.margin.viz.left + "," + vars.axes.margin.viz.top + ")"
plane = vars.group.selectAll("g#d3plus_graph_plane").data [0]
plane.transition().duration vars.draw.timing
.attr "transform", planeTrans
Expand Down Expand Up @@ -231,7 +231,7 @@ module.exports = (vars) ->
if axis.indexOf("x") is 0
vars.width.viz/2
else
-(vars.axes.height/2+vars.axes.margin.top)
-(vars.axes.height/2+vars.axes.margin.viz.top)
.attr "y",
if axis is "x"
vars.height.viz - vars[axis].label.height/2 - vars[axis].label.padding
Expand Down Expand Up @@ -294,7 +294,7 @@ module.exports = (vars) ->
if vars[axis].value

axisLabel = vars[axis].label.fetch vars
labelData = if axisData and axisLabel then [0] else []
labelData = if axisData and axisLabel and !vars.small then [0] else []
affixes = vars.format.affixes.value[vars[axis].value]
if axisLabel and !vars[axis].affixes.value and affixes
sep = vars[axis].affixes.separator.value
Expand Down
2 changes: 1 addition & 1 deletion src/viz/types/helpers/graph/stack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = (vars, data) ->
flip = vars[stacked].scale.viz 0
scale = vars[stacked].scale.value
opposite = if stacked is "x" then "y" else "x"
margin = if stacked is "y" then vars.axes.margin.top else vars.axes.margin.left
margin = if stacked is "y" then vars.axes.margin.viz.top else vars.axes.margin.viz.left
offset = if scale is "share" then "expand" else "zero"

stack = d3.layout.stack()
Expand Down
4 changes: 2 additions & 2 deletions src/viz/types/line.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ line = (vars) ->
else
d.d3plus.x2 = true
d.d3plus.x = vars.x2.scale.viz fetchValue(vars, d, vars.x2.value)
d.d3plus.x += vars.axes.margin.left
d.d3plus.x += vars.axes.margin.viz.left

yval = fetchValue(vars, d, vars.y.value)

Expand All @@ -39,7 +39,7 @@ line = (vars) ->
else
d.d3plus.y2 = true
d.d3plus.y = vars.y2.scale.viz fetchValue(vars, d, vars.y2.value)
d.d3plus.y += vars.axes.margin.top
d.d3plus.y += vars.axes.margin.viz.top

if vars.axes.stacked then stack vars, data else data

Expand Down
4 changes: 2 additions & 2 deletions src/viz/types/scatter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ scatter = (vars) ->
for d in vars.data.viz

d.d3plus.x = vars.x.scale.viz fetchValue(vars, d, vars.x.value)
d.d3plus.x += vars.axes.margin.left
d.d3plus.x += vars.axes.margin.viz.left

d.d3plus.y = vars.y.scale.viz fetchValue(vars, d, vars.y.value)
d.d3plus.y += vars.axes.margin.top
d.d3plus.y += vars.axes.margin.viz.top

if typeof vars.size.value is "number" or !vars.size.value
d.d3plus.r = vars.axes.scale 0
Expand Down

0 comments on commit 21b5e34

Please sign in to comment.