Relative bar chart overlay on line chart in chart.js

不想你离开。 提交于 2021-02-10 19:41:37

问题


I'm trying to build a chart in chart.js, showing price data of a virtual item, with a line chart showing pricing, and a backlay bar chart, showing sales volume, like this:

My issue is, the y-axis is the same for both, meaning the price data is shown at the bottom with unnoticeable differences, since the volume is in the hundreds.

I want the price data to be on the y-axis, with the bars shown relatively, with the highest volume being shown as 100% height, not the values on the y-axis, something more like this:

Is this possible, and how would it be done? Thanks!

Code:

let marketData = [["Nov 28 2018 20: +0",30.332,"103"],["Nov 28 2018 21: +0",25.801,"188"],["Nov 28 2018 22: +0",25.451,"262"],["Nov 28 2018 23: +0",12.484,"693"]];

let lineData = [];
let barData = [];
let labels = [];

marketData.forEach(function(thing) {
    labels.push(thing[0].replace(' +0', '00'));
    lineData.push(thing[1]);
    barData.push(thing[2]);
});

new Chart(document.getElementById("mixed-chart"), {
    type: 'bar',
    data: {
      labels: labels,
      datasets: [
        {
          label: "Price",
          type: "line",
          borderColor: "#3e95cd",
          data: lineData,
          fill: false
        },
        {
          label: "Sold",
          type: "bar",
          backgroundColor: "rgba(0,0,0,0.2)",
          data: barData
        }
      ]
    },
    options: {
      title: {
        display: true,
        text: 'Sale price vs sale volume'
      },
      legend: { display: false }
    }
});

回答1:


use a second y-axis to give the bar series a different scale

assign each data set to its own axis by using property --> yAxisID

then set the matching id of the y-axis in the scales.yAxes option

see following working snippet...

$(document).ready(function() {
  let marketData = [["Nov 28 2018 20: +0",30.332,"103"],["Nov 28 2018 21: +0",25.801,"188"],["Nov 28 2018 22: +0",25.451,"262"],["Nov 28 2018 23: +0",12.484,"693"]];

  let lineData = [];
  let barData = [];
  let labels = [];

  marketData.forEach(function(thing) {
    labels.push(thing[0].replace(' +0', '00'));
    lineData.push(thing[1]);
    barData.push(thing[2]);
  });

  new Chart(document.getElementById("mixed-chart"), {
    type: 'bar',
    data: {
      labels: labels,
      datasets: [
        {
          label: "Price",
          type: "line",
          borderColor: "#3e95cd",
          data: lineData,
          fill: false,
          yAxisID: 'A'  // <-- set y-axis id
        },
        {
          label: "Sold",
          type: "bar",
          backgroundColor: "rgba(0,0,0,0.2)",
          data: barData,
          yAxisID: 'B'  // <-- set y-axis id
        }
      ]
    },
    options: {
      title: {
        display: true,
        text: 'Sale price vs sale volume'
      },
      legend: {display: false},
      scales: {
        yAxes: [{
          id: 'A',  // <-- set y-axis id
          position: 'left',
        }, {
          id: 'B',  // <-- set y-axis id
          position: 'right',
          
          // hide grid lines and labels
          gridLines: {
            display: false
          },
          ticks: {
            display: false
          }
        }]
      }
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="mixed-chart"></canvas>


来源:https://stackoverflow.com/questions/53557106/relative-bar-chart-overlay-on-line-chart-in-chart-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!