使用flot堆叠的时间条形图

时间:2015-12-21 11:25:29

标签: flot

http://www.flotcharts.org/flot/examples/stacking/获取示例我试图实现相同的但x轴是time

https://jsfiddle.net/shorif2000/u6kvfjzc/

processData(json.rows.incidents.data, json.rows.incidents.tab);

function processData(rows, tab) {

  p_start = new Date().getTime();
  console.log("start  process: " + p_start);


  var arr = filterData(rows);

  p_end = new Date().getTime();
  console.log("finish  process: " + p_end);
  console.log("process  duration : " + ((p_end - p_start) / 1000) + "s");

  plot_graph(arr, tab);

}


function filterData(data) {
  var arr = [];
  for (i = 0; i < data.length; i++) {

    var to_seconds = moment(data[i].TIMESTAMP, 'YYYY-MM-DD').unix() * 1000; 
    if (typeof arr[data[i].PRIORITY] == 'undefined' && !(arr[data[i].PRIORITY] instanceof Array)) {
      arr[data[i].PRIORITY] = [];
    }
    arr[data[i].PRIORITY].push({
      0: to_seconds,
      1: parseInt(data[i].VALUE)
    });
  }

  return arr;
}






function plot_graph(arr, id) {
  var stack = 0,
    bars = true,
    lines = false,
    steps = false;
  var data = [{
    stack: stack,
    lines: {
      show: lines,
      fill: true,
      steps: steps
    },
    bars: {
      show: bars,
      barWidth: 0.6
    },
    "points": {
      "show": false
    },
    data: arr
  }];
  console.log(data);
  $.plot("#" + id + "network-graph", data, {
    series: {
      stack: stack,
      lines: {
        show: lines,
        fill: true,
        steps: steps
      },
      bars: {
        show: bars,
        barWidth: 0.6
      }
    },
    xaxis: {
      mode: "time",
      timeformat: "%y/%m/%d"
    }
  });
}

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。 https://jsfiddle.net/shorif2000/epnv9wrw/。我不得不循环数组对象并为它创建标签

var datasets = [];  
    var i = 0;
    for (var key in arr) {      
       datasets.push({label:key,data:arr[key],color:i});
       ++i;
    }