Skip to content

Commit 63d0e09

Browse files
author
Maciej Maciaszek
committed
Add option to provide bars model
1 parent fca3a20 commit 63d0e09

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

examples/multiChart2.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
.y(function(d) {
364364
return d[1]
365365
})
366+
.barsModel(nv.models.historicalBar)
366367
.xScale(d3.time.scale())
367368
;
368369

src/models/multiChart.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
nv.models.multiChart = function() {
1+
nv.models.multiChart = function () {
22
"use strict";
33

44
//============================================================
@@ -38,8 +38,10 @@ nv.models.multiChart = function() {
3838
scatters1 = nv.models.scatter().yScale(yScale1).duration(duration),
3939
scatters2 = nv.models.scatter().yScale(yScale2).duration(duration),
4040

41-
bars1 = nv.models.multiBar().stacked(false).yScale(yScale1).duration(duration),
42-
bars2 = nv.models.multiBar().stacked(false).yScale(yScale2).duration(duration),
41+
barsModel,
42+
43+
bars1 = getBarModel(yScale1),
44+
bars2 = getBarModel(yScale2),
4345

4446
stack1 = nv.models.stackedArea().yScale(yScale1).duration(duration),
4547
stack2 = nv.models.stackedArea().yScale(yScale2).duration(duration),
@@ -54,6 +56,18 @@ nv.models.multiChart = function() {
5456

5557
var charts = [lines1, lines2, scatters1, scatters2, bars1, bars2, stack1, stack2];
5658

59+
function getBarModel(yScale) {
60+
var model;
61+
62+
if (typeof barsModel === 'function') {
63+
model = barsModel();
64+
} else {
65+
model = nv.models.multiBar().stacked(false).duration(duration);
66+
}
67+
68+
return model.yScale(yScale);
69+
}
70+
5771
function chart(selection) {
5872
selection.each(function(data) {
5973
var container = d3.select(this),
@@ -66,6 +80,11 @@ nv.models.multiChart = function() {
6680
var availableWidth = nv.utils.availableWidth(width, container, margin),
6781
availableHeight = nv.utils.availableHeight(height, container, margin);
6882

83+
charts.forEach(function (chart) {
84+
chart.x(getX);
85+
chart.y(getY);
86+
});
87+
6988
var dataLines1 = data.filter(function(d) {return d.type == 'line' && d.yAxis == 1});
7089
var dataLines2 = data.filter(function(d) {return d.type == 'line' && d.yAxis == 2});
7190
var dataScatters1 = data.filter(function(d) {return d.type == 'scatter' && d.yAxis == 1});
@@ -75,6 +94,15 @@ nv.models.multiChart = function() {
7594
var dataStack1 = data.filter(function(d) {return d.type == 'area' && d.yAxis == 1});
7695
var dataStack2 = data.filter(function(d) {return d.type == 'area' && d.yAxis == 2});
7796

97+
// fixes tooltips for bar models other than multibar
98+
data.forEach(function(series, i) {
99+
series.values.forEach(function(point) {
100+
point.series = i;
101+
point.key = series.key;
102+
});
103+
});
104+
105+
// TODO: duplicate in linePlusBarChart.js#
78106
// Display noData message if there's nothing to show.
79107
if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
80108
nv.utils.noData(chart, container);
@@ -516,6 +544,16 @@ nv.models.multiChart = function() {
516544

517545
chart._options = Object.create({}, {
518546
// simple options, just get/set the necessary values
547+
barsModel: {get: function(){return barsModel}, set: function(_){
548+
var bars1Idx = charts.indexOf(bars1),
549+
bars2Idx = charts.indexOf(bars2);
550+
551+
barsModel = _;
552+
bars1 = getBarModel(yScale1);
553+
bars2 = getBarModel(yScale2);
554+
charts.splice(bars1Idx, bars1Idx + 1, bars1);
555+
charts.splice(bars2Idx, bars2Idx + 1, bars2);
556+
}},
519557
width: {get: function(){return width;}, set: function(_){width=_;}},
520558
height: {get: function(){return height;}, set: function(_){height=_;}},
521559
showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
@@ -541,25 +579,9 @@ nv.models.multiChart = function() {
541579
}},
542580
x: {get: function(){return getX;}, set: function(_){
543581
getX = _;
544-
lines1.x(_);
545-
lines2.x(_);
546-
scatters1.x(_);
547-
scatters2.x(_);
548-
bars1.x(_);
549-
bars2.x(_);
550-
stack1.x(_);
551-
stack2.x(_);
552582
}},
553583
y: {get: function(){return getY;}, set: function(_){
554584
getY = _;
555-
lines1.y(_);
556-
lines2.y(_);
557-
scatters1.y(_);
558-
scatters2.y(_);
559-
stack1.y(_);
560-
stack2.y(_);
561-
bars1.y(_);
562-
bars2.y(_);
563585
}},
564586
useVoronoi: {get: function(){return useVoronoi;}, set: function(_){
565587
useVoronoi=_;

0 commit comments

Comments
 (0)