简体   繁体   English

为colorbrewer渲染d3.js库可视化

[英]rendering d3.js library visualizations for colorbrewer

I'm trying to implement a tutorial called 'Mapping Minimum Wages in Europe' to render a d3 map of the European Union, visualizing data across the different component nations. 我正在尝试实施一个名为“映射欧洲最低工资”的教程,以绘制欧盟的d3地图,以可视化各个组成国家的数据。

When I try to execute the last step of the tutorial, incorporating colorbrewer with the d3.js library to colour the countries in a different way based on the data, using the following function: 当我尝试执行本教程的最后一步时,请使用以下功能将colorbrewer与d3.js库合并,以基于数据的不同方式为国家/地区着色:

queue()
    .defer(d3.json, "eu.json")
    .defer(d3.json, "data.json")
    .await(ready);

function ready(error, europe, data) {
    if (error) return console.error(error);

    var quantize = d3.scale.quantile()
             .domain(d3.extent(d3.values(data), function (d) { return d.value; }))
             .range(d3.range(6)),
        cb = "Reds";

    function fill(datum, index) {
          var iso = datum.properties.iso_n3,
               val = data[iso] && data[iso].value;
          if (val) {
              var c = colorbrewer[cb][6][quantize(val)];
              return c;
          } else {
              return "lightgray";
          }
    }


    var svg = d3.select("#container").append("svg")
        .attr("width", width)
        .attr("height", height);

    var eu = topojson.feature(europe, europe.objects.europe),
        countries = eu.features;

    projection.scale(1).translate([0, 0])

    var b = path.bounds(eu),
        s = .95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
        t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];

    projection.scale(s).translate(t);

    svg.selectAll("path")
        .data(countries)
        .enter().append("path")
        .attr("d", path)
        .attr("class", "country")
        .classed("eu-country", isEuCountry);

    svg.selectAll(".eu-country")
        .style("fill", fill);
}

the program crashes and my browser loads nothing. 程序崩溃,我的浏览器未加载任何内容。

I've narrowed it down to that code there. 我将其范围缩小到那里的代码。

Perhaps someone more familiar with Javascript might be able to figure out what is going wrong here. 也许更熟悉Javascript的人也许能够找出问题所在。

The files named in the component: 组件中命名的文件:

queue()
    .defer(d3.json, "eu.json")
    .defer(d3.json, "data.json")
    .await(ready);

are in the same directory as my index.html file. 与我的index.html文件位于同一目录中。 So that isn't the issue. 所以这不是问题。

您需要以这种方式像Java #include语句一样包含coldbrewer

    <script src="http://d3js.org/colorbrewer.v1.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM