【发布时间】:2013-10-08 03:59:19
【问题描述】:
如果这似乎是一个重复的 D3 问题,我们深表歉意。我花了 2 天时间试图弄清楚如何做到这一点。
我正在尝试创建一个多折线图,其中 x 轴作为序数比例,y 轴作为正常线性比例。我所看到的一切都涉及结合使用时间和线性比例,我似乎无法转换示例以使它们按我想要的方式工作。
这是我的 JSON 数据示例:
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 },
];
这是我迄今为止尝试创建图表的地方:
//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);
我不知道如何为 4 个不同的“供应商”创建线条,其中“半”日期存储桶作为 X 坐标,“价值”作为 Y 坐标。理想情况下,我会在图表上有 4 条线,每个供应商一条线,每条线都有不同的颜色。
任何帮助/指导将不胜感激。
【问题讨论】:
标签: javascript graph d3.js linegraph