Charts.js 数据不适合第二个 y 轴

发布于 2025-01-17 06:35:10 字数 4182 浏览 1 评论 0原文

我有一个简单的元素,包含整个数据集。
子集“更改”适用于第二个 Y 轴,但使用下面的代码,“更改”数据集仍然相对于左轴显示。
请指教,文档对此主题不是很清楚。我希望蓝线与右轴相关,使用下面的代码。

  let chartElement = $('#mainChart');
  const data = chartElement.data('chart-dataset');
  const cfg = {
      type: 'line',
      data: {
          datasets: [{
              'label': 'Total balance',
              data: data.stats,
              parsing: {
                  xAxisKey: 'time',
                  yAxisKey: 'total',
                  yAxisID: 'y',
              },
              backgroundColor: 'rgb(98,250,2)',
              borderColor: 'rgb(98,250,2)',
          }, {
              'label': 'Risk balance',
              data: data.stats,
              parsing: {
                  xAxisKey: 'time',
                  yAxisKey: 'risk',
                  yAxisID: 'y',
              },
              backgroundColor: 'rgb(255,191,34)',
              borderColor: 'rgb(255,191,34)',
          }, {
              'label': 'Average risk',
              data: data.stats,
              parsing: {
                  xAxisKey: 'time',
                  yAxisKey: 'avRisk',
                  yAxisID: 'y',
              },
              backgroundColor: 'rgb(236,4,4)',
              borderColor: 'rgb(236,4,4)',
          }, {
              'label': 'Change %',
              data: data.change,
              parsing: {
                  xAxisKey: 'time',
                  yAxisKey: 'change',
                  yAxisID: 'yPercent',
              },
              backgroundColor: 'rgb(31,116,241)',
              borderColor: 'rgb(31,116,241)',
          }]
      },
      options: {
          scales: {
              y: {
                  id: 'y',
                  type: 'linear',
                  position: 'left',
                  ticks: {
                      beginAtZero: true,
                  }
              },
              yPercent: {
                  id: 'yPercent',
                  type: 'linear',
                  position: 'right',
                  suggestedMax: 1.0,
                  suggestedMin: -1.0,
                  ticks: {
                      precision: 4,
                  }
              },
          }
      }
  }
  const mainChart = new Chart(
      chartElement,
      cfg
  )
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="mainChart" data-chart-dataset="{&quot;stats&quot;:[{&quot;time&quot;:&quot;2022-03-27 00:00:05&quot;,&quot;total&quot;:424.97014172,&quot;risk&quot;:307.70675152,&quot;avRisk&quot;:34.18963905777778},{&quot;time&quot;:&quot;2022-03-27 00:30:06&quot;,&quot;total&quot;:424.97666652,&quot;risk&quot;:307.70675152,&quot;avRisk&quot;:34.18963905777778},{&quot;time&quot;:&quot;2022-03-27 01:00:06&quot;,&quot;total&quot;:425.22301931,&quot;risk&quot;:307.70675152,&quot;avRisk&quot;:34.18963905777778},{&quot;time&quot;:&quot;2022-03-27 01:30:06&quot;,&quot;total&quot;:424.91940821,&quot;risk&quot;:307.70675151999995,&quot;avRisk&quot;:34.189639057777775}],&quot;change&quot;:[{&quot;time&quot;:&quot;2022-03-27 00:00:05&quot;,&quot;change&quot;:0},{&quot;time&quot;:&quot;2022-03-27 00:30:06&quot;,&quot;change&quot;:0.0015353549248324776},{&quot;time&quot;:&quot;2022-03-27 01:00:06&quot;,&quot;change&quot;:0.05796854495972885},{&quot;time&quot;:&quot;2022-03-27 01:30:06&quot;,&quot;change&quot;:-0.07140043840822045}]}" width="1639" height="819" style="display: block; box-sizing: border-box; height: 819px; width: 1639px;"></canvas>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>

I have a simple element, containing whole dataset.
Subset "change" is meant for second Y-Axis but using the code below, the "change" dataset still displays in relation to left axis.
Please advise, documentation is not very clear on this topic. I would expect the blue line to be in relation to the right axis, using below code.

  let chartElement = $('#mainChart');
  const data = chartElement.data('chart-dataset');
  const cfg = {
      type: 'line',
      data: {
          datasets: [{
              'label': 'Total balance',
              data: data.stats,
              parsing: {
                  xAxisKey: 'time',
                  yAxisKey: 'total',
                  yAxisID: 'y',
              },
              backgroundColor: 'rgb(98,250,2)',
              borderColor: 'rgb(98,250,2)',
          }, {
              'label': 'Risk balance',
              data: data.stats,
              parsing: {
                  xAxisKey: 'time',
                  yAxisKey: 'risk',
                  yAxisID: 'y',
              },
              backgroundColor: 'rgb(255,191,34)',
              borderColor: 'rgb(255,191,34)',
          }, {
              'label': 'Average risk',
              data: data.stats,
              parsing: {
                  xAxisKey: 'time',
                  yAxisKey: 'avRisk',
                  yAxisID: 'y',
              },
              backgroundColor: 'rgb(236,4,4)',
              borderColor: 'rgb(236,4,4)',
          }, {
              'label': 'Change %',
              data: data.change,
              parsing: {
                  xAxisKey: 'time',
                  yAxisKey: 'change',
                  yAxisID: 'yPercent',
              },
              backgroundColor: 'rgb(31,116,241)',
              borderColor: 'rgb(31,116,241)',
          }]
      },
      options: {
          scales: {
              y: {
                  id: 'y',
                  type: 'linear',
                  position: 'left',
                  ticks: {
                      beginAtZero: true,
                  }
              },
              yPercent: {
                  id: 'yPercent',
                  type: 'linear',
                  position: 'right',
                  suggestedMax: 1.0,
                  suggestedMin: -1.0,
                  ticks: {
                      precision: 4,
                  }
              },
          }
      }
  }
  const mainChart = new Chart(
      chartElement,
      cfg
  )
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="mainChart" data-chart-dataset="{"stats":[{"time":"2022-03-27 00:00:05","total":424.97014172,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 00:30:06","total":424.97666652,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 01:00:06","total":425.22301931,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 01:30:06","total":424.91940821,"risk":307.70675151999995,"avRisk":34.189639057777775}],"change":[{"time":"2022-03-27 00:00:05","change":0},{"time":"2022-03-27 00:30:06","change":0.0015353549248324776},{"time":"2022-03-27 01:00:06","change":0.05796854495972885},{"time":"2022-03-27 01:30:06","change":-0.07140043840822045}]}" width="1639" height="819" style="display: block; box-sizing: border-box; height: 819px; width: 1639px;"></canvas>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空袭的梦i 2025-01-24 06:35:10

yAxisID 不应该是解析对象的一部分。您需要在数据集的根目录中定义它:

