我试图从1个系列中绘制一张1440点(24小时)的图表,每个点都有自己的颜色。 我能够用钩子绘制它,描述here,但我在线上有丑陋的边框和阴影。我也无法填充x轴和线之间的空间。 情节也没有读取这些选项:
var plot = $.plot(
$('.LCGraph'),[
{
data: d2,
points: {
show: true
}
}
],{
grid: {
hoverable: false,
clickable: false,
borderWidth: {
top: 0.1,
right: 0.1,
left: 0.1,
bottom: 0.1
},
borderColor: {
top: "#AAAAAA",
left: "#AAAAAA",
right:"#AAAAAA",
bottom:"#AAAAAA"
},
},
bars: {
show: false
},
xaxis: {
show: true
},
hooks: {
draw: [raw]
}
});
我该如何解决这个问题?
P.S。使用了这个钩子:
$(function () {
var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];
var colors = ["#cc4444", "#ff0000", "#0000ff", "#00ff00"];
var radius = [10, 20, 30, 40];
function raw(plot, ctx) {
var data = plot.getData();
var axes = plot.getAxes();
var offset = plot.getPlotOffset();
for (var i = 0; i < data.length; i++) {
var series = data[i];
for (var j = 0; j < series.data.length; j++) {
var color = colors[j];
var d = (series.data[j]);
var x = offset.left + axes.xaxis.p2c(d[0]);
var y = offset.top + axes.yaxis.p2c(d[1]);
var r = radius[j];
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2,true);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
}
};
var plot = $.plot(
$("#placeholder"),
[{ data: d2, points: { show: true } }],
{ hooks: { draw : [raw] } }
);
});
答案 0 :(得分:2)
以防有人寻找解决方案。
我已经覆盖了钩子,手动将线条画到X轴上。
function raw(plot, ctx) {
var data = plot.getData();
var axes = plot.getAxes();
var offset = plot.getPlotOffset();
var bottom = axes.yaxis.p2c(0)+offset.top;
for (var i = 0; i < data.length; i++) {
var series = data[i];
for (var j = 0; j < series.data.length; j++) {
var color = colors[j];
var d = (series.data[j]);
var x = offset.left + axes.xaxis.p2c(d[0]);
var y = offset.top + axes.yaxis.p2c(d[1]);
var r = 0.9;
ctx.lineWidth = 0.4;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.arc(x,y-r,r,0,Math.PI*2,true);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
ctx.shadowSize = 0;
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(x, bottom);
ctx.stroke();
ctx.closePath();
ctx.fill();
}
}
}
也许它可以被优化,但它可以正常工作。为了防止这种丑陋的flot颜色,我改变了绘图的选项以隐藏实际的图形:
var plot = $.plot(
$('#graphContainer'),[
{
data: d2,
points: {
show: true
}
}
],{series:{
shadowSize: 0,
color: "rgba(0,0,0,0)",
lines: {
lineWidth: 0.1,
fill: false,
show: false,
color: "rgba(0,0,0,0)"
},
points: {
shadowSize: 0,
lineWidth: 0.1,
fill: false,
show: false,
color: "rgba(0,0,0,0)"
}
},
grid: {
backgroundColor: "#F1F1F1",
hoverable: false,
clickable: false,
borderWidth: {top: 0.1, right: 0.1, left: 0.1, bottom: 0.1},
borderColor: {
top: "#AAAAAA",
left: "#AAAAAA",
right:"#AAAAAA",
bottom:"#AAAAAA"
},
},
bars: {
show: false
},
xaxis: {
show: true,
},
hooks: {
draw: [raw]
}
});
所以你需要d2-Array和你的数据和颜色 - 每个点都有颜色的数组。 d2和颜色的长度应该相同。您还可以更改var r
中ctx.lineWidth
和function raw(plot, ctx)
的值,以获得您想要的外观。