【发布时间】:2020-11-11 02:24:41
【问题描述】:
我尝试显示 7 个带有阈值的图表,查看示例,我尝试实现它,但它仅适用于第一个元素,如果有可能的话:
for(var i=0;i
},
color: {
pattern: ['red', 'blue', 'red' ],
threshold: {
//Si se pasa de este rango se pondra en rojo , si esta dentro estara en color azul
values: [arreglo[i].temperatura -10,arreglo[i].temperatura+10]
}
},
onrendered: function() {
drawThresholds(this, thresholdOpts, opts);
}
};
chart = c3.generate(opts);
function drawThresholds(chart, thOpts, chOpts)
{
console.log(chart,thOpts,chOpts);
d3.selectAll("line.myline").remove();
d3.selectAll("rect.myrect").remove();
d3.selectAll("text.mytxt").remove();
radius = chart.radius,
iradius = chart.innerRadius;
for (var j=0; j<chOpts.color.threshold.values.length;j++)//recorre los 3 valores
{ //for(var i=0; i<chOpts.color.threshold.values.length;i++)
v = chOpts.color.threshold.values[j];
col = chOpts.color.pattern[parseInt(j) + 1];
angle = Math.PI * v / 100;
x0 = (iradius * Math.cos(angle));
y0 = (iradius * Math.sin(angle));
x1 = (radius * Math.cos(angle));
y1 = (radius * Math.sin(angle));
d3.select(".c3-chart-arcs").append("line")
.attr({
x1: -x0,
y1: -y0,
x2: -x1,
y2: -y1
})
.attr('class', 'myline')
.style("stroke-width", thOpts.strokeWidth)
.style("stroke", 'black');
x1 = ((radius + thOpts.boxSize) * Math.cos(angle)) + thOpts.boxSize / 2;
y1 = ((radius + thOpts.boxSize) * Math.sin(angle)) + thOpts.boxSize / 2;
d3.select(".c3-chart-arcs").append("rect")
.attr({
width: thOpts.boxSize,
height: thOpts.boxSize,
x: -x1,
y: -y1
})
.attr('class', 'myrect')
.style("fill", thOpts.boxFill ? col : "none")
.style("stroke-width", thOpts.strokeWidth)
.style("stroke", col);
txtSize = measure(v, "mytxt");
x1 = ((radius + thOpts.boxSize) * Math.cos(angle)) + txtSize.width / 2;
y1 = ((radius + thOpts.boxSize) * Math.sin(angle)) + txtSize.height / 2 + 4;
d3.select(".c3-chart-arcs").append("text")
.attr({
x: -x1,
y: -y1
})
.attr('class', 'mytxt')
.text(v);
}
}//function
function measure(text, classname) {
if (!text || text.length === 0) return {
height: 0,
width: 0
};
container = d3.select('body').append('svg').attr('class', classname);
container.append('text').attr({
x: -1000,
y: -1000
}).text(text);
bbox = container.node().getBBox();
container.remove();
return {
height: bbox.height,
width: bbox.width
};
}
}
【问题讨论】:
标签: javascript d3.js c3.js