【问题标题】:Flot dynamically show/hide xaxis label based on dataFlot 根据数据动态显示/隐藏 xaxis 标签
【发布时间】:2018-12-18 07:47:52
【问题描述】:

我需要有关 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}

【问题讨论】:

    标签: javascript jquery graph flot


    【解决方案1】:

    要仅显示一些刻度标签,您可以对 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

    【讨论】:

    • 嗨@Raidri!是你在我上一个问题中帮助了我!再次感谢你!!!感谢您的“statement.filter()”!我的问题非常具有误导性,我的意思是保留 30 个刻度/柱,但隐藏 6 月 8 日至 31 日的 xaxis 标签。我发现了一个重复的问题,但不幸的是,没有发布解决方案。 :(stackoverflow.com/questions/17723904/…我希望你能帮我指出正确的方向!
    • 更新后的答案正是我需要的答案!我刚开始使用 Flot,您的意见对我很有帮助!我也会研究你的代码输入。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2022-07-05
    相关资源
    最近更新 更多