无论如何,是否可以将一个Barchart与Chart JS中的另一个Barchart重叠,而无需堆叠它们?

发布于 2025-02-09 17:04:10 字数 2077 浏览 1 评论 0原文

我尝试使用stackoffset,但这似乎不起作用,

这是我的代码:

import React, {
  useEffect
} from 'react';
import {
  Bar
} from 'react-chartjs-2';
import useWindowWidth from 'wa-shared/src/hooks/useWindowWidth';

const state = {
  count: 0,
  data: {
    labels: [
      ['Total Users'],
      ['Users with', 'Active', 'Session'],
      ['Created 1', 'or More', 'Videos'],
      ['Shared 1', 'or More', 'Videos']
    ],
    datasets: [{
        backgroundColor: '#26863D',
        data: [70, 66, 57, 1],
        barThickness: '35',
      },
      {
        backgroundColor: '#F2F2F2',
        data: [70, 70, 70, 70],
        borderRadius: '5',
        barThickness: '35',
      }
    ]
  }
}

const App = ({}) => {
  const width = useWindowWidth();
  useEffect(() => {});
  return ( <
    div className = 'singlebar-chart'
    id = "barchart" >
    <Bar> data = {
      state.data
    }
    options = {
      {
        scales: {
          x: {
            grid: {
              display: false,
            },
            offset: true,
            ticks: {
              maxRotation: 0,
              minRotation: 0,
              font: {
                size: 8,
              }
            },
          },
          y: {
            min: 0,
            max: 100,
            ticks: {
              fontColor: '#8A8A8A',
              font: {
                size: 8,
              },
              stepSize: 25,
              callback: function(value) {
                return value + "%"
              },
            },
            scaleLabel: {
              display: true,
              labelString: "Percentage",
            },
            grid: {
              display: false,
            }
          }
        },
        plugins: {
          legend: {
            display: false,
          },
        }
      }
      </Bar>
    </div>
  )
}

export default App;
.singlebar-chart{
    width: 95%;
}

I tried using the stack and offset but it does not seem to work

This is my code:

import React, {
  useEffect
} from 'react';
import {
  Bar
} from 'react-chartjs-2';
import useWindowWidth from 'wa-shared/src/hooks/useWindowWidth';

const state = {
  count: 0,
  data: {
    labels: [
      ['Total Users'],
      ['Users with', 'Active', 'Session'],
      ['Created 1', 'or More', 'Videos'],
      ['Shared 1', 'or More', 'Videos']
    ],
    datasets: [{
        backgroundColor: '#26863D',
        data: [70, 66, 57, 1],
        barThickness: '35',
      },
      {
        backgroundColor: '#F2F2F2',
        data: [70, 70, 70, 70],
        borderRadius: '5',
        barThickness: '35',
      }
    ]
  }
}

const App = ({}) => {
  const width = useWindowWidth();
  useEffect(() => {});
  return ( <
    div className = 'singlebar-chart'
    id = "barchart" >
    <Bar> data = {
      state.data
    }
    options = {
      {
        scales: {
          x: {
            grid: {
              display: false,
            },
            offset: true,
            ticks: {
              maxRotation: 0,
              minRotation: 0,
              font: {
                size: 8,
              }
            },
          },
          y: {
            min: 0,
            max: 100,
            ticks: {
              fontColor: '#8A8A8A',
              font: {
                size: 8,
              },
              stepSize: 25,
              callback: function(value) {
                return value + "%"
              },
            },
            scaleLabel: {
              display: true,
              labelString: "Percentage",
            },
            grid: {
              display: false,
            }
          }
        },
        plugins: {
          legend: {
            display: false,
          },
        }
      }
      </Bar>
    </div>
  )
}

export default App;
.singlebar-chart{
    width: 95%;
}

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

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

发布评论

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

评论(1

怂人 2025-02-16 17:04:10

您可以使用第二个X轴并将其他数据集映射到该轴:

var options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: 'pink'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        backgroundColor: 'orange',
        xAxisID: 'x2'
      }
    ]
  },
  options: {
    scales: {
      x: {},
      x2: {
        display: false // Dont show the axes since it is just a duplicate
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
</body>

尽管这给出了与设置在X轴上堆叠的相同结果。

You can use a second x axis and map the other dataset to that axes:

var options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: 'pink'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        backgroundColor: 'orange',
        xAxisID: 'x2'
      }
    ]
  },
  options: {
    scales: {
      x: {},
      x2: {
        display: false // Dont show the axes since it is just a duplicate
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
</body>

Although this gives the exect same result as setting stacked true on the x axis.

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