score:2

Accepted answer

I have removed for loop because its not needed. We can add a callback function to check for 'A' type records and set 'fill' attribute to RED color if it satisfies the condition.

<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>

score:0

Take a look at this WEBSITE http://jsbin.com/kipakaloho/1/edit?html,js,output You can easily change the code and see what will happen. Look at the code that I used in Chart.js 2.2 to change the color of a specific point. Basically you just have to declare in Config of your chart a empty list, then change the color as you wish. Or take a look at the JS code:

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);

Best Regards, Daniel


Related Query

More Query from same tag