let chartElement = $('#mainChart');
const data = chartElement.data('chart-dataset');
const cfg = {
  type: 'line',
  data: {
    datasets: [{
      'label': 'Total balance',
      data: data.stats,
      parsing: {
        xAxisKey: 'time',
        yAxisKey: 'total',
      },
      backgroundColor: 'rgb(98,250,2)',
      borderColor: 'rgb(98,250,2)',
    }, {
      'label': 'Risk balance',
      data: data.stats,
      parsing: {
        xAxisKey: 'time',
        yAxisKey: 'risk',
      },
      backgroundColor: 'rgb(255,191,34)',
      borderColor: 'rgb(255,191,34)',
    }, {
      'label': 'Average risk',
      data: data.stats,
      parsing: {
        xAxisKey: 'time',
        yAxisKey: 'avRisk',
      },
      backgroundColor: 'rgb(236,4,4)',
      borderColor: 'rgb(236,4,4)',
    }, {
      'label': 'Change %',
      data: data.change,
      parsing: {
        xAxisKey: 'time',
        yAxisKey: 'change',
      },
      yAxisID: 'yPercent',
      backgroundColor: 'rgb(31,116,241)',
      borderColor: 'rgb(31,116,241)',
    }]
  },
  options: {
    scales: {
      y: {
        id: 'y',
        type: 'linear',
        position: 'left',
        ticks: {
          beginAtZero: true,
        }
      },
      yPercent: {
        id: 'yPercent',
        type: 'linear',
        position: 'right',
        suggestedMax: 1.0,
        suggestedMin: -1.0,
        ticks: {
          precision: 4,
        }
      },
    }
  }
}
const mainChart = new Chart(
  chartElement,
  cfg
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="mainChart" data-chart-dataset="{"stats":[{"time":"2022-03-27 00:00:05","total":424.97014172,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 00:30:06","total":424.97666652,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 01:00:06","total":425.22301931,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 01:30:06","total":424.91940821,"risk":307.70675151999995,"avRisk":34.189639057777775}],"change":[{"time":"2022-03-27 00:00:05","change":0},{"time":"2022-03-27 00:30:06","change":0.0015353549248324776},{"time":"2022-03-27 01:00:06","change":0.05796854495972885},{"time":"2022-03-27 01:30:06","change":-100.07140043840822045}]}"
  width="1639" height="819" style="display: block; box-sizing: border-box; height: 819px; width: 1639px;"></canvas>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>

yAxisID is not supposed to be part of the parsing object. You need to define it in the root of the dataset:

let chartElement = $('#mainChart');
const data = chartElement.data('chart-dataset');
const cfg = {
  type: 'line',
  data: {
    datasets: [{
      'label': 'Total balance',
      data: data.stats,
      parsing: {
        xAxisKey: 'time',
        yAxisKey: 'total',
      },
      backgroundColor: 'rgb(98,250,2)',
      borderColor: 'rgb(98,250,2)',
    }, {
      'label': 'Risk balance',
      data: data.stats,
      parsing: {
        xAxisKey: 'time',
        yAxisKey: 'risk',
      },
      backgroundColor: 'rgb(255,191,34)',
      borderColor: 'rgb(255,191,34)',
    }, {
      'label': 'Average risk',
      data: data.stats,
      parsing: {
        xAxisKey: 'time',
        yAxisKey: 'avRisk',
      },
      backgroundColor: 'rgb(236,4,4)',
      borderColor: 'rgb(236,4,4)',
    }, {
      'label': 'Change %',
      data: data.change,
      parsing: {
        xAxisKey: 'time',
        yAxisKey: 'change',
      },
      yAxisID: 'yPercent',
      backgroundColor: 'rgb(31,116,241)',
      borderColor: 'rgb(31,116,241)',
    }]
  },
  options: {
    scales: {
      y: {
        id: 'y',
        type: 'linear',
        position: 'left',
        ticks: {
          beginAtZero: true,
        }
      },
      yPercent: {
        id: 'yPercent',
        type: 'linear',
        position: 'right',
        suggestedMax: 1.0,
        suggestedMin: -1.0,
        ticks: {
          precision: 4,
        }
      },
    }
  }
}
const mainChart = new Chart(
  chartElement,
  cfg
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="mainChart" data-chart-dataset="{"stats":[{"time":"2022-03-27 00:00:05","total":424.97014172,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 00:30:06","total":424.97666652,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 01:00:06","total":425.22301931,"risk":307.70675152,"avRisk":34.18963905777778},{"time":"2022-03-27 01:30:06","total":424.91940821,"risk":307.70675151999995,"avRisk":34.189639057777775}],"change":[{"time":"2022-03-27 00:00:05","change":0},{"time":"2022-03-27 00:30:06","change":0.0015353549248324776},{"time":"2022-03-27 01:00:06","change":0.05796854495972885},{"time":"2022-03-27 01:30:06","change":-100.07140043840822045}]}"
  width="1639" height="819" style="display: block; box-sizing: border-box; height: 819px; width: 1639px;"></canvas>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文