score:1
Accepted answer
First, the scale functions x
and y
should be used to position your circles, not the xAxis
and yAxis
functions.
Second, you keep using d.x
and d.y
but your data does not have x
and y
properties (hint, it does have "creat_time" and "roundTripTime" though).
Third, you are very confused about how to handle the enter
and update
pattern. On enter
just do the append. On update
do the positioning.
Forth, only put the things you wish to transition after the .transition()
call (ie the opacity).
Putting all the advice together:
<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="https://rawgit.com/jaz303/tipsy/master/src/stylesheets/tipsy.css" rel="stylesheet" type="text/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: 16px;
}
.tick line{
opacity: 0.2;
}
</style>
</head>
<body>
<script>
var xAxisGroup = null,
dataCirclesGroup = null,
dataLinesGroup = null;
var maxDataPointsForDots = 50,
transitionDuration = 1000;
var pointRadius = 4;
var data = [{
"creat_time": "2013-03-12 05:09:04",
"record_status": "ok",
"roundTripTime": "0"
}, {
"creat_time": "2013-03-12 14:59:06",
"record_status": "ok",
"roundTripTime": "0"
}, {
"creat_time": "2013-03-12 14:49:04",
"record_status": "ok",
"roundTripTime": "0"
}, {
"creat_time": "2013-03-13 14:39:06",
"record_status": "ok",
"roundTripTime": "0"
},{
"creat_time": "2013-03-12 14:29:03",
"record_status": "ok",
"roundTripTime": "0"
}];
var margin = {top: 20, right: 20, bottom: 30, left: 50};
var width = 960 - margin.left - margin.right;
var height = 100 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.creat_time); })
.y(function(d) { return y(d.roundTripTime); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
data.forEach(function(d) {
d.creat_time = parseDate(d.creat_time);
d.roundTripTime = +d.roundTripTime;
});
x.domain(d3.extent(data, function(d) { return d.creat_time; }));
y.domain(d3.extent(data, function(d) { return d.roundTripTime;}));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
// Draw the points
if (!dataCirclesGroup) {
dataCirclesGroup = svg.append('svg:g');
}
var circles = dataCirclesGroup.selectAll('.data-point')
.data(data);
circles
.enter()
.append('svg:circle')
.attr('class', 'data-point')
.style('opacity', 1e-6);
circles
.attr('cx', function(d) { return x(d.creat_time) })
.attr('cy', function(d) { return y(d.roundTripTime) })
.attr('r', function() { return (data.length <= maxDataPointsForDots) ? pointRadius : 0 })
.transition()
.duration(transitionDuration)
.style('opacity', 1);
circles
.exit()
.transition()
.duration(transitionDuration)
// Leave the cx transition off. Allowing the points to fall where they lie is best.
//.attr('cx', function(d, i) { return xAxis(i) })
.attr('cy', function() { return 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.x;
return d.creat_time.toLocaleDateString();
}
});
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- D3 line chart does not show tool tip and data points properly
- D3.js horizontal line chart does not show tool tip properly
- D3 bar chart does not dynamically update y-axis and x-axis properly
- How to show the unhighlighted/ not selected data points on scatter plots when using brush/group in dc.js? And is multiple brushes possible in dc.js
- Django nvd3 Line Chart tool tip not working in case of intersection two series
- D3 line chart and adding data points real time
- NVD3 stacked bar chart does not show x-axis properly
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- Data points and ticks in the scaleBand axis are not aligned
- D3 - Large GeoJSON File does not show draw map properly using projections
- How to hide and show points on a line graph
- d3 multiline update path and transition path does not work with nested data
- nvd3 line chart not shown properly. (dots and shaded area)
- Visualization data inserted in the webpage is not properly indented and not appearing at desired position
- Slice and bounce effect not working properly in pie chart
- d3 line and area charts not updating with new data array
- NVD3 Line Chart doesn't display when data passed using ajax - data.map is not a function
- d3 Bar chart with a tool tip is not working
- Incomplete line chart with missing points and missing lines
- d3js - line chart `circle` placement to the lines are not properly sets
- D3 bar chart not working properly with all negative and positive values
- Line and Rect intersection not calculated properly on one axis
- Multiline chart x-axis ticks not aligned with data points
- d3 line and points on map from csv data
- Chart does not reflect active entries unless I set it to array.slice() in the markup -- this will disable selecting points - Stackblitz inside
- D3 on Angular: Changing data object does not refresh chart
- Line chart not hitting the right value on chart and has a smooth line
- How do you get JSON data and create a line chart with d3.js?
- D3 Multi Line Chart Not Displaying Properly With X Axis Value
- Why does my bar chart using scaleBand not line up with my tick marks?
More Query from same tag
- D3 Tree layout visualization - Inherit child with multiple parents
- D3: ordinal scale not working with array of objects
- Add caption to d3 line element
- D3js force duplicate nodes on enter()
- D3: How to dynamically refresh a graph by changing the data file source?
- How to select elements in a forEach loop? D3
- Multidonut chart d3 legendds not working
- d3v4 - zooming (Equivalent to d3.zoom.x)
- javascript / d3 - images in tooltip
- d3.event's x and y coordinates are incorrect while showing tooltip
- D3JS: set colour gradient above a certain threshold
- Draw D3 Simple Line chart With an Array
- get bounds of featureCollection in d3
- d3.js axis labels - color not changing
- d3 text-wrap plugin changes coordinates
- How to integrate `d3js` graph with angular directive
- when i call d3.behavior.zoom my graph range.domain() becomes empty
- How to add marking in bar chart using d3
- d3 locale time format
- Creating a dynamic list of DIVs with D3
- dc scatter plot binding onClick event
- Error: <path> attribute d: expected number - when trying to build a line chart with D3
- Multi Line Graph D3.js
- Toggle D3 shape depending on Boolean
- In d3 selection method chaining, how can I reference the values of the selected element?
- d3.js: barchart with time series - 1 tick & bar per date, rounding numbers
- d3.js - group 2 data values in a stacked bar chart
- JSON instead of CSV for bubble chart
- how to iterate over local directory in javascript
- Simple D3 demo not working with altered structure