1

我想显示 3 个数据系列。

每当我在中设置stackSeries: true选项时,我都会得到

Uncaught TypeError: Cannot read property '0' of undefined

或者

Uncaught TypeError: Cannot read property '1' of undefined 

尝试显示数据时。

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

更新

$(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 } ?>

});
4

1 回答 1

3

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

例如,这个数据系列会抛出一个错误:

[
[[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]],
]
于 2014-11-21T18:17:11.360 回答