回答編集履歴

2

後から変える

2025/02/21 11:22

投稿

TN8001
TN8001

スコア9937

test CHANGED
@@ -35,7 +35,7 @@
35
35
  dt.Rows.Add(4, 1, 0, 0);
36
36
  dt.Rows.Add(5, 0, 0, 0);
37
37
 
38
- var chart = new Chart()
38
+ var chart = new Chart
39
39
  {
40
40
  DataSource = dt,
41
41
  Dock = DockStyle.Fill,
@@ -45,19 +45,19 @@
45
45
  chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
46
46
  chart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
47
47
 
48
- chart.Series.Add(new Series()
48
+ chart.Series.Add(new Series
49
49
  {
50
50
  ChartType = SeriesChartType.StackedArea,
51
51
  XValueMember = "x",
52
52
  YValueMembers = "y1",
53
53
  });
54
- chart.Series.Add(new Series()
54
+ chart.Series.Add(new Series
55
55
  {
56
56
  ChartType = SeriesChartType.StackedArea,
57
57
  XValueMember = "x",
58
58
  YValueMembers = "y2",
59
59
  });
60
- chart.Series.Add(new Series()
60
+ chart.Series.Add(new Series
61
61
  {
62
62
  BorderWidth = 2,
63
63
  BorderColor = Color.White,
@@ -91,14 +91,14 @@
91
91
  {
92
92
  InitializeComponent();
93
93
 
94
- var chart = new Chart()
94
+ var chart = new Chart
95
95
  {
96
96
  Dock = DockStyle.Fill,
97
97
  Parent = this,
98
98
  };
99
99
  chart.ChartAreas.Add("");
100
100
 
101
- chart.Series.Add(new Series()
101
+ chart.Series.Add(new Series
102
102
  {
103
103
  ChartType = SeriesChartType.StackedArea,
104
104
  Points = {
@@ -110,7 +110,7 @@
110
110
  new DataPoint(5, 0),
111
111
  },
112
112
  });
113
- chart.Series.Add(new Series()
113
+ chart.Series.Add(new Series
114
114
  {
115
115
  ChartType = SeriesChartType.StackedArea,
116
116
  Points = {
@@ -122,7 +122,7 @@
122
122
  new DataPoint(5, 0) { Color = Color.Transparent, },
123
123
  },
124
124
  });
125
- chart.Series.Add(new Series()
125
+ chart.Series.Add(new Series
126
126
  {
127
127
  ChartType = SeriesChartType.StackedArea,
128
128
  Points = {
@@ -138,4 +138,77 @@
138
138
  }
139
139
  }
140
140
  ```
141
+
142
+ `DataSource`で入れているなら、後から変えてもいいです。
143
+ ```cs
144
+ using System.Data;
145
+ using System.Drawing;
146
+ using System.Windows.Forms;
147
+ using System.Windows.Forms.DataVisualization.Charting;
148
+
149
+ namespace Qpq8e05s3vhidml
150
+ {
151
+ public partial class Form1 : Form
152
+ {
153
+ public Form1()
154
+ {
155
+ InitializeComponent();
156
+
157
+ var dt = new DataTable();
158
+ dt.Columns.Add("x");
159
+ dt.Columns.Add("y1");
160
+ dt.Columns.Add("y2");
161
+ dt.Columns.Add("y3");
162
+ dt.Rows.Add(0, 2, 1, 1);
163
+ dt.Rows.Add(1, 2, 0, 1);
164
+ dt.Rows.Add(2, 2, 0, 1);
165
+ dt.Rows.Add(3, 1, 0, 0);
166
+ dt.Rows.Add(4, 1, 0, 0);
167
+ dt.Rows.Add(5, 0, 0, 0);
168
+
169
+ var chart = new Chart
170
+ {
171
+ DataSource = dt,
172
+ Dock = DockStyle.Fill,
173
+ Parent = this,
174
+ };
175
+ chart.ChartAreas.Add("");
176
+
177
+ chart.Series.Add(new Series
178
+ {
179
+ ChartType = SeriesChartType.StackedArea,
180
+ XValueMember = "x",
181
+ YValueMembers = "y1",
182
+ });
183
+ chart.Series.Add(new Series
184
+ {
185
+ ChartType = SeriesChartType.StackedArea,
186
+ XValueMember = "x",
187
+ YValueMembers = "y2",
188
+ });
189
+ chart.Series.Add(new Series
190
+ {
191
+ ChartType = SeriesChartType.StackedArea,
192
+ XValueMember = "x",
193
+ YValueMembers = "y3",
194
+ });
195
+
196
+ chart.DataBind();
197
+ foreach (var series in chart.Series)
198
+ {
199
+ var p = series.Points;
200
+ if (p.Count < 2) continue;
201
+ for (var i = 1; i < p.Count; i++)
202
+ {
203
+ if (p[i - 1].YValues[0] == 0 && p[i].YValues[0] == 0)
204
+ {
205
+ p[i].Color = Color.Transparent;
206
+ }
207
+ }
208
+ }
209
+ }
210
+ }
211
+ }
212
+ ```
213
+
141
214
  ![いい感じグラフ](https://ddjkaamml8q8x.cloudfront.net/questions/2025-02-20/bf19afab-db87-4dd5-83ca-ed818f3176a5.png)

1

いい感じ

2025/02/20 05:06

投稿

TN8001
TN8001

スコア9937

test CHANGED
@@ -71,3 +71,71 @@
71
71
  }
72
72
  ```
73
73
  ![改善グラフ](https://ddjkaamml8q8x.cloudfront.net/questions/2025-02-19/585e4149-7a63-4c32-a661-4e7cb7b6005a.png)
74
+
75
+ ---
76
+
77
+ 点ごとに色を変えることもできるんでしたっけ。
78
+ [C#でグラフを作成して、特定の箇所のみ色を変更したいです](https://teratail.com/questions/214711)
79
+
80
+ 登録(時に連続0を見て色を変える)作業が面倒ですが、見た目はいいですね!^^
81
+ ```cs
82
+ using System.Drawing;
83
+ using System.Windows.Forms;
84
+ using System.Windows.Forms.DataVisualization.Charting;
85
+
86
+ namespace Qpq8e05s3vhidml
87
+ {
88
+ public partial class Form1 : Form
89
+ {
90
+ public Form1()
91
+ {
92
+ InitializeComponent();
93
+
94
+ var chart = new Chart()
95
+ {
96
+ Dock = DockStyle.Fill,
97
+ Parent = this,
98
+ };
99
+ chart.ChartAreas.Add("");
100
+
101
+ chart.Series.Add(new Series()
102
+ {
103
+ ChartType = SeriesChartType.StackedArea,
104
+ Points = {
105
+ new DataPoint(0, 2),
106
+ new DataPoint(1, 2),
107
+ new DataPoint(2, 2),
108
+ new DataPoint(3, 1),
109
+ new DataPoint(4, 1),
110
+ new DataPoint(5, 0),
111
+ },
112
+ });
113
+ chart.Series.Add(new Series()
114
+ {
115
+ ChartType = SeriesChartType.StackedArea,
116
+ Points = {
117
+ new DataPoint(0, 1),
118
+ new DataPoint(1, 0),
119
+ new DataPoint(2, 0) { Color = Color.Transparent, },
120
+ new DataPoint(3, 0) { Color = Color.Transparent, },
121
+ new DataPoint(4, 0) { Color = Color.Transparent, },
122
+ new DataPoint(5, 0) { Color = Color.Transparent, },
123
+ },
124
+ });
125
+ chart.Series.Add(new Series()
126
+ {
127
+ ChartType = SeriesChartType.StackedArea,
128
+ Points = {
129
+ new DataPoint(0, 1),
130
+ new DataPoint(1, 1),
131
+ new DataPoint(2, 1),
132
+ new DataPoint(3, 0),
133
+ new DataPoint(4, 0) { Color = Color.Transparent, },
134
+ new DataPoint(5, 0) { Color = Color.Transparent, },
135
+ },
136
+ });
137
+ }
138
+ }
139
+ }
140
+ ```
141
+ ![いい感じグラフ](https://ddjkaamml8q8x.cloudfront.net/questions/2025-02-20/bf19afab-db87-4dd5-83ca-ed818f3176a5.png)