我从后端获取json
数据以绘制line
。我得到的问题是,即使数据是null
(20个中有5个说没有数据)我得到了line
。
我的期望是,一旦数据的值为null,我就不想绘制线条。
示例数据:
[
{
"ActualPercentage": 48.78,
"PlanPercentage": 44.05,
"Week": "2015-10-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 45.25,
"Week": "2015-11-29T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 46.45,
"Week": "2015-12-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 47.65,
"Week": "2016-01-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 48.85,
"Week": "2016-02-28T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 50.05,
"Week": "2016-03-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 51.25,
"Week": "2016-04-29T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 100,
"Week": "2016-05-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 100,
"Week": "2016-06-29T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 100,
"Week": "2016-07-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": 100, //let this line alone stop here.
"Week": "2016-08-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null, //still drawing!?
"Week": "2016-09-29T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null,
"Week": "2016-10-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null,
"Week": "2016-11-29T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null,
"Week": "2016-12-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null,
"Week": "2017-01-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null,
"Week": "2017-02-27T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null,
"Week": "2017-03-30T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null,
"Week": "2017-04-29T18:30:00.000Z"
},
{
"ActualPercentage": 100,
"PlanPercentage": null,
"Week": "2017-05-30T18:30:00.000Z"
}
]
我在这里画了两行。让第一行绘制为填充数据,第二行让它停下来找到值。
怎么做?
这是我的代码:
var that = this;
var parseDate = d3.time.format("%Y%m%d").parse;
var width = this.width;
var height = this.height;
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var toolTipScale = d3.scale.linear().domain([height + 90, 80]).range([0, 100]);
var iScale = d3.scale.linear().domain([width + 80, 80]).range([this.datas.length, 0]);
var color = d3.scale.ordinal()
.domain(["ActualPercentage", "PlanPercentage", "WeeklyActual", "WeeklyPlan"])
.range(["#FF0000", "#009933" , "#FFFFFF", "#ff0099"]);
var xAxis = d3.svg.axis()
.scale(x)
.tickSize(-height)
.ticks(d3.time.month, 1)
.tickFormat(d3.time.format("%Y-%b"))
.tickSubdivide(true)
.orient("bottom")
.outerTickSize(0); //removing right border as the way top too.
var yAxis = d3.svg.axis().scale(y).tickSize( -width ).tickSubdivide(true).orient("left").outerTickSize(0);
var line = d3.svg.line()
// .interpolate("basis")
.x(function(d) { return x(d.date); })
.defined(function(d) { return d.y; })
.y(function(d) { return y(d.temperature); });
var svg = this.svg;
var div = d3.select("body").append("div")
.attr("class", "tooltips")
.style("opacity", 0);
color.domain(d3.keys(this.datas[0]).filter(function(key) {
return key !== "Week" && key !== "Value" && key !== 'WeeklyActual' && key !== 'WeeklyPlan'
}));
this.datas.forEach(function(d) {
var dateObj = new Date(d.Week);
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
d.Week = new Date(month+'/'+day+'/'+year );
// console.log( d.Week );
});
var cities = color.domain().map(function(name) {
return {
name: name,
values: that.datas.map(function(d) {
return {date: d.Week, temperature: (+d[name]) };
})
};
});
x.domain(d3.extent(this.datas,
function(d) {
return d.Week;
})
);
y.domain([
d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.temperature; }); }),
d3.max(cities, function(c) { return d3.max(c.values, function(v) { return v.temperature; }); })
]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr('class', 'text-xaxis')
.style("text-anchor", "end")
// .attr('dx', -40)
// .attr('dy', 60)
.attr("transform", function(d) {
return "rotate(-65)"
});
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.selectAll("text")
.attr('class', 'text-yaxis')
.attr('dx', -10)
var city = svg.selectAll(".city")
.data(cities)
.enter().append("g")
.attr('id', function(d) {
return d.name;
})
.attr("class", "city");
var path = city.append("path")
.attr("class", "line")
.attr("d", function(d) {
return line(d.values);
})
.style("stroke", function(d) { return color(d.name); });
var totalLength = [
path[0][0].getTotalLength(),
path[0][1].getTotalLength()
];
path
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0);
var circles = city.selectAll("circle")
.data(function(d) {
return d.values
})
.enter()
.append("circle")
.attr("r", 3.5)
.attr("cx", function(d, i) {
return x(d.date)
})
.attr("cy", function(d) {
return y(d.temperature)
})
.style('cursor', 'pointer')
.attr("fill", "transparent")
.attr("stroke", "yellow")
.attr("stroke-width", 0);
circles.transition().duration(3000).attr("stroke-width", 2);
circles.on("mouseover", function(d, i) {
div.transition().duration(200).style("opacity", 0.9);
var data = d3.select(this.parentNode).select("path").data()[0];
var date = d.date;
var dateValues = date.getDate() +'/' + (date.getMonth() + 1) +'/' + date.getFullYear();
div.html( "<span>"+ data.name + ": </span><span>" + dateValues + '</span><span>' + d.temperature +'</span>')
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 60) + "px");
})
.on("mouseout", function(d, i) {
div.transition().duration(500).style("opacity", 0);
})
答案 0 :(得分:0)
从softwarenewbie7331
的想法我修改了这样的代码:(Y值)
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.defined(function(d) { return d.temperature })
.y(function(d) { return y(d.temperature); });
这对我来说很好。