首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图表1到n的关系

图表1到n的关系
EN

Stack Overflow用户
提问于 2020-08-06 22:13:30
回答 1查看 70关注 0票数 0

嘿我有个chartjs的问题

我有下面的柱状图

代码语言:javascript
复制
<canvas id="bar-chart" width="800" height="450"></canvas>

new Chart(document.getElementById("bar-chart"), {
    type: 'bar',
    data: {
      labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
      datasets: [
        {
          label: "Population (millions)",
          backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
          data: [2478,5267,734,784,433]
        }
      ]
    },
    options: {
      legend: { display: false },
      title: {
        display: true,
        text: 'Predicted world population (millions) in 2050'
      }
    }
});

我想让每个标签都有3个值,而不是1,因为现在知道怎么做了吗?

例如,现在非洲我有2478,但我想要2478,432,888

EN

回答 1

Stack Overflow用户

发布于 2020-08-09 22:54:42

如果我正确理解了您的问题,您必须定义多个数据集才能获得所需的结果。

请看一下你修改后的代码,它使用随机数据。

代码语言:javascript
复制
new Chart(document.getElementById("bar-chart"), {
  type: 'bar',
  data: {
    labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
    datasets: [{
        label: "Population (millions)",
        data: [2478, 5267, 734, 784, 433],
        backgroundColor: ["rgba(255, 99, 132, 0.6)", "rgba(255, 159, 64, 0.6)", "rgba(255, 205, 86, 0.6)", "rgba(75, 192, 192, 0.6)", "rgba(54, 162, 235, 0.6)"],
        borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(54, 162, 235)"],
        borderWidth: 1
      },
      {
        label: "Female (millions)",
        data: [1278, 2667, 370, 400, 216],
        backgroundColor: ["rgba(255, 99, 132, 0.4)", "rgba(255, 159, 64, 0.4)", "rgba(255, 205, 86, 0.4)", "rgba(75, 192, 192, 0.4)", "rgba(54, 162, 235, 0.4)"],
        borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(54, 162, 235)"],
        borderWidth: 1
      },
      {
        label: "Male (millions)",
        data: [1200, 2600, 364, 384, 217],
        backgroundColor: ["rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)", "rgba(255, 205, 86, 0.2)", "rgba(75, 192, 192, 0.2)", "rgba(54, 162, 235, 0.2)"],
        borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(54, 162, 235)"],
        borderWidth: 1
      }
    ]
  },
  options: {
    legend: {
      display: false
    },
    title: {
      display: true,
      text: 'Predicted world population (millions) in 2050'
    },
    tooltips: {
      mode: 'index'
    }
  }
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"></script>
<canvas id="bar-chart" width="800" height="450"></canvas>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63285495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档