## Tuesday, October 4, 2011

### Dygraphs Coordinate Systems, 3/3

This is the last of three posts about the coordinate systems used throughout Dygraphs.
• Part 1: Introduction and the Dygraph Data Coordinate System
• Part 2: DOM Coordinate System
• Part 3: Percentage Coordinate System
Dygraph Percentage Coordinate System

Update: Yes, it's really the fraction of the drawable area (from zero to one) rather than a percentage (0 to 100.) If we rename the methods, I'll update this post.

There’s an additional coordinate system which represents percentages from the edges of the drawing area, relative to upper-left corner. These aren’t often required for public API, but can be useful when having to work strictly in the viewable area of the canvas. This is different than the DOM coordinate system which has its origin off the viewable area of the canvas. In this case, this coordinate system is all about the viewable area of the canvas.

These are accessible by the methods toPercentXCoord and toPercentYCoord.

toPercentXCoord takes a data X data value and converts it to a percentage from the left edge of the canvas. This diagram gives you an idea:
And to hit the point home, here’s a snippet of code which demonstrates values created by this function:

for (x = 1; x <= 9; x++) {
console.log(x, '=>', g.toPercentXCoord(x));
}
1 "=>" 0
2 "=>" 0.125
3 "=>" 0.25
4 "=>" 0.375
5 "=>" 0.5
6 "=>" 0.625
7 "=>" 0.75
8 "=>" 0.875
9 "=>" 1

toPercentYCoord does the same thing along the Y axis, but goes from top to bottom.
The drawing area along the y-axis goes from 0-131, so let’s look at toPercentYCoord.

g.toPercentYCoord(0)
1

g.toPercentYCoord(131)
0

g.toPercentYCoord(120)
0.08396946564885496

g.toPercentYCoord(65.5)
0.5

Converting percentages back to data coordinates can be performed with this function:

`function(pct) {`
`  var range = g.xAxisRange();`
`  return ((range - range) * pct) + range;`
`}`

and a similar one for y-axis conversion. Validation is left up to the reader.

## Summary

Each coordinate system has its own origin:
• The Data coordinate system has its initial origin in the lower left corner, with increasing values moving up and to the right. This doesn’t mean the lower-right corner is (0,0), since the graph can be moved or resized.
• The DOM coordinate system has its origin in the upper left corner of the unclipped canvas, with increasing values moving down and to the right. This is not the same corner as the percentage coordinate system. However, you are limited to drawing inside the graph area.
• The percentage coordinate system has its origin in the upper left corner of the clipped canvas, with increasing values moving down and to the right.

API that can be used for translating between these coordinate systems are:
• toDomXCoord and toDomYCoord convert data coordinate values to DOM coordinate values
• toDataXCoord and toDataYCoord convert DOM coordinate values to data coordinate values
• toPercentXCoord and toPercentYCoord convert data coordinate values to canvas percentages.