Skip to content
Browse files

Merge branch 'martins_fixes'

Conflicts:
	PlotKit/Base.js
	PlotKit/Canvas.js
	PlotKit/Layout.js
	PlotKit/SweetCanvas.js
  • Loading branch information...
2 parents 237db1c + cd59e37 commit a7c28f1e8046f884a2dc8e2bf88a739c79441c40 Martin Kleppmann committed Apr 13, 2009
Showing with 59 additions and 76 deletions.
  1. +1 −1 PlotKit/Base.js
  2. +15 −18 PlotKit/Canvas.js
  3. +6 −6 PlotKit/EasyPlot.js
  4. +24 −41 PlotKit/Layout.js
  5. +8 −7 PlotKit/SVG.js
  6. +5 −3 PlotKit/SweetCanvas.js
View
2 PlotKit/Base.js
@@ -143,7 +143,7 @@ MochiKit.Base.update(PlotKit.Base, {
colorScheme: function() {
var mb = MochiKit.Base;
- var mc = MochiKit.Color
+ var mc = MochiKit.Color;
var scheme = ["red", "orange", "yellow", "green", "cyan", "blue", "purple", "magenta"];
var makeColor = function(name) {
View
33 PlotKit/Canvas.js
@@ -191,7 +191,7 @@ PlotKit.CanvasRenderer.prototype._renderBarChartWrap = function(data, plotFunc)
var context = this.element.getContext("2d");
var colorCount = this.options.colorScheme.length;
var colorScheme = this.options.colorScheme;
- var setNames = MochiKit.Base.keys(this.layout.datasets);
+ var setNames = this.layout.datasetNames;
var setCount = setNames.length;
for (var i = 0; i < setCount; i++) {
@@ -243,7 +243,7 @@ PlotKit.CanvasRenderer.prototype._renderLineChart = function() {
var context = this.element.getContext("2d");
var colorCount = this.options.colorScheme.length;
var colorScheme = this.options.colorScheme;
- var setNames = MochiKit.Base.keys(this.layout.datasets);
+ var setNames = this.layout.datasetNames;
var setCount = setNames.length;
var bind = MochiKit.Base.bind;
var partial = MochiKit.Base.partial;
@@ -279,16 +279,13 @@ PlotKit.CanvasRenderer.prototype._renderLineChart = function() {
this.area.h * point.y + this.area.y);
};
MochiKit.Iter.forEach(this.layout.points, partial(addPoint, ctx), this);
- if (this.options.shouldFill) {
- ctx.lineTo(this.area.w + this.area.x,
- this.area.h + this.area.y);
- ctx.lineTo(this.area.x, this.area.y + this.area.h);
- ctx.closePath();
- }
};
if (this.options.shouldFill) {
bind(makePath, this)(context);
+ context.lineTo(this.area.w + this.area.x, this.area.h + this.area.y);
+ context.lineTo(this.area.x, this.area.y + this.area.h);
+ context.closePath();
context.fill();
}
if (this.options.shouldStroke) {
@@ -336,7 +333,7 @@ PlotKit.CanvasRenderer.prototype._renderPieChart = function() {
context.arc(centerx, centery, radius,
slices[i].startAngle - Math.PI/2,
slices[i].endAngle - Math.PI/2,
- false);
+ slices[i].fraction == 1);
context.lineTo(centerx, centery);
context.closePath();
};
@@ -408,7 +405,7 @@ PlotKit.CanvasRenderer.prototype._renderAxis = function() {
context.closePath();
context.stroke();
- var label = DIV(labelStyle, tick[1]);
+ var label = MochiKit.DOM.DIV(labelStyle, tick[1]);
label.style.top = (y - this.options.axisLabelFontSize) + "px";
label.style.left = (x - this.options.padding.left - this.options.axisTickSize) + "px";
label.style.textAlign = "right";
@@ -441,7 +438,7 @@ PlotKit.CanvasRenderer.prototype._renderAxis = function() {
context.closePath();
context.stroke();
- var label = DIV(labelStyle, tick[1]);
+ var label = MochiKit.DOM.DIV(labelStyle, tick[1]);
label.style.top = (y + this.options.axisTickSize) + "px";
label.style.left = (x - this.options.axisLabelWidth/2) + "px";
label.style.textAlign = "center";
@@ -537,7 +534,7 @@ PlotKit.CanvasRenderer.prototype._renderPieAxis = function() {
attrib["top"] = (labely - this.options.axisLabelFontSize) + "px";
}
- var label = DIV({'style': attrib}, this.layout.xticks[i][1]);
+ var label = MochiKit.DOM.DIV({'style': attrib}, this.layout.xticks[i][1]);
this.xlabels.push(label);
MochiKit.DOM.appendChildNodes(this.container, label);
}
@@ -630,16 +627,16 @@ PlotKit.CanvasRenderer.prototype.onmouseover = function(e) {
var layoutObject = this._resolveObject(e);
var eventObject = this._createEventObject(layoutObject, e);
if (eventObject != null)
- signal(this, "onmouseover", eventObject);
+ MochiKit.Signal.signal(this, "onmouseover", eventObject);
};
PlotKit.CanvasRenderer.prototype.onmouseout = function(e) {
var layoutObject = this._resolveObject(e);
var eventObject = this._createEventObject(layoutObject, e);
if (eventObject == null)
- signal(this, "onmouseout", e);
+ MochiKit.Signal.signal(this, "onmouseout", e);
else
- signal(this, "onmouseout", eventObject);
+ MochiKit.Signal.signal(this, "onmouseout", eventObject);
};
@@ -653,13 +650,13 @@ PlotKit.CanvasRenderer.prototype.onmousemove = function(e) {
}
if ((layoutObject != null) && (this.event_isinside == null))
- signal(this, "onmouseover", eventObject);
+ MochiKit.Signal.signal(this, "onmouseover", eventObject);
if ((layoutObject == null) && (this.event_isinside != null))
- signal(this, "onmouseout", eventObject);
+ MochiKit.Signal.signal(this, "onmouseout", eventObject);
if ((layoutObject != null) && (this.event_isinside != null))
- signal(this, "onmousemove", eventObject);
+ MochiKit.Signal.signal(this, "onmousemove", eventObject);
this.event_isinside = layoutObject;
//log("move", x, y);
View
12 PlotKit/EasyPlot.js
@@ -61,27 +61,27 @@ PlotKit.EasyPlot = function(style, options, divElem, datasources) {
}
// load data sources
- if (isArrayLike(datasources)) {
+ if (MochiKit.Base.isArrayLike(datasources)) {
for (var i = 0; i < datasources.length; i++) {
if (typeof(datasources[i]) == "string") {
this.deferredCount++;
// load CSV via ajax
var d = MochiKit.Async.doSimpleXMLHttpRequest(datasources[i]);
d.addCallback(MochiKit.Base.bind(PlotKit.EasyPlot.onDataLoaded, this));
}
- else if (isArrayLike(datasources[i])) {
+ else if (MochiKit.Base.isArrayLike(datasources[i])) {
this.layout.addDataset("data-" + i, datasources[i]);
}
}
}
- else if (!isUndefinedOrNull(datasources)) {
+ else if (!MochiKit.Base.isUndefinedOrNull(datasources)) {
throw "Passed datasources are not Array like";
}
// setup canvas to render
if (CanvasRenderer.isSupported()) {
- this.element = CANVAS({"id": this.divElem.getAttribute("id") + "-canvas",
+ this.element = MochiKit.DOM.CANVAS({"id": this.divElem.getAttribute("id") + "-canvas",
"width": this.width,
"height": this.height}, "");
this.divElem.appendChild(this.element);
@@ -97,7 +97,7 @@ PlotKit.EasyPlot = function(style, options, divElem, datasources) {
this.renderer = new SweetSVGRenderer(this.element, this.layout, options);
}
- if ((this.deferredCount == 0) && (PlotKit.Base.keys(this.layout.datasets).length > 0)) {
+ if ((this.deferredCount == 0) && (this.layout.datasetNames.length > 0)) {
this.layout.evaluate();
this.renderer.clear();
this.renderer.render();
@@ -120,7 +120,7 @@ PlotKit.EasyPlot.onDataLoaded = function(request) {
this.layout.addDataset("data-ajax-" + this.deferredCount, table);
this.deferredCount--;
- if ((this.deferredCount == 0) && (PlotKit.Base.keys(this.layout.datasets).length > 0)) {
+ if ((this.deferredCount == 0) && (this.layout.datasetNames.length > 0)) {
this.layout.evaluate();
this.renderer.clear();
this.renderer.render();
View
65 PlotKit/Layout.js
@@ -63,7 +63,8 @@ PlotKit.Layout = function(style, options) {
"yNumberOfTicks": 5,
"xTickPrecision": 1,
"yTickPrecision": 1,
- "pieRadius": 0.4
+ "pieRadius": 0.4,
+ "piePercentages": true
};
// valid external options : TODO: input verification
@@ -103,12 +104,12 @@ PlotKit.Layout = function(style, options) {
// internal states
this.datasets = new Array();
+ this.datasetNames = new Array();
this.minxdelta = 0;
this.xrange = 1;
this.yrange = 1;
this.hitTestCache = {x2maxy: null};
-
};
// --------------------------------------------------------------------
@@ -118,10 +119,13 @@ PlotKit.Layout = function(style, options) {
PlotKit.Layout.prototype.addDataset = function(setname, set_xy) {
this.datasets[setname] = set_xy;
+ if (MochiKit.Base.findValue(this.datasetNames, setname) < 0)
+ this.datasetNames.push(setname);
};
PlotKit.Layout.prototype.removeDataset = function(setname, set_xy) {
delete this.datasets[setname];
+ this.datasetNames = MochiKit.Base.filter(function(name) { return name != setname; }, this.datasetNames);
};
PlotKit.Layout.prototype.addDatasetFromTable = function(name, tableElement, xcol, ycol, lcol) {
@@ -243,21 +247,13 @@ PlotKit.Layout.prototype.hitTest = function(x, y) {
}
else if (this.style == "pie") {
- x = 2 * x - 0.5;
var dist = Math.sqrt((y-0.5)*(y-0.5) + (x-0.5)*(x-0.5));
if (dist > this.options.pieRadius)
return null;
// TODO: actually doesn't work if we don't know how the Canvas
// lays it out, need to fix!
- var angle = 0.0;
- if (y < 0.5 && x < 0.5) {
- angle = Math.atan2(y - 0.5, x - 0.5) + 5 * Math.PI/2;
- }
- else {
- angle = Math.atan2(y - 0.5, x - 0.5) + Math.PI/2;
- }
-
+ var angle = Math.atan2(y - 0.5, x - 0.5) - Math.PI/2;
for (var i = 0; i < this.slices.length; i++) {
var slice = this.slices[i];
if (slice.startAngle < angle && slice.endAngle >= angle)
@@ -354,9 +350,7 @@ PlotKit.Layout.prototype._uniqueXValues = function() {
// Create the bars
PlotKit.Layout.prototype._evaluateBarCharts = function() {
- var items = PlotKit.Base.items;
-
- var setCount = items(this.datasets).length;
+ var setCount = this.datasetNames.length;
// work out how far separated values are
var xdelta = 10000000;
@@ -397,8 +391,8 @@ PlotKit.Layout.prototype._evaluateBarCharts = function() {
// add all the rects
this.bars = new Array();
- var i = 0;
- for (var setName in this.datasets) {
+ for (var i = 0; i < this.datasetNames.length; i++) {
+ var setName = this.datasetNames[i];
var dataset = this.datasets[setName];
if (PlotKit.Base.isFuncLike(dataset)) continue;
for (var j = 0; j < dataset.length; j++) {
@@ -417,15 +411,12 @@ PlotKit.Layout.prototype._evaluateBarCharts = function() {
this.bars.push(rect);
}
}
- i++;
}
};
// Create the horizontal bars
PlotKit.Layout.prototype._evaluateHorizBarCharts = function() {
- var items = PlotKit.Base.items;
-
- var setCount = items(this.datasets).length;
+ var setCount = this.datasetNames.length;
// work out how far separated values are
var xdelta = 10000000;
@@ -460,8 +451,8 @@ PlotKit.Layout.prototype._evaluateHorizBarCharts = function() {
// add all the rects
this.bars = new Array();
- var i = 0;
- for (var setName in this.datasets) {
+ for (var i = 0; i < this.datasetNames.length; i++) {
+ var setName = this.datasetNames[i];
var dataset = this.datasets[setName];
if (PlotKit.Base.isFuncLike(dataset)) continue;
for (var j = 0; j < dataset.length; j++) {
@@ -487,21 +478,18 @@ PlotKit.Layout.prototype._evaluateHorizBarCharts = function() {
this.bars.push(rect);
}
}
- i++;
}
};
// Create the line charts
PlotKit.Layout.prototype._evaluateLineCharts = function() {
- var items = PlotKit.Base.items;
-
- var setCount = items(this.datasets).length;
+ var setCount = this.datasetNames.length;
// add all the rects
this.points = new Array();
- var i = 0;
- for (var setName in this.datasets) {
+ for (var i = 0; i < this.datasetNames.length; i++) {
+ var setName = this.datasetNames[i];
var dataset = this.datasets[setName];
if (PlotKit.Base.isFuncLike(dataset)) continue;
dataset.sort(function(a, b) { return MochiKit.Base.compare(parseFloat(a[0]), parseFloat(b[0])); });
@@ -526,21 +514,19 @@ PlotKit.Layout.prototype._evaluateLineCharts = function() {
this.points.push(point);
}
}
- i++;
}
};
// Create the pie charts
PlotKit.Layout.prototype._evaluatePieCharts = function() {
- var map = PlotKit.Base.map;
- var items = PlotKit.Base.items;
+ var map = MochiKit.Base.map;
var sum = MochiKit.Iter.sum;
var getter = MochiKit.Base.itemgetter;
- var setCount = items(this.datasets).length;
+ var setCount = this.datasetNames.length;
// we plot the y values of the first dataset
- var dataset = items(this.datasets)[0][1];
+ var dataset = this.datasets[this.datasetNames[0]];
var total = sum(map(getter(1), dataset));
this.slices = new Array();
@@ -688,7 +674,7 @@ PlotKit.Layout.prototype._evaluatePieTicks = function() {
if (slice) {
if (isNil(label))
label = tick.v.toString();
- label = SPAN(null, label, " (" + formatter(slice.fraction) + ")");
+ if (this.options.piePercentages) label += " (" + formatter(slice.fraction) + ")";
this.xticks.push([tick.v, label]);
}
}
@@ -713,25 +699,22 @@ PlotKit.Layout.prototype._regenerateHitTestCache = function() {
var map = MochiKit.Base.map;
// generate a lookup table for x values to y values
- var setNames = keys(this.datasets);
- for (var i = 0; i < setNames.length; i++) {
- var dataset = this.datasets[setNames[i]];
+ for (var i = 0; i < this.datasetNames.length; i++) {
+ var dataset = this.datasets[this.datasetNames[i]];
for (var j = 0; j < dataset.length; j++) {
var xval = dataset[j][0];
var yval = dataset[j][1];
if (this.hitTestCache.xlookup[xval])
- this.hitTestCache.xlookup[xval].push([yval, setNames[i]]);
+ this.hitTestCache.xlookup[xval].push([yval, this.datasetNames[i]]);
else
- this.hitTestCache.xlookup[xval] = [[yval, setNames[i]]];
+ this.hitTestCache.xlookup[xval] = [[yval, this.datasetNames[i]]];
}
}
for (var x in this.hitTestCache.xlookup) {
var yvals = this.hitTestCache.xlookup[x];
this.hitTestCache.x2maxy[x] = listMax(map(itemgetter(0), yvals));
}
-
-
};
// --------------------------------------------------------------------
View
15 PlotKit/SVG.js
@@ -54,6 +54,7 @@ PlotKit.SVGRenderer.SVGNS = 'http://www.w3.org/2000/svg';
PlotKit.SVGRenderer.prototype.__init__ = function(element, layout, options) {
var isNil = MochiKit.Base.isUndefinedOrNull;
+ var Color = MochiKit.Color.Color;
// default options
this.options = {
@@ -149,7 +150,7 @@ PlotKit.SVGRenderer.prototype._renderBarOrLine = function(data, plotFunc, startF
var colorCount = this.options.colorScheme.length;
var colorScheme = this.options.colorScheme;
- var setNames = MochiKit.Base.keys(this.layout.datasets);
+ var setNames = this.layout.datasetNames;
var setCount = setNames.length;
for (var i = 0; i < setCount; i++) {
@@ -179,7 +180,7 @@ PlotKit.SVGRenderer.prototype._renderBarOrLine = function(data, plotFunc, startF
plotFunc(attrs, obj);
};
- MochiKit.Iter.forEach(data, bind(forEachFunc, this));
+ MochiKit.Iter.forEach(data, MochiKit.Base.bind(forEachFunc, this));
if (endFunc)
endFunc(attrs);
}
@@ -337,7 +338,7 @@ PlotKit.SVGRenderer.prototype._renderAxis = function() {
this._drawLine(x, y, x - 3, y, lineAttrs);
if (this.options.axisLabelUseDiv) {
- var label = DIV(labelStyle, tick[1]);
+ var label = MochiKit.DOM.DIV(labelStyle, tick[1]);
label.style.top = (y - this.options.axisLabelFontSize) + "px";
label.style.left = (x - this.options.padding.left + this.options.axisTickSize) + "px";
label.style.textAlign = "left";
@@ -378,7 +379,7 @@ PlotKit.SVGRenderer.prototype._renderAxis = function() {
}
};
- MochiKit.Iter.forEach(this.layout.yticks, bind(drawTick, this));
+ MochiKit.Iter.forEach(this.layout.yticks, MochiKit.Base.bind(drawTick, this));
}
this._drawLine(this.area.x, this.area.y, this.area.x, this.area.y + this.area.h, lineAttrs);
@@ -392,7 +393,7 @@ PlotKit.SVGRenderer.prototype._renderAxis = function() {
this._drawLine(x, y, x, y + this.options.axisTickSize, lineAttrs);
if (this.options.axisLabelUseDiv) {
- var label = DIV(labelStyle, tick[1]);
+ var label = MochiKit.DOM.DIV(labelStyle, tick[1]);
label.style.top = (y + this.options.axisTickSize) + "px";
label.style.left = (x - this.options.axisLabelWidth/2) + "px";
label.style.textAlign = "center";
@@ -417,7 +418,7 @@ PlotKit.SVGRenderer.prototype._renderAxis = function() {
}
};
- MochiKit.Iter.forEach(this.layout.xticks, bind(drawTick, this));
+ MochiKit.Iter.forEach(this.layout.xticks, MochiKit.Base.bind(drawTick, this));
}
this._drawLine(this.area.x, this.area.y + this.area.h, this.area.x + this.area.w, this.area.y + this.area.h, lineAttrs)
@@ -526,7 +527,7 @@ PlotKit.SVGRenderer.prototype._renderPieAxis = function() {
}
if (this.options.axisLabelUseDiv) {
- var label = DIV({'style': attrib}, this.layout.xticks[i][1]);
+ var label = MochiKit.DOM.DIV({'style': attrib}, this.layout.xticks[i][1]);
this.xlabels.push(label);
MochiKit.DOM.appendChildNodes(this.container, label);
}
View
8 PlotKit/SweetCanvas.js
@@ -80,7 +80,7 @@ PlotKit.SweetCanvasRenderer.prototype._renderBarChart = function() {
var colorCount = this.options.colorScheme.length;
var colorScheme = this.options.colorScheme;
- var setNames = PlotKit.Base.keys(this.layout.datasets);
+ var setNames = this.layout.datasetNames;
var setCount = setNames.length;
var chooseColor = function(name) {
@@ -135,10 +135,11 @@ PlotKit.SweetCanvasRenderer.prototype._renderBarChart = function() {
};
PlotKit.SweetCanvasRenderer.prototype._renderLineChart = function() {
+ var Color = MochiKit.Color.Color;
var context = this.element.getContext("2d");
var colorCount = this.options.colorScheme.length;
var colorScheme = this.options.colorScheme;
- var setNames = PlotKit.Base.keys(this.layout.datasets);
+ var setNames = this.layout.datasetNames;
var setCount = setNames.length;
var bind = MochiKit.Base.bind;
@@ -160,7 +161,7 @@ PlotKit.SweetCanvasRenderer.prototype._renderLineChart = function() {
ctx_.lineTo(this.area.w * point.x + this.area.x,
this.area.h * point.y + this.area.y);
};
- MochiKit.Iter.forEach(this.layout.points, partial(addPoint, ctx), this);
+ MochiKit.Iter.forEach(this.layout.points, MochiKit.Base.partial(addPoint, ctx), this);
ctx.lineTo(this.area.w + this.area.x,
this.area.h + this.area.y);
ctx.lineTo(this.area.x, this.area.y + this.area.h);
@@ -203,6 +204,7 @@ PlotKit.SweetCanvasRenderer.prototype._renderLineChart = function() {
};
PlotKit.SweetCanvasRenderer.prototype._renderPieChart = function() {
+ var Color = MochiKit.Color.Color;
var context = this.element.getContext("2d");
var colorCount = this.options.colorScheme.length;

0 comments on commit a7c28f1

Please sign in to comment.
Something went wrong with that request. Please try again.