douxiong2001 2017-04-12 15:05
浏览 143
已采纳

使用作为参数传入的JS字符串变量作为chart.js中数据参数的值?

This is the relative Javascript that is not working quite right. The parameter thisChartData is a string and it alerts just fine; it's generated by a PHP script elsewhere (but that's not important).

If I copy and paste what the alert output into the data section the chart generates fine. But for some reason I can't use the parameter name in the data section

            function drawChart( thisChartData, thisChartTitle ) {
                var ctx = $("#my-chart");
                alert(thisChartData); // alerts->    '131', '1043', '144', '43'

            //chart data
                var ctxData = {                     
                    datasets: [{
                        data: [ thisChartData ], //using the paramter variable doesn't work
                        backgroundColor: [ <?php echo $bg_color_list; ?> ]                      
                    }]
                };

Meanwhile the code below works fine, I need the data to be variable depending on what I pass to the function because I'm going to have several data sets I want to scroll through.

     function drawChart( thisChartData, thisChartTitle ) {
            var ctx = $("#my-chart");
            alert(thisChartData); // alerts->    '131', '1043', '144', '43'

        //chart data
            var ctxData = {                     
                datasets: [{
                    data: [ '131', '1043', '144', '43' ], 
                    backgroundColor: [ <?php echo $bg_color_list; ?> ]                        
                }]
            };
  • 写回答

1条回答 默认 最新

  • drwo32555 2017-04-12 15:08
    关注

    data: [ thisChartData ] should just be data: thisChartData, and when you call drawChart, pass in an array. E.g.:

    function drawChart( thisChartData, thisChartTitle ) {
        var ctx = $("#my-chart");
    
        //chart data
        var ctxData = {                     
            datasets: [{
                data: thisChartData,  // <======
                backgroundColor: [ <?php echo $bg_color_list; ?> ]                      
            }]
        };
    

    and

    drawChart(['131', '1043', '144', '43'], "title");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序