3

我正在使用 flot.js 和 flot 的类别插件来创建使用类别而不是 x 轴刻度的数值的条形图。

当我只使用一个系列时,它的效果非常好:http: //jsfiddle.net/tFy46/1/

但是,当我使用多个系列时,结果会堆叠在一起,而不是并排排列。

我的代码:

var graphCanvas = $('#graph-canvas'),
    graphData = [
        {
            "color": "#f00",
            "data": [
                ["1st Nov to 3rd Nov", 5.304752, 4.04044192]
            ]
        },
        {
            "color": "#0f0",
            "data": [
                ["1st Nov to 3rd Nov", 8.80990656, 3.86548928],
                ["11th Nov to 15th Nov", 37.89327872, -2.1734158399999997]
            ]
        }
    ],
    graphOptions = {
        legend: {
            show: false
        },
        series: {
            bars: {
                align: 'center',
                fill: 0.7,
                show: true
            }
        },
        xaxis: {
            mode: 'categories'
        }
    };
$.plot(graphCanvas, graphData, graphOptions);

http://jsfiddle.net/tFy46/

(之前有人问过如何使用 flot js 绘制按类别分组的多个条形图,但“使用 flot 'categories' 插件”的公认答案不是一个有用的答案。)

4

1 回答 1

7

“使用 flot 'categories' 插件”不是一个有用的答案?:)

我会放弃它并自己标记数据(这就是插件在后台所做的所有事情):

graphData = [
    {
        "color": "#f00",
        "data": [
            [0, 5.304752, 4.04044192]
        ]
    },
    {
        "color": "#0f0",
        "data": [
            [0.5, 8.80990656, 3.86548928],
            [1.5, 37.89327872, -2.1734158399999997]
        ]
    }
],
graphOptions = {
    legend: {
        show: false
    },
    series: {
        bars: {
            align: 'center',
            fill: 0.7,
            show: true,
            barWidth: 0.45
        }
    },
    xaxis: {
        ticks: [[0.25,"1st Nov to 3rd Nov"],[1.5,"11th Nov to 15th Nov"]]
    }
};

更新了小提琴

在此处输入图像描述

于 2013-11-12T14:45:29.893 回答