gpt4 book ai didi

javascript - 如何根据条件更改折线图中数据点的颜色?

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

我使用 D3.js 设计了一个折线图。该线仅在 x 轴上。它包含数据点,当鼠标在该数据点上移动时,仅显示一个工具提示,其中包含有关该数据点的一些特定数据。我需要一个接一个地附加数据点,这意味着第一个数据点出现,下一个数据点在 1 分钟后出现等等。所以我在一个时间段内将数据点附加到行中。这意味着我设置了一个 setInterval 函数,并在该函数中一个一个地追加了一个数据点。 (为此,我逐步增加了保存数据的数组,每次覆盖数据点时,它都显示为在时间间隔内追加新数据点。)

然后我需要更改特定数据点的颜色。根据这个例子,如果一个特定的数据元素有“A”作为它的“记录”值,那么我需要改变它的数据点的颜色。但是根据覆盖或任何其他错误,它无法正常工作。有人可以告诉我哪里错了吗?有没有可能比这种方式更容易完成这项任务?

(我尽力解释了这一点。有时您可能无法理解我在说什么。所以也许这段代码可以帮助您正确理解它)

<html>
<head>
<title>myD3Trial1</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.js" charset="utf-8"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://rawgit.com/jaz303/tipsy/master/src/javascripts/jquery.tipsy.js"></script>
<link href="tipsy.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="D3LineChart.css">
<style>
.axis path,
.axis line{
fill: none;
stroke: blue;
stroke-width: 2px;
}

.line{
fill: none;
stroke: black;
stroke-width: 1px;
}

.tick text{
font-size: bold 11px;
}

.tick line{
opacity: 0.2;
}
</style>
</head>
<body>

<div class="chart3"></div>

<script>

var line_xAxisGroup = null,
line_dataCirclesGroup = null,
line_dataLinesGroup = null;

var line_maxDataPointsForDots = 50,
line_transitionDuration = 1000;

var line_pointRadius = 7;

var line_parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;

//set original data set for here

var OriginalDataForLineChart = [{
"Date": "2013-03-12 05:09:04",
"Record":"A",
"Value": "0"
}, {
"Date": "2013-03-12 14:59:06",
"Record":"B",
"Value": "0"
}, {
"Date": "2013-03-12 14:49:04",
"Record":"C",
"Value": "0"
}, {
"Date": "2013-03-13 14:39:06",
"Record":"D",
"Value": "0"
},{
"Date": "2013-03-12 14:29:03",
"Record":"A",
"Value": "0"
}];

var line_margin = {top: 20, right: 20, bottom: 30, left: 50};
var line_width = 1100 - line_margin.left - line_margin.right;
var line_height = 100 - line_margin.top - line_margin.bottom;

var line_x = d3.time.scale()
.range([0, line_width]);

var line_y = d3.scale.linear()
.range([line_height, 0]);

var line_xAxis = d3.svg.axis()
.scale(line_x)
.orient("bottom");

var line_yAxis = d3.svg.axis()
.scale(line_y)
.orient("left");

var line_line = d3.svg.line()
.x(function(d) { return line_x(d.Date); })
.y(function(d) { return line_y(d.Value); });


var line_svg = d3.select(".chart3").append("svg")
.attr("width", line_width + line_margin.left + line_margin.right)
.attr("height", line_height + line_margin.top + line_margin.bottom)
.append("g")
.attr("transform", "translate(" + line_margin.left + "," + line_margin.top + ")");

OriginalDataForLineChart.forEach(function(d) {
d.Date = line_parseDate(d.Date);
d.Value = +d.Value;
});

line_x.domain(d3.extent(OriginalDataForLineChart, function(d) { return d.Date; }));
line_y.domain(d3.extent(OriginalDataForLineChart, function(d) { return d.Value;}));

line_svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + line_height + ")")
.call(line_xAxis);



line_svg.append("path")
.datum(OriginalDataForLineChart)
.attr("class", "line")
.attr("d", line_line);

var i = 1;
var interval = setInterval(function() {
updateLineChart(i);
i++;
if(i==OriginalDataForLineChart.length) clearInterval(interval);
},1000);


var duplicateDataForLineChart = [];

function updateLineChart(index){

for(var counut2 = 0 ; counut2<index ; counut2++){
duplicateDataForLineChart[counut2] = OriginalDataForLineChart[counut2];
}

if (!line_dataCirclesGroup) {
line_dataCirclesGroup = line_svg.append('svg:g');
}

var line_circles = line_dataCirclesGroup.selectAll('.data-point').data(duplicateDataForLineChart);
//.data(data);

line_circles
.enter()
.append('svg:circle')
.attr('class', 'data-point')
.style('opacity', 1e-6);

for(var x=0 ; x<duplicateDataForLineChart.length ; x++){ //<==I add this for loop to change data point color
if(duplicateDataForLineChart[x].Record=="A"){
line_circles
.enter()
.append('svg:circle')
.attr('class', 'data-point')
.style('opacity', 1e-6)
.style('fill','#FF45FF');
}
}

line_circles
.attr('cx', function(d) { return line_x(d.Date); })
.attr('cy', function(d) { return line_y(d.Value); })
.attr('r', function() { return (duplicateDataForLineChart.length <= line_maxDataPointsForDots) ? line_pointRadius : 0 })
.transition()
.duration(line_transitionDuration)
.style('opacity', 1);

line_circles
.exit()
.transition()
.duration(line_transitionDuration)
// Leave the cx transition off. Allowing the points to fall where they lie is best.
//.attr('cx', function(d, i) { return line_xAxis(i) })
.attr('cy', function() { return line_y(0) })
.style("opacity", 1e-6)
.remove();

$('svg circle').tipsy({
gravity: 'width',
html: true,
title: function() {
console.log(this.__data__);
var d = this.__data__;
//var pDate = d.line_x;
return d.Date;//.toLocaleDateString();//+'</br>'+d.Date.to;
}
});

}


