

{"id":1525,"date":"2020-07-08T01:17:19","date_gmt":"2020-07-07T16:17:19","guid":{"rendered":"https:\/\/curecode.jp\/tech\/?p=1525"},"modified":"2020-07-08T01:17:19","modified_gmt":"2020-07-07T16:17:19","slug":"chart-js-canvas-responsive-width-and-fixed-height","status":"publish","type":"post","link":"https:\/\/curecode.jp\/tech\/chart-js-canvas-responsive-width-and-fixed-height\/","title":{"rendered":"Chart.js \u3067\u7e26\u5e45\u3092\u56fa\u5b9a\u3001\u6a2a\u5e45\u306e\u307f\u30ec\u30b9\u30dd\u30f3\u30b7\u30d6\u306b\u5bfe\u5fdc\u3059\u308b"},"content":{"rendered":"<p>\n  JavaSCript \u306e\u30b0\u30e9\u30d5\u63cf\u753b\u30e9\u30a4\u30d6\u30e9\u30ea\u306e1\u3064\u306b\u3001Chart.js \u304c\u3042\u308a\u3088\u304f\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u3002\n<\/p>\n<p>\n  Chart.js \u306f canvas \u8981\u7d20\u306e\u4e0a\u306b\u63cf\u753b\u3057\u307e\u3059\u3002\n<\/p>\n<p>\n  \u30b0\u30e9\u30d5\u63cf\u753b\u9818\u57df\u306e\u7e26\u6a2a\u6bd4\uff08\u30a2\u30b9\u30da\u30af\u30c8\u6bd4\uff09\u306f canvas \u306e HTML \u306b <code>width<\/code> \u3068 <code>height<\/code> \u3067\u6307\u5b9a\u3057\u307e\u3059\u3002 \u3053\u308c\u304c\u8868\u793a\u30b5\u30a4\u30ba\u306b\u3082\u306a\u308a\u307e\u3059\u3002\n<\/p>\n<pre><code class=\"html\">\r\n  <canvas id=\"chart\" width=\"400\" height=\"300\">\r\n<\/code><\/pre>\n<p>\n  <code>width<\/code> \u3068 <code>height<\/code> \u3092\u6307\u5b9a\u3057\u306a\u3044\u3068\u304d\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306f <code>width=300, height=150<\/code> \u3067\u3059\u3002\n<\/p>\n<p>\n  \u30d6\u30e9\u30a6\u30b6\u306e\u8868\u793a\u30b5\u30a4\u30ba\u306b\u5fdc\u3058\u3066\u3001\u30b0\u30e9\u30d5\u306e\u6a2a\u5e45\u3092\u52d5\u7684\u306b\u5909\u3048\u305f\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057 canvas \u3068\u30ec\u30b9\u30dd\u30f3\u30b7\u30d6\u306e\u76f8\u6027\u306f\u306a\u304b\u306a\u304b\u3084\u3063\u304b\u3044\u306a\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002canvas \u306e\u5e45\u3092 CSS \u3067 <code>width: 100%; <\/code> \u306a\u3069\u3068\u6307\u5b9a\u3057\u3066\u3082\u3046\u307e\u304f\u3044\u304d\u307e\u305b\u3093\u3002\u307c\u3084\u3051\u305f\u308a\u3086\u304c\u3093\u3060\u308a\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002\n<\/p>\n<p>\n  CSS \u3067\u5e45\u3068\u9ad8\u3055\u3092\u6307\u5b9a\u3057\u305f\u3064\u3082\u308a\u3067\u3082\u3001\u305d\u308c\u306f HTML \u3067\u5e45\u3068\u9ad8\u3055\u3092\u6307\u5b9a\u3059\u308b\u306e\u3068\u306f\u610f\u5473\u304c\u7570\u306a\u308a\u307e\u3059\u3002canvas \u8981\u7d20\u306e\u63cf\u753b\u9818\u57df\u306f HTML \u3067\u6307\u5b9a\u3057\u305f\u5024\uff08\u6307\u5b9a\u3057\u3066\u3044\u306a\u3051\u308c\u3070\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\uff09\u306e\u307e\u307e\u3067\u3042\u308a\u3001\u305d\u308c\u304c CSS \u3067\u3057\u3066\u3044\u3057\u305f\u30b5\u30a4\u30ba\u306b\u5f15\u304d\u5ef6\u3070\u3055\u308c\u3066\u8868\u793a\u3055\u308c\u308b\u3060\u3051\u3060\u304b\u3089\u3067\u3059\u3002\n<\/p>\n<h3>canvas \u306e\u6a2a\u5e45\u3092\u52d5\u7684\u306b\u6307\u5b9a\u3059\u308b<\/h3>\n<p>\n  canvas \u306e\u8868\u793a\u30b5\u30a4\u30ba\u3060\u3051\u3067\u306a\u304f\u63cf\u753b\u9818\u57df\u306e\u30b5\u30a4\u30ba\u3082\u52d5\u7684\u306b\u5909\u66f4\u3059\u308b\u306b\u306f\u3001JavaScript \u3092\u4f7f\u3063\u3066 canvas \u8981\u7d20\u306e <code>width<\/code> \u3068 <code>height<\/code> \u5c5e\u6027\u306e\u5024\u3092\u30bb\u30c3\u30c8\u3057\u307e\u3059\u3002\n<\/p>\n<p>\n  \u3053\u306e\u3068\u304d  canvas \u306b\u89aa\u8981\u7d20\u3092\u4f5c\u3063\u3066\u304a\u304d\u307e\u3059\u3002\n<\/p>\n<pre><code class=\"html\">\r\n<div class=\"canvas-container\">\r\n    <canvas id=\"chart\"><\/canvas>\r\n<\/div>\r\n<\/code><\/pre>\n<p>\n  \u89aa\u8981\u7d20\u306e\u5e45\u3068\u9ad8\u3055\u306f CSS \u3067\u6307\u5b9a\u3057\u3066\u304a\u304d\u307e\u3059\u3002\n<\/p>\n<pre><code class=\"css>\r\n.canvas-container {\r\n    position: relative;\r\n    width:  calc(100% - 40px);\r\n    height: 300px;\r\n    overflow: hidden;\r\n    margin:20px;\r\n}\r\n<\/code><\/pre>\n<p>\n  \u753b\u9762\u306e\u8aad\u307f\u8fbc\u307f\u5b8c\u4e86\u5f8c\u306b canvas \u306e\u5e45\u3068\u9ad8\u3055\u306e\u5024\u3092 JavaScript \u3067\u30bb\u30c3\u30c8\u3057\u307e\u3059\u3002jQuery \u3067\u3084\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\n<\/p>\n<pre><code class=\"js\">\r\n$(function () {\r\n    var container = $('.canvas-container');\r\n    var ctx= $('#chart');\r\n    ctx.attr('width', container.width());\r\n    ctx.attr('height', 300);\r\n});\r\n<\/code><\/pre>\n<p>\n  \u4e0a\u306e\u4f8b\u3067\u306f\u9ad8\u3055\u306f 300px \u56fa\u5b9a\u3068\u3057\u3066\u66f8\u3044\u3066\u3044\u307e\u3059\u304c\u3001\u3082\u3061\u308d\u3093\u3053\u3061\u3089\u3082\u52d5\u7684\u306b\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\n<\/p>\n<p><h3>Chart.js \u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3057\u3066\u30ec\u30b9\u30dd\u30f3\u30b7\u30d6\u306b\u3059\u308b<\/h3>\n<p>\n  \u3055\u3066 canvas \u306e\u30b5\u30a4\u30ba\u3092\u52d5\u7684\u306b\u5909\u3048\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u30a6\u30a3\u30f3\u30c9\u30a6\u3092\u30ea\u30b5\u30a4\u30ba\u3057\u305f\u3068\u304d\u306b canvas \u306e\u518d\u63cf\u753b\u3092\u3057\u306a\u3044\u3068\u4e0d\u90fd\u5408\u3067\u3059\u3002\n<\/p>\n<p>\n  Chart.js \u3067\u306f options\u306b <code>responsive: true,<\/code>  \u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u81ea\u52d5\u7684\u306b\u5bfe\u5fdc\u3057\u3066\u304f\u308c\u307e\u3059\u3002\u4eca\u56de\u306e\u3088\u3046\u306b\u5e45\u3068\u9ad8\u3055\u306e\u6bd4\u7387\u304c\u52d5\u7684\u306b\u5909\u5316\u3059\u308b\u5834\u5408\u306f\u3001<code>maintainAspectRatio: false,<\/code> \u3082\u6307\u5b9a\u3057\u3066\u3084\u308b\u3068\u3046\u307e\u304f\u884c\u304d\u307e\u3059\u3002\n<\/p>\n<pre><code class=\"javascript\">\r\n$(function () {\r\n    var container = $('.canvas-container');\r\n    var chart= $('#chart');\r\n    ctx.attr('width', container.width());\r\n    ctx.attr('height', 300);\r\n});\r\nvar myChart = new Chart(ctx, {\r\n    type: 'line',\r\n    data: {\r\n       ...\r\n    },\r\n    options: {\r\n        responsive: true,\r\n        maintainAspectRatio: false,\r\n        scales: {\r\n           ...\r\n    });\r\n});\r\n<\/code><\/pre>\n<h3>IT\u30a8\u30f3\u30b8\u30cb\u30a2\u52df\u96c6\u4e2d\uff01<\/h3>\n<p>\u30ad\u30e5\u30a2\u30b3\u30fc\u30c9\u682a\u5f0f\u4f1a\u793e\u306fIT\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u52df\u96c6\u3057\u3066\u304a\u308a\u307e\u3059\u3002\u5c11\u4eba\u6570\u306e\u8077\u5834\u306a\u306e\u3067\u3001\u4e0a\u6d41\u30fb\u4e0b\u6d41\u3084\u30b5\u30fc\u30d0\u30fc\u30fb\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5bfe\u5fdc\u306e\u57a3\u6839\u306a\u304f\u3001\u3042\u306a\u305f\u306e\u5f37\u307f\u3092\u6d3b\u304b\u3057\u306a\u304c\u3089 \u3044\u308d\u3044\u308d\u306a\u3053\u3068\u306b\u30c1\u30e3\u30ec\u30f3\u30b8\u53ef\u80fd\u3067\u3059\u3002\u30a8\u30f3\u30b8\u30cb\u30a2\u3068\u3057\u3066\u306e\u672a\u7d4c\u9a13\u306e\u65b9\u3001\u7d4c\u9a13\u304c\u5c11\u306a\u3044\u65b9\u3082\u6b53\u8fce\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4e0b\u8a18\u3088\u308aIT\u30a8\u30f3\u30b8\u30cb\u30a2\u52df\u96c6\u306e\u63a1\u7528\u60c5\u5831\u3092\u3054\u89a7\u3044\u305f\u3060\u3051\u307e\u3059\u3002<\/p>\n<div class=\"curecode_recruit_bn\">\n\t\t\t\t<a href=\"https:\/\/curecode.jp\/recruit\/itengineer\/\" target=\"_blank\" rel=\"noopener noreferrer\"><br \/>\n\t\t\t\t\t\t<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/curecode.jp\/tech\/wp-content\/themes\/tech\/images\/it_recruit.png?20241220\" alt=\"\u4e8b\u696d\u62e1\u5927\u306b\u4f34\u3044\u65b0\u3057\u3044\u9818\u57df\u306b\u4e00\u7dd2\u306b\u53d6\u308a\u7d44\u3093\u3067\u3044\u304f\u30e1\u30f3\u30d0\u30fc\u3092\u52df\u96c6\u3057\u307e\u3059\u3002\"  width=\"500\" height=\"180\"\/><br \/>\n\t\t\t\t<\/a>\n\t\t<\/div>\n<h3>\u30ad\u30e5\u30a2\u30b3\u30fc\u30c9\u516c\u5f0f\u30a4\u30f3\u30b9\u30bf\u30b0\u30e9\u30e0<\/h3>\n<p>\u30ad\u30e5\u30a2\u30b3\u30fc\u30c9\u682a\u5f0f\u4f1a\u793e\u306e\u65b0\u30b5\u30fc\u30d3\u30b9\u60c5\u5831\u3084\u3001\u30aa\u30d5\u30a3\u30b9\u30e9\u30a4\u30d5\u306e\u7d20\u6575\u306a\u77ac\u9593\u307e\u3067\u3002\u79c1\u305f\u3061\u306e\u65e5\u3005\u306e\u55b6\u307f\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<div class=\"curecode_recruit_bn\">\n\t\t\t\t<a href=\"https:\/\/www.instagram.com\/curecode\/\" target=\"_blank\" rel=\"noopener noreferrer\"><br \/>\n\t\t\t\t\t\t<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/curecode.jp\/tech\/wp-content\/themes\/tech\/images\/insta_bn.png?20241220\" alt=\"\u4e8b\u696d\u62e1\u5927\u306b\u4f34\u3044\u65b0\u3057\u3044\u9818\u57df\u306b\u4e00\u7dd2\u306b\u53d6\u308a\u7d44\u3093\u3067\u3044\u304f\u30e1\u30f3\u30d0\u30fc\u3092\u52df\u96c6\u3057\u307e\u3059\u3002\"  width=\"500\" height=\"180\"\/><br \/>\n\t\t\t\t<\/a>\n\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>JavaSCript \u306e\u30b0\u30e9\u30d5\u63cf\u753b\u30e9\u30a4\u30d6\u30e9\u30ea\u306e1\u3064\u306b\u3001Chart.js \u304c\u3042\u308a\u3088\u304f\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u3002  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":592,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[166,10],"tags":[171,163,17,68],"class_list":["post-1525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","category-jquery","tag-chart-js","tag-css","tag-html5","tag-jquery"],"_links":{"self":[{"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/posts\/1525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/comments?post=1525"}],"version-history":[{"count":4,"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/posts\/1525\/revisions"}],"predecessor-version":[{"id":1529,"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/posts\/1525\/revisions\/1529"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/media\/592"}],"wp:attachment":[{"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/media?parent=1525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/categories?post=1525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/curecode.jp\/tech\/wp-json\/wp\/v2\/tags?post=1525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}