如何获取flot中的x轴标签?

发布于 2025-01-08 17:17:32 字数 1426 浏览 0 评论 0原文

我正在使用 Flot 来创建图表。 这是我的代码 jsFiddle 代码

function drawSeriesHook(plot, canvascontext, series) {
var ctx = canvascontext,
    plotOffset = plot.offset(),
    labelText = 'VALUE', // customise this text, maybe to series.label
    points = series.datapoints.points,
    ps = series.datapoints.pointsize,
    xaxis = series.xaxis,
    yaxis = series.yaxis,
    textWidth, textHeight, textX, textY;
// only draw label for top yellow series
if (series.color === '#F8C095') {
    ctx.save();
    ctx.translate(plotOffset.left, plotOffset.top);

    ctx.lineWidth = series.bars.lineWidth;

    ctx.fillStyle = '#000'; // customise the colour here
    for (var i = 0; i < points.length; i += ps) {
        if (points[i] == null) continue;

        textWidth = ctx.measureText(labelText).width; // measure how wide the label will be
        textHeight = parseInt(ctx.font); // extract the font size from the context.font string
        textX = xaxis.p2c(points[i] + series.bars.barWidth / 2) - textWidth / 2;
        textY = yaxis.p2c(points[i + 1]) - textHeight / 2;
        ctx.fillText(labelText, textX, textY); // draw the label
    }
    ctx.restore();
}
}
  1. 在我的drawseriesHook函数中,我需要获取像Data1这样的x轴标签, Data2,Data3,Data4
  2. 有没有办法在不使用series.label & 的情况下唯一标识最后一个数据项? series.color.目前我已经通过修改函数中的series.color来识别它

I am using Flot to create graphs.
This is my Code here jsFiddle code

function drawSeriesHook(plot, canvascontext, series) {
var ctx = canvascontext,
    plotOffset = plot.offset(),
    labelText = 'VALUE', // customise this text, maybe to series.label
    points = series.datapoints.points,
    ps = series.datapoints.pointsize,
    xaxis = series.xaxis,
    yaxis = series.yaxis,
    textWidth, textHeight, textX, textY;
// only draw label for top yellow series
if (series.color === '#F8C095') {
    ctx.save();
    ctx.translate(plotOffset.left, plotOffset.top);

    ctx.lineWidth = series.bars.lineWidth;

    ctx.fillStyle = '#000'; // customise the colour here
    for (var i = 0; i < points.length; i += ps) {
        if (points[i] == null) continue;

        textWidth = ctx.measureText(labelText).width; // measure how wide the label will be
        textHeight = parseInt(ctx.font); // extract the font size from the context.font string
        textX = xaxis.p2c(points[i] + series.bars.barWidth / 2) - textWidth / 2;
        textY = yaxis.p2c(points[i + 1]) - textHeight / 2;
        ctx.fillText(labelText, textX, textY); // draw the label
    }
    ctx.restore();
}
}
  1. In My drawseriesHook function i need to get the x axis labels like Data1,Data2,Data3,Data4
  2. Is there any way to uniquely identify the last data item without using series.label & series.color.Currently i have identified it by modifying the series.color in the function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2025-01-15 17:17:32

1.) 好吧,我想你想为每个栏设置labelText。在 for 循环中添加以下行:

labelText = series.xaxis.ticks[i / ps].label;

另请参阅更新的示例

2.) 您可以向 data 添加自己的值,例如标记最后一个:

var data = [
    {
        label : 'Used',
        color : '#EF7816',
        data : [ [ 1, 85], [ 2, 50 ], [ 3, 18], [ 4, 8 ] ],
        last : false
    },
    ...
    {
        color : '#F8C095',
        data : [ [ 1, 12], [ 2, 10], [ 3, 3], [ 4, 2] ],
        last : true
    }
];

在您的钩子中,您可以检查标志:

if (series.last) { ...

另请参阅下一个 更新示例

1.) Ok, I think you want to set labelText for each bar. Add the following line in the for-loop:

labelText = series.xaxis.ticks[i / ps].label;

Also see the updated example.

2.) You can add own values to data, e.g. to mark the last one:

var data = [
    {
        label : 'Used',
        color : '#EF7816',
        data : [ [ 1, 85], [ 2, 50 ], [ 3, 18], [ 4, 8 ] ],
        last : false
    },
    ...
    {
        color : '#F8C095',
        data : [ [ 1, 12], [ 2, 10], [ 3, 3], [ 4, 2] ],
        last : true
    }
];

In your hook you can check the flag:

if (series.last) { ...

Also see the next updated example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文