d3.js - Can't fill circle background with color threshold -
i try display circles on map particular dataset. dataset provides center circle. dataset contains identifier (attribute name
), year (attribute year
) , value (attribute value
)
i display 2 things @ level:
- the radius of circle according value
- the background color (fill) of circle according year , using threshold
- a tooltip when circle clicked.
here code use:
var circle = d3.geo.circle(); var color = d3.scale.threshold() .domain([ 1800, 1850, 1900, 1950, 2000, 2050 ]) .range("#f2f0f7", "#dadaeb", "#bcbddc", "#9e9ac8", "#756bb1", "#54278f"); var elements = layerelement.selectall('circle') .data(data) .enter() .append('path') .attr('id', function(d) { return d.name; }); elements .datum(function(d) { return circle .origin(origin({d: d})) .angle(radius({d: d}))(); }) .attr('class', 'point') .attr('d', path) .style('fill', function(d) { return color(d.year); }) .style('opacity', '0.6'); elements.on('click', function(d) { (...) });
i set identifier each circle. see them within in-memory svg dom:
<path id="hoba" class="point" d="m488.55415440889976,286.8579825670507l488.45185788883936,284.8328597859807l488.56757478032006,282.785303550314l488.90003726486356,280.73774731464727l489.445602813917,278.71262453357724l490.1982940971579,276.7321228760774l491.14986447137636,274.8179411327541l492.2898883324236,272.99105147935524l493.6058753403125,271.2714697012249l495.0834072659968,269.6780358961697l496.7062959605052,268.2282080584055l498.45676071568937,266.9378708051187l500.3156230733832,265.82116134127676l502.2625169486039,264.890314569452...l508.2862800372698,302.266499816963l506.206659850514,302.3738076922315l504.15052774957604,302.26649981696306l502.14041114802717,301.9457518786948l500.19833330399786,301.41507805895924l498.3455720287532,300.680292531176l496.6024265625401,299.74944575935126l494.9879951718629,298.63273629550935l493.5199659048794,297.34239904222255l492.2144227974436,295.8925712044583l491.08566965304294,294.2991373994032l490.1460733273343,292.5795556212728l489.4059282342853,290.752665967874l488.8733435584207,288.8384842245506z" style="fill: yellow; opacity: 0.75;">
my problems are:
within function
attr
, first parameter corresponds selected shape there no identifier in it. here content get:{"type":"polygon","coordinates":[[[5.279833759995999,-21.628058088269754],(...)[5.525725679844768,-22.85403683844725],[5.279833759996005,-21.628058088269807]]]}
so can't corresponding value apply background color
i have same problem when clicking element
my question how identifier selected element (circle shape in case).
thanks help! thierry
your call datum
generating path each data item throwing away rest of it, including properties. if want keep properties, nest object generates:
.datum(function(d) { return { circle: circle .origin(origin({d: d})) .angle(radius({d: d}))(), d: d }; })
and rest of calls follow: like
.attr('d', function(d) { return path(d.circle); })
and
.style('fill', function(d) { return color(d.d.year); })
Comments
Post a Comment