简体   繁体   中英

Flot Strange Line Chart

I have a chart with ordering by date.

My problem is the chart lines joining false from start to end.

在此输入图像描述

My options:

var options =
    {
        grid:
        {
            color: "#dedede",
            borderWidth: 1,
            borderColor: "transparent",
            clickable: true,
            hoverable: true
        },
        series: {
            grow: {active:false},
            lines: {
                show: true,
                fill: false,
                lineWidth: 2,
                steps: false
            },
            points: {
                show:true,
                radius: 5,
                lineWidth: 3,
                fill: true,
                fillColor: "#000"
            }
        },
        legend: { position: "nw", backgroundColor: null, backgroundOpacity: 0, noColumns: 2 },
        yaxis: { tickSize:50 },
        xaxis: {
            mode: "time",
            tickFormatter: function(val, axis) {
                var d = new Date(val);
                return d.getUTCDate() + "/" + (d.getUTCMonth() + 1);
            }
        },
        colors: [],
        shadowSize:1,
        tooltip: true,
        tooltipOpts: {
            content: "%s : %y.0",
            shifts: {
                x: -30,
                y: -50
            },
            defaultTheme: false
        }
    };

Note: I'm not re-ordering any data. Just giving the timestamp with this function:

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

Setting the data like this:

$.each(e.data, function(i, e){
    data.push([gd(parseInt(e['year']), parseInt(e['month']), parseInt(e['day'])), parseInt(e['value'])]);
});

var entity = {
    label: e.campaign,
    data:  data,
    lines: {fillColor: randomColor},
    points: {fillColor: randomColor}
};

entities.push(entity);

Console log:

在此输入图像描述

When creating line charts, flot will connect the data points using the order from the data series, ignoring the actual x-coordinates. That's why data series should be in ascending order.

A minimal example (using your data in ascending order):

var d1 = [
    [1401310800000, 275],
    [1401397200000, 270],
    [1401483600000, 313],
    [1401570000000, 279], 
    [1401656400000, 216], 
    [1401742800000, 255], 
    [1401829200000, 244],
    [1401915600000, 70]                     
];

$.plot("#chart", [ d1 ]);

Here is a jsfiddle showing the chart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM