【问题标题】:Stackbar chart is overlapping in flot chart堆积条形图在浮动图中重叠
【发布时间】:2015-08-27 20:58:26
【问题描述】:

在浮动图表中正确绘制堆积条形图时需要帮助。不知道为什么我的条没有堆叠,我正在动态准备我的数据集。以下是我的数据格式。

[{
     "data": [
         [1415491200000, 1],
         [1415577600000, 2],
         [1415750400000, 1]
     ],
     "label": "MANG",
     "bars": {
         "show": "true",
         "barWidth": 36000000,
         "fillColor": "#FFEE11",
         "order": 1,
         "align": "center"
     },
     "stack": true
 }]

图表选项

{
    xaxis: {
        mode: "time",
        timeformat: "%m/%d/%y",
        minTickSize: [1, "day"]
    },
    grid: {
        labelMargin: 10,
        hoverable: true,
        borderWidth: 0
    },
    series: {
        stack: true
    },
    colors: colorCodes,
    tooltip: true,
    legend: {
        show: true,
        noColumns: 0, // number of colums in legend table
        labelFormatter: null, // fn: string -> string
        labelBoxBorderColor: "#888", // border color for the little label boxes
        container: "#adoptionLegendContainer", // container (as jQuery object) to put legend in, null means default on top of graph
        position: "nw", // position of default legend container within plot
        margin: [5, 10], // distance from grid edge to default legend container within plot
        backgroundOpacity: 0 // set to 0 to avoid background
    }
}

请帮忙。 here is a plunker link

注意:在 11/26/14 中分别有 2 个和 3 个计数的 2 个任务,所以基本上 bar 应该在 yaxis 中绘制到 5,但它是重叠的。

【问题讨论】:

    标签: javascript css angularjs flot flot.tooltip


    【解决方案1】:

    我在这个问题上花了很多时间,终于可以发布解决您所有问题的方法了。

    首先,您忘记在索引中包含<script src="http://cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.stack.min.js"></script>。这意味着堆叠图表并没有真正被绘制出来。其次,我稍微清理了您的数据并删除了数据中不必要的双重声明。然后,在我包含堆栈脚本后,我修复了您的鼠标悬停显示错误数量的任务。

    最后我试图找出为什么flot重叠并得出以下结论:flot不知道如何处理不同长度的数据序列。这意味着,如果您有 3 个数据系列,每个数据系列的长度都不同,那么这些条形会相互重叠,看起来是随机的。但是,如果您确保所有系列的长度相同,则条形堆叠不会出现问题。

    在我看来,最好的解决方案是确保在服务器端,您的所有系列都具有相同的长度,并且在每个数据滴答声上都有一个值.理想情况下,您可以将零值添加到所有在数据刻度处缺失值的系列中,并确保所有系列的长度相同。

    这是我的解决方案的代码:

    数据和选项:

        $scope.tasksRunChartOptions = {
    
          xaxis: {
            mode: "time",
            timeformat: "%m/%d/%y",
            minTickSize: [1, "day"]
          },
          grid: {
            labelMargin: 10,
            hoverable: true,
            borderWidth: 0
          },
          series: {
            stack: true,
            "bars":{
                    "show":"true",
                    "barWidth":36000000,
                    "order":1,
                    "align":"center"
                }
          },
          colors: colorCodes,
          tooltip: true,
                legend: {
                    show: true,
                    noColumns: 0, // number of colums in legend table
                    labelFormatter: null, // fn: string -> string
                    labelBoxBorderColor: "#888", // border color for the little label boxes
                    container: "#adoptionLegendContainer", // container (as jQuery object) to put legend in, null means default on top of graph
                    position: "nw", // position of default legend container within plot
                    margin: [5, 10], // distance from grid edge to default legend container within plot
                    backgroundOpacity: 0 // set to 0 to avoid background
                }
        };
    
        $scope.translate = function(value) {
          var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
          var myDate = new Date(value);
          return myDate.getDate() + " " + monthNames[myDate.getMonth()] + " " + myDate.getFullYear();
        }
    
        $scope.reportTasksRunRange = {
          min: 1415059200000,
          max: 1418342400000, 
          floor: 1415059200000,
          ceil: 1418342400000,
          step: (1412467200000 - 1412380800000)
        };
    
    
       $scope.tasksRunData = [
        {
            "data":
            [[1415491200000,1],[1415577600000,3],[1415664000000,2],[1415750400000,1],[1415836800000,3],[1415923200000,1],[1416009600000,7],[1416096000000,2],[1416268800000,2],[1416441600000,1],[1416528000000,12],[1416787200000,1],[1416873600000,1],[1416960000000,3],[1417046400000,2],[1417132800000,2],[1417392000000,4],[1417478400000,3],[1417737600000,1],[1417910400000,4],[1417996800000,6],[1418083200000,2],[1418169600000,4],[1418256000000,3]],
            "label":"ICS-MANG"
        },
      {
            "data":
            [[1415491200000,2],[1415577600000,3],[1415664000000,3],[1415750400000,1],[1415836800000,1],[1415923200000,2],[1416009600000,15],[1416096000000,4],[1416268800000,1],[1416441600000,3],[1416528000000,1],[1416787200000,1],[1416873600000,1],[1416960000000,3],[1417046400000,3],[1417132800000,2],[1417392000000,4],[1417478400000,3],[1417737600000,3],[1417910400000,1],[1417996800000,6],[1418083200000,5],[1418169600000,4],[1418256000000,3]],
            "label":"Neeraj_secure"
        },
        {
            "data":
            [[1415491200000,2],[1415577600000,3],[1415664000000,3],[1415750400000,1],[1415836800000,1],[1415923200000,3],[1416009600000,1],[1416096000000,2],[1416268800000,4],[1416441600000,1],[1416528000000,1],[1416787200000,1],[1416873600000,1],[1416960000000,3],[1417046400000,4],[1417132800000,2],[1417392000000,4],[1417478400000,3],[1417737600000,7],[1417910400000,20],[1417996800000,6],[1418083200000,4],[1418169600000,4],[1418256000000,3]],
            "label":"Bkrishna",
        }];
    

    显示我的解决方案的 plnkr 是 here.

    【讨论】:

    • 不知道还需要添加那个库。无赖。
    【解决方案2】:

    在 Google 代码上打开了一份报告,描述了问题和可能的修复:

    https://code.google.com/p/flot/issues/detail?id=247

    在我的情况下,对数据刻度进行排序也是必要的。

    【讨论】: