Apologies if this seems like a duplicate D3 question. I've spent 2 days trying to figure out how to do this.
I'm trying to create a multi-line chart with the x-axis as an ordinal scale, and the y axis as a normal linear scale. Everything I've seen involves using time and linear scales combined and I can't seem to convert the examples to make them work how I want.
Here's my sample JSON data:
var data =
[
{ "Supplier": "Supplier1", "Half": "2013 2H", "Value": 99.86047786 },
{ "Supplier": "Supplier1", "Half": "2013 1H", "Value": 93.86047786 },
{ "Supplier": "Supplier1", "Half": "2012 2H", "Value": 98.86047786 },
{ "Supplier": "Supplier1", "Half": "2012 1H", "Value": 96.86047786 },
{ "Supplier": "Supplier2", "Half": "2013 2H", "Value": 97.86047786 },
{ "Supplier": "Supplier2", "Half": "2013 1H", "Value": 91.86047786 },
{ "Supplier": "Supplier2", "Half": "2012 2H","Value": 93.86047786 },
{ "Supplier": "Supplier2", "Half": "2012 1H", "Value": 94.86047786 },
{ "Supplier": "Supplier3", "Half": "2013 2H", "Value": 92.86047786 },
{ "Supplier": "Supplier3", "Half": "2013 1H", "Value": 91.86047786 },
{ "Supplier": "Supplier3", "Half": "2012 2H", "Value": 88.86047786 },
{ "Supplier": "Supplier3", "Half": "2012 1H", "Value": 87.86047786 },
{ "Supplier": "Supplier4", "Half": "2013 2H", "Value": 88.86047786 },
{ "Supplier": "Supplier4", "Half": "2013 1H", "Value": 86.86047786 },
{ "Supplier": "Supplier4", "Half": "2012 2H", "Value": 83.86047786 },
{ "Supplier": "Supplier4", "Half": "2012 1H", "Value": 81.86047786 },
];
And here's where I've gotten so far in trying to create the chart:
//Width and height
var margin = {top: 20, right: 20, bottom: 30, left: 40};
var width = 600 - margin.left - margin.right;
var height= 500-margin.top -margin.bottom;
var w = width;
var h = height;
var xScale = d3.scale.ordinal()
.domain(data.map(function (d) {return d.Half; }))
.rangeRoundBands([margin.left, width], 0.05);
var xAxis = d3.svg.axis().scale(xScale).orient("bottom").tickSize(height - margin.bottom);
var yScale = d3.scale.linear()
.domain([0, d3.max(data, function(d) {return d.Value; })])
.range([h,0]);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + 0 + ")")
.call(xAxis);
data = d3.nest().key(function(d) { return d.Supplier; }).entries(data);
What I can't figure out is how to create the lines for the 4 different "suppliers" with the "Half" date buckets as the X coordinates and the "Value" as the Y coordinates. Ideally I would have 4 lines on the graph, one for each supplier, each with a different color.
Any help/direction would be greatly appreciated.