回答編集履歴

1

追記

2018/09/25 01:41

投稿

退会済みユーザー
test CHANGED
@@ -7,3 +7,337 @@
7
7
 
8
8
 
9
9
  Series.ChartType プロパティを SeriesChartType.StackedColumn に設定する必要があるはずです。
10
+
11
+
12
+
13
+ **【追伸】**
14
+
15
+
16
+
17
+ 以前作ったサンプルをアップしておきます。
18
+
19
+
20
+
21
+ このようなグラフなら、
22
+
23
+
24
+
25
+ ![イメージ説明](e5803991a7096523521ab13cc8b5d889.jpeg)
26
+
27
+
28
+
29
+ 以下のコードでできます。
30
+
31
+
32
+
33
+ ```
34
+
35
+ using System;
36
+
37
+ using System.Collections.Generic;
38
+
39
+ using System.ComponentModel;
40
+
41
+ using System.Data;
42
+
43
+ using System.Drawing;
44
+
45
+ using System.Linq;
46
+
47
+ using System.Text;
48
+
49
+ using System.Threading.Tasks;
50
+
51
+ using System.Windows.Forms;
52
+
53
+ using System.Data.SqlClient;
54
+
55
+ using System.Windows.Forms.DataVisualization.Charting;
56
+
57
+
58
+
59
+ namespace WindowsFormsChart
60
+
61
+ {
62
+
63
+ public partial class Form1 : Form
64
+
65
+ {
66
+
67
+ public Form1()
68
+
69
+ {
70
+
71
+ InitializeComponent();
72
+
73
+ }
74
+
75
+
76
+
77
+ private DataTable CreateDataTable()
78
+
79
+ {
80
+
81
+ string connString = @"Data Source=(local)\sqlexpress;Initial Catalog=Database;Integrated Security=True";
82
+
83
+ string selectQuery =
84
+
85
+ @"SELECT
86
+
87
+ Month,
88
+
89
+ SUM(CASE WHEN Name = N'三吉' THEN Sales ELSE 0 END) AS 三吉,
90
+
91
+ SUM(CASE WHEN Name = N'春日' THEN Sales ELSE 0 END) AS 春日,
92
+
93
+ SUM(CASE WHEN Name = N'東雲' THEN Sales ELSE 0 END) AS 東雲,
94
+
95
+ SUM(CASE WHEN Name = N'府中' THEN Sales ELSE 0 END) AS 府中,
96
+
97
+ SUM(CASE WHEN Name = N'広島' THEN Sales ELSE 0 END) AS 広島
98
+
99
+ FROM Shop GROUP BY Month";
100
+
101
+ using (SqlConnection connection = new SqlConnection(connString))
102
+
103
+ {
104
+
105
+ SqlDataAdapter adapter = new SqlDataAdapter();
106
+
107
+ adapter.SelectCommand = new SqlCommand(selectQuery, connection);
108
+
109
+ DataTable table = new DataTable();
110
+
111
+ adapter.Fill(table);
112
+
113
+ return table;
114
+
115
+ }
116
+
117
+ }
118
+
119
+
120
+
121
+ private void Form1_Load(object sender, EventArgs e)
122
+
123
+ {
124
+
125
+ Chart chart = new Chart()
126
+
127
+ {
128
+
129
+ Name = "chart1",
130
+
131
+ Width = 600,
132
+
133
+ DataSource = CreateDataTable()
134
+
135
+ };
136
+
137
+
138
+
139
+ this.Controls.Add(chart);
140
+
141
+
142
+
143
+ Legend legend = new Legend()
144
+
145
+ {
146
+
147
+ DockedToChartArea = "ChartArea1",
148
+
149
+ IsDockedInsideChartArea = false,
150
+
151
+ Name = "Legend1"
152
+
153
+ };
154
+
155
+ chart.Legends.Add(legend);
156
+
157
+
158
+
159
+ Series series = new Series()
160
+
161
+ {
162
+
163
+ Name = "三吉",
164
+
165
+ ChartType = SeriesChartType.StackedColumn,
166
+
167
+ CustomProperties = "DrawingStyle=Cylinder",
168
+
169
+ IsValueShownAsLabel = true,
170
+
171
+ Label = "#PERCENT{P1}",
172
+
173
+ Legend = "Legend1",
174
+
175
+ XValueMember = "Month",
176
+
177
+ YValueMembers = "三吉"
178
+
179
+ };
180
+
181
+ chart.Series.Add(series);
182
+
183
+
184
+
185
+ series = new Series()
186
+
187
+ {
188
+
189
+ Name = "春日",
190
+
191
+ ChartType = SeriesChartType.StackedColumn,
192
+
193
+ CustomProperties = "DrawingStyle=Cylinder",
194
+
195
+ IsValueShownAsLabel = true,
196
+
197
+ Label = "#PERCENT{P1}",
198
+
199
+ Legend = "Legend1",
200
+
201
+ XValueMember = "Month",
202
+
203
+ YValueMembers = "春日"
204
+
205
+ };
206
+
207
+ chart.Series.Add(series);
208
+
209
+
210
+
211
+ series = new Series()
212
+
213
+ {
214
+
215
+ Name = "東雲",
216
+
217
+ ChartType = SeriesChartType.StackedColumn,
218
+
219
+ CustomProperties = "DrawingStyle=Cylinder",
220
+
221
+ IsValueShownAsLabel = true,
222
+
223
+ Label = "#PERCENT{P1}",
224
+
225
+ Legend = "Legend1",
226
+
227
+ XValueMember = "Month",
228
+
229
+ YValueMembers = "東雲"
230
+
231
+ };
232
+
233
+ chart.Series.Add(series);
234
+
235
+
236
+
237
+ series = new Series()
238
+
239
+ {
240
+
241
+ Name = "府中",
242
+
243
+ ChartType = SeriesChartType.StackedColumn,
244
+
245
+ CustomProperties = "DrawingStyle=Cylinder",
246
+
247
+ IsValueShownAsLabel = true,
248
+
249
+ Label = "#PERCENT{P1}",
250
+
251
+ Legend = "Legend1",
252
+
253
+ XValueMember = "Month",
254
+
255
+ YValueMembers = "府中"
256
+
257
+ };
258
+
259
+ chart.Series.Add(series);
260
+
261
+
262
+
263
+ series = new Series()
264
+
265
+ {
266
+
267
+ Name = "広島",
268
+
269
+ ChartType = SeriesChartType.StackedColumn,
270
+
271
+ CustomProperties = "DrawingStyle=Cylinder",
272
+
273
+ IsValueShownAsLabel = true,
274
+
275
+ Label = "#PERCENT{P1}",
276
+
277
+ Legend = "Legend1",
278
+
279
+ XValueMember = "Month",
280
+
281
+ YValueMembers = "広島"
282
+
283
+ };
284
+
285
+ chart.Series.Add(series);
286
+
287
+
288
+
289
+ ChartArea chartArea = new ChartArea()
290
+
291
+ {
292
+
293
+ Name = "ChartArea1",
294
+
295
+ AxisY = new Axis() { Title = "売上高" },
296
+
297
+ AxisX = new Axis() { Title = "売上月" }
298
+
299
+ };
300
+
301
+ chart.ChartAreas.Add(chartArea);
302
+
303
+ }
304
+
305
+ }
306
+
307
+ }
308
+
309
+ ```
310
+
311
+
312
+
313
+ Chart のサンプルは持っているでしょうか?
314
+
315
+
316
+
317
+ 持っていなければ MSDN のサイトからサンプルを入手できるので、ダウンロードして動くように設定することをお勧めします。
318
+
319
+
320
+
321
+ 解説が英語であるのを厭わなければこのサンプルは今後の開発に非常に有益なものになると思います。
322
+
323
+
324
+
325
+ このスレッドの質問者さんの問題がサンプルを見ると解決するかどうかは分かりませんが、解決のためのヒントはあるかもしれません。例えば下記:
326
+
327
+
328
+
329
+ ![イメージ説明](6eb40f4b7639cd34e223e64090cf4932.jpeg)
330
+
331
+
332
+
333
+ 解決できなくとも、今後の開発に役立つはずですので、ダウンロードして設定する手間をかけても損はないと思います。
334
+
335
+
336
+
337
+ サンプルの入手先や設定手順は以下の記事にありますので、興味があれば見てください。
338
+
339
+
340
+
341
+ Chart Samples
342
+
343
+ [http://surferonwww.info/BlogEngine/post/2016/02/14/chart-samples.aspx](http://surferonwww.info/BlogEngine/post/2016/02/14/chart-samples.aspx)