回答編集履歴

3

修正

2021/10/25 13:14

投稿

退会済みユーザー
test CHANGED
@@ -76,24 +76,6 @@
76
76
 
77
77
  ```js
78
78
 
79
-
80
-
81
- const colors = {
82
-
83
- typeA: "#121554",
84
-
85
- typeB: "#1d3681",
86
-
87
- typeC: "#2e70a7",
88
-
89
- typeD: "#4eadc7",
90
-
91
- undefined: "#a7d8bf",
92
-
93
- };
94
-
95
-
96
-
97
79
  const types = [
98
80
 
99
81
  { name: "typeA", color: "#121554" },

2

修正

2021/10/25 13:14

投稿

退会済みユーザー
test CHANGED
@@ -94,23 +94,39 @@
94
94
 
95
95
 
96
96
 
97
+ const types = [
98
+
99
+ { name: "typeA", color: "#121554" },
100
+
101
+ { name: "typeB", color: "#1d3681" },
102
+
103
+ { name: "typeC", color: "#2e70a7" },
104
+
105
+ { name: "typeD", color: "#4eadc7" },
106
+
107
+ { name: "undefined", color: "#a7d8bf" },
108
+
109
+ ];
110
+
111
+
112
+
97
113
  const labels = ["A", "B"];
98
114
 
99
115
 
100
116
 
101
- const datasets = Object.keys(groupBy).map((e) => {
117
+ const datasets = types.map((e) => {
102
118
 
103
119
  return {
104
120
 
105
- label: e,
121
+ label: e.name,
106
122
 
107
123
  borderWidth: 1,
108
124
 
109
- backgroundColor: colors[e] || colors.undefined,
125
+ backgroundColor: e.color,
110
126
 
111
- borderColor: colors[e] || colors.undefined,
127
+ borderColor: e.color,
112
128
 
113
- data: labels.map((i) => groupBy[e][i] || 0),
129
+ data: labels.map((i) => groupBy[e.name] && groupBy[e.name][i] || 0),
114
130
 
115
131
  };
116
132
 

1

修正

2021/10/25 13:14

投稿

退会済みユーザー
test CHANGED
@@ -1,4 +1,4 @@
1
- 元の配列 group の要素最初と最後入れ替えてみると
1
+ たとえば質問文記載されている配列 group の最後の要素最初に持ってきてスクリプト実行すると
2
2
 
3
3
  groupBy は
4
4
 
@@ -54,11 +54,11 @@
54
54
 
55
55
  {
56
56
 
57
- typeA: {A: 60, B: 66}
57
+ typeA: {A: 60, B: 66},
58
58
 
59
- typeB: {A: 120, B: 257}
59
+ typeB: {A: 120, B: 257},
60
60
 
61
- typeC: {B: 0, A: 83}
61
+ typeC: {B: 0, A: 83},
62
62
 
63
63
  typeD: {B: 120, A: 0}
64
64
 
@@ -78,7 +78,7 @@
78
78
 
79
79
 
80
80
 
81
- cconst colors = {
81
+ const colors = {
82
82
 
83
83
  typeA: "#121554",
84
84