如何在图表中绘制两个混合条形图的比较?

发布于 2025-01-18 01:33:09 字数 5721 浏览 4 评论 0原文

因此,我使用Chart.js的图形: 没有问题,但是现在我想比较这样的条: http://jsfiddle.net/jsfiddle.net/kyo3bqzh/ < /a> 或这里:

var data = {
  labels: ["1_jan", "2_jan", "3_jan"],
            datasets: [
              {
            type: 'bar',
            label: 'fees',
            data: [-5,-5,-6],
            backgroundColor: 'yellow',
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 1
          },
          {
            type: 'bar',
            label: 'net revenue',
            data: [20,32,20],
            backgroundColor: 'pink',
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            order: 1,
            stack: 1
          },
          {
            type: 'line',
            label: 'net revenue',
            data: [20,32,20],
            backgroundColor: 'pink',
            borderColor: 'black',
            fill: false,
            pointRadius: 8,
            pointHoverRadius: 8,
            bezierCurve: false,
            order: 2,
            stack: 1
          },
          {
            type: 'bar',
            label: 'refunds',
            data: [-1,-5,-6],
            backgroundColor: 'lightblue',
            borderColor: 'black',
            fill: false,
            pointRadius:6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 1
          },
          {
            type: 'bar',
            label: 'taxes',
            backgroundColor: 'blue',
            data: [-1,-5,-6],
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 1
          },
          
          
          
          
          
        {
            type: 'bar',
            label: 'fees',
            data: [-10,-10,-16],
            backgroundColor: 'yellow',
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
          stack: 2
          },
          {
            type: 'bar',
            label: 'net revenue',
            data: [30,40,50],
            backgroundColor: 'pink',
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            order: 1,
            stack: 2
          },
          {
            type: 'line',
            label: 'net revenue',
            data: [30,40,50],
            backgroundColor: 'pink',
            borderColor: 'black',
            fill: false,
            pointRadius: 8,
            pointHoverRadius: 8,
            bezierCurve: false,
            order: 2,
            stack: 2
          },
          {
            type: 'bar',
            label: 'refunds',
            data: [-10,-14,-19],
            backgroundColor: 'lightblue',
            borderColor: 'black',
            fill: false,
            pointRadius:6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 2
          },
          {
            type: 'bar',
            label: 'taxes',
            backgroundColor: 'blue',
            data: [-10,-12,-21],
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 2
          }
            ]
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, {
  type: 'bar',
  data: data,
  options: {
        legend: {
          display: false,
        },
        tooltips: {
          intersect: true,
          mode: 'x',
        },
        responsive: true,
        maintainAspectRatio: false,
        elements: {
          line: {
            tension: 0,
          },
          plugins: {
            title: {
              display: true,
              text: 'title',
            },
          },
        },
        scales: {
          xAxes: [
            {
              stacked:true,
              gridLines: {
                display: false,
              },
              ticks: {
                autoSkip: true,
                maxTicksLimit: 9,
                maxRotation: 0,
                minRotation: 0,
                padding: 20,
              },
            },
          ],
          yAxes: [
            {
              stacked:true,
              gridLines: {
                drawBorder: false,
              },
              ticks: {
                fontSize: 14,
                fontFamily: 'IBM Plex Sans',
                padding: 10,
                beginAtZero:true
              },
            },
          ],
        }
      }
});
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js'></script>
<canvas id="myChart" style="height: 300px"></canvas>
For comparison and grouping i tried using stack property. The problem here is the line chart, i want to draw this line chart like on this pic: expected_result And when i hovering over any column of a pair of columns, I want to see the information of both columns in the tooltip.

我该怎么做?

So, i have this graphic by chart.js: http://jsfiddle.net/wdxk1nsb/1/
there is no problems, but now i want to compare this bars like this: http://jsfiddle.net/kyo3bqzh/
or here:

var data = {
  labels: ["1_jan", "2_jan", "3_jan"],
            datasets: [
              {
            type: 'bar',
            label: 'fees',
            data: [-5,-5,-6],
            backgroundColor: 'yellow',
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 1
          },
          {
            type: 'bar',
            label: 'net revenue',
            data: [20,32,20],
            backgroundColor: 'pink',
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            order: 1,
            stack: 1
          },
          {
            type: 'line',
            label: 'net revenue',
            data: [20,32,20],
            backgroundColor: 'pink',
            borderColor: 'black',
            fill: false,
            pointRadius: 8,
            pointHoverRadius: 8,
            bezierCurve: false,
            order: 2,
            stack: 1
          },
          {
            type: 'bar',
            label: 'refunds',
            data: [-1,-5,-6],
            backgroundColor: 'lightblue',
            borderColor: 'black',
            fill: false,
            pointRadius:6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 1
          },
          {
            type: 'bar',
            label: 'taxes',
            backgroundColor: 'blue',
            data: [-1,-5,-6],
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 1
          },
          
          
          
          
          
        {
            type: 'bar',
            label: 'fees',
            data: [-10,-10,-16],
            backgroundColor: 'yellow',
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
          stack: 2
          },
          {
            type: 'bar',
            label: 'net revenue',
            data: [30,40,50],
            backgroundColor: 'pink',
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            order: 1,
            stack: 2
          },
          {
            type: 'line',
            label: 'net revenue',
            data: [30,40,50],
            backgroundColor: 'pink',
            borderColor: 'black',
            fill: false,
            pointRadius: 8,
            pointHoverRadius: 8,
            bezierCurve: false,
            order: 2,
            stack: 2
          },
          {
            type: 'bar',
            label: 'refunds',
            data: [-10,-14,-19],
            backgroundColor: 'lightblue',
            borderColor: 'black',
            fill: false,
            pointRadius:6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 2
          },
          {
            type: 'bar',
            label: 'taxes',
            backgroundColor: 'blue',
            data: [-10,-12,-21],
            borderColor: 'black',
            fill: false,
            pointRadius: 6,
            pointHoverRadius: 6,
            bezierCurve: false,
            stack: 2
          }
            ]
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, {
  type: 'bar',
  data: data,
  options: {
        legend: {
          display: false,
        },
        tooltips: {
          intersect: true,
          mode: 'x',
        },
        responsive: true,
        maintainAspectRatio: false,
        elements: {
          line: {
            tension: 0,
          },
          plugins: {
            title: {
              display: true,
              text: 'title',
            },
          },
        },
        scales: {
          xAxes: [
            {
              stacked:true,
              gridLines: {
                display: false,
              },
              ticks: {
                autoSkip: true,
                maxTicksLimit: 9,
                maxRotation: 0,
                minRotation: 0,
                padding: 20,
              },
            },
          ],
          yAxes: [
            {
              stacked:true,
              gridLines: {
                drawBorder: false,
              },
              ticks: {
                fontSize: 14,
                fontFamily: 'IBM Plex Sans',
                padding: 10,
                beginAtZero:true
              },
            },
          ],
        }
      }
});
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js'></script>
<canvas id="myChart" style="height: 300px"></canvas>

For comparison and grouping i tried using stack property.
The problem here is the line chart, i want to draw this line chart like on this pic: expected_result
And when i hovering over any column of a pair of columns, I want to see the information of both columns in the tooltip.

How can i do this?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文