Argument passed D3-Tip callback is full data array - javascript

I am working with Techan JS, with d3 tip plugin.
Callback function of D3-Tip is passed the whole data array instead of object in context. I am probably hooking it on the wrong place.
/* Initialize tooltip */
var tip = d3.tip().attr('class', 'd3-tip').html(function(d) {
console.log(d);
return d;
});
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse,
timeFormat = d3.time.format('%Y-%m-%d'),
valueFormat = d3.format(',.2fs');
var x = techan.scale.financetime()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var candlestick = techan.plot.candlestick()
.xScale(x)
.yScale(y);
var trendline = techan.plot.trendline()
.xScale(x)
.yScale(y)
.on("mouseenter", enter)
.on("mouseout", out)
.on("drag", drag);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("#candle-stick").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 + ")");
var valueText = svg.append('text')
.style("text-anchor", "end")
.attr("class", "coords")
.attr("x", width - 5)
.attr("y", 15);
var data = [
{ date: "9-Jun-14", open: 62.40, high: 63.34, low: 61.79, close: 62.88, volume: 37617413 },
{ date: "6-Jun-14",open: 63.37, high: 63.48, low: 62.15, close: 62.50,volume: 42442096 },
{ date: "5-Jun-14",open: 63.66, high: 64.36, low: 62.82, close: 63.19,volume: 47352368 },
{ date: "4-Jun-14",open: 62.45, high: 63.59, low: 62.07, close: 63.34,volume: 36513991 },
{ date: "3-Jun-14",open: 62.62, high: 63.42, low: 62.32, close: 62.87,volume: 32216707 },
{ date: "2-Jun-14",open: 63.23, high: 63.59, low: 62.05, close: 63.08,volume: 35995537 },
{ date: "30-May-14",open: 63.95, high: 64.17, low: 62.56, close: 63.30,volume: 45283577 }
];
var accessor = candlestick.accessor();
data = data.slice(0,200).map(function(d) {
return {
date: parseDate(d.date),
open: +d.open,
high: +d.high,
low: +d.low,
close: +d.close,
volume: +d.volume
};
}).sort(function(a, b) { return d3.ascending(accessor.d(a), accessor.d(b)); });
x.domain(data.map(accessor.d));
y.domain(techan.scale.plot.ohlc(data, accessor).domain());
svg.append("g")
.datum(data)
.attr("class", "candlestick")
.call(candlestick)
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
/* Invoke the tip in the context of your visualization */
svg.call(tip)
// functions
function enter(d) {
valueText.style("display", "inline");
refreshText(d);
}
function out(d) {
valueText.style("display", "none");
}
You can see the code live at http://jsfiddle.net/sisir/ghox8ewa/1/
(look at the console when hovering on a candlestick)

/* Initialize tooltip */
var tip = d3.tip().attr('class', 'd3-tip').html(function(d) {
console.log(d);
return d;
});
In the above tooltip you are returning d for html, change it to
/* Initialize tooltip */
var tip = d3.tip().attr('class', 'd3-tip').html(function(d, i) {
return d[i].open;//your required text here
});
Hope this is what you are looking for....
If not ask for more.
The problem in the above chart is the approach we are following,
we are drawing candlestick and binding tooltip to this candlestick by writing below code
svg.append("g")
.datum(data)
.attr("class", "candlestick")
.call(candlestick)
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
from the above code that will return a 'g' of class 'candlestick'
(to view that write this code
var candle= svg.append("g")
.datum(data)
.attr("class", "candlestick")
.call(candlestick)
console.log(candle);
) and we are binding tooltip to that so it is returning data(whole data object) as argument to our tooltip,
To fulfill our requirement,
I've implemented and developed required code in the below fiddle
http://jsfiddle.net/ghox8ewa/5/
Kindly follow the link and try to analyze....
If you still have doubts ask me for more.

Related

Error: <rect> attribute y: Expected length, "NaN". D3 JS

