Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

dc.js - Change text and remove Select All from dc.selectMenu

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

    </head>

    <body>

    <div id = "selectbox"> </div>
    <div id = "chart1"> </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.5.0/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dc/3.0.6/dc.min.js"></script>



 <script>   
    var facts = crossfilter([
    {"Period":"Jan-18","Department":"Dept 1","Percentage":0.2098},
    {"Period":"Feb-18","Department":"Dept 1","Percentage":0.6058},
    {"Period":"Mar-18","Department":"Dept 1","Percentage":0.691},
    {"Period":"Jan-18","Department":"Dept 2","Percentage":0.2705},
    {"Period":"Feb-18","Department":"Dept 2","Percentage":0.4113},
    {"Period":"Mar-18","Department":"Dept 2","Percentage":0.3698},
    {"Period":"Jan-18","Department":"Dept 3","Percentage":0.3239},
    {"Period":"Feb-18","Department":"Dept 3","Percentage":0.4638},
    {"Period":"Mar-18","Department":"Dept 3","Percentage":0.4036},
    {"Period":"Jan-18","Department":"Dept 4","Percentage":0.8288},
    {"Period":"Feb-18","Department":"Dept 4","Percentage":0.0809},
    {"Period":"Mar-18","Department":"Dept 4","Percentage":0.9152},
    {"Period":"Jan-18","Department":"All","Percentage":0.6104},
    {"Period":"Feb-18","Department":"All","Percentage":0.3348},
    {"Period":"Mar-18","Department":"All","Percentage":0.8257},
    ])

    var PeriodDimension = facts.dimension( function (d) {return d.Period} );
    var DeptDimension = facts.dimension( function (d) {return d.Department} );

    dc.selectMenu("#selectbox")
        .dimension(DeptDimension)
        .group(DeptDimension.group())
        .promptText("removethisline")
        .controlsUseVisibility(true);

     dc.renderAll();
 </script>

</body>

JS Fiddle Here

Two Problems

1) If you notice in JSFiddle, we get keys and values in the drop-down. We only need keys i.e. we only need All, Dept one, Dept two and so on in the drop-down.

2) Since I already have a data-inbuilt "All" option, how do I remove the promptText value from the drop-down?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. It's a little buried in the documentation but the root example shows how you can use .title() to set the option text, like so:

    .title(kv => kv.key)
    
  2. That first element seems to be hard-coded in there, so I'd listen to the pretransition event and use an attribute selector to choose the option with the blank value and remove it:

    selbox.on('pretransition', function() {
      selbox.select('option[value=""]').remove();
    })
    .filter("Dept 1")
    

Note: you may run into other charts not adding up correctly if your rows aren't completely distinct.

Fork of your fiddle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...