jquery - float 条形图数据标签左偏移

标签 jquery charts alignment flot label

我正在将数据从 xml 对象读取到数组中,总共六项。图表渲染良好,每个垂直条的所有颜色和高度都可以。当我尝试向每个垂直条添加数据标签时,标签都出现在距相关条顶部的正确高度处,但它们都与左轴对齐。

enter image description here

下面代码中 o.left 的 left 属性始终返回 NaN 值。

$.each(plot.getData()[0].data, function(i, el){

            var o = plot.pointOffset({x: el[0], y: el[1]});

            $('<div class="data-point-label">' + el[1] + '</div>').css( {
                position: 'absolute',
            --->left: o.left + 4,<--
                top: o.top - 20,
                display: 'none',
                color:'#000',
                fontSize:'12pt'
            }).appendTo(plot.getPlaceholder()).slideToggle();
        });

我不确定我将 xml 中的数据添加到数组中的方法是否不正确,或者绘图选项中的某些内容,如下所示:

$(function() {

        Init() //Load XML and load into array named data

        plot = $.plot("#placeholder", [ data ], {
        grid: {
                backgroundColor: { colors: [ "#FFF", "#FFF" ] },
                borderWidth: {
                    top: 0,
                    right: 0,
                    bottom: 0,
                    left: 0

                }
            },
            series: {
                bars: {
                    show: true,
                    lineWidth: 0, // in pixels
                    barWidth: 0.9, // in units of the x axis
                    fill: true,
                    fillColor: "#0000FF",
                    align: "center", // "left", "right", or "center"
                    horizontal: false,
                    zero: true,
                }
            },
            xaxis: {
                mode: "categories",
                tickLength: 0,
                autoscaleMargin: 0.1
            }
        }); 

这可能与上面的模式:“类别”行有关,因为 x 轴包含文本值?有什么指点吗?

最佳答案

是的,你的假设是正确的。如果您检查 plot.getData()[0].data:

>plot.getData()[0].data
  [
    Array[2]
      0: "Team 1"
      1: 3
      length: 2
      __proto__: Array[0]
    , 
    ...

因此,el[0] 最终成为“Team 1”,这当然不适用于需要数字的 pointOffset

easiest fix to your problem是使用:

var o = plot.pointOffset({x: i, y: el[1]});  // using i instead of el[0]

这是有效的,因为这就是类别插件“在幕后”所做的事情,它将文本字符串替换为 [0,1,2,3]...

我解决这个问题的方法是停止使用类别插件。它只会造成更多的问题,而不是值得的。使用 ticks 选项,然后使用 existing code works fine .

关于jquery - float 条形图数据标签左偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25646524/

相关文章:

javascript - 在事件菜单中锚定菜单不同的 css

Jquery UI 选项卡在加载时禁用

javascript - 使用 Jq 图和 JSON 创建饼图

html - 将水平列表与另一个 div 对齐

html - Textarea 对填充的响应不佳

javascript - 将键盘快捷键关联到鼠标事件的方法

javascript - 更简单的 Jquery Slider 代码改进

.net-4.0 - 隐藏饼图中的标签

wpf - 如何在代码 AxisLabel 中格式化 DependentRangeAxis?

android - 如何在一行 TextView 的左右两侧对齐文本?