Dimple.js选择下拉标准时更新图表

时间:2016-07-26 18:19:55

标签: jquery html d3.js angular-ui-bootstrap dimple.js

我是D3.js和酒窝的新手。我已经在Dimple.js图表​​上工作了几个星期了。到现在为止,酒窝对我来说很有用。我正在为我的图表创建过滤条件。所以我使用bootstrap来创建下拉菜单。

我在“大陆”下拉菜单中创建了我想要实现的示例Here,如果我选择欧洲中东,我想只看到那个带有相关数据的图表。

d3.select("#btn").on("click", function(){
myChart.data = [

      {"Point":"Turkey","Population":75.01,"Year":2013,"Percent":1.689,"continent":"Middle East"},
{"Point":"Iran","Population":77.15,"Year":2013,"Percent":0.689,"continent":"Middle East"},
{"Point":"Israel","Population":8.05,"Year":2013,"Percent":2.689,"continent":"Middle East"},
{"Point":"Egypt","Population":87.61,"Year":2013,"Percent":0.689,"continent":"Middle East"},
{"Point":"Jordan","Population":6.46,"Year":2013,"Percent":3.689,"continent":"Middle East"},
{"Point":"Turkey","Population":63.24,"Year":2000,"Percent":0.689,"continent":"Middle East"},
{"Point":"Iran","Population":65.85,"Year":2000,"Percent":3.689,"continent":"Middle East"},
{"Point":"Israel","Population":6.28,"Year":2000,"Percent":0.689,"continent":"Middle East"},
{"Point":"Egypt","Population":68.33,"Year":2000,"Percent":0.689,"continent":"Middle East"},
{"Point":"Jordan","Population":4.79,"Year":2000,"Percent":0.689,"continent":"Middle East"}];

我知道如果我将数据拆分为两个并在d3.select("#btn")和我的HTML中提供每个ID。它会起作用,但这不是我想要的。

我真的很感激任何帮助和建议。

1 个答案:

答案 0 :(得分:2)

参考here

不干净,可以修改。有关更好的理解,请参阅参考。



var svg = dimple.newSvg("#chartContainer", 570, 400);
var data = [
    {"Point":"France","Population":65.92,"Year":2013,"Percent":1.689,"continent":"Europe"},
    {"Point":"Germany","Population":82.13,"Year":2013,"Percent":0.689,"continent":"Europe"},
    {"Point":"Italy","Population":60.23,"Year":2013,"Percent":2.689,"continent":"Europe"},
    {"Point":"Greece","Population":10.96,"Year":2013,"Percent":0.689,"continent":"Europe"},
    {"Point":"Belgium","Population":11.18,"Year":2013,"Percent":3.689,"continent":"Europe"},
    {"Point":"France","Population":60.91,"Year":2000,"Percent":0.689,"continent":"Europe"},
    {"Point":"Germany","Population":82.21,"Year":2000,"Percent":3.689,"continent":"Europe"},
    {"Point":"Italy","Population":56.94,"Year":2000,"Percent":0.689,"continent":"Europe"},
    {"Point":"Greece","Population":10.80,"Year":2000,"Percent":0.689,"continent":"Europe"},
    {"Point":"Belgium","Population":10.25,"Year":2000,"Percent":0.689,"continent":"Europe"},
  
    {"Point":"Turkey","Population":75.01,"Year":2013,"Percent":1.689,"continent":"Middle East"},
    {"Point":"Iran","Population":77.15,"Year":2013,"Percent":0.689,"continent":"Middle East"},
    {"Point":"Israel","Population":8.05,"Year":2013,"Percent":2.689,"continent":"Middle East"},
    {"Point":"Egypt","Population":87.61,"Year":2013,"Percent":0.689,"continent":"Middle East"},
    {"Point":"Jordan","Population":6.46,"Year":2013,"Percent":3.689,"continent":"Middle East"},
    {"Point":"Turkey","Population":63.24,"Year":2000,"Percent":0.689,"continent":"Middle East"},
    {"Point":"Iran","Population":65.85,"Year":2000,"Percent":3.689,"continent":"Middle East"},
    {"Point":"Israel","Population":6.28,"Year":2000,"Percent":0.689,"continent":"Middle East"},
    {"Point":"Egypt","Population":68.33,"Year":2000,"Percent":0.689,"continent":"Middle East"},
    {"Point":"Jordan","Population":4.79,"Year":2000,"Percent":0.689,"continent":"Middle East"}];


function getData(data, key) {
	var extData = [];
	if(key == "")
	{
		return data;
	}
	for(var i = 0; i < data.length; i++) {
		if(data[i]["continent"] == key) {
			extData.push(data[i])
		}
	}
	return extData
}	
var myChart = new dimple.chart(svg, getData(data,""));
  
var x = myChart.addCategoryAxis("x", ["Point","Year"]);
      
var y = myChart.addMeasureAxis("y", "Population");
var series1 = myChart.addSeries( "Point", dimple.plot.bar);
x.showGridlines = true;
x.addOrderRule("Date");


var myLegend = myChart.addLegend(530, 100, 60, 300, "Right");
myChart.draw();

        // This is a critical step.  By doing this we orphan the legend. This
        // means it will not respond to graph updates.  Without this the legend
        // will redraw when the chart refreshes removing the unchecked item and
        // also dropping the events we define below.
        myChart.legends = [];

        // This block simply adds the legend title. I put it into a d3 data
        // object to split it onto 2 lines.  This technique works with any
        // number of lines, it isn't dimple specific.
        svg.selectAll("title_text")
          .data(["Click legend to","show/hide owners:"])
          .enter()
          .append("text")
            .attr("x", 499)
            .attr("y", function (d, i) { return 80 + i * 14; })
            .style("font-family", "sans-serif")
            .style("font-size", "10px")
            .style("color", "Black")
            .text(function (d) { return d; });

        // Get a unique list of Owner values to use when filtering
        var filterValues = dimple.getUniqueValues(data, "Point");
        // Get all the rectangles from our now orphaned legend
        myLegend.shapes.selectAll("rect")
          // Add a click event to each rectangle
          .on("click", function (e) {
            // This indicates whether the item is already visible or not
            var hide = false;
            var newFilters = [];
            // If the filters contain the clicked shape hide it
            filterValues.forEach(function (f) {
              if (f === e.aggField.slice(-1)[0]) {
                hide = true;
              } else {
                newFilters.push(f);
              }
            });
            // Hide the shape or show it
            if (hide) {
              d3.select(this).style("opacity", 0.2);
            } else {
              newFilters.push(e.aggField.slice(-1)[0]);
              d3.select(this).style("opacity", 0.8);
            }
            // Update the filters
            filterValues = newFilters;
            // Filter the data
            myChart.data = dimple.filterData(data, "Point", filterValues);
            // Passing a duration parameter makes the chart animate. Without
            // it there is no transition
            myChart.draw(500);
          }); 
			
		  d3.selectAll('.dropdown-submenu > a').on("click", function(d) {
				myChart.data = getData(data,this.text);
				myChart.draw(500);
		  });
&#13;
<script src="http://d3js.org/d3.v3.min.js"></script>
  <script src="http://dimplejs.org/dist/dimple.v1.1.1.min.js"></script>
  <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap.no-icons.min.css" rel="stylesheet">
  <link href="http://netdna.bootstrapcdn.com/font-awesome/2.0/css/font-awesome.css" rel="stylesheet">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.0.0.js"></script> 
<div class="dropdown">
            <a id="dLabel" role="button" data-toggle="dropdown" class="btn btn-primary" data-target="#" href="/page.html">
                Continent <span class="caret"></span>
            </a>
        <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
             

              <li class="dropdown-submenu">
                <a tabindex="-1" href="#" >Europe</a>
                
              </li>
              
              <li class="dropdown-submenu">
                <a tabindex="-1" href="#" >Middle East</a>
              </li>
            </ul>
        </div>
  <div id="chartContainer"> 
&#13;
&#13;
&#13;

编辑:

getData函数根据键修改数据。它需要两个参数作为输入。第一个是原始数据,第二个是大陆。如果大陆为空,它将返回整个数据。如果非洲大陆是欧洲,它将返回欧洲数据。

第一次分配数据时,我将关键参数设置为空,以便返回数据。如果只想显示欧洲数据,请将空字符串更改为欧洲。

var myChart = new dimple.chart(svg, getData(data,"")); // entire data
var myChart = new dimple.chart(svg, getData(data,"Europe"));  // only Europe data