X轴上的标签不会在图表中完全显示

发布于 2025-02-08 00:27:09 字数 2229 浏览 0 评论 0原文

我有一个来自后端的数据(烧瓶 - 塞拉尔奇与mysql),其中数据包含一周,月和年的整数日期,看起来像这样:

在这种情况下,x代表一年中的一周:

let month_true = [
  {
    bob: {
      "base-certificate": 60,
      "case-certificate": 1,
      "standard-certificate": 7,
    },
    x: 9,
  },
  {
    bob: {
      "base-certificate": 30,
      "case-certificate": 4,
      "standard-certificate": 5,
    },
    x: 11,
  },
];

我所做的第一件事是要映射数据并将X更改为JavaScript和Chart.js的可读格式,使用将一周中的一周数转换为实际日期的函数:

函数:

 function getDateOfWeek(w, y) {
      var d = 1 + (w - 1) * 7; // 1st of January + 7 days for each week
    
      return new Date(y, 0, d);
    }

通过数据映射:

let newData = month_true.map(function (m) {
  let dateObject = new Date();
  let year = dateObject.getFullYear();
  const weekDate = getDateOfWeek(m.x, year);

  let r = new Date(Date.parse(weekDate));
  const newd = {
    x: r.setHours(0, 0, 0, 0),
    base: m.bob["base-certificate"],
    case: m.bob["case-certificate"],
    standard: m.bob["standard-certificate"],
  };
  return newd;
});

let newdate = newData.map(function (d) {
  return d.x;
});

现在构造图表:

let ctx = document.getElementById("chart").getContext("2d");
let config = {
  //labels: newdate,
  type: "bar",
  data: {
    datasets: [
      {
        label: "base",
        data: newData,
        parsing: {
          xAxisKey: "x",
          yAxisKey: "base",
        },
      },
      {
        label: "Case",
        data: newData,
        parsing: {
          xAxisKey: "x",
          yAxisKey: "case",
        },
      },
      {
        label: "Standard",
        data: newData,
        parsing: {
          xAxisKey: "x",
          yAxisKey: "standard",
        },
      },
    ],
  },
  options: {
    scales: {
      x: {
        ticks: {
          beginAtZero: true,
        },
        offset: true,
        type: "time",
        time: {
          //isoWeekday: true,
          unit: "week",
        },
      },
    },
  },
};

let myChart = new Chart(ctx, config);

它已绘制但已绘制,但是X轴未显示第二个标签:

在这里输入图像描述

我能做什么tomake标签完全出现了吗?

I have a data from the backend(flask-sqlalchemy with mysql) in which data contains date in integer for week, month and year that looks like this:

in this case the x represents the week of the year:

let month_true = [
  {
    bob: {
      "base-certificate": 60,
      "case-certificate": 1,
      "standard-certificate": 7,
    },
    x: 9,
  },
  {
    bob: {
      "base-certificate": 30,
      "case-certificate": 4,
      "standard-certificate": 5,
    },
    x: 11,
  },
];

The first thing I did was to map the data and change the x into a readable format for Javascript and chart.js, using a function that converts week of the year number to actual date:

the function:

 function getDateOfWeek(w, y) {
      var d = 1 + (w - 1) * 7; // 1st of January + 7 days for each week
    
      return new Date(y, 0, d);
    }

Mapping through the data:

let newData = month_true.map(function (m) {
  let dateObject = new Date();
  let year = dateObject.getFullYear();
  const weekDate = getDateOfWeek(m.x, year);

  let r = new Date(Date.parse(weekDate));
  const newd = {
    x: r.setHours(0, 0, 0, 0),
    base: m.bob["base-certificate"],
    case: m.bob["case-certificate"],
    standard: m.bob["standard-certificate"],
  };
  return newd;
});

let newdate = newData.map(function (d) {
  return d.x;
});

now construct the chart:

let ctx = document.getElementById("chart").getContext("2d");
let config = {
  //labels: newdate,
  type: "bar",
  data: {
    datasets: [
      {
        label: "base",
        data: newData,
        parsing: {
          xAxisKey: "x",
          yAxisKey: "base",
        },
      },
      {
        label: "Case",
        data: newData,
        parsing: {
          xAxisKey: "x",
          yAxisKey: "case",
        },
      },
      {
        label: "Standard",
        data: newData,
        parsing: {
          xAxisKey: "x",
          yAxisKey: "standard",
        },
      },
    ],
  },
  options: {
    scales: {
      x: {
        ticks: {
          beginAtZero: true,
        },
        offset: true,
        type: "time",
        time: {
          //isoWeekday: true,
          unit: "week",
        },
      },
    },
  },
};

let myChart = new Chart(ctx, config);

It got plotted but the x-axis is not showing the second label:

enter image description here

what can I can do tomake the labels appear completely?

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

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

发布评论

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

评论(1

り繁华旳梦境 2025-02-15 00:27:09

这是因为默认情况下,时间尺度自动生成ticks,如果您不想包含数据中的标签,则需要设置tick。source。

options: {
  scales: {
    x: {
      type: 'time',
      ticks: {
        source: 'data'
      }
    }
  }
}

This is because by default the time scale auto generates ticks, if you dont want to include the labels from your data you need to set ticks.source to data:

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