[D3] Basic Interactivity with D3 v4

Data visualizations are a lot more interesting when they’re interactive. Whether it’s clicks, roll overs, or drags, it makes things more compelling, and D3 is up to the task. This lesson demonstrates how to implement basic interactions and shows how D3 can do things vanilla CSS can’t.

var scores = [
  { name: ‘Alice‘, score: 96 },
  { name: ‘Billy‘, score: 83 },
  { name: ‘Cindy‘, score: 91 },
  { name: ‘David‘, score: 96 },
  { name: ‘Emily‘, score: 88 }
];

const bars = d3.select(‘.chart‘)
    .append(‘svg‘)
        .attr(‘width‘, 300)
        .attr(‘height‘, 300)
        .style(‘background‘, ‘white‘)
    .selectAll(‘g‘)
    .data(scores)
    .enter()
        .append(‘g‘)
        .attr(‘transform‘, (d, i) => ‘translate(0, ‘ + i * 33 + ‘)‘);

bars.append(‘rect‘)
    .attr(‘width‘, d => d.score)
    .attr(‘class‘, ‘bar‘)
    .on(‘mouseover‘, function(d, i, elements) {
        // transform the hover item to scale 1.1
        d3.select(this).classed(‘barOn‘, true);

        // set not hover elements to opacity 0.8
        d3.selectAll(elements)
            .filter(‘:not(:hover)‘)
            .style(‘opacity‘, 0.6);
    })
    .on(‘mouseout‘, function(d, i, elements) {
        d3.select(this).classed(‘barOn‘, false);
        d3.selectAll(elements)
            .style(‘opacity‘, 1);
    });

bars.append(‘text‘)
    .text(d => d.name)
    .attr(‘y‘, 20)
时间: 2024-09-30 07:08:27

[D3] Basic Interactivity with D3 v4的相关文章

D3.js 入门学习(二) V4的改动

//d3.scan /* 新的d3.scan方法对数组进行线性扫描,并根据指定的比较函数返回至少一个元素的索引. 这个方法有点类似于d3.min和d3.max. 而d3.scan可以得到极值的索引而不仅仅是计算极值. */ var a1 = [1,3,5,2,9]; var i = d3.scan(a1,function(a,b){ return b-a; // 返回最大值的索引, a-b; 返回最小值的索引 }); console.log(i); //4; //d3.ticks d3.tick

D3学习之:D3.js中的12中地图投影方式

特别感谢:1.[张天旭]的D3API汉化说明,已被引用到官方网站: 2.[馒头华华]提供的ourd3js.com上提供的学习系列教程,让我们这些新人起码有了一个方向. 不得不说,学习国外的新技术真的是一个很艰苦的过程. 在学习D3绘制地图的过程中,有朋友建议看一下其中投影的说明比较好,于是,凭借我这半吊子不到的英文水平,大致给翻译了下来,仅供参考: 原文链接:https://github.com/mbostock/d3/wiki/Geo-Projections#albers D3中一共提供了12

[D3] Reuse Transitions in D3 v4

D3 transitions start executing as soon as they’re created, and they’re destroyed once they end. This can present some challenges when attempting to create reusable transitions. This lesson demonstrates how to overcome those challenges using various a

[D3] 12. Basic Transitions with D3

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="../bower_components/underscore/underscore-min.js"></script> <script src="../v

d3.js 颜色比例尺 d3.scale.category10();

var width = 600; var height = 600; var dataset = d3.range(10); //返回[0,1,2,3,4,5,6,7,8,9] console.log(dataset); // 定义表示颜色的序数比例尺 var color = d3.scale.category10(); var svg = d3.select("body").append("svg") .attr("width",width)

[D3] Build a Line Chart with D3 v4

Line charts are often used to plot temporal data, like a stock price over time. In this lesson we’ll see how to use D3 APIs to create our own simplified version of the charts seen on Google Finance. var margin = { top: 10, right: 20, bottom: 65, left

[D3] Create Chart Axes with D3 v4

Most charts aren’t complete without axes to provide context and labeling for the graphical elements being displayed. This lesson introduces D3’s APIs for creating, customizing, and displaying axes while building on topics from previous lessons. var m

[D3] Build a Scatter Plot with D3 v4

Scatter plots, sometimes also known as bubble charts, are another common type of visualization. They’re extremely versatile thanks to their ability to display multiple dimensions of data simultaneously using x and y position, opacity, color, and even

[D3 + AngularJS] 15. Create a D3 Chart as an Angular Directive

Integrating D3 with Angular can be very simple. In this lesson, you will learn basic integration as well as how to create D3 charts that can be packaged as AngularJS directives. <!DOCTYPE html> <html ng-app="app"> <head lang="