I am currently working on implementing a line chart using data in JSON format following the steps outlined in this tutorial:
[{"Machine":"S2","Data":[{"Percentage":0,"Week":33,"Year":2014,"Monday":"11/08/14","Items":0},{"Percentage":0,"Week":34,"Year":2014,"Monday":"18/08/14","Items":0},{"Percentage":0,"Week":35,"Year":2014,"Monday":"25/08/14","Items":0},{"Percentage":0,"Week":36,"Year":2014,"Monday":"01/09/14","Items":0},{"Percentage":1.141848548192784,"Week":37,"Year":2014,"Monday":"08/09/14","Items":2},{"Percentage":58.264732011508123,"Week":38,"Year":2014,"Monday":"15/09/14","Items":4},{"Percentage":0,"Week":39,"Year":2014,"Monday":"22/09/14","Items":0}]},{"Machine":"S3","Data":[{"Percentage":0,"Week":33,"Year":2014,"Monday":"11/08/14","Items":0},{"Percentage":0,"Week":34,"Year":2014,"Monday":"18/08/14",...
<p>This is the code I have implemented:</p>
<pre><code>function CreateOeeChart(json) {
// Define canvas / graph dimensions
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse date/time values causing issues
var parseDate = d3.time.format("%d/%m/%Y").parse;
// Set ranges for x and y axes
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Axes definitions
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Line definition
var line = d3.svg.line()
.x(function(d) { return x(d.Monday); })
.y(function(d) { return y(d.Percentage); });
// SVG canvas creation
var svg = d3.select("#oeeChart")
.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 + ")");
// Modify date data
jsonString.forEach(function (d) {
d.Data.forEach(function (v) {
v.Monday = parseDate(v.Monday);
});
});
// Data range scaling
x.domain(d3.extent(jsonString, function (d) {
d.Data.forEach(function (v) {
return v.Monday;
});
}));
y.domain([0, 100]);
// Loop through JSON object to draw lines
jsonString.forEach(function (d) {
svg.append("path")
.attr("class", "line")
.attr("d", line(d.Data));
});
// Adding X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Adding Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
}
Encountering an error "Invalid value for path attribute..." while trying to append path elements could be due to incorrect data formatting or parsing. Double-check the JSON structure and how data is being extracted for plotting.