I am trying D3 to develop new barchart. This is my first try to d3
I followed one article Here and based on it I am trying to develop one barchart by writing below code.
My code look like
function get_data() {
console.log("create post is working!") // sanity check
return $.ajax({
url : "/group/guest/query/", // the endpoint
type : "GET", // http method
});
};
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
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")
.ticks(10, "");
var svg = d3.select("#chart").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 + ")");
var ajdata = get_data();
var k = [];
ajdata.success(function (data) {
var obj = jQuery.parseJSON(data);
obj.forEach(function(d) {
d.created_date = d.created_date;
d.jobs_fail = +d.jobs_fail;
k.push(d.created_date)
});
x.domain(obj.map(function(d) {
return d.date_created;
}));
y.domain([0, d3.max(obj, function(d) {
//alert(JSON.stringify(d.jobs_fail));
return d.jobs_fail;
})]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Count");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.date_created);
})
.attr("width", x.rangeBand())
.attr("y", function(d) {
return y(d.jobs_fail);
})
.attr("height", function(d) {
return height - y(d.jobs_fail);
});
function type(d) {
d.jobs_fail = +d.jobs_fail;
return d;
}
});
I am pulling the data form one ajax request and the ajax response looks like below.
[ { "date": "2017-12-28", "jobs_fail": 2, "jobs_resub": 7, "jobs_success": 18 }, { "date": "2017-12-27", "jobs_fail": 20, "jobs_resub": 31, "jobs_success": 50 }, { "date": "2017-12-26", "jobs_fail": 22, "jobs_resub": 27, "jobs_success": 49 }, { "date": "2017-12-25", "jobs_fail": 11, "jobs_resub": 8, "jobs_success": 18 }, { "date": "2017-12-24", "jobs_fail": 5, "jobs_resub": 2, "jobs_success": 4 }, { "date": "2017-12-23", "jobs_fail": 10, "jobs_resub": 16, "jobs_success": 23 }, { "date": "2017-12-22", "jobs_fail": 51, "jobs_resub": 54, "jobs_success": 97 } ]
When I ran this code I am getting the error.But my x axis dates (created_date) filed data is appearing on chart x axis.
Error: <rect> attribute y: Expected length, "NaN".
I understand this is something on y axis but I am not able to find any solution for it please guide me what might I am doing wrong here.
Hi All after spending lots of time and help from comment I am able to fix this issue.
So problem is in below piece of code.
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.date_created);
})
.attr("width", x.rangeBand())
.attr("y", function(d) {
return y(d.jobs_fail);
})
.attr("height", function(d) {
return height - y(d.jobs_fail);
});
I am storing the parsed data into a variable called obj whereas I am using the data under .data since data has no value so it is not able to attach the values just replace data with obj and it works for me.
Thanks all for your support

d3 chart refresh duplicate

I´m using d3.js library but i´m having so problems with linear graphics. It looks like on each page refresh, the graphics is replicated, and after some running time i got lots of duplicated graphics. I´m also using static data (i entered some random data for the graphic to work). Can someone help me? Why is this happening? Any solutions?
chart3-controller.js
module.exports = function Chart3Ctrl($scope) {
d3 = require('d3')
$scope.chart = function() {
var arrData = [
["2012-10-02", 200],
["2012-10-09", 300],
["2012-10-12", 150]
];
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 50
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y-%m-%d").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.date);
})
.y(function(d) {
return y(d.close);
});
var svg = d3.select("#chart3").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 +
")");
var data = arrData.map(function(d) {
return {
date: parseDate(d[0]),
close: d[1]
};
});
console.log(data);
x.domain(d3.extent(data, function(d) {
return d.date;
}));
y.domain(d3.extent(data, function(d) {
return d.close;
}));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
}
}
chart3.pug
.widget-container.fluid-height.stf-cpu(ng-controller='Chart3Ctrl')
.heading
i.fa
span(translate) Chart 3
.widget-content.padded
div#chart3(style='width:100%; height:100%;')
{{ chart() }}
Please any help welcome! :)
Thanx
Have you tried to remove the chart at the top of the function?
so;
d3.selectAll('#chart3 svg').remove();
this would remove any instance of the chart before re drawing, a better approach might be to wrap inside an if statement to check if the svg exists first and then drawing it if it doesn't, using jquery this could be ;
if(!$('#chart3 svg').length > 0) {
run code here...
}

d3 Error: <path> attribute d: Expected number date

