无法使用带有errorBars的jQuery Flot orderBars对齐条形

时间:2015-12-17 19:36:55

标签: javascript jquery flot

我试图拥有一个多系列条形图(使用orderBars),其中中间系列需要显示errorBars以进行变量采样。我似乎无法让网格在网格中正确对齐,我无法弄清楚如何使其工作。现在到我完成的地方,左边和右边右栏正确对齐,但中间栏跳起来并没有正确对齐。

Flot bars

这是我目前的编码:

$(function() {
    var figure0 = [{
        label: "Cost 1", 
        data: [[1,5229.7], [2,4496.8], [3,4307.0], [4,4205.6], [5,3809.7]],
        bars: {
            order: 0
        }
    }, {
        label: "Cost 2", 
        data: [[1,4973.5,500], [2,3380.4,100], [3,3105.7,100], [4,3000.8,100], [5,2939.0,100]],
        points: {
            radius: 0,
            errorbars: "y", 
            yerr: {
                show: true,
                upperCap: "-",
                lowerCap: "-",
                radius: 5,
                color: "black"
            }
        },
        bars: {
            align: "center",
            order: 1
        }
    }, {
        label: "Cost 3", 
        data: [[1,1045.2], [2,881.8], [3,809.0], [4,850.8], [5,771.5]],
        bars: {
            order: 2
        }
    }];

    var formatFigure0 = {
        series: {
            stack: false,
            bars: {
                show: true,
                fill: true,
                barWidth: 0.2,
                lineWidth: 1,
                fillColor: { colors: [{ opacity: 1 }, { opacity: 1 }] }
            }
        },
        xaxis: {
            show: true,
            ticks: [1, 2, 3, 4, 5],
            tickDecimals: 0,
            tickSize: 1,
            axisLabel: "Quintiles"
        },
        yaxis: {
            show: true,
            tickDecimals: 0,
            tickSize: 1000,
            axisLabel: "Dollars (millions)"
        },
        legend: {
            show: true,
            position: "ne",
            margin: 10,
        },
        grid: {
            hoverable: false
        }
    };

    $.plot($("#figure0DIV"), figure0, formatFigure0);
});

1 个答案:

答案 0 :(得分:0)

errorbarsorderBars插件并没有真正协同工作。您可以让它们工作,但只有当错误栏适用于中心的栏时才能使用它们:

enter image description here

要实现这一点,我必须对您的代码进行一些更改(请参阅此fiddle):

  • 为所有三个条形图提供相同的对齐方式(当使用orderBars时,您可以完全指定无对齐方式)
  • 从第二个数据系列中的每个数据点中删除第三个值,因为这被解释为条形的偏移(跳转)(此处也删除了错误条)
  • 为错误添加新的数据数据系列(此处必须为每个数据点设置三个值)并禁用栏(此“hack”也用于Flot页面上的example

更新的代码(替换原始的第二个数据系列):

}, {
  label: "Cost 2",
  data: [
    [1, 4973.5], [2, 3380.4], [3, 3105.7], [4, 3000.8], [5, 2939.0]
  ],
  bars: {
    order: 1
  }
}, {
  data: [
    [1, 4973.5, 500], [2, 3380.4, 100], [3, 3105.7, 100], [4, 3000.8, 100], [5, 2939.0, 100]
  ],
  points: {
    radius: 0,
    errorbars: "y",
    yerr: {
      show: true,
      upperCap: "-",
      lowerCap: "-",
      radius: 5,
      color: "black"
    }
  },
  bars: {
    show: false
  },
  lines: {
    show: false
  }
},

PS:如果你想让它与所有条形的误差条一起使用,那么你必须自己计算条形的x偏移量并相应地改变误差数据系列的x值。