gpt4 book ai didi

javascript - 在 Flot 的水平条形图中移动条形图的位置

转载 作者:行者123 更新时间:2023-11-29 19:57:53 25 4
gpt4 key购买 nike

我有 2 个图表是在 Flot 中生成的。 Sample charts

我想将图表中的条形图从 x 轴移开 1 个条形图。我希望它有类似于带有 10 个柱的图形的间隙。例如,是否有更简单的方法来指定我希望条形图距 y 轴 10 个像素?

这是生成这些图表的代码:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" language="javascript" src="./flot/jquery-1.8.2.js"></script>
<script type="text/javascript" language="javascript" src="./flot/jquery.flot.js"></script>
<script type="text/javascript" language="javascript" src="./flot/jquery.flot.categories.js"></script>
<style type="text/css">
.graph-container.store-container {
height: 180px !important;
}
.graph-container.operator-container {
height: 450px !important;
}

#top-stores-plot-numbers {
height: 70px !important;
}
#top-operators-plot-numbers {
height: 340px !important;
}
</style>

<script type="text/javascript">
$(function() {
var plotOptions = {
legend: {
show: false
},
series: {
bars: {
show: true,
barWidth: 0.7,
align: "center",
horizontal: true
}
},
yaxis: {
mode: "categories",
tickLength: 2,
axisMargin: 10,
autoscaleMargin: 0.05
},
xaxis: {
autoscaleMargin:0.1,
min: 0,
ticks: 2
}
};

var sn_data = [
[ 6087, "ACME Store"],
];
var sn_ticks = [
[ 0, "ACME Store"],
];
var sn_options = plotOptions;
sn_options["yaxis"]["ticks"] = sn_ticks;
sn_options["yaxis"]["max"] = 1;
$.plot("#top-stores-plot-numbers", [ sn_data ], sn_options);

var on_data = [
[ 50, "CRISTINA"],
[ 68, "CASHIER2"],
[ 96, "MIESZKO"],
[ 115, "CASHIER1"],
[ 209, "TRAINING 1"],
[ 379, "JADE"],
[ 640, "HOST1"],
[ 711, "CAROLINA"],
[ 795, "SUPERVISER"],
[ 1376, "TRAINING"],
];
var on_ticks = [
[ 0, "CRISTINA"],
[ 1, "CASHIER2"],
[ 2, "MIESZKO"],
[ 3, "CASHIER1"],
[ 4, "TRAINING 1"],
[ 5, "JADE"],
[ 6, "HOST1"],
[ 7, "CAROLINA"],
[ 8, "SUPERVISER"],
[ 9, "TRAINING"],
];
var on_options = plotOptions;
on_options["yaxis"]["ticks"] = on_ticks;
on_options["yaxis"]["max"] = 10;
$.plot("#top-operators-plot-numbers", [on_data], on_options);
});
</script>
</head>
<body>
<div class="graph-container store-container">
<h2>Top 10 by Number</h2>
<div id="top-stores-plot-numbers" class="graph-placeholder"></div>
</div>
<div class="graph-container operator-container">
<h2>Top 10 by Number</h2>
<div id="top-operators-plot-numbers" class="graph-placeholder"></div>
</div>
</body>
</html>

最佳答案

您需要更改图表之间的 autoscaleMargin,而不是 y 轴的 max

当您有 1 个柱时,将其设置为 0.5。当您有 10 个柱时,将其设置为 0.05。去掉对 yaxis.max 的任何提及,这样就可以了。

我在 plotOptions 中将您的 yaxis 对象更改为如下所示:

            yaxis: {
mode: "categories",
tickLength: 2,
autoscaleMargin: 0.5
}

这就是 1 个条形图所需的全部内容。

在为第二张图调用 $.plot 之前,像这样更改 autoscaleMargin:

            on_options["yaxis"]["autoscaleMargin"] = 0.05;

就是这样,看看它的实际效果(减去类别插件):http://jsfiddle.net/ryleyb/tVmTt/

关于javascript - 在 Flot 的水平条形图中移动条形图的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15656196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com