Hi Dmitry,
I ran with your line graph example (
http://raphaeljs.com/
analytics.html), and here is some code I added to label and scale the
y-axis. I'm using Prototype 1.6.0.3. This code isn't groundbreaking by
far, but maybe it'll save you a little time. I'm a boob (big + noob),
but I'd like to help where I can...
/**
* I return the appropriate scale for the y-axis.
* For example, a line graph with values between 0 and 1 should
have tick marks every 0.1 units while a graph
* with values between 1,000 and 2,000 should have tick marks
every 500 units.
*
* @param iMaxDiff int the difference between the largest and
smallest value on the graph
* @return int
*
*/
getYAxisScale: function (iMaxDiff) {
var iScale; // integer scale of y-axis
if (iMaxDiff < 500) {
if (iMaxDiff < 10) {
if (iMaxDiff < 1) {
iScale = 0.1;
} else {
iScale = 2;
}
} else {
if (iMaxDiff < 100) {
iScale = 5;
} else {
iScale = 25;
}
}
} else {
if (iMaxDiff < 1000) {
iScale = 50;
} else {
if (iMaxDiff < 10000) {
iScale = 500;
} else {
iScale = 1000;
}
}
}
return iScale;
},
For the y-axis, I found myself comparing small differences in big
numbers, and starting the y-axis from zero didn't make for a good
chart. In addition, I wanted the y-axis to begin and end at whole
numbers that made sense (instead of some Excel charts that start at
1,267 and run to 1,789 - who can remember that?).
The Raphael object (this.oR), width, height, gutter sizes, colors, and
text attributes are all defined as properties of my Chart_TimeSeries
class. I put a single character in front of most of my vars so I can
remember their type (i=int, s=str, o=obj, etc). In addition, the class
has an array of values (this.valuesArr), and an array of dates
(this.datesArr).
I just included the parts of the fxn that changed...
/**
* I draw this line chart.
*
*/
drawChart: function () {
var X, // width of one point (eg, if there are 5
values and chart width is 500px, each point gets 100px)
Y, // height of one unit of value (eg, if
value is 213 and chart height is 300px, each y 'unit' is 1.4px)
x, // x-coordinate of point
y, // y-coordinate of point
iMax, // min value in values array
iMin, // max value in values array
diffMinArr, // the difference between each value and the minimum
value
iMaxDiff, // the biggest difference (the peak-to-peak
amplitude)
iScale, // scale of the y-axis
iCeil, // closest multiple of scale that's larger than
max value
iFloor, // closest multiple of scale that's smaller than
min value
iWall, // difference between ceiling and floor
iTicks, // number of tick marks on left axis
diffArr; // the difference between each value and the
computed floor
// set grid and y-axis vars
iMax = Math.max.apply(Math, this.valuesArr);
iMin = Math.min.apply(Math, this.valuesArr);
diffMinArr = [];
for (var i=0, len=this.valuesArr.length; i<len; ++i) {
diffMinArr.push(this.valuesArr[i] - iMin);
}
iMaxDiff = iMax - iMin;
iScale = this.getYAxisScale(iMaxDiff);
iCeil = Math.ceil(iMax/iScale) * iScale;
iFloor = Math.floor(iMin/iScale) * iScale;
iWall = iCeil - iFloor;
iTicks = iWall / iScale;
X = (this.iWidth - this.iLeftgutter) / this.datesArr.length;
Y = (this.iHeight - this.iBottomgutter - this.iTopgutter) / iWall;
// adjust differences to new floor
diffArr = [];
for (i=0, len=this.valuesArr.length; i<len; ++i) {
diffArr.push(this.valuesArr[i] - iFloor);
}
// draw grid
// only change here is the number of grid lines change
with the data set
this.oR.drawGrid(this.iLeftgutter + X * 0.5, this.iTopgutter,
this.iWidth - this.iLeftgutter - X, this.iHeight - this.iTopgutter -
this.iBottomgutter, this.valuesArr.length - 1, iTicks, "#ccc");
// add y-axis labels
for (i=0; i<=iTicks; ++i) {
this.oR.text(this.iLeftgutter + X * 0.5 - 15, this.iTopgutter + i *
iScale * Y, (iCeil - i * iScale).comma()).attr(this.oYLabel).toBack();
}
// set chart vars
... no changes from your example ...
}
Hope that helps.
On 22 June, 21:21, Dmitry Baranovskiy <
dmitry.baranovs...@gmail.com>
wrote: