是否可以在图表中的条形图中具有跨线,例如下图

发布于 2025-02-05 11:33:25 字数 1128 浏览 3 评论 0 原文

我看到我们可以更改颜色,但是真的有可能具有这样的彩色线条

https://codepen.io/diasraphael/pen/mdxzdqr

var ctx = document.getElementById("mybarChart").getContext("2d");

var mybarChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ['1', '2', '3'],
    datasets: [{
      label: 'Candidate A Votes',
      backgroundColor: "#000080",
      data: [90,0,0]
    }, {
      label: 'Candidate B Votes2',
      backgroundColor: "#d3d3d3",
      data: [0,70,0]
    }, {
      label: 'Candidate C  Votes3',
      backgroundColor: "#add8e6",
      data: [0,0,45]
    }]
  },

  options: {
    legend: {
      display: true,
      position: 'top',
      labels: {
        fontColor: "#000080",
      }
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    }
  }
});

chartjs cross lines

I see we can change the color but is it really possible to have colored lines like this

https://codepen.io/diasraphael/pen/mdXzdqr

var ctx = document.getElementById("mybarChart").getContext("2d");

var mybarChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ['1', '2', '3'],
    datasets: [{
      label: 'Candidate A Votes',
      backgroundColor: "#000080",
      data: [90,0,0]
    }, {
      label: 'Candidate B Votes2',
      backgroundColor: "#d3d3d3",
      data: [0,70,0]
    }, {
      label: 'Candidate C  Votes3',
      backgroundColor: "#add8e6",
      data: [0,0,45]
    }]
  },

  options: {
    legend: {
      display: true,
      position: 'top',
      labels: {
        fontColor: "#000080",
      }
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    }
  }
});

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

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

发布评论

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

评论(1

你的心境我的脸 2025-02-12 11:33:25

是的,如所描述的在这里您可以使用 canvas模式

为了使它变得更加容易,您可以使用将您想要的模式绘制为背景的外部LIB:

const options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: pattern.draw('diagonal-right-left', '#1f77b4')
    }]
  },
  options: {}
}

const 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>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/patternomaly.js"></script>
</body>

Yes as described here you can use a canvas pattern.

To make it a bit easyer you can use an external lib that draws the pattern you want as the background:

const options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: pattern.draw('diagonal-right-left', '#1f77b4')
    }]
  },
  options: {}
}

const 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>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/patternomaly.js"></script>
</body>

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