merging in css changes
commit
71c7fb3c51
@ -0,0 +1,163 @@
|
||||
<!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;
|
||||
}
|
||||
|
||||
#chart1 {
|
||||
margin: 10px;
|
||||
min-width: 100px;
|
||||
min-height: 100px;
|
||||
/*
|
||||
Minimum height and width is a good idea to prevent negative SVG dimensions...
|
||||
For example width should be =< margin.left + margin.right + 1,
|
||||
of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
|
||||
*/
|
||||
}
|
||||
|
||||
#chart1 svg {
|
||||
height: 500px;
|
||||
}
|
||||
|
||||
</style>
|
||||
<body>
|
||||
|
||||
<div id="chart1">
|
||||
<svg></svg>
|
||||
</div>
|
||||
|
||||
<script src="../lib/d3.v2.js"></script>
|
||||
<script src="../nv.d3.js"></script>
|
||||
<script src="../src/tooltip.js"></script>
|
||||
<script src="../src/models/legend.js"></script>
|
||||
<script src="../src/models/axis.js"></script>
|
||||
<script src="../src/models/multiBarHorizontal.js"></script>
|
||||
<script src="../src/models/multiBarHorizontalChart.js"></script>
|
||||
<script src="stream_layers.js"></script>
|
||||
<script>
|
||||
|
||||
|
||||
|
||||
long_short_data = [
|
||||
{
|
||||
key: 'Short',
|
||||
color: '#d62728',
|
||||
values: [
|
||||
{
|
||||
"label" : "Information Technology" ,
|
||||
"value" : -1.8746444827653
|
||||
} ,
|
||||
{
|
||||
"label" : "Consumer Discretionary" ,
|
||||
"value" : -8.0961543492239
|
||||
} ,
|
||||
{
|
||||
"label" : "Health Care" ,
|
||||
"value" : -0.57072943117674
|
||||
} ,
|
||||
{
|
||||
"label" : "Consumer Staples" ,
|
||||
"value" : -2.4174010336624
|
||||
} ,
|
||||
{
|
||||
"label" : "Financials" ,
|
||||
"value" : -0.72009071426284
|
||||
} ,
|
||||
{
|
||||
"label" : "Industrials" ,
|
||||
"value" : -0.77154485523777
|
||||
} ,
|
||||
{
|
||||
"label" : "Energy" ,
|
||||
"value" : -0.90152097798131
|
||||
} ,
|
||||
{
|
||||
"label" : "Materials" ,
|
||||
"value" : -0.91445417330854
|
||||
} ,
|
||||
{
|
||||
"label" : "Telecommunication Services" ,
|
||||
"value" : -0.055746319141851
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
key: 'Long',
|
||||
color: '#1f77b4',
|
||||
values: [
|
||||
{
|
||||
"label" : "Information Technology" ,
|
||||
"value" : 25.307646510375
|
||||
} ,
|
||||
{
|
||||
"label" : "Consumer Discretionary" ,
|
||||
"value" : 16.756779544553
|
||||
} ,
|
||||
{
|
||||
"label" : "Health Care" ,
|
||||
"value" : 18.451534877007
|
||||
} ,
|
||||
{
|
||||
"label" : "Consumer Staples" ,
|
||||
"value" : 8.6142352811805
|
||||
} ,
|
||||
{
|
||||
"label" : "Financials" ,
|
||||
"value" : 7.8082472075876
|
||||
} ,
|
||||
{
|
||||
"label" : "Industrials" ,
|
||||
"value" : 5.259101026956
|
||||
} ,
|
||||
{
|
||||
"label" : "Energy" ,
|
||||
"value" : 0.30947953487127
|
||||
} ,
|
||||
{
|
||||
"label" : "Materials" ,
|
||||
"value" : 0
|
||||
} ,
|
||||
{
|
||||
"label" : "Telecommunication Services" ,
|
||||
"value" : 0
|
||||
}
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
|
||||
nv.addGraph(function() {
|
||||
var chart = nv.models.multiBarHorizontalChart()
|
||||
.x(function(d) { return d.label })
|
||||
.y(function(d) { return d.value })
|
||||
.margin({top: 30, right: 20, bottom: 50, left: 160})
|
||||
.showValues(true)
|
||||
.tooltips(false)
|
||||
.showControls(false);
|
||||
|
||||
chart.yAxis
|
||||
.tickFormat(d3.format(',.2f'));
|
||||
|
||||
d3.select('#chart1 svg')
|
||||
.datum(long_short_data)
|
||||
.transition().duration(500)
|
||||
.call(chart);
|
||||
|
||||
nv.utils.windowResize(chart.update);
|
||||
|
||||
return chart;
|
||||
});
|
||||
|
||||
|
||||
|
||||
</script>
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,247 @@
|
||||
|
||||
nv.models.multiBarHorizontalChart = function() {
|
||||
var margin = {top: 30, right: 20, bottom: 50, left: 60},
|
||||
width = null,
|
||||
height = null,
|
||||
color = d3.scale.category20().range(),
|
||||
showControls = true,
|
||||
tooltips = true,
|
||||
tooltip = function(key, x, y, e, graph) {
|
||||
return '<h3>' + x + '</h3>' +
|
||||
'<p>' + y + '</p>'
|
||||
};
|
||||
|
||||
|
||||
var multibar = nv.models.multiBarHorizontal().stacked(false),
|
||||
x = multibar.xScale(),
|
||||
y = multibar.yScale(),
|
||||
xAxis = nv.models.axis().scale(x).orient('left').highlightZero(false),
|
||||
yAxis = nv.models.axis().scale(y).orient('bottom'),
|
||||
legend = nv.models.legend().height(30),
|
||||
controls = nv.models.legend().height(30),
|
||||
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
|
||||
|
||||
xAxis.tickFormat(function(d) { return d });
|
||||
yAxis.tickFormat(d3.format(',.1f'));
|
||||
|
||||
var showTooltip = function(e, offsetElement) {
|
||||
//console.log('left: ' + offsetElement.offsetLeft);
|
||||
//console.log('top: ' + offsetElement.offsetLeft);
|
||||
|
||||
//TODO: FIX offsetLeft and offSet top do not work if container is shifted anywhere
|
||||
//var offsetElement = document.getElementById(selector.substr(1)),
|
||||
var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
|
||||
top = e.pos[1] + ( offsetElement.offsetTop || 0),
|
||||
x = xAxis.tickFormat()(multibar.x()(e.point)),
|
||||
y = yAxis.tickFormat()(multibar.y()(e.point)),
|
||||
content = tooltip(e.series.key, x, y, e, chart);
|
||||
|
||||
nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w');
|
||||
};
|
||||
|
||||
//TODO: let user select default
|
||||
var controlsData = [
|
||||
{ key: 'Grouped' },
|
||||
{ key: 'Stacked', disabled: true },
|
||||
];
|
||||
|
||||
function chart(selection) {
|
||||
selection.each(function(data) {
|
||||
var container = d3.select(this);
|
||||
|
||||
var availableWidth = (width || parseInt(container.style('width')) || 960)
|
||||
- margin.left - margin.right,
|
||||
availableHeight = (height || parseInt(container.style('height')) || 400)
|
||||
- margin.top - margin.bottom;
|
||||
|
||||
|
||||
multibar
|
||||
.width(availableWidth)
|
||||
.height(availableHeight)
|
||||
.color(data.map(function(d,i) {
|
||||
return d.color || color[i % 10];
|
||||
}).filter(function(d,i) { return !data[i].disabled }))
|
||||
|
||||
|
||||
|
||||
var wrap = container.selectAll('g.wrap.multiBarHorizontalChart').data([data]);
|
||||
var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 multiBarHorizontalChart').append('g');
|
||||
|
||||
gEnter.append('g').attr('class', 'x axis');
|
||||
gEnter.append('g').attr('class', 'y axis');
|
||||
gEnter.append('g').attr('class', 'linesWrap');
|
||||
gEnter.append('g').attr('class', 'legendWrap');
|
||||
gEnter.append('g').attr('class', 'controlsWrap');
|
||||
|
||||
|
||||
|
||||
//TODO: margins should be adjusted based on what components are used: axes, axis labels, legend
|
||||
margin.top = legend.height();
|
||||
|
||||
var g = wrap.select('g')
|
||||
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
||||
|
||||
|
||||
legend.width(availableWidth / 2);
|
||||
|
||||
g.select('.legendWrap')
|
||||
.datum(data)
|
||||
.attr('transform', 'translate(' + (availableWidth / 2) + ',' + (-margin.top) +')')
|
||||
.call(legend);
|
||||
|
||||
if (showControls) {
|
||||
controls.width(180).color(['#444', '#444', '#444']);
|
||||
g.select('.controlsWrap')
|
||||
.datum(controlsData)
|
||||
.attr('transform', 'translate(0,' + (-margin.top) +')')
|
||||
.call(controls);
|
||||
}
|
||||
|
||||
|
||||
var linesWrap = g.select('.linesWrap')
|
||||
.datum(data.filter(function(d) { return !d.disabled }))
|
||||
|
||||
|
||||
d3.transition(linesWrap).call(multibar);
|
||||
|
||||
|
||||
xAxis
|
||||
.scale(x)
|
||||
.ticks( availableHeight / 24 )
|
||||
.tickSize(-availableWidth, 0);
|
||||
|
||||
d3.transition(g.select('.x.axis'))
|
||||
.call(xAxis);
|
||||
|
||||
var xTicks = g.select('.x.axis').selectAll('g');
|
||||
|
||||
xTicks
|
||||
.selectAll('line, text')
|
||||
.style('opacity', 1)
|
||||
|
||||
xTicks.filter(function(d,i) {
|
||||
return i % Math.ceil(data[0].values.length / (availableWidth / 100)) !== 0;
|
||||
})
|
||||
.selectAll('line, text')
|
||||
.style('opacity', 0)
|
||||
|
||||
yAxis
|
||||
.domain(y.domain())
|
||||
.range(y.range())
|
||||
.ticks( availableWidth / 100 )
|
||||
.tickSize( -availableHeight, 0);
|
||||
|
||||
g.select('.y.axis')
|
||||
.attr('transform', 'translate(0,' + availableHeight + ')');
|
||||
d3.transition(g.select('.y.axis'))
|
||||
.call(yAxis);
|
||||
|
||||
|
||||
|
||||
|
||||
legend.dispatch.on('legendClick', function(d,i) {
|
||||
d.disabled = !d.disabled;
|
||||
|
||||
if (!data.filter(function(d) { return !d.disabled }).length) {
|
||||
data.map(function(d) {
|
||||
d.disabled = false;
|
||||
wrap.selectAll('.series').classed('disabled', false);
|
||||
return d;
|
||||
});
|
||||
}
|
||||
|
||||
selection.transition().call(chart);
|
||||
});
|
||||
|
||||
controls.dispatch.on('legendClick', function(d,i) {
|
||||
if (!d.disabled) return;
|
||||
controlsData = controlsData.map(function(s) {
|
||||
s.disabled = true;
|
||||
return s;
|
||||
});
|
||||
d.disabled = false;
|
||||
|
||||
switch (d.key) {
|
||||
case 'Grouped':
|
||||
multibar.stacked(false);
|
||||
break;
|
||||
case 'Stacked':
|
||||
multibar.stacked(true);
|
||||
break;
|
||||
}
|
||||
|
||||
selection.transition().call(chart);
|
||||
});
|
||||
|
||||
|
||||
multibar.dispatch.on('elementMouseover.tooltip', function(e) {
|
||||
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
|
||||
dispatch.tooltipShow(e);
|
||||
});
|
||||
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, container[0][0]) } ); // TODO: maybe merge with above?
|
||||
|
||||
multibar.dispatch.on('elementMouseout.tooltip', function(e) {
|
||||
dispatch.tooltipHide(e);
|
||||
});
|
||||
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
|
||||
|
||||
|
||||
//TODO: decide if this makes sense to add into all the models for ease of updating (updating without needing the selection)
|
||||
chart.update = function() {
|
||||
selection.transition().call(chart);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
|
||||
chart.dispatch = dispatch;
|
||||
chart.multibar = multibar; // really just makign the accessible for multibar.dispatch, may rethink slightly
|
||||
chart.legend = legend;
|
||||
chart.xAxis = xAxis;
|
||||
chart.yAxis = yAxis;
|
||||
|
||||
d3.rebind(chart, multibar, 'x', 'y', 'xDomain', 'yDomain', 'forceX', 'forceY', 'clipEdge', 'id', 'showValues', 'valueFormat');
|
||||
|
||||
|
||||
chart.margin = function(_) {
|
||||
if (!arguments.length) return margin;
|
||||
margin = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.width = function(_) {
|
||||
if (!arguments.length) return width;
|
||||
width = d3.functor(_);
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.height = function(_) {
|
||||
if (!arguments.length) return height;
|
||||
height = d3.functor(_);
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.showControls = function(_) {
|
||||
if (!arguments.length) return showControls;
|
||||
showControls = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.tooltips = function(_) {
|
||||
if (!arguments.length) return tooltips;
|
||||
tooltips = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.tooltipContent = function(_) {
|
||||
if (!arguments.length) return tooltip;
|
||||
tooltip = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
|
||||
return chart;
|
||||
}
|
Loading…
Reference in New Issue