繁体   English   中英

jQuery Flot Chart为每个系列添加标签

[英]jQuery Flot Chart add label per series

我正在使用http://www.flotcharts.org/库绘制2个相连的系列。 我可以成功创建Graph,您可以在下面看到我的JSFIDDLE。

要做:问题是我想在每个系列的顶部中心添加一个标签。 我想以这样一种方式添加标签:如果调整图的大小,它将保持在系列的顶部中心。 我怎样才能做到这一点 ?

图片:

在此处输入图片说明

JSFIDDLE: http : //jsfiddle.net/bababalcksheep/ktZ5X/2/

HTML / CSS:

html,正文{高度:100%; }

JS:

var options = {
    series: {
        lines:  { show: true,lineWidth: 2,fill: true},
        points: { show: true,radius: 3,lineWidth: 1},
        shadowSize: 2
    },
    legend: { show: false}
};

var DATA = [{
    "label": "Group-1",
        "data": [
        [0.25, 0.25],
        [0.5, 0.5],
        [0.875, 0.875],
        [1, 1]
    ]
}, {
    "label": "Group-2",
        "data": [
        [1, 0.5],
        [1.25, 0.25],
        [1.5, 0.5],
        [1.88, 0.875],
        [2, 1]
    ]
}];
var plot = $.plot("#placeholder", DATA, options);

我同意@iLikePrograms,将其添加到挂接事件中是最好的方法。 这是最新的小提琴

在您的选项中添加:

hooks: { drawSeries : [addLabel] }

其中addLabel函数为:

addLabel = function(plot, ctx, series){
    var xaxis = series.xaxis;
    var yaxis = series.yaxis;
    // space midway on series
    var x = (series.data[series.data.length-1][0] + series.data[0][0])/2;
    // just below top of plot
    var y = yaxis.max - (yaxis.tickSize / 2);
    ctx.font = "16px 'Segoe UI'";
    ctx.fillStyle = "black";
    var text = series.label;
    ctx.fillText(text, xaxis.p2c(x), yaxis.p2c(y)); // add the label
} 

您可以为此使用注释插件。 请参见此更新的小提琴

新选项:

    comment: {
        show: true
    },
    comments: [{
        x: 0.625,
        y: 1,
        contents: "Group-1"
    }, {
        x: 1.5,
        y: 1,
        contents: "Group-2"
    }]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM