我使用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>
答案 0 :(得分:2)
我已经删除了for循环,因为它不需要。 我们可以添加一个回调函数来检查'A'类型记录,如果满足条件,则将'fill'属性设置为RED颜色。
<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>
答案 1 :(得分:0)
看看这个网站 http://jsbin.com/kipakaloho/1/edit?html,js,output 您可以轻松更改代码并查看将要发生的情况。查看我在Chart.js 2.2中使用的代码来更改特定点的颜色。基本上你只需在图表的Config中声明一个空列表,然后根据需要更改颜色。 或者看一下JS代码:
var canvas = document.getElementById('updating-chart'),
ctx = canvas.getContext('2d'),
startingData = {
labels: [1, 2, 3, 4, 5, 6, 7],
datasets: [
{
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: [],
pointStrokeColor: "#fff",
pointBackgroundColor : [],
pointRadius: 20,
data: [65, 59, 80, 81, 56, 55, 40]
},
]
};
// Reduce the animation steps for demo clarity.
var myLiveChart = new Chart(ctx,{
type: 'line',
data: startingData
});
setInterval(function(){
// Update one of the points in the first dataset
//index [2] correspond to the myLiveChart.data.datasets[0].data[2] from my data
myLiveChart.data.datasets[0].pointBackgroundColor[2]= "rgb(0,0,0)"
myLiveChart.update();
}, 500);
最诚挚的问候, 丹尼尔