CadentOrange
CadentOrange

Reputation: 3343

Shifting the position of bars in a horizontal bar chart in Flot

I have 2 charts that are generated in Flot. Sample charts

I would like to move the bar in the chart with 1 bar away from the x-axis. I would like it to have gaps similar to the graph with 10 bars. For example, is there a simpler way of specifying that I want the bar to be 10 pixels away from the y-axis?

This is the code that generates these charts:

<!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>

Upvotes: 2

Views: 6269

Answers (1)

Ryley
Ryley

Reputation: 21226

You need to change the autoscaleMargin between graphs, not the max for the y-axis.

When you have 1 bar, set it to 0.5. When you have 10 bars, set it to 0.05. Take out any mention of yaxis.max and that should do it.

I changed your yaxis object to look like this in plotOptions:

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

That's all you need for the 1 bar graph.

Before calling $.plot for the 2nd graph, change the autoscaleMargin like so:

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

That's it, see it in action (minus the categories plugin): http://jsfiddle.net/ryleyb/tVmTt/

Upvotes: 3

Related Questions