jqPlot stackSeries:true,Uncaught TypeError:无法读取未定义的属性“0”

时间:2012-11-23 15:06:39

标签: charts jqplot

我想显示3个数据系列。

每当我设置stackSeries: true选项时,我就会得到

Uncaught TypeError: Cannot read property '0' of undefined

Uncaught TypeError: Cannot read property '1' of undefined 

尝试显示数据时。

当我删除stackSeries: true attribue时,会呈现图表。怎么了?

更新

$(document).ready(function(){
    var bar1 = [
    <?php
        if(!empty($bar1)){
            $i = 0;
            foreach($bar1 as $bar){
                $i++;
                if(!empty($bar['jobs']['date_published'])){
                    if($i == count($bar1))
                        echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].']';
                    else
                        echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].'], ';
                }
            }
        }
    ?>
    ];
    var bar2 = [
    <?php
        if(!empty($bar2)){
            $i = 0;
            foreach($bar2 as $bar){
                $i++;
                if(!empty($bar['jobs']['date_published'])){
                    if($i == count($bar2))
                        echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].']';
                    else
                        echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].'], ';
                }
            }
        }
    ?>
    ];
    var bar3 = [
    <?php
        if(!empty($bar3)){
            $i = 0;
            foreach($bar3 as $bar){
                $i++;
                if(!empty($bar['jobs']['date_published'])){
                    if($i == count($bar3))
                        echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].']';
                    else
                        echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].'], ';
                }
            }
        }
    ?>
    ];
    <?php   
        if(!empty($bar1) && !empty($bar2) && !empty($bar3))
            echo 'var labels = ["Bar 1", "Bar 2", "Bar 3"];';
        else if(!empty($bar1) && !empty($bar2))
            echo 'var labels = ["Bar 1", "Bar 2"];';
        else if(!empty($bar2) && !empty($bar3))
            echo 'var labels = ["Bar 2", "Bar 3"];';
        else if(!empty($bar1) && !empty($bar3))
            echo 'var labels = ["Bar 1", "Bar 3"];';
        else if(!empty($bar1))
            echo 'var labels = ["Bar 1"];';
        else if(!empty($bar2))
            echo 'var labels = ["Bar 2"];';
        else if(!empty($bar3))
            echo 'var labels = ["Bar 3"];';
    ?>
    optionsObj = {
        //stackSeries: true,
        seriesColors:[<?php if(!empty($bar1)) echo '"#4bb2c5",';
                            if(!empty($bar2)) echo '"#F78181",';
                            if(!empty($bar3)) echo '"#9AFE2E",';
                            echo '"#4bb2c5"'; ?>],
        animate:true,
        legend:{
            show:true,
            location:"sw",
            rowSpacing:"0px",
            labels:labels
        },
        title:{
            text:"Jobs",
            fontSize:"12px"                     
        },
        seriesDefaults:{
            showMarker: true,
            pointLabels: { show: true },
            renderer: $.jqplot.BarRenderer,
            rendererOptions: {
                barWidth: 2
            }
        },
        axes:{
            xaxis:{
                pad:1,
                tickOptions:{
                    fontSize:"10px"
                },
                renderer:$.jqplot.DateAxisRenderer
            },
            yaxis:{autoscale: true}
        },
        highlighter: {show: true},
        grid:{drawGridlines:true}
    };
    <?php $empty = false;
            if(!empty($bar1) && !empty($bar2) && !empty($bar3)){ ?>
            $.jqplot('chart1', [bar1, bar2, bar3], optionsObj);
    <?php } else if(!empty($bar1) && !empty($bar2)){ ?>
            $.jqplot('chart1', [bar1, bar2], optionsObj);
    <?php } else if(!empty($bar2) && !empty($bar3)){ ?>
            $.jqplot('chart1', [bar2, bar3], optionsObj);
    <?php } else if(!empty($bar1) && !empty($bar3)){ ?>
            $.jqplot('chart1', [bar1, bar3], optionsObj);
    <?php } else if(!empty($bar1)){ ?>
            $.jqplot('chart1', [bar1], optionsObj);
    <?php } else if(!empty($bar2)){ ?>
            $.jqplot('chart1', [bar2], optionsObj);
    <?php } else if(!empty($bar3)){ ?>
            $.jqplot('chart1', [bar3], optionsObj);
    <?php } ?>

});

1 个答案:

答案 0 :(得分:2)

我发现如果我的数据集在任何地方都有不连续性,那么堆叠图会产生错误。

例如,此数据系列将引发错误:

[
[[0,22],[1,22],[2,22]],
[[0,33],[2,33]],
]

这个不会:

[
[[0,22],[1,22],[2,22]],
[[0,33],[1,33],[2,33]],
]

注意两者之间的区别。对于第二个系列,第一个没有X = 1的数据点。修复方法是确保所有系列都包含所有可能的X值,并将任何缺失的值设置为零:

[
[[0,22],[1,22],[2,22]],
[[0,33],[1,0],[2,33]],
]