So I've found a half dozen posts regarding this error but none seem to resolve my issue.
The data as it comes from firebase:
data = [{percent:24,year:1790},....]
var parseDate = d3.time.format("%Y").parse;
// so I want to convert the year to a 'date' for d3 time scale
data.forEach(function(d) {
d.year = parseDate(d.year.toString());
d.percent = +d.percent;
});
which then the data looks like
console.log(data);
[{percent:24,year:Fri Jan 01 1790 00:00:00 GMT-0500 (EST)}...]
my x scale
var x = d3.time.scale().range([0, this.width]);
my x domain
x.domain(d3.extent(data, function(d){return d.year; }));
my line
var line = d3.svg.line()
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.percent); });
then my chart (there is a base chart that this is extending)
this.chart.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
Which when added has the exception ...
Error: <path> attribute d: Expected number,
UPDATE
So here is the working code using sample data the code is more or less a copy paste from d3 example site using a csv instead of tsv
This works fine
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatDate = d3.time.format("%Y");
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.date); })
.y(function(d) { return y(d.close); });
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 + ")");
d3.csv("./data/debt-public-percent-gdp.csv", type, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.close; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
function type(d) {
d.date = formatDate.parse(d.date);
d.close = +d.close;
return d;
}
This does not work
Using the same datasource here is my 'Base Chart'
export class BaseChart{
constructor(data,elem,config = {}){
var d3 = require('d3');
this.data = data;
this.svg = d3.select('#'+elem).append('svg');
this.margin = {
left: 30,
top: 30,
right: 30,
bottom: 30,
};
let width = config.width ? config.width : 600;
let height = config.height ? config.height : 300;
this.svg.attr('height', height);
this.svg.attr('width', width);
this.width = width - this.margin.left - this.margin.right;
this.height = height - this.margin.top - this.margin.bottom;
this.chart = this.svg.append('g')
.attr('width', this.width)
.attr('height', this.height)
.attr('transform', `translate(${this.margin.left},${this.margin.top})`);
}
}
Chart that Extends the Base Chart
export class FedDebtPercentGDP extends BaseChart{
constructor(data, elem, config){
super(data, elem, config);
var x = d3.time.scale().range([0, this.width]);
var y = d3.scale.linear().range([this.height, 0]);
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.close; }));
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.date); })
.y(function(d) { return y(d.close); });
this.chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + this.height + ")")
.call(xAxis);
//
this.chart.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
this.chart.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
}
}
The code that calls the chart
d3.csv('./data/debt-public-percent-gdp.csv', type, function(error, data) {
new FedDebtPercentGDP(data, "debt-to-gdp", {width: '100%'});
});
The data
date,close
1790,30
1791,29
1792,28
1793,24
1794,22
1795,19
1796,16
1797,17
1798,16
1799,16
1800,15
1801,13
1802,14
1803,14
1804,13
1805,11
1806,10
1807,10
1808,9
1809,7
1810,6
1811,6
1812,7
1813,8
1814,9
1815,10
1816,10
1817,8
1818,7
1819,7
1820,8
1821,9
1822,8
1823,8
1824,8
1825,7
1826,6
1827,6
1828,5
1829,4
1830,3
1831,2
1832,1
1833,0
1834,0
1835,0
1836,0
1837,0
1838,1
1839,0
1840,0
1841,1
1842,1
1843,2
1844,1
1845,1
1846,1
1847,2
1848,2
1849,3
1850,2
...
There are quite a few problems with the code that you've provided. I guess you did not paste it all. The excerpt you pasted definitely does not have this.chart defined, for example, so it is not possible to reconstruct your error message.
That being said, it is still possible to identify the main problem:
The error message says it all: as you have parsed d.year and turned it into a string, but the path needs a number as an argument there is a 'type mismatch'-kind of error in your code.
You can see how your data looks like in the console log that you've provided. year is turned into a string.
I would recommend leaving d.year as it is, and if you really need, you can create a new attribute called for example date if you need the date in that format. So if it is needed for displaying the date, you can use that one, while year can be used for path calculation in its original format.

Javascript with techan.js mouseenter and mouseout events not triggering

I am not sure if I am messing it up, kinda new to javascript, what I am trying to do is showing some text when mouse enters a specific region and hide it when it leaves. You can find a fully working example of what I am trying to do at this link.
Somehow I needed to encapsulate this code and what I did was something like below (anyways, just so you don't lose time, this code is almost a copy and paste from the link above, I just encapsulated it into a function and changed the way it consumed data):
function candlestickChart(targetContainer, data) {
var margin = { top: 20, right: 20, bottom: 30, left: 50 },
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse,
timeFormat = d3.time.format('%Y-%m-%d'),
valueFormat = d3.format(',.2fs');
var x = techan.scale.financetime()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var candlestick = techan.plot.candlestick()
.xScale(x)
.yScale(y);
var trendline = techan.plot.trendline()
.xScale(x)
.yScale(y)
.on("mouseenter", enter)
.on("mouseout", out)
.on("drag", drag);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select(targetContainer).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 + ")");
var valueText = svg.append('text')
.style("text-anchor", "end")
.attr("class", "coords")
.attr("x", width - 5)
.attr("y", 15);
accessor = candlestick.accessor();
data = data.slice(0, 3500).map(function (data) {
return {
date: new Date(data.date),
open: +data.open,
high: +data.high,
low: +data.low,
close: +data.close,
volume: +data.volume
}
}).sort(function (a, b) { return d3.ascending(accessor.d(a), accessor.d(b)); });
var trendlineData = [
{ start: { date: new Date(2014, 2, 11), value: 72.50 }, end: { date: new Date(2014, 5, 9), value: 63.34 } },
{ start: { date: new Date(2013, 10, 21), value: 43 }, end: { date: new Date(2014, 2, 17), value: 70.50 } }
];
x.domain(data.map(accessor.d));
y.domain(techan.scale.plot.ohlc(data, accessor).domain());
svg.append("g")
.datum(data)
.attr("class", "candlestick")
.call(candlestick);
svg.append("g")
.datum(trendlineData)
.attr("class", "trendlines")
.call(trendline)
.call(trendline.drag);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
function enter(d) {
valueText.style("display", "inline");
refreshText(d);
}
function out(d) {
valueText.style("display", "none");
}
function drag(d) {
refreshText(d);
}
function refreshText(d) {
valueText.text(
"Start: [" + timeFormat(d.start.date) + ", " + valueFormat(d.start.value) +
"] End: [" + timeFormat(d.end.date) + ", " + valueFormat(d.end.value) + "]"
);
}
}
After doing that, mouseenter and mouseout events stopped fireing, am I getting it wrong?

d3js charts, Use json strings instead of json file

This is my very first to d3js.I have use this d3js Line Chart Sample.But after feeding the data it doesn't draw the chart but i can see the data has been loaded by using the firebug.But the data doesn't print in the graph at all. Could n't figure out the problem.Any help will be really appreciated.
This is My code,
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b").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.timeStamp);
})
.y(function(d) {return y(d.memberAverageLoadAverage); });
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 + ")");
var json1=[
{
"clusterId": "",
"timeStamp": 1437063744524,
"memberAverageLoadAverage": 20,
"memberId": ""
},
{
"clusterId": "",
"timeStamp": 1437069850060,
"memberAverageLoadAverage": 20,
"memberId": ""
},
{
"clusterId": "",
"timeStamp": 1437069910059,
"memberAverageLoadAverage": 20,
"memberId": ""
},
{
"clusterId": "",
"timeStamp": 1437069970060,
"memberAverageLoadAverage": 20,
"memberId": ""
},
{
"clusterId": "",
"timeStamp": 1437070030056,
"memberAverageLoadAverage": 20,
"memberId": ""
}
];
root = json1;
x.domain(d3.extent(root, function(d) { return d.timeStamp; }));
y.domain(d3.extent(root, function(d) { return d.memberAverageLoadAverage; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("memberAverageLoadAverage");
svg.append("path")
.datum(root)
.attr("class", "line")
.attr("d", line);
After removing the JSON.parse for the JS object and changing the memberAverageLoadAverage to not all be the same value, I was able to display a graph. The reason I needed to change the memberAverageLoadAverage values is that the extent call was making the y axis go from 20 to 20 so the line was at the bottom of the screen and collided with the x axis line. I would suggest setting the y.domain to [0, d3.max(...)] instead of using d3.extent.
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b").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.timeStamp);
})
.y(function(d) {return y(d.memberAverageLoadAverage); });
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 + ")");
var json1=[{"clusterId":"","timeStamp":1437063744524,"memberAverageLoadAverage":11,"memberId":""},{"clusterId":"","timeStamp":1437069850060,"memberAverageLoadAverage":5,"memberId":""},{"clusterId":"","timeStamp":1437069910059,"memberAverageLoadAverage":6,"memberId":""},{"clusterId":"","timeStamp":1437069970060,"memberAverageLoadAverage":15,"memberId":""},{"clusterId":"","timeStamp":1437070030056,"memberAverageLoadAverage":20,"memberId":""}];
var data = json1;
x.domain(d3.extent(data, function(d) { return d.timeStamp; }));
y.domain([0, d3.max(data, function(d) { return d.memberAverageLoadAverage; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Should be a comment, but I suggest a couple of things. 1. I suggest using externally loaded JSON data if you have a server you can use to get the http request. If not, then you can use local data, but either way you have to have a data.foreach function in which you create all of your values, similar to the oneseen below.
data.foreach(function(d) {
d.timeStamp = d.timeStamp;
d.memberAverageLoadAverage = +d.memberAverageLoadAverage;
});
I also strongly suggest you reformat your JSON because your keys are not done phenomenally well, so I suggest you take a look here and reformat the data. Let me know how you do and what else I can help you with.
I am not sure if you can parse this. It is an array. Anyways, even if you can, I don't think there is a need to parse it.
`var json1=`[{"clusterId":"","timeStamp":1437063744524,"memberAverageLoadAverage":20,"memberId":""},

Categories