如何在 CanvasJS 中从 4 个数组加载数据点以创建图表

How to load datapoints from 4 array in CanvasJS to create a chart

提问人:Shiva 提问时间:12/8/2022 更新时间:12/8/2022 访问量:45

问:

我想使用图表 canvasjs 来显示 4 种不同的数据。 我的数据是过去 10 天内 4 个不同账户的余额,我想要天数的 x 轴和余额金额的 y 轴。

我的代码 :

const ColorBac = ["rgba(255, 0, 50, 0.2)", "rgba(27, 68, 128, 0.2)", "rgba(246, 162, 30, 0.2)", "rgba(23, 135, 95, 0.2)"];
const Colorborder = ["rgba(255, 0, 50, 0.6)", "rgba(27, 68, 128, 0.6)", "rgba(246, 162, 30, 0.6)", "rgba(23, 135, 95, 0.6)"];

Api = api('Chart');
console.log("load Api Array");
console.log(Api.ExData);

function setColor(chart) {
  for (var i = 0; i < chart.options.data.length; i++) {
    dataSeries = chart.options.data[i];

    for (var j = 0; j < dataSeries.dataPoints.length; j++) {
      if (dataSeries.dataPoints[j].y <= 0)
        dataSeries.dataPoints[j].color = ColorBac[j];
    }
  }
}

function ShowChart() {
  var dataPoints = [];
  //GetData();
  $.each(Api.ExData, function(index, Info) {
    dataPoints.push({
      x: parseInt(Info.fldHesabID),
      y: parseInt(Info.fldMojoodi),
      label: Info.fldDate,
      mouseover: onMouseover,
    });
  });
  console.log('load dataPoints');
  console.log(dataPoints);
  ///////
  var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    theme: "light2", // "light1", "light2", "dark1", "dark2"

    axisY: {
      //title: "Reserves(MMbbl)"
      //labelAngle: 30
    },
    axisX: {
      interval: 1,
      //minimum: 0,
      //  maximum: 20,
      labelAngle: 30
    },
    data: [{
      type: "line",
      toolTipContent: "{label}: {y} ",
      dataPoints: dataPoints,
    }]
  });

  console.log('load Chart');
  setColor(chart);
  chart.render();

  function onMouseover(e) {  
    createGauge(e.dataPoint.progressVal);
  }
}
ShowChart();
console.log('end Chart');
<div id="chartContainer" style="height: 370px; width: 100%;border: 1px solid green;"></div>
<br>

<script type="text/javascript" src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>

我的数据输出是照片的形式。

请帮我显示这张图表

javascript 图表 canvasjs

评论

1赞 Manoj Mohan 1/2/2023
对于这种情况,您可以使用多系列图表。对于每个帐户,您可以创建 dataPoints 数组,并在循环访问从 API 获取的数据时以 dataPoint 格式推送“label”和“y”值的值。多系列图表示例链接 - canvasjs.com/javascript-charts/multi-series-chart

答: 暂无答案