我正在使用DC.js和交叉过滤器。我正在尝试使用复合图来容纳条形图和折线图。但是,当我对数据应用过滤器时,条形和线形不会更新。如果我将图表作为独立的条形图/折线图,它们确实可以正确更新。
这是图表属性
综合图
var chartScorecardComposite = dc.compositeChart("#regionMarketChart")
.width(1002)
.height(300)
.margins({top: 30, right: 50, bottom: 50, left: 40})
.dimension(chartIssuedJobsCountDim)
.yAxisLabel("Issued on Time %")
.rightYAxisLabel("Jobs Issued")
.shareTitle(false)
.x(d3.time.scale().domain([minDate, maxDate]))
.y(d3.scale.linear().domain([0, 120]))
.xUnits(d3.time.months)
.compose([
dc.barChart(chartScorecardComposite)
.dimension(chartIssuedJobsCountDim)
.group(chartIssuedJobsCountGroup)
.valueAccessor(function (d) {
return d.value.jobCount;
})
.title(function(d) {
return (d.key.getMonth() + 1) + "/" + d.key.getFullYear()
+ "\n# of Jobs: "
+ d.value.jobCount;
})
.renderTitle(true)
.centerBar(true)
.gap(5)
.useRightYAxis(true)
.colors(["#79BAEC"]),
dc.lineChart(chartScorecardComposite)
.dimension(chartIssuedJobsCountDim)
.group(chartIssuedOnTimeGroup)
.valueAccessor(function (d) {
return d.value.onTimePercent;
})
.title(function(d) {
return (d.key.getMonth() + 1) + "/" + d.key.getFullYear()
+ "\n% of Jobs Issued on Time: "
+ d.value.onTimePercent + "%";
})
.renderTitle(true)
.colors(["#808080"])
])
.elasticY(true)
.renderHorizontalGridLines(true)
.brushOn(false)
.renderlet(function (chart) { // Rotate the x-axis labels 45 degrees
chart.selectAll("g.x text")
.attr('dx', '-25')
.attr('dy', '7')
.attr('transform', "rotate(-45)");
})
.xAxis().ticks(d3.time.months).tickFormat(function(d) {
return (d.getMonth() + 1) + "/" + d.getFullYear()
})
条形图(渲染CompositeChart时注释掉)
var issuedJobsBarChart = dc.barChart("#regionMarketChart")
.yAxisLabel("Jobs Issued")
.dimension(chartIssuedJobsCountDim)
.group(chartIssuedJobsCountGroup)
.valueAccessor(function (d) {
return d.value.jobCount;
})
.title(function(d) {
return (d.key.getMonth() + 1) + "/" + d.key.getFullYear()
+ "\n# of Jobs: "
+ d.value.jobCount;
})
.renderTitle(true)
.elasticY(true)
.elasticX(true)
.centerBar(true)
.gap(5)
.brushOn(false)
.x(d3.time.scale().domain([minDate, maxDate]))
.xUnits(d3.time.months)
.xAxis().tickFormat(function(d) {
return (d.getMonth() + 1) + "/" + d.getFullYear()
})
折线图(渲染CompositeChart时注释掉)
var issuedOnTimeLineChart = dc.lineChart(chartScorecardComposite)
.width(800)
.height(250)
.margins({top: 30, right: 25, bottom: 50, left: 40})
.yAxisLabel("Issued on Time %")
.renderHorizontalGridLines(true)
.renderVerticalGridLines(true)
.dimension(chartIssuedJobsCountDim)
.group(chartIssuedOnTimeGroup)
.valueAccessor(function (d) {
return d.value.onTimePercent;
})
.brushOn(false)
.y(d3.scale.linear().domain([0, 120]))
.x(d3.time.scale().domain([minDate, maxDate]))
.xUnits(d3.time.months)
.xAxis().ticks(d3.time.months).tickFormat(function(d) {
return (d.getMonth() + 1) + "/" + d.getFullYear()
})
谢谢您的帮助
最佳答案
经过一段时间的代码使用,我能够通过分别定义折线图和条形图,然后在组合图的compose方法中对其进行调用来实现此目的。
var issuedOnTimeLineChart = dc.lineChart(chartScorecardComposite)
.dimension(chartIssuedJobsCountDim)
.group(chartIssuedOnTimeGroup)
.valueAccessor(function (d) {
return d.value.onTimePercent;
})
.colors(["#808080"])
.brushOn(false)
.y(d3.scale.linear().domain([0, 120]))
.x(d3.time.scale().domain([minDate, maxDate]))
var issuedJobsBarChart = dc.barChart(chartScorecardComposite)
.dimension(chartIssuedJobsCountDim)
.group(chartIssuedJobsCountGroup)
.valueAccessor(function (d) {
return d.value.jobCount;
})
.title(function(d) {
return (d.key.getMonth() + 1) + "/" + d.key.getFullYear()
+ "\n# of Jobs: "
+ d.value.jobCount;
})
.renderTitle(true)
.centerBar(true)
.gap(5)
.colors(["#79BAEC"])
.brushOn(false)
.useRightYAxis(true)
.x(d3.time.scale().domain([minDate, maxDate]))
.xUnits(d3.time.months)
//Define the composite chart
var chartScorecardComposite = dc.compositeChart("#regionMarketChart")
.width(1002)
.height(300)
.margins({top: 30, right: 50, bottom: 50, left: 40})
.dimension(chartIssuedJobsCountDim)
.yAxisLabel("Issued on Time %")
.rightYAxisLabel("Jobs Issued")
.shareTitle(false)
.x(d3.time.scale().domain([minDate, maxDate]))
.y(d3.scale.linear().domain([0, 120]))
.xUnits(d3.time.months)
.compose([
issuedJobsBarChart,
issuedOnTimeLineChart
])
.elasticY(true)
.renderHorizontalGridLines(true)
.brushOn(false)
.renderlet(function (chart) { // Rotate the x-axis labels 45 degrees
chart.selectAll("g.x text")
.attr('dx', '-25')
.attr('dy', '7')
.attr('transform', "rotate(-45)");
})
.xAxis().ticks(d3.time.months).tickFormat(function(d) {
return (d.getMonth() + 1) + "/" + d.getFullYear()
})
这允许应用于维度的所有过滤器正确呈现和更新