Search code examples
javascriptjqueryflot

Jquery Flot wrong graph when repeating number


I'm receiving this: [[8,2],[13,1],[17,1],[18,1],[7,1]] on a ajax call and passing it to a jquery flot.

The problem is that the graph is starting on [7,1] its supposed to start on [8,2] and end on [7,1] but its not and its causing an weird graph:

see attached pic

Here is my script:

$.ajax({
        url: "{{ URL::to('/ajaxcall') }}",
        method: 'GET',
        dataType: 'json',
        success: onOutboundReceived
    });

    function onOutboundReceived(series) {
        var series = series;
        //$.plot($('#site_tay'), finalData, options);
        var plot_statistics = $.plot($("#site_tay"), [{
            data: series,
            label: "Cotizaciones"
        }
        ], {
            series: {
                lines: {
                    show: true,
                    lineWidth: 2,
                    fill: true,
                    fillColor: {
                        colors: [{
                            opacity: 0.2
                        }, {
                            opacity: 0.01
                        }
                        ]
                    }
                },
                points: {
                    show: true
                },
                shadowSize: 2
            },
            legend:{
                show: false
            },
            grid: {
                labelMargin: 10,
                axisMargin: 500,
                hoverable: true,
                clickable: true,
                tickColor: "rgba(255,255,255,0.22)",
                borderWidth: 0
            },
            colors: ["#FFFFFF", "#4A8CF7", "#52e136"],
            xaxis: {
                ticks: 11,
                tickDecimals: 0
            },
            yaxis: {
                ticks: 5,
                tickDecimals: 0
            }
        });
        $("#site_tay").bind("plothover", function (event, pos, item) {

            var str = "(" + pos.x.toFixed(2) + ", " + pos.y.toFixed(2) + ")";

            if (item) {
                if (previousPoint != item.dataIndex) {
                    previousPoint = item.dataIndex;
                    $("#tooltip").remove();
                    var x = item.datapoint[0],
                            y = item.datapoint[1];
                    showTooltip(item.pageX, item.pageY,
                            item.series.label + " del dia " + x + " = " + y);
                }
            } else {
                $("#tooltip").remove();
                previousPoint = null;
            }
        });
        function showTooltip(x, y, contents) {
            $("<div id='tooltip'>" + contents + "</div>").css({
                position: "absolute",
                display: "none",
                top: y + 5,
                left: x + 5,
                border: "1px solid #000",
                padding: "5px",
                'color':'#fff',
                'border-radius':'2px',
                'font-size':'11px',
                "background-color": "#000",
                opacity: 0.80
            }).appendTo("body").fadeIn(200);
        }
    }

How can i fix this?


Solution

  • The graph is "starting" on 7 because 7 is the smallest x value in your data and the x axis is ordered like the natural numbers.

    If your x values should not be treated as numbers use the categories plugin (see this fiddle):

    xaxis: {
        mode: 'categories'
    },