gpt4 book ai didi

javascript - D3 - 多折线图,第二线圆圈不显示

转载 作者:行者123 更新时间:2023-11-30 11:46:39 25 4
gpt4 key购买 nike

我正在制作一个比较折线图,根据字母绘制频率和频率 2。此外,我希望在两条线的节点处制作圆圈。但是,我在第一组线节点中得到圆圈。我没有得到第二行的圆圈。

如果我在注释第一组圆圈代码,那么我可以在第二行看到红色圆圈。

请帮忙,让我也能看到第二行的红色圆圈,以及第一行的红色圆圈。

片段:

<html>

<head>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.12/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"></script>

</head>

<body ng-app="myApp" ng-controller="myCtrl">

<svg></svg>

<script>

//module declaration
var app = angular.module('myApp',[]);

//Controller declaration
app.controller('myCtrl',function($scope){

$scope.svgWidth = 800;//svg Width
$scope.svgHeight = 500;//svg Height

//Data in proper format
var data = [
{"letter": "A","frequency": "5.01", "frequency2":"18.03"},
{"letter": "B","frequency": "7.80", "frequency2":"15.03"},
{"letter": "C","frequency": "15.35","frequency2":"27.03"},
{"letter": "D","frequency": "22.70","frequency2":"12.03"},
{"letter": "E","frequency": "34.25", "frequency2":"21.03"},
{"letter": "F","frequency": "10.21","frequency2":"28.03"},
{"letter": "G","frequency": "7.68","frequency2":"09.03"},
];

//removing prior svg elements ie clean up svg
d3.select('svg').selectAll("*").remove();

//resetting svg height and width in current svg
d3.select("svg").attr("width", $scope.svgWidth).attr("height", $scope.svgHeight);

//Setting up of our svg with proper calculations
var svg = d3.select("svg");
var margin = {top: 20, right: 20, bottom: 30, left: 40};
var width = svg.attr("width") - margin.left - margin.right;
var height = svg.attr("height") - margin.top - margin.bottom;

//Plotting our base area in svg in which chart will be shown
var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

//X and Y scaling
var x = d3.scaleBand().rangeRound([0, width]).padding(0.4);
var y = d3.scaleLinear().rangeRound([height, 0]);

x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return +d.frequency; })]);

//Final Plotting

//for x axis
g.append("g")
.call(d3.axisBottom(x))
.attr("transform", "translate(0," + height + ")");

//for y axis
g.append("g")
.call(d3.axisLeft(y))
.append("text").attr("transform", "rotate(-90)").attr("text-anchor", "end");


/**************** First Path ******************/

//the line function for path
var lineFunction = d3.line()
.x(function(d) {return x(d.letter); })
.y(function(d) { return y(d.frequency); })
.curve(d3.curveLinear);

//defining the lines
var path = g.append("path");

//plotting lines
path
.attr("d", lineFunction(data))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none")
.append("circle");


g.selectAll('circle')
.data(data)
.enter().append('circle')
.attr('cx', function(d) {
return x(d.letter);
})
.attr('cy', function(d) {
return y(d.frequency);
})
.attr('r', 6)
.style("fill", "blue");

/**************** Second Path *********************/

//the line function for path
var lineFunction = d3.line()
.x(function(d) {return x(d.letter); })
.y(function(d) { return y(d.frequency2); })
.curve(d3.curveLinear);

//defining the lines
var path = g.append("path");

//plotting lines
path
.attr("d", lineFunction(data))
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("fill", "none")
.append("circle");


g.selectAll('circle')
.data(data)
.enter().append('circle')
.attr('cx', function(d) {
return x(d.letter);
})
.attr('cy', function(d) {
return y(d.frequency2);
})
.attr('r', 6)
.style("fill", "red");

});

</script>

</body>

</html>

结果:

enter image description here

我想我在某个地方犯了一些愚蠢的错误。请帮助。

最佳答案

这里有两个问题:

首先,当您对两组圈子执行此操作时:

g.selectAll('circle')

您的第二组正在选择现有元素,第二组的“输入”选择将为空。

尝试为每个组使用不同的选择器,如下所示:

//for the first group
g.selectAll('.circles1')

//for the second group
g.selectAll('.circles2')

第二个问题是,对于第二组圆,你应该使用frequency2:

.attr('cy', function(d) {
return y(d.frequency2);
})

这是一个工作演示:

var data = [
{"letter": "A","frequency": "5.01", "frequency2":"18.03"},
{"letter": "B","frequency": "7.80", "frequency2":"15.03"},
{"letter": "C","frequency": "15.35","frequency2":"27.03"},
{"letter": "D","frequency": "22.70","frequency2":"12.03"},
{"letter": "E","frequency": "34.25", "frequency2":"21.03"},
{"letter": "F","frequency": "10.21","frequency2":"28.03"},
{"letter": "G","frequency": "7.68","frequency2":"09.03"},
];

//removing prior svg elements ie clean up svg
d3.select('svg').selectAll("*").remove();

//resetting svg height and width in current svg
d3.select("svg").attr("width", 500).attr("height", 300);

//Setting up of our svg with proper calculations
var svg = d3.select("svg");
var margin = {top: 20, right: 20, bottom: 30, left: 40};
var width = svg.attr("width") - margin.left - margin.right;
var height = svg.attr("height") - margin.top - margin.bottom;

//Plotting our base area in svg in which chart will be shown
var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

//X and Y scaling
var x = d3.scaleBand().rangeRound([0, width]).padding(0.4);
var y = d3.scaleLinear().rangeRound([height, 0]);

x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return +d.frequency; })]);

//Final Plotting

//for x axis
g.append("g")
.call(d3.axisBottom(x))
.attr("transform", "translate(0," + height + ")");

//for y axis
g.append("g")
.call(d3.axisLeft(y))
.append("text").attr("transform", "rotate(-90)").attr("text-anchor", "end");


/**************** First Path ******************/

//the line function for path
var lineFunction = d3.line()
.x(function(d) {return x(d.letter); })
.y(function(d) { return y(d.frequency); })
.curve(d3.curveLinear);

//defining the lines
var path = g.append("path");

//plotting lines
path
.attr("d", lineFunction(data))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none")
.append("circle");


g.selectAll('.circles1')
.data(data)
.enter().append('circle')
.attr('cx', function(d) {
return x(d.letter);
})
.attr('cy', function(d) {
return y(d.frequency);
})
.attr('r', 6)
.style("fill", "blue");

/**************** Second Path *********************/

//the line function for path
var lineFunction = d3.line()
.x(function(d) {return x(d.letter); })
.y(function(d) { return y(d.frequency2); })
.curve(d3.curveLinear);

//defining the lines
var path = g.append("path");

//plotting lines
path
.attr("d", lineFunction(data))
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("fill", "none")
.append("circle");


g.selectAll('.circles2')
.data(data)
.enter().append('circle')
.attr('cx', function(d) {
return x(d.letter);
})
.attr('cy', function(d) {
return y(d.frequency2);
})
.attr('r', 6)
.style("fill", "red");
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>

关于javascript - D3 - 多折线图,第二线圆圈不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40765801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com