浮标根据数据动态显示/隐藏xaxis标签

时间:2018-12-18 07:47:52

标签: javascript jquery graph flot

我需要有关xaxis刻度标签的帮助..再次..我陷入了这个问题。 我想在数据为0时隐藏xaxis的ticklabel和bar。

$(function() {

var statement = [
    [gd(2018, 6, 1), 0],
    [gd(2018, 6, 2), 2000000],
    [gd(2018, 6, 3), 240000000],
    [gd(2018, 6, 4), 260000000],
    [gd(2018, 6, 5), 280000000],
    [gd(2018, 6, 6), 300000000],
    [gd(2018, 6, 7), 320000000],
    [gd(2018, 6, 8), 0],
    [gd(2018, 6, 9), 0],
    [gd(2018, 6, 10), 0],
    [gd(2018, 6, 11), 0],
    [gd(2018, 6, 12), 0],
    [gd(2018, 6, 13), 0],
    [gd(2018, 6, 14), 0],
    [gd(2018, 6, 15), 0],
    [gd(2018, 6, 16), 0],
    [gd(2018, 6, 17), 0],
    [gd(2018, 6, 18), 0],
    [gd(2018, 6, 19), 0],
    [gd(2018, 6, 20), 0],
    [gd(2018, 6, 21), 0],
    [gd(2018, 6, 22), 0],
    [gd(2018, 6, 23), 0],
    [gd(2018, 6, 24), 0],
    [gd(2018, 6, 25), 0],
    [gd(2018, 6, 26), 0],
    [gd(2018, 6, 27), 0],
    [gd(2018, 6, 28), 0],
    [gd(2018, 6, 29), 0],
    [gd(2018, 6, 30), 0]
];
var dataset = [{
    label: "Electricity Consumption",
    data: statement,
    color: "#ffa500",
    bars: {
        show: true,
        align: "center",
        barWidth: 24 * 60 * 60 * 600,
        lineWidth: 1
    }
}];


var options = {
    xaxis: {
        mode: "time",
        tickSize: [1, "day"],
        timeformat: "%d %b",
        tickLength: 0,
        rotateTicks: 135,
        axisLabel: "",
        axisLabelUseCanvas: true,
        axisLabelFontSizePixels: 8,
        axisLabelFontFamily: "Verdana, Arial",
        axisLabelPadding: 5,
        color: "black"
    },
    yaxes: [{
        position: "left",
        color: "black",
        axisLabel: "Usage",
        axisLabelUseCanvas: true,
        axisLabelFontSizePixels: 12,
        axisLabelFontFamily: "Verdana, Arial",
        axisLabelPadding: 3,
        tickDecimals: 0,
        tickFormatter: function numberWithCommas(x) {
            return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
        }
    }],
    legend: {
        container: $("#legendContainer"),
        noColumns: 2,
        labelBoxBorderColor: "#000000",
        position: "nw"
    },
    grid: {
        hoverable: true,
        borderWidth: 1,
        backgroundColor: {
            colors: ["#ffffff", "#EDF5FF"]
        }
    }
};


$.plot($("#placeholder"), dataset, options);

function gd(year, month, day) {
    console.log(Date.UTC(year, month - 1, day), new Date(Date.UTC(year, month - 1, day)));
    return Date.UTC(year, month - 1, day);
}

});

这是相应的jfiddle(由Raidri提供)。

我想从7月8日至31日隐藏标签,因为用法为0,但仍显示7月1日。

有没有办法做到这一点?我只有这个设置

xaxis: {show:false}

1 个答案:

答案 0 :(得分:0)

例如,要仅显示某些刻度标签,可以对x轴使用条件tickFormatter函数

tickFormatter: function (tickValue, axis) {
    // looking for datapoint at this tick
    var value = dataset[0].data.find(dp => dp[0] == tickValue);
    if ((tickValue == dataset[0].data[0][0]) || (value && value[1] > 0.0)) {
        // first tick on the axis or a tick where the value is greater zero
        var tickDate = new Date(tickValue);
        // manually setting the tick format
        return (tickDate.getDate() < 10 ? ('0' + tickDate.getDate()) : tickDate.getDate()) + '<br />' + monthNames[tickDate.getMonth()];
    }
    else {
        return '';
    }
}

有关完整示例,请参见此updated fiddle