merging in pull request from @Nathanaela removing jQuery dependence, added pie chart model, some other useful changes
commit
9352d35cfc
@ -0,0 +1,6 @@
|
||||
@echo off
|
||||
copy src\intro.js /B + src\core.js /B + src\tooltip.js /B temp1.js /B
|
||||
copy src\models\*.js /B + src\charts\*.js /B temp2.js /B
|
||||
copy temp1.js /B + temp2.js /B + src\outro.js /B nv.d3.js /B
|
||||
del temp1.js
|
||||
del temp2.js
|
@ -0,0 +1,167 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8">
|
||||
<link href="../src/d3.css" rel="stylesheet" type="text/css">
|
||||
<style>
|
||||
|
||||
body {
|
||||
overflow-y:scroll;
|
||||
}
|
||||
|
||||
text {
|
||||
font: 12px sans-serif;
|
||||
}
|
||||
|
||||
</style>
|
||||
<body>
|
||||
|
||||
<svg id="test1"></svg>
|
||||
|
||||
<script src="../lib/d3.v2.js"></script>
|
||||
<script src="../nv.d3.js"></script>
|
||||
<script src="../src/models/pie.js"></script>
|
||||
<script>
|
||||
|
||||
var testdata = [
|
||||
{
|
||||
label: "One",
|
||||
y: 5
|
||||
},
|
||||
{
|
||||
label: "Two",
|
||||
y: 2
|
||||
},
|
||||
{
|
||||
label: "Three",
|
||||
y: 9
|
||||
},
|
||||
{
|
||||
label: "Four",
|
||||
y: 7
|
||||
},
|
||||
{
|
||||
label: "Five",
|
||||
y: 4
|
||||
},
|
||||
{
|
||||
label: "Six",
|
||||
y: 3
|
||||
}
|
||||
];
|
||||
|
||||
var testdata2 = [
|
||||
{
|
||||
label: "One",
|
||||
y: 10
|
||||
},
|
||||
{
|
||||
label: "Two",
|
||||
y: 5
|
||||
},
|
||||
{
|
||||
label: "Three",
|
||||
y: 3
|
||||
},
|
||||
{
|
||||
label: "Four",
|
||||
y: 7
|
||||
},
|
||||
{
|
||||
label: "Five",
|
||||
y: 2
|
||||
}
|
||||
|
||||
];
|
||||
|
||||
var td = 0;
|
||||
var a = (Math.random()*10)+1;
|
||||
if (a > 5) td = 1;
|
||||
|
||||
//Format A
|
||||
nv.addGraph({
|
||||
generate: function() {
|
||||
var width = nv.utils.windowSize().width - 40,
|
||||
height = nv.utils.windowSize().height - 40;
|
||||
|
||||
|
||||
|
||||
var chart = nv.models.pie()
|
||||
.width(width)
|
||||
.height(height)
|
||||
.title('Hi this is the chart title')
|
||||
;
|
||||
|
||||
if (td === 0) {
|
||||
d3.select("#test1")
|
||||
.attr('width', width)
|
||||
.attr('height', height)
|
||||
.datum(testdata)
|
||||
.call(chart);
|
||||
} else {
|
||||
d3.select("#test1")
|
||||
.attr('width', width)
|
||||
.attr('height', height)
|
||||
.datum(testdata2)
|
||||
.call(chart);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return chart;
|
||||
},
|
||||
callback: function(graph) {
|
||||
|
||||
graph.dispatch.on('tooltipShow', function(e) {
|
||||
var offsetElement = document.getElementById("chart"),
|
||||
left = e.pos[0],
|
||||
top = e.pos[1];
|
||||
|
||||
var content = '<h3>' + e.label + '</h3>' +
|
||||
'<p>' +
|
||||
e.value +
|
||||
'</p>';
|
||||
|
||||
nv.tooltip.show([left, top], content);
|
||||
});
|
||||
|
||||
graph.dispatch.on('tooltipHide', function(e) {
|
||||
nv.tooltip.cleanup();
|
||||
});
|
||||
|
||||
graph.dispatch.on('chartClick', function(e) {
|
||||
console.log('Click Switching to');
|
||||
if (td === 0) {
|
||||
d3.select("#test1")
|
||||
.datum(testdata2)
|
||||
.call(graph);
|
||||
td = 1;
|
||||
|
||||
} else {
|
||||
d3.select("#test1")
|
||||
.datum(testdata)
|
||||
.call(graph);
|
||||
td = 0;
|
||||
}
|
||||
});
|
||||
|
||||
graph.dispatch.on('elementClick', function(e) { console.log("Clicked on Element",e); });
|
||||
graph.dispatch.on('elementDblClick', function(e) { console.log("Double Clicked on Element",e); });
|
||||
//graph.dispatch.on('chartClick', function(e) { console.log("Clicked on chart",e); });
|
||||
|
||||
window.onresize = function() {
|
||||
var width = nv.utils.windowSize().width - 40,
|
||||
height = nv.utils.windowSize().height - 40;
|
||||
|
||||
d3.select("#test1")
|
||||
.attr('width', width)
|
||||
.attr('height', height)
|
||||
.call(
|
||||
graph
|
||||
.width(width)
|
||||
.height(height)
|
||||
)
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
</script>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1 +0,0 @@
|
||||
jquery-1.7.1.min.js
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,263 @@
|
||||
|
||||
nv.models.pie = function() {
|
||||
var margin = {top: 20, right: 20, bottom: 20, left: 20},
|
||||
width = 500,
|
||||
height = 500,
|
||||
animate = 2000,
|
||||
radius = Math.min(width-(margin.right+margin.left), height-(margin.top+margin.bottom)) / 2,
|
||||
label ='label',
|
||||
field ='y',
|
||||
id = Math.floor(Math.random() * 10000), //Create semi-unique ID in case user doesn't select one
|
||||
color = d3.scale.category20(),
|
||||
showLabels = true,
|
||||
donut = false,
|
||||
title = '';
|
||||
|
||||
var lastWidth = 0,
|
||||
lastHeight = 0;
|
||||
|
||||
|
||||
var dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'tooltipShow', 'tooltipHide');
|
||||
|
||||
function chart(selection) {
|
||||
selection.each(function(data) {
|
||||
|
||||
var svg = d3.select(this)
|
||||
.on("click", function(d,i) {
|
||||
dispatch.chartClick({
|
||||
data: d,
|
||||
index: i,
|
||||
pos: d3.event,
|
||||
id: id
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
var background = svg.selectAll('svg.margin').data([data]);
|
||||
var parent = background.enter();
|
||||
parent.append("text")
|
||||
.attr("class", "title")
|
||||
.attr("dy", ".91em")
|
||||
.attr("text-anchor", "start")
|
||||
.text(title);
|
||||
parent.append('svg')
|
||||
.attr('class','margin')
|
||||
.attr('x', margin.left)
|
||||
.attr('y', margin.top)
|
||||
.style('overflow','visible');
|
||||
|
||||
var wrap = background.selectAll('g.wrap').data([data]);
|
||||
wrap.exit().remove();
|
||||
var wEnter = wrap.enter();
|
||||
|
||||
wEnter
|
||||
.append('g')
|
||||
.attr('class', 'wrap')
|
||||
.attr('id','wrap-'+id)
|
||||
.append('g')
|
||||
.attr('class', 'pie');
|
||||
|
||||
|
||||
|
||||
wrap
|
||||
.attr('width', width) //-(margin.left+margin.right))
|
||||
.attr('height', height) //-(margin.top+margin.bottom))
|
||||
.attr("transform", "translate(" + radius + "," + radius + ")");
|
||||
|
||||
|
||||
|
||||
|
||||
var arc = d3.svg.arc()
|
||||
.outerRadius((radius-(radius / 5)));
|
||||
|
||||
if (donut) arc.innerRadius(radius / 2);
|
||||
|
||||
|
||||
// Setup the Pie chart and choose the data element
|
||||
var pie = d3.layout.pie()
|
||||
.value(function (d) { return d[field]; });
|
||||
|
||||
var slices = background.select('.pie').selectAll(".slice")
|
||||
.data(pie);
|
||||
|
||||
slices.exit().remove();
|
||||
|
||||
var ae = slices.enter().append("svg:g")
|
||||
.attr("class", "slice")
|
||||
.on('mouseover', function(d,i){
|
||||
d3.select(this).classed('hover', true);
|
||||
dispatch.tooltipShow({
|
||||
label: d.data[label],
|
||||
value: d.data[field],
|
||||
data: d.data,
|
||||
index: i,
|
||||
pos: [d3.event.pageX, d3.event.pageY],
|
||||
id: id
|
||||
});
|
||||
|
||||
})
|
||||
.on('mouseout', function(d,i){
|
||||
d3.select(this).classed('hover', false);
|
||||
dispatch.tooltipHide({
|
||||
label: d.data[label],
|
||||
value: d.data[field],
|
||||
data: d.data,
|
||||
index: i,
|
||||
id: id
|
||||
});
|
||||
})
|
||||
.on('click', function(d,i) {
|
||||
dispatch.elementClick({
|
||||
label: d.data[label],
|
||||
value: d.data[field],
|
||||
data: d.data,
|
||||
index: i,
|
||||
pos: d3.event,
|
||||
id: id
|
||||
});
|
||||
d3.event.stopPropagation();
|
||||
})
|
||||
.on('dblclick', function(d,i) {
|
||||
dispatch.elementDblClick({
|
||||
label: d.data[label],
|
||||
value: d.data[field],
|
||||
data: d.data,
|
||||
index: i,
|
||||
pos: d3.event,
|
||||
id: id
|
||||
});
|
||||
d3.event.stopPropagation();
|
||||
});
|
||||
|
||||
var paths = ae.append("svg:path")
|
||||
.attr('class','path')
|
||||
.attr("fill", function(d, i) { return color(i); });
|
||||
//.attr('d', arc);
|
||||
|
||||
slices.select('.path')
|
||||
.attr('d', arc)
|
||||
.transition()
|
||||
.ease("bounce")
|
||||
.duration(animate)
|
||||
.attrTween("d", tweenPie);
|
||||
|
||||
if (showLabels) {
|
||||
// This does the normal label
|
||||
ae.append("text");
|
||||
|
||||
slices.select("text")
|
||||
.transition()
|
||||
.duration(animate)
|
||||
.ease('bounce')
|
||||
.attr("transform", function(d) {
|
||||
d.outerRadius = radius + 10; // Set Outer Coordinate
|
||||
d.innerRadius = radius + 15; // Set Inner Coordinate
|
||||
return "translate(" + arc.centroid(d) + ")";
|
||||
})
|
||||
.attr("text-anchor", "middle") //center the text on it's origin
|
||||
.style("font", "bold 12px Arial")
|
||||
.text(function(d, i) { return d.data[label]; });
|
||||
}
|
||||
|
||||
|
||||
// Computes the angle of an arc, converting from radians to degrees.
|
||||
function angle(d) {
|
||||
var a = (d.startAngle + d.endAngle) * 90 / Math.PI - 90;
|
||||
return a > 90 ? a - 180 : a;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function tweenPie(b) {
|
||||
b.innerRadius = 0;
|
||||
var i = d3.interpolate({startAngle: 0, endAngle: 0}, b);
|
||||
return function(t) {
|
||||
return arc(i(t));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
chart.margin = function(_) {
|
||||
if (!arguments.length) return margin;
|
||||
margin = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.width = function(_) {
|
||||
if (!arguments.length) return width;
|
||||
if (margin.left + margin.right + 20 > _) {
|
||||
width = margin.left + margin.right + 20; // Min width
|
||||
} else {
|
||||
width = _;
|
||||
}
|
||||
radius = Math.min(width-(margin.left+margin.right), height-(margin.top+margin.bottom)) / 2;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.height = function(_) {
|
||||
if (!arguments.length) return height;
|
||||
if (margin.top + margin.bottom + 20 > _) {
|
||||
height = margin.top + margin.bottom + 20; // Min height
|
||||
} else {
|
||||
height = _;
|
||||
}
|
||||
radius = Math.min(width-(margin.left+margin.right), height-(margin.top+margin.bottom)) / 2;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.animate = function(_) {
|
||||
if (!arguments.length) return animate;
|
||||
animate = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.labelField = function(_) {
|
||||
if (!arguments.length) return (label);
|
||||
label = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.dataField = function(_) {
|
||||
if (!arguments.length) return (field);
|
||||
field = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.showLabels = function(_) {
|
||||
if (!arguments.length) return (showLabels);
|
||||
showLabels = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.donut = function(_) {
|
||||
if (!arguments.length) return (donut);
|
||||
donut = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.title = function(_) {
|
||||
if (!arguments.length) return (title);
|
||||
title = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.id = function(_) {
|
||||
if (!arguments.length) return id;
|
||||
id = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.dispatch = dispatch;
|
||||
|
||||
|
||||
|
||||
return chart;
|
||||
}
|
@ -1 +1 @@
|
||||
})();
|
||||
})();
|
Loading…
Reference in New Issue