Scott Miller
Scott Miller

Reputation: 332

DC.js composite chart not updating when filter is applied

I am working with DC.js and crossfilter. I am trying to use a composite chart to house a bar chart and a line chart. However, when I apply a filter to the data, the bars and line don't update. If I have the charts as standalone bar/line charts, they do update properly.

Here are the chart properties

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([
            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()
        })

Bar Chart (commented out when compositeChart is rendered)

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()
        })

Line Chart (commented out when compositeChart is rendered)

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()
        })

Thanks for the help

Upvotes: 2

Views: 3111

Answers (2)

Doug Mair
Doug Mair

Reputation: 68

I see the same results.

I think it worked when they were declared first, because the value of chartScorecardComposite was not set in the closure.

Upvotes: 0

Scott Miller
Scott Miller

Reputation: 332

After some playing with the code, I was able to accomplish this by defining the line and bar charts separately and then calling them in the compose method of the composite chart.

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()
        })

This allowed any filters that were applied to the dimension to render and update properly

Upvotes: 2

Related Questions