首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于epoch.js的实时图表生成

基于epoch.js的实时图表生成
EN

Stack Overflow用户
提问于 2015-10-21 09:06:54
回答 1查看 3.1K关注 0票数 0

我正在尝试使用epoch.js创建一个简单的实时图表,它可以在单击事件上自我更新。

下面发布的我的代码共有3个函数。它们是:

1)生成随机值

2)以毫秒为单位生成当前日期和时间。

3) onclick事件,更新图表数据点。

虽然我有正确格式的数据点,如图表所需。我无法更新它。

感谢任何帮助,找出为什么图形不工作,因为它应该。

代码语言:javascript
复制
///////////////this function generates the date and time in milliseconds//////////
function getTimeValue() {
  var dateBuffer = new Date();
  var Time = dateBuffer.getTime();
  return Time;
}

////////////// this function generates a random value ////////////////////////////
function getRandomValue() {
  var randomValue = Math.random() * 100;
  return randomValue;
}

////////////// this function is used to update the chart values ///////////////	
function updateGraph() {
  var newBarChartData = [{
    label: "Series 1",
    values: [{
      time: getTimeValue(),
      y: getRandomValue()
    }]
  }, ];
  barChartInstance.push(newBarChartData);
}

////////////// real time graph generation////////////////////////////////////////	  
var barChartData = [{
  label: "Series 1",
  values: [{
    time: getTimeValue(),
    y: getRandomValue()
  }]
}, ];

var barChartInstance = $('#barChart').epoch({
  type: 'time.bar',
  axes: ['right', 'bottom', 'left'],
  data: barChartData
});
代码语言:javascript
复制
<head>
  <script src="https://code.jquery.com/jquery-1.11.3.js">
  </script>
  <script src="http://www.goldhillcoldtouch.co.uk/wp-content/uploads/d3.min.js">
  </script>
  <script src="http://www.goldhillcoldtouch.co.uk/wp-content/uploads/epoch.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://www.goldhillcoldtouch.co.uk/wp-content/uploads/epoch.min.css">
</head>

<div id="barChart" class="epoch category10" style="width:320px; height: 240px;"></div>
<p id="updateMessage" onclick="updateGraph()">click me to update chart</p>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-21 10:40:23

在更新图形时,您正在将错误的对象推送到barChartInstance。您只需要推送包含新数据点的数组,而不是再次推送完整的配置。

代码语言:javascript
复制
function updateGraph() {
  var newBarChartData = [{time: getTimeValue(), y:getRandomValue()}];

  /* Wrong: don't use the full configuration for an update.
  var newBarChartData = [{
    label: "Series 1",
    values: [{
      time: getTimeValue(),
      y: getRandomValue()
    }]
  }, ];
  */
  barChartInstance.push(newBarChartData);
}

代码语言:javascript
复制
///////////////this function generates the date and time in milliseconds//////////
function getTimeValue() {
  var dateBuffer = new Date();
  var Time = dateBuffer.getTime();
  return Time;
}

////////////// this function generates a random value ////////////////////////////
function getRandomValue() {
  var randomValue = Math.random() * 100;
  return randomValue;
}

////////////// this function is used to update the chart values ///////////////	
function updateGraph() {
  var newBarChartData = [{time: getTimeValue(), y:getRandomValue()}];

  /*
  var newBarChartData = [{
    label: "Series 1",
    values: [{
      time: getTimeValue(),
      y: getRandomValue()
    }]
  }, ];
  */
  barChartInstance.push(newBarChartData);
}

////////////// real time graph generation////////////////////////////////////////	  
var barChartData = [{
  label: "Series 1",
  values: [{
    time: getTimeValue(),
    y: getRandomValue()
  }]
}, ];

var barChartInstance = $('#barChart').epoch({
  type: 'time.bar',
  axes: ['right', 'bottom', 'left'],
  data: barChartData
});
代码语言:javascript
复制
<head>
  <script src="https://code.jquery.com/jquery-1.11.3.js">
  </script>
  <script src="http://www.goldhillcoldtouch.co.uk/wp-content/uploads/d3.min.js">
  </script>
  <script src="http://www.goldhillcoldtouch.co.uk/wp-content/uploads/epoch.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://www.goldhillcoldtouch.co.uk/wp-content/uploads/epoch.min.css">
</head>

<div id="barChart" class="epoch category10" style="width:320px; height: 240px;"></div>
<p id="updateMessage" onclick="updateGraph()">click me to update chart</p>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33255163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档