javascript - 如何使用 Chart.js 指定 "per dataset"解析?

标签 javascript chart.js

我试图弄清楚在指定数据集时如何在 ChartJS 中使用 yAxisKey 选项,但是在重现 this example 时遇到了问题从文档。我尝试搜索涉及 yAxisKey(或 xAxisKey)、parsing 选项以及有关指定 datasets,但不幸的是到目前为止是空白的。

示例

<!doctype html>
<html>
<head>
    <title>Example Chart</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
</head>

<body>
    <canvas id="canvas"></canvas>
    <script>
        const data = [{x: 'Jan', net: 100, cogs: 50, gm: 50}, {x: 'Feb', net: 120, cogs: 55, gm: 75}];
        const config = {
                type: 'bar',
                data: {
                    labels: ['Jan', 'Feb'],
                    datasets: [{
                        label: 'Net sales',
                        data: data,
                        parsing: {
                            yAxisKey: 'net'
                        }
                    }, {
                        label: 'Cost of goods sold',
                        data: data,
                        parsing: {
                            yAxisKey: 'cogs'
                        }
                    }, {
                        label: 'Gross margin',
                        data: data,
                        parsing: {
                            yAxisKey: 'gm'
                        }
                    }]
                },
            };

        window.onload = function() {
            const ctx = document.getElementById('canvas').getContext('2d');
            let chart  = new Chart(ctx, config);
        };
    </script>
</body>
</html>

这会为我生成一个看起来很空的图表。

Screenshot of empty chart results

我是否遗漏了一些明显的东西?对语法有误解?

提前致谢!

最佳答案

由于您当前使用的 Chart.js 版本 2.8.0 中尚不提供 per-dataset parsing 选项,因此您仍然可以通过 Array.map() 获得相同的结果。功能。

请查看您修改后的代码,看看它是如何工作的。

<!doctype html>
<html>

<head>
  <title>Example Chart</title>
  <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
</head>

<body>
  <canvas id="canvas"></canvas>
  <script>
    const data = [
      { x: 'Jan', net: 100, cogs: 50, gm: 50 }, 
      { x: 'Feb', net: 120, cogs: 55, gm: 75 }
    ];
    const config = {
      type: 'bar',
      data: {
        labels: data.map(o => o.x),
        datasets: [{
          label: 'Net sales',
          data: data.map(o => o.net)
        }, {
          label: 'Cost of goods sold',
          data: data.map(o => o.cogs)
        }, {
          label: 'Gross margin',
          data: data.map(o => o.gm)
        }]
      },
      options: {
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true
            }
          }]
        }
      }
    };

    window.onload = function() {
      const ctx = document.getElementById('canvas').getContext('2d');
      let chart = new Chart(ctx, config);
    };
  </script>
</body>

</html>

关于javascript - 如何使用 Chart.js 指定 "per dataset"解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63511006/

相关文章:

javascript - 滑入/滑出标题 div/显示部分隐藏的 div

javascript - Chart.js 中的水平轴标签

javascript - 如何强制我的 ChartJS Canvas 图例保留在单个列中?

javascript - 条形图的 Chart new.js 自定义问题

javascript - 我无法使用宽度和高度属性正确地重新缩放 <canvas>

javascript - 如何在按下按钮后增加变量

javascript 存在的问题和陷阱

javascript - Charts.JS - 图表太多? - 加载问题

javascript - 如何创建一个将任何给定字符串与第二个参数连接起来的函数?

javascript - Angular 将值存储到服务以在多个页面上访问