add data to chart using c3.js

只谈情不闲聊 提交于 2019-12-11 09:25:08

问题


I have a chart, in which I would like to add more data without replacing the data I have, either with a .push or adding a set of data without losing the ones I previously have.

How can I do it?

columns: [
      ['data1', -4,3,4,7,8,9,8,7,3],
      ['data1_x', -5,2,3,4,5,4,3,2,1],

. . .

//I need add the new data withouth remove the previous data

chart.load({
    xs: {
        data1: 'data1_x'
    },
    columns: [
        ["data1_x", 3,6],
        ["data1", 5,8]
    ]
});

https://jsfiddle.net/aL8xhgpn/


回答1:


You can move the data into an array

var data1 = ['data1',-4,3,4,7,8,9,8,7,3];
var data1_x = ['data1_x', -5,2,3,4,5,4,3,2,1];

and push data into it.

Working sample below.

var data1 = ['data1', -4, 3, 4, 7, 8, 9, 8, 7, 3];
var data1_x = ['data1_x', -5, 2, 3, 4, 5, 4, 3, 2, 1];
var chart = c3.generate({
  data: {
    xs: {
      data1: "data1_x",
      data2: "data2_x"
    },
    xSort: false,
    columns: [
      data1,
      data1_x, ['data2', Math.random() * 10, Math.random() * 10, Math.random() * 10, Math.random() * 10, Math.random() * 10, Math.random() * 10],
      ['data2_x', Math.random() * 10, Math.random() * 10, Math.random() * 10, Math.random() * 10, Math.random() * 10, Math.random() * 10]
    ]

  },
  axis: {
    x: {
      tick: {
        values: d3.range(-5, 10)
      }
    }
  }
});

chart.internal.xAxis.g.attr('transform', "translate(0," + chart.internal.y(0) + ")");
chart.internal.yAxis.g.attr('transform', "translate(" + chart.internal.x(0) + ", 0)");
data1.push(5)
data1.push(8)
data1_x.push(3)
data1_x.push(6)


setTimeout(load, 5000);

function load() {
  chart.load({
    xs: {
      data1: 'data1_x'
    },
    columns: [
      data1_x,
      data1
    ]
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.18/c3.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.18/c3.js"></script>
<div id="chart"></div>


来源:https://stackoverflow.com/questions/46334644/add-data-to-chart-using-c3-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!