var render = function() {
var w = element[0]().offsetWidth;
var h = element[0]().offsetHeight;
var width = w - options.margin.left - options.margin.right;
var height = h - options.margin.top - options.margin.bottom;
//先要移除之前的记录
d3.select(element[0]()).select("svg").remove();
//svg的位置、大小
var svg = d3.select(element[0]()).append("svg")
.attr("width", width + options.margin.left + options.margin.right)
.attr("height", height + options.margin.top + options.margin.bottom)
.append("g")
.attr("transform", "translate(" + options.margin.left + "," + options.margin.top + ")");
//找到data中每一个维度的坐标,方便绘制
var xu = {};
var x = []();
var yu = {};
var y = []();
for (var i in scope.data) {
if (typeof(xu[scope.data[i]().x]) == "undefined") {
x.push(scope.data[i]().x);
}
xu[scope.data[i]().x] = 0;
if (typeof(yu[scope.data[i]().y]) == "undefined") {
y.push(scope.data[i]().y);
}
yu[scope.data[i]().y] = 0;
}
for (d in scope.data) {
scope.data[d]().xIndex = x.indexOf(scope.data[d]().x);
scope.data[d]().yIndex = y.indexOf(scope.data[d]().y);
}
//根据维度来决定热力图每一块的长宽,更dynamic
var xGridSize = Math.floor(width / x.length);
var yGridSize = Math.floor(height / y.length);
var legendElementWidth = Math.floor(width \* options.legendWidth / (options.buckets));
var legendElementHeight = height / 20;
// 绘制y轴旁的元素名
var yLabels = svg.selectAll(".yLabel")
.data(y)
.enter().append("text")
.text(function (d) { return d; })
.attr("x", 0)
.attr("y", function (d, i) { return i \* yGridSize; })
.style("text-anchor", "end")
.attr("transform", "translate(-6," + yGridSize / 1.5 + ")")
.attr("class", function (d, i) { return ("yLabel axis"); });
// 绘制x轴旁的元素名
var xLabels = svg.selectAll(".xLabel")
.data(x)
.enter().append("text")
.text(function(d) { return d; })
.attr("y", function(d, i) { return i \* xGridSize; })
.attr("x", 0)
.style("text-anchor", "start")
.attr("transform", "rotate(-90) translate(10, " + xGridSize / 2 + ")")
.attr("class", function(d, i) { return ("xLabel axis"); });
var colorScales = []();
if (options.breaks != null && options.breaks.length \> 0) {
for (b in options.colors) {
colorScales.push(d3.scale.quantile()
.domain([0, options.buckets - 1, d3.max(scope.data, function(d) { return d.value; })]())
.range(options.colors[b]()));
}
} else {
colorScales.push(d3.scale.quantile()
.domain([0, options.buckets - 1, d3.max(scope.data, function(d) { return d.value; })]())
.range(options.colors));
}
//绘制热力图中的最小cell单位,并制定颜色,动作
var cards = svg.selectAll(".square")
.data(scope.data);
cards.enter().append("rect")
.filter(function(d) { return d.value != null })
.attr("x", function(d) { return d.xIndex \* xGridSize; })
.attr("y", function(d) { return d.yIndex \* yGridSize; })
.attr("class", "square")
.attr("width", xGridSize)
.attr("height", yGridSize)
.on("click", function(d) { scope.dispatch.click(d); })
.on("mouseover", function(d) { scope.dispatch.mouseover(d); })
.on("mouseout", function(d) { scope.dispatch.mouseout(d); })
.on("mousemove", function(d) { scope.dispatch.mousemove(d); })
.style("fill", "#ffffff");
//增加动画效果 cards.transition().duration(options.duration).style("fill", function(d) {
if (options.customColors && options.customColors.hasOwnProperty(d.value)) {
return options.customColors[d.value]();
} else if (options.breaks != null && options.breaks.length \> 0) {
for (b in options.breaks) {
if (d.xIndex \< options.breaks[b]()) {
return colorScales[b]()(d.value);
}
}
return colorScales[options.breaks.length]()(d.value);
} else {
return colorScales[0]()(d.value);
}
});
cards.exit().remove();
if (options.legend) {
var legend = svg.selectAll(".legend")
.data([0]().concat(colorScales[0]().quantiles()).concat(d3.max(scope.data, function (d) { return d.value; })), function(d) { return d; });
legend.enter().append("g").attr("class", "legend");
//下面的参考数值对应的颜色
legend.append("rect")
.attr("x", function(d, i) { return legendElementWidth \* i; })
.attr("y", height \* 1.05)
.attr("width", legendElementWidth)
.attr("height", legendElementHeight)
.style("fill", function(d, i) { return options.colors[i](); })
.style("visibility", function(d, i) { return(i \< options.buckets ? "visible" : "hidden") });
//参考数值
legend.append("text")
.attr("class", "legendLabel")
.text(function(d,i) { return (0.1\*i+0.1).toFixed(1); })
.attr("x", function(d, i) { return legendElementWidth \* i; })
.attr("y", height \* 1.15)
.style("text-anchor", "middle");
legend.exit().remove();
}
};