简体   繁体   English

没有为id =“ 0”定义x-C3.js堆积的条形图

[英]x is not defined for id = “0” - C3.js stacked bar chart

I am getting the error: Uncaught Error: x is not defined for id = "0". 我收到错误:未捕获的错误:x未定义为id =“ 0”。 When trying to pass data into c3.js timeseries chart. 尝试将数据传递到c3.js时间序列图时。

https://c3js.org/samples/timeseries.html https://c3js.org/samples/timeseries.html

I've tried using the variable in columns as 我试过使用列中的变量作为

columns: json_beams and json: json_beams 列:json_beams和json:json_beams

as shown in the code below. 如下面的代码所示。

The base code provided from c3.js works fine c3.js提供的基本代码工作正常

Here is the output of my javascript variable json_beams. 这是我的javascript变量json_beams的输出。

Here is how I passed the array to javascript: 这是我将数组传递给javascript的方法:

<script>
  var json_beams = "<?php echo addslashes(json_encode($data1)); ?>";
</script>

default timeseries columns: 默认时间序列列:

      columns: [
          ['x', '00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00', '05:00:00'],
          ['data1', 30, 200, 100, 400, 150, 250],
          ['data2', 130, 340, 200, 500, 250, 350]
       ]

The output of the json_beams variable in console is: (Note: pasting this directly into Columns:' works fine. 控制台中json_beams变量的输出为:(注意:将其直接粘贴到Columns:'可以正常工作。

[["x","00:00:00","01:00:00","02:00:00","03:00:00","04:00:00","05:00:00","06:00:00","07:00:00","08:00:00","09:00:00","10:00:00","11:00:00","12:00:00","13:00:00","14:00:00","15:00:00","16:00:00","17:00:00","18:00:00","19:00:00","20:00:00","21:00:00","22:00:00","23:00:00","24:00:00"],["Beam 0","836","1141","1339","910","655","303","307","211","345","277","235","225","236","239","213","200","190","207","648","937","973","1076","1108","1075"],["Beam 1","913","1152","1333","939","708","326","323","297","394","292","266","244","271","257","231","210","224","190","754","1023","997","1035","1141","1044"]

My code: 我的代码:

    <script>
var chart = c3.generate({
    data: {
        x: 'x',
        xFormat: '%H:%M:%S',
      columns: json_beams
       ]
//         json: json_beams
    },
    axis: {
        x: {
            type: 'timeseries',
            tick: {
                format: '%H:%M:%S'
            }
        }
    }
});

Resulting error is: 产生的错误是:

Uncaught Error: x is not defined for id = "0". 未捕获的错误:未为id =“ 0”定义x。

Regardless of being added to 'columns:' or 'json:' 无论添加到“ columns:”还是“ json:”

According to https://www.php.net/manual/en/function.addslashes.php addSlashes returns a string. 根据https://www.php.net/manual/zh/function.addslashes.php,addSlashes返回一个字符串。 In the section 'columns: json_beams' json_beams should be an array of arrays. 在“列:json_beams”部分中,json_beams应该是一个数组数组。 You will need to convert the string into an array. 您将需要将字符串转换为数组。 This tutorial might help: https://jonsuh.com/blog/convert-loop-through-json-php-javascript-arrays-objects/ 本教程可能会有所帮助: https : //jonsuh.com/blog/convert-loop-through-json-php-javascript-arrays-objects/

The reason this worked when you copied and pasted it rather than used a variable is that it was no longer a string. 当您复制并粘贴而不是使用变量时,此方法起作用的原因是它不再是字符串。

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

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