繁体   English   中英

JavaScript switch 语句并应用返回到 function 参数

[英]JavaScript switch statement and applying return to a function parameter

我正在使用 switch 语句在 jqPlot jQuery 插件的模式之间切换。

如何将开关中的值应用到 function 参数?

我有

type = detectType(gType);

我需要这个来显示,其中类型在下面的参数中。 它不返回该值:

function detectType(value){
    switch(value){
        case 'bar':
            return 'renderer:$.jqplot.BarRenderer,';
            break;
        case 'pie':
            return 'renderer: $.jqplot.PieRenderer,';
            break;
    }
}

function generateGraph(id,gType) {
    type = detectType(gType);
    var s1 = [2, 6, 7, 10];
    var s2 = [7, 5, 3, 4];
    var s3 = [14, 9, 3, 8];

$.jqplot(''+id+'', [s1, s2, s3], {
        stackSeries: true,
        captureRightClick: true,
        seriesDefaults: {
            type
            rendererOptions: {
                barMargin: 30,
                highlightMouseDown: true   
            },
            pointLabels: {
                show: true
            }
        },
        axes: {
            xaxis: {
                renderer: $.jqplot.CategoryAxisRenderer
            },
            yaxis: {
                padMin: 0
            }
        },
        legend: {
           show: true,
           location: 's',
           placement: 'outside'
        }
    });
}

你不能用type字符串做你想做的事情。 这是无效的语法。

相反, detectType应该只返回渲染器 object 本身,而不是作为字符串。 然后,将其分配给系列默认值中的renderer 像这样:

function detectType(value){
    switch(value){
        case 'bar':
            return $.jqplot.BarRenderer;

        case 'pie':
            return $.jqplot.PieRenderer;
    }
}

然后:

//...
seriesDefaults: {
    renderer: type
    //...
}
//...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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