teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2018/09/25 01:41

投稿

退会済みユーザー
answer CHANGED
@@ -2,4 +2,171 @@
2
2
 
3
3
  > 積上げグラフが上手く表示出来ません
4
4
 
5
- Series.ChartType プロパティを SeriesChartType.StackedColumn に設定する必要があるはずです。
5
+ Series.ChartType プロパティを SeriesChartType.StackedColumn に設定する必要があるはずです。
6
+
7
+ **【追伸】**
8
+
9
+ 以前作ったサンプルをアップしておきます。
10
+
11
+ このようなグラフなら、
12
+
13
+ ![イメージ説明](e5803991a7096523521ab13cc8b5d889.jpeg)
14
+
15
+ 以下のコードでできます。
16
+
17
+ ```
18
+ using System;
19
+ using System.Collections.Generic;
20
+ using System.ComponentModel;
21
+ using System.Data;
22
+ using System.Drawing;
23
+ using System.Linq;
24
+ using System.Text;
25
+ using System.Threading.Tasks;
26
+ using System.Windows.Forms;
27
+ using System.Data.SqlClient;
28
+ using System.Windows.Forms.DataVisualization.Charting;
29
+
30
+ namespace WindowsFormsChart
31
+ {
32
+ public partial class Form1 : Form
33
+ {
34
+ public Form1()
35
+ {
36
+ InitializeComponent();
37
+ }
38
+
39
+ private DataTable CreateDataTable()
40
+ {
41
+ string connString = @"Data Source=(local)\sqlexpress;Initial Catalog=Database;Integrated Security=True";
42
+ string selectQuery =
43
+ @"SELECT
44
+ Month,
45
+ SUM(CASE WHEN Name = N'三吉' THEN Sales ELSE 0 END) AS 三吉,
46
+ SUM(CASE WHEN Name = N'春日' THEN Sales ELSE 0 END) AS 春日,
47
+ SUM(CASE WHEN Name = N'東雲' THEN Sales ELSE 0 END) AS 東雲,
48
+ SUM(CASE WHEN Name = N'府中' THEN Sales ELSE 0 END) AS 府中,
49
+ SUM(CASE WHEN Name = N'広島' THEN Sales ELSE 0 END) AS 広島
50
+ FROM Shop GROUP BY Month";
51
+ using (SqlConnection connection = new SqlConnection(connString))
52
+ {
53
+ SqlDataAdapter adapter = new SqlDataAdapter();
54
+ adapter.SelectCommand = new SqlCommand(selectQuery, connection);
55
+ DataTable table = new DataTable();
56
+ adapter.Fill(table);
57
+ return table;
58
+ }
59
+ }
60
+
61
+ private void Form1_Load(object sender, EventArgs e)
62
+ {
63
+ Chart chart = new Chart()
64
+ {
65
+ Name = "chart1",
66
+ Width = 600,
67
+ DataSource = CreateDataTable()
68
+ };
69
+
70
+ this.Controls.Add(chart);
71
+
72
+ Legend legend = new Legend()
73
+ {
74
+ DockedToChartArea = "ChartArea1",
75
+ IsDockedInsideChartArea = false,
76
+ Name = "Legend1"
77
+ };
78
+ chart.Legends.Add(legend);
79
+
80
+ Series series = new Series()
81
+ {
82
+ Name = "三吉",
83
+ ChartType = SeriesChartType.StackedColumn,
84
+ CustomProperties = "DrawingStyle=Cylinder",
85
+ IsValueShownAsLabel = true,
86
+ Label = "#PERCENT{P1}",
87
+ Legend = "Legend1",
88
+ XValueMember = "Month",
89
+ YValueMembers = "三吉"
90
+ };
91
+ chart.Series.Add(series);
92
+
93
+ series = new Series()
94
+ {
95
+ Name = "春日",
96
+ ChartType = SeriesChartType.StackedColumn,
97
+ CustomProperties = "DrawingStyle=Cylinder",
98
+ IsValueShownAsLabel = true,
99
+ Label = "#PERCENT{P1}",
100
+ Legend = "Legend1",
101
+ XValueMember = "Month",
102
+ YValueMembers = "春日"
103
+ };
104
+ chart.Series.Add(series);
105
+
106
+ series = new Series()
107
+ {
108
+ Name = "東雲",
109
+ ChartType = SeriesChartType.StackedColumn,
110
+ CustomProperties = "DrawingStyle=Cylinder",
111
+ IsValueShownAsLabel = true,
112
+ Label = "#PERCENT{P1}",
113
+ Legend = "Legend1",
114
+ XValueMember = "Month",
115
+ YValueMembers = "東雲"
116
+ };
117
+ chart.Series.Add(series);
118
+
119
+ series = new Series()
120
+ {
121
+ Name = "府中",
122
+ ChartType = SeriesChartType.StackedColumn,
123
+ CustomProperties = "DrawingStyle=Cylinder",
124
+ IsValueShownAsLabel = true,
125
+ Label = "#PERCENT{P1}",
126
+ Legend = "Legend1",
127
+ XValueMember = "Month",
128
+ YValueMembers = "府中"
129
+ };
130
+ chart.Series.Add(series);
131
+
132
+ series = new Series()
133
+ {
134
+ Name = "広島",
135
+ ChartType = SeriesChartType.StackedColumn,
136
+ CustomProperties = "DrawingStyle=Cylinder",
137
+ IsValueShownAsLabel = true,
138
+ Label = "#PERCENT{P1}",
139
+ Legend = "Legend1",
140
+ XValueMember = "Month",
141
+ YValueMembers = "広島"
142
+ };
143
+ chart.Series.Add(series);
144
+
145
+ ChartArea chartArea = new ChartArea()
146
+ {
147
+ Name = "ChartArea1",
148
+ AxisY = new Axis() { Title = "売上高" },
149
+ AxisX = new Axis() { Title = "売上月" }
150
+ };
151
+ chart.ChartAreas.Add(chartArea);
152
+ }
153
+ }
154
+ }
155
+ ```
156
+
157
+ Chart のサンプルは持っているでしょうか?
158
+
159
+ 持っていなければ MSDN のサイトからサンプルを入手できるので、ダウンロードして動くように設定することをお勧めします。
160
+
161
+ 解説が英語であるのを厭わなければこのサンプルは今後の開発に非常に有益なものになると思います。
162
+
163
+ このスレッドの質問者さんの問題がサンプルを見ると解決するかどうかは分かりませんが、解決のためのヒントはあるかもしれません。例えば下記:
164
+
165
+ ![イメージ説明](6eb40f4b7639cd34e223e64090cf4932.jpeg)
166
+
167
+ 解決できなくとも、今後の開発に役立つはずですので、ダウンロードして設定する手間をかけても損はないと思います。
168
+
169
+ サンプルの入手先や設定手順は以下の記事にありますので、興味があれば見てください。
170
+
171
+ Chart Samples
172
+ [http://surferonwww.info/BlogEngine/post/2016/02/14/chart-samples.aspx](http://surferonwww.info/BlogEngine/post/2016/02/14/chart-samples.aspx)