</script>
</body>
</html>

最佳答案

我已经删除了 for 循环,因为它不需要。我们可以添加一个回调函数来检查 'A' 类型的记录,如果满足条件,则将 'fill' 属性设置为红色。

<html>
<head>
<title>myD3Trial1</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.js" charset="utf-8"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://rawgit.com/jaz303/tipsy/master/src/javascripts/jquery.tipsy.js"></script>
<link href="tipsy.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="D3LineChart.css">
<style>
.axis path,
.axis line{
fill: none;
stroke: blue;
stroke-width: 2px;
}

.line{
fill: none;
stroke: black;
stroke-width: 1px;
}

.tick text{
font-size: bold 11px;
}

.tick line{
opacity: 0.2;
}
</style>
</head>
<body>

<div class="chart3"></div>

<script>

var line_xAxisGroup = null,
line_dataCirclesGroup = null,
line_dataLinesGroup = null;

var line_maxDataPointsForDots = 50,
line_transitionDuration = 1000;

var line_pointRadius = 7;

var line_parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;

//set original data set for here

var OriginalDataForLineChart = [{
"Date": "2013-03-12 05:09:04",
"Record":"A",
"Value": "0"
}, {
"Date": "2013-03-12 14:59:06",
"Record":"B",
"Value": "0"
}, {
"Date": "2013-03-12 14:49:04",
"Record":"C",
"Value": "0"
}, {
"Date": "2013-03-13 14:39:06",
"Record":"D",
"Value": "0"
},{
"Date": "2013-03-12 14:29:03",
"Record":"A",
"Value": "0"
}];

var line_margin = {top: 20, right: 20, bottom: 30, left: 50};
var line_width = 1100 - line_margin.left - line_margin.right;
var line_height = 100 - line_margin.top - line_margin.bottom;

var line_x = d3.time.scale()
.range([0, line_width]);

var line_y = d3.scale.linear()
.range([line_height, 0]);

var line_xAxis = d3.svg.axis()
.scale(line_x)
.orient("bottom");

var line_yAxis = d3.svg.axis()
.scale(line_y)
.orient("left");

var line_line = d3.svg.line()
.x(function(d) { return line_x(d.Date); })
.y(function(d) { return line_y(d.Value); });


var line_svg = d3.select(".chart3").append("svg")
.attr("width", line_width + line_margin.left + line_margin.right)
.attr("height", line_height + line_margin.top + line_margin.bottom)
.append("g")
.attr("transform", "translate(" + line_margin.left + "," + line_margin.top + ")");

OriginalDataForLineChart.forEach(function(d) {
d.Date = line_parseDate(d.Date);
d.Value = +d.Value;
});

line_x.domain(d3.extent(OriginalDataForLineChart, function(d) { return d.Date; }));
line_y.domain(d3.extent(OriginalDataForLineChart, function(d) { return d.Value;}));

line_svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + line_height + ")")
.call(line_xAxis);



line_svg.append("path")
.datum(OriginalDataForLineChart)
.attr("class", "line")
.attr("d", line_line);

var i = 1;
var interval = setInterval(function() {
updateLineChart(i);

if(i==OriginalDataForLineChart.length + 1) clearInterval(interval);
i++;
},1000);


var duplicateDataForLineChart = [];

function updateLineChart(index){

for(var counut2 = 0 ; counut2<index ; counut2++){
duplicateDataForLineChart[counut2] = OriginalDataForLineChart[counut2];
}

if (!line_dataCirclesGroup) {
line_dataCirclesGroup = line_svg.append('svg:g');
}

var line_circles = line_dataCirclesGroup.selectAll('.data-point').data(duplicateDataForLineChart);
//.data(data);

line_circles
.enter()
.append('svg:circle')
.attr('class', 'data-point')
.style('opacity', 1e-6);


line_circles
.attr('cx', function(d) { return line_x(d.Date); })
.attr('cy', function(d) { return line_y(d.Value); })
.attr('r', function() { return (duplicateDataForLineChart.length <= line_maxDataPointsForDots) ? line_pointRadius : 0 })
.style('fill', function(d){
if(d.Record == 'A'){
return 'RED';}
else{
return 'GREEN';
}
})
.transition()
.duration(line_transitionDuration)
.style('opacity', 1);

line_circles
.exit()
.transition()
.duration(line_transitionDuration)
// Leave the cx transition off. Allowing the points to fall where they lie is best.
//.attr('cx', function(d, i) { return line_xAxis(i) })
.attr('cy', function() { return line_y(0) })
.style("opacity", 1e-6)
.remove();

$('svg circle').tipsy({
gravity: 'width',
html: true,
title: function() {
console.log(this.__data__);
var d = this.__data__;
//var pDate = d.line_x;
return d.Date;//.toLocaleDateString();//+'</br>'+d.Date.to;
}
});

}


</script>
</body>
</html>

关于javascript - 如何根据条件更改折线图中数据点的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33330401/

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