【问题标题】:Get Selected Slice Color from Pie chart using DC.js使用 DC.js 从饼图中获取选定的切片颜色
【发布时间】:2015-08-03 10:17:18
【问题描述】:

我有一个协调的饼图和条形图。我的饼图包含年份列表,我的条形图包含每年的信息。当我在饼图中单击一年时,条形图会显示该年的相关详细信息。

场景 需要在饼图切片单击时将条形颜色更改为饼图中选定的切片颜色。

请指教。

【问题讨论】:

    标签: javascript d3.js dc.js crossfilter


    【解决方案1】:

    首先为你的条形图和饼图设置一个通用的色标

    pieChart
        .colors(d3.scale.category10())
        ...
    

    barChart
        .colors(d3.scale.category10())
        ...
    

    请注意,您可能需要根据您拥有的值的数量选择不同的值。

    然后,为条形图设置 colorAccessor 以便它根据与每个条形对应的饼图索引选择一个索引。如果您可以访问所有唯一的 pieValues(您可以在 pie 组上使用 .all() 和 .map() 轻松获得。类似

    var pieValues = year_total.all().map(function(d) { return d.key });
    

    其中 year_total 是饼图维度的组。

    假设您有可用的基础数据,使用类似的方法很容易获得正确的索引

    .colorAccessor(function(d, i){ return years.indexOf(data[i].Year); })
    

    仅此而已!


    小提琴 - http://jsfiddle.net/r8yern6o/


    基本代码改编自 http://www.codeproject.com/Articles/697043/Making-Dashboards-with-Dc-js-Part-2-Graphing 的优秀教程(链接到 https://github.com/dc-js/dc.js/wiki > 书籍和教程部分)

    为了后代,假设您已包含所有库

    JS

    var barChart  = dc.barChart("#chart-line-hitsperday");
    
    var data = [
        {date: "12/27/2012", http_404: 2, http_200: 190, http_302: 100},
        {date: "12/28/2012", http_404: 2, http_200: 10, http_302: 100},
        {date: "12/29/2012", http_404: 1, http_200: 300, http_302: 200},
        {date: "12/30/2012", http_404: 2, http_200: 90, http_302: 0},
        {date: "12/31/2012", http_404: 2, http_200: 90, http_302: 0},
        {date: "01/01/2013", http_404: 2, http_200: 90, http_302: 0},
        {date: "01/02/2013", http_404: 1, http_200: 10, http_302: 1},
        {date: "01/03/2013", http_404: 2, http_200: 90, http_302: 0},
        {date: "01/04/2013", http_404: 2, http_200: 90, http_302: 0},
        {date: "01/05/2013", http_404: 2, http_200: 90, http_302: 0},
        {date: "01/06/2013", http_404: 2, http_200: 200, http_302: 1},
        {date: "01/07/2013", http_404: 1, http_200: 200, http_302: 100}
    ];
    var ndx = crossfilter(data);
    var parseDate = d3.time.format("%m/%d/%Y").parse;
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.total= d.http_404+d.http_200+d.http_302;
        d.Year=d.date.getFullYear();
    });
    
    var dateDim = ndx.dimension(function(d) {return d.date;});
    var hits = dateDim.group().reduceSum(function(d) {return d.total;});
    var minDate = dateDim.bottom(1)[0].date;
    var maxDate = dateDim.top(1)[0].date;
    
    var pieChart = dc.pieChart("#chart-ring-year");
    var yearDim  = ndx.dimension(function(d) {return +d.Year;});
    var year_total = yearDim.group().reduceSum(function(d) {return d.http_200+d.http_302;});
    var pieValues = year_total.all().map(function(d) { return d.key });
    
    barChart
        .colors(d3.scale.category10())
        .width(500).height(200)
        .dimension(dateDim)
        .group(hits)
        .x(d3.time.scale().domain([minDate,maxDate]))
        .brushOn(false)
        .xUnits(d3.time.days)
        .colorAccessor(function(d, i){ return pieValues.indexOf(data[i].Year); })
        .yAxisLabel("Hits per day");
    
    pieChart
        .colors(d3.scale.category10())
        .width(150).height(150)
        .dimension(yearDim)
        .group(year_total)
        .innerRadius(30);
    
    dc.renderAll();
    

    HTML

    <div id="chart-ring-year"></div>
    <div id="chart-line-hitsperday"></div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      相关资源
      最近更新 更多