在 C3.js 中设置图形名称

Setting the graph name in C3.js

我有一个图表,其中我使用以下函数加载新数据:

function insertGraph(yAxis, xAxis, Header) {
    setTimeout(function () {
        chart.load ({
            bindto: "#graph",
            xs: {
                'y':'x'
            },
            columns: [
                yAxis, 
                xAxis
            ]
        });
    }, 100);
}

传入的 yAxis、xAxis 和 Header 的值示例如下所示:

y轴:

["y", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

x轴:

["x", 0, 131.35, 26971.3, 27044.75, 27351.4, 27404.483333333334, 27419.416666666668, 33128.96666666667, 33549.13333333333, 34049.48333333333, 77464.26666666666, 77609.71666666666, 174171.85, 259166.98333333334]

Header:

MakeModeChange

一切正常,除了加载图表时它给出数据名称 "y",我需要它具有 Header(在本例中为 MakeModeChange)作为数据名称。我尝试像下面的代码一样使用 name 但什么也没发生:

function insertGraph(yAxis, xAxis, Header) {
    setTimeout(function () {
        console.log(Header);
        console.log(yAxis);
        chart.load ({
            bindto: "#graph",
            xs: {
                'y':'x'
            },
            columns: [
                yAxis, 
                xAxis
            ],
            names: {
                y: 'Header'
            }   
        });
    }, 100);
}

我还尝试将传递给 yAxis 的内容更改为如下所示:

["MakeModeChange", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

然后将加载函数更改为如下所示:

function insertGraph(yAxis, xAxis, Header) {
    setTimeout(function () {
        chart.load ({
            bindto: "#graph",
            xs: {
                Header:'x'
            },
            columns: [
                yAxis, 
                xAxis
            ], 
        });
    }, 100);
}

但是我得到以下错误:

Uncaught Error: x is not defined for id = "MakeModeChange".

知道如何进行这项工作吗?

您的 x 轴和 y 轴 variable/arrays 方向错了吗?我本来希望 x 轴值为 1, 2, 3, 4, etc,y 轴值为 0, 131.35, etc.

尽管如此,数组中的y值将是系列名称,然后您使用xs对象指定x值数组。此 x 值数组的名称无关紧要。

See/run 下面的代码片段。

function createGraph(xAxis, yAxis, Header) {

  // create the xs object with a key name of the header variable
  var myxs = {};
  myxs[Header] = 'x';
  
  // set the 1st position value in the yaxis to the header
  yAxis[0] = Header;
  
  c3.generate({
    data: {
      xs: myxs,
      columns: [
        xAxis,
        yAxis

      ]
    }
  });
}

createGraph(
  ["x", 0, 131.35, 26971.3, 27044.75, 27351.4, 27404.483333333334, 27419.416666666668, 33128.96666666667, 33549.13333333333, 34049.48333333333, 77464.26666666666, 77609.71666666666, 174171.85, 259166.98333333334],
  ["y", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
  "Variable Name"
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id='chart' />