Skip to content

Commit

Permalink
Merge branch '1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 31, 2012
2 parents 11f59fd + f388016 commit 9ff1a4e
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 42 deletions.
90 changes: 70 additions & 20 deletions cubism.v1.js
@@ -1,5 +1,5 @@
(function(exports){ (function(exports){
var cubism = exports.cubism = {version: "1.1.0"}; var cubism = exports.cubism = {version: "1.2.0"};
var cubism_id = 0; var cubism_id = 0;
function cubism_identity(d) { return d; } function cubism_identity(d) { return d; }
cubism.option = function(name, defaultValue) { cubism.option = function(name, defaultValue) {
Expand Down Expand Up @@ -153,7 +153,7 @@ cubism.context = function() {


function cubism_context() {} function cubism_context() {}


var cubism_contextPrototype = cubism_context.prototype; var cubism_contextPrototype = cubism.context.prototype = cubism_context.prototype;


cubism_contextPrototype.constant = function(value) { cubism_contextPrototype.constant = function(value) {
return new cubism_metricConstant(this, +value); return new cubism_metricConstant(this, +value);
Expand Down Expand Up @@ -191,16 +191,30 @@ cubism_contextPrototype.graphite = function(host) {
context = this; context = this;


source.metric = function(expression) { source.metric = function(expression) {
var sum = "sum";

var metric = context.metric(function(start, stop, step, callback) { var metric = context.metric(function(start, stop, step, callback) {
var target = expression;

// Apply the summarize, if necessary.
if (step !== 1e4) target = "summarize(" + target + ",'"
+ (!(step % 36e5) ? step / 36e5 + "hour" : !(step % 6e4) ? step / 6e4 + "min" : step + "sec")
+ "','" + sum + "')";

d3.text(host + "/render?format=raw" d3.text(host + "/render?format=raw"
+ "&target=" + encodeURIComponent("alias(" + expression + ",'')") + "&target=" + encodeURIComponent("alias(" + target + ",'')")
+ "&from=" + cubism_graphiteFormatDate(start - 2 * step) // off-by-two? + "&from=" + cubism_graphiteFormatDate(start - 2 * step) // off-by-two?
+ "&until=" + cubism_graphiteFormatDate(stop - 1000), function(text) { + "&until=" + cubism_graphiteFormatDate(stop - 1000), function(text) {
if (!text) return callback(new Error("unable to load data")); if (!text) return callback(new Error("unable to load data"));
callback(null, cubism_graphiteParse(text)); callback(null, cubism_graphiteParse(text));
}); });
}, expression += ""); }, expression += "");
metric.summarize = summarize;
metric.summarize = function(_) {
sum = _;
return metric;
};

return metric; return metric;
}; };


Expand All @@ -217,13 +231,6 @@ cubism_contextPrototype.graphite = function(host) {
return host; return host;
}; };


function summarize(method) {
var step = Math.round(context.step() / 1e3);
if (step === 10) return this;
step = !(step % 3600) ? step / 3600 + "hour" : !(step % 60) ? step / 60 + "min" : step + "sec";
return source.metric("summarize(" + this + ",'" + step + "','" + method + "')");
}

return source; return source;
}; };


Expand Down Expand Up @@ -254,6 +261,8 @@ function cubism_metric(context) {


var cubism_metricPrototype = cubism_metric.prototype; var cubism_metricPrototype = cubism_metric.prototype;


cubism.metric = cubism_metric;

cubism_metricPrototype.valueAt = function() { cubism_metricPrototype.valueAt = function() {
return NaN; return NaN;
}; };
Expand Down Expand Up @@ -957,25 +966,47 @@ var cubism_axisFormatSeconds = d3.time.format("%I:%M:%S %p"),
cubism_axisFormatMinutes = d3.time.format("%I:%M %p"), cubism_axisFormatMinutes = d3.time.format("%I:%M %p"),
cubism_axisFormatDays = d3.time.format("%B %d"); cubism_axisFormatDays = d3.time.format("%B %d");
cubism_contextPrototype.rule = function() { cubism_contextPrototype.rule = function() {
var context = this; var context = this,
metric = cubism_identity;


function rule(selection) { function rule(selection) {
var id = ++cubism_id; var id = ++cubism_id;


var line = selection.append("div") var line = selection.append("div")
.datum({id: id}) .datum({id: id})
.attr("class", "line") .attr("class", "line")
.style("position", "fixed") .call(cubism_ruleStyle);
.style("top", 0)
.style("right", 0) selection.each(function(d, i) {
.style("bottom", 0) var that = this,
.style("width", "1px") id = ++cubism_id,
.style("pointer-events", "none"); metric_ = typeof metric === "function" ? metric.call(that, d, i) : metric;

if (!metric_) return;

function change(start, stop) {
var values = [];

for (var i = 0, n = context.size(); i < n; ++i) {
if (metric_.valueAt(i)) {
values.push(i);
}
}

var lines = selection.selectAll(".metric").data(values);
lines.exit().remove();
lines.enter().append("div").attr("class", "metric line").call(cubism_ruleStyle);
lines.style("left", cubism_ruleLeft);
}

context.on("change.rule-" + id, change);
metric_.on("change.rule-" + id, change);
});


context.on("focus.rule-" + id, function(i) { context.on("focus.rule-" + id, function(i) {
line line.datum(i)
.style("display", i == null ? "none" : null) .style("display", i == null ? "none" : null)
.style("left", function() { return this.parentNode.getBoundingClientRect().left + i + "px"; }); .style("left", cubism_ruleLeft);
}); });
} }


Expand All @@ -990,6 +1021,25 @@ cubism_contextPrototype.rule = function() {
} }
}; };


rule.metric = function(_) {
if (!arguments.length) return metric;
metric = _;
return rule;
};

return rule; return rule;
}; };

function cubism_ruleStyle(line) {
line
.style("position", "absolute")
.style("top", 0)
.style("bottom", 0)
.style("width", "1px")
.style("pointer-events", "none");
}

function cubism_ruleLeft(i) {
return i + "px";
}
})(this); })(this);
2 changes: 1 addition & 1 deletion cubism.v1.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ {
"name": "cubism", "name": "cubism",
"version": "1.1.0", "version": "1.2.0",
"description": "A JavaScript library for time series visualization.", "description": "A JavaScript library for time series visualization.",
"keywords": [ "keywords": [
"time series", "time series",
Expand Down
2 changes: 1 addition & 1 deletion src/context.js
Expand Up @@ -131,7 +131,7 @@ cubism.context = function() {


function cubism_context() {} function cubism_context() {}


var cubism_contextPrototype = cubism_context.prototype; var cubism_contextPrototype = cubism.context.prototype = cubism_context.prototype;


cubism_contextPrototype.constant = function(value) { cubism_contextPrototype.constant = function(value) {
return new cubism_metricConstant(this, +value); return new cubism_metricConstant(this, +value);
Expand Down
2 changes: 1 addition & 1 deletion src/cubism.js
@@ -1 +1 @@
var cubism = exports.cubism = {version: "1.1.0"}; var cubism = exports.cubism = {version: "1.2.0"};
25 changes: 16 additions & 9 deletions src/graphite.js
Expand Up @@ -4,16 +4,30 @@ cubism_contextPrototype.graphite = function(host) {
context = this; context = this;


source.metric = function(expression) { source.metric = function(expression) {
var sum = "sum";

var metric = context.metric(function(start, stop, step, callback) { var metric = context.metric(function(start, stop, step, callback) {
var target = expression;

// Apply the summarize, if necessary.
if (step !== 1e4) target = "summarize(" + target + ",'"
+ (!(step % 36e5) ? step / 36e5 + "hour" : !(step % 6e4) ? step / 6e4 + "min" : step + "sec")
+ "','" + sum + "')";

d3.text(host + "/render?format=raw" d3.text(host + "/render?format=raw"
+ "&target=" + encodeURIComponent("alias(" + expression + ",'')") + "&target=" + encodeURIComponent("alias(" + target + ",'')")
+ "&from=" + cubism_graphiteFormatDate(start - 2 * step) // off-by-two? + "&from=" + cubism_graphiteFormatDate(start - 2 * step) // off-by-two?
+ "&until=" + cubism_graphiteFormatDate(stop - 1000), function(text) { + "&until=" + cubism_graphiteFormatDate(stop - 1000), function(text) {
if (!text) return callback(new Error("unable to load data")); if (!text) return callback(new Error("unable to load data"));
callback(null, cubism_graphiteParse(text)); callback(null, cubism_graphiteParse(text));
}); });
}, expression += ""); }, expression += "");
metric.summarize = summarize;
metric.summarize = function(_) {
sum = _;
return metric;
};

return metric; return metric;
}; };


Expand All @@ -30,13 +44,6 @@ cubism_contextPrototype.graphite = function(host) {
return host; return host;
}; };


function summarize(method) {
var step = Math.round(context.step() / 1e3);
if (step === 10) return this;
step = !(step % 3600) ? step / 3600 + "hour" : !(step % 60) ? step / 60 + "min" : step + "sec";
return source.metric("summarize(" + this + ",'" + step + "','" + method + "')");
}

return source; return source;
}; };


Expand Down
2 changes: 2 additions & 0 deletions src/metric.js
Expand Up @@ -5,6 +5,8 @@ function cubism_metric(context) {


var cubism_metricPrototype = cubism_metric.prototype; var cubism_metricPrototype = cubism_metric.prototype;


cubism.metric = cubism_metric;

cubism_metricPrototype.valueAt = function() { cubism_metricPrototype.valueAt = function() {
return NaN; return NaN;
}; };
Expand Down
59 changes: 50 additions & 9 deletions src/rule.js
@@ -1,23 +1,45 @@
cubism_contextPrototype.rule = function() { cubism_contextPrototype.rule = function() {
var context = this; var context = this,
metric = cubism_identity;


function rule(selection) { function rule(selection) {
var id = ++cubism_id; var id = ++cubism_id;


var line = selection.append("div") var line = selection.append("div")
.datum({id: id}) .datum({id: id})
.attr("class", "line") .attr("class", "line")
.style("position", "fixed") .call(cubism_ruleStyle);
.style("top", 0)
.style("right", 0) selection.each(function(d, i) {
.style("bottom", 0) var that = this,
.style("width", "1px") id = ++cubism_id,
.style("pointer-events", "none"); metric_ = typeof metric === "function" ? metric.call(that, d, i) : metric;

if (!metric_) return;

function change(start, stop) {
var values = [];

for (var i = 0, n = context.size(); i < n; ++i) {
if (metric_.valueAt(i)) {
values.push(i);
}
}

var lines = selection.selectAll(".metric").data(values);
lines.exit().remove();
lines.enter().append("div").attr("class", "metric line").call(cubism_ruleStyle);
lines.style("left", cubism_ruleLeft);
}

context.on("change.rule-" + id, change);
metric_.on("change.rule-" + id, change);
});


context.on("focus.rule-" + id, function(i) { context.on("focus.rule-" + id, function(i) {
line line.datum(i)
.style("display", i == null ? "none" : null) .style("display", i == null ? "none" : null)
.style("left", function() { return this.parentNode.getBoundingClientRect().left + i + "px"; }); .style("left", cubism_ruleLeft);
}); });
} }


Expand All @@ -32,5 +54,24 @@ cubism_contextPrototype.rule = function() {
} }
}; };


rule.metric = function(_) {
if (!arguments.length) return metric;
metric = _;
return rule;
};

return rule; return rule;
}; };

function cubism_ruleStyle(line) {
line
.style("position", "absolute")
.style("top", 0)
.style("bottom", 0)
.style("width", "1px")
.style("pointer-events", "none");
}

function cubism_ruleLeft(i) {
return i + "px";
}

0 comments on commit 9ff1a4e

Please sign in to comment.