如何从javascript散点图中的另一个变量为x和y添加新值

How to add a new value for x and y from another variable in javascript scatter chart?

本文关键字:新值 添加 变量 另一个 javascript 散点图      更新时间:2024-04-28

我在插入x和y的值时遇到问题。我希望插入到x和y数组中的值来自一个变量。我想在数组中再推一个x和y。我想推送x和y的时间值。

 <script type="text/javascript">
      window.onload = function () {
      var chart = new CanvasJS.Chart("chartContainer",
      {
      title:{
        text: "Converting in Local Time"
      },
      axisX:{
        title: "time",
        gridThickness: 2,
        interval:1, 
        intervalType: "hour",        
        valueFormatString: "hh:MM", 
        labelAngle: -20
      },
      axisY:{
        title: "distance"
      },
      data: [
      {        
        type: "line",
        dataPoints: [//array
        {x: new Date( Date.UTC (2016, 0, 1, 1,0) ), y: 26 },
        {x: new Date( Date.UTC (2016, 0, 1,2,0) ), y: 38  },
        {x: new Date( Date.UTC(2016, 0, 1,3,0) ), y: 43 },
        ]}
      ]
    });
    chart.push({
        x:new Date( Date.UTC(2016, 0, 1,4,0) ),
        y:28
        });
chart.render();
}
</script>
<script type="text/javascript" src="js/canvasjs.min.js"></script>
</head>
<body>
  <div id="chartContainer" style="height: 300px; width: 50%;">
  </div>
</body>
</html>

不要推入图表变量

试试

chart.data.dataPoints.push( {x:new Date( Date.UTC(2016, 0, 1,4,0) ),y:28 });