上記の円グラフは、凡例を使用して各円セグメントを識別します。この方法は、情報が単一の領域に配置されているため非常に効果があります。ラベルの数に関係なく、凡例内にすっきりと配置されます。
しかし、場合によっては、ラベルをデータ点の近くに配置して関連付けをより明確する場合があります。また、データ点または他のグラフ要素に関する情報を含むラベルを追加し、それらのラベルを特定の位置または特定のグラフ要素の近くに配置する場合もあります。
これを行うため、C1Chart は ChartLabels プロパティを提供します。これを使うと Label オブジェクトを作成して任意のグラフ要素に添付できます。
たとえば、下図は上記と同じ円グラフを示しますが、この場合、凡例の代わりにラベルが各セグメントに添付されます。

このグラフでは、グラフラベルの多用による一般的な困難を示しています。ラベルが多すぎると、ラベル同士が重なり合って正確に配置することができない場合があります(この例ではラベルは C1Chart によって自動的に配置されました)。
この2番目のグラフを作成するには、まず、コードは1番目のグラフを作成するために使用した同じ手順を実行します。以下の追加コードを使用して、ラベルを作成します。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
' 凡例を非表示にし、ラベルを設定します。
C1Chart1.Legend.Visible = false
Dim s As Style = C1Chart1.ChartLabels.DefaultLabelStyle
s.Font = new Font("Tahoma", 7)
s.BackColor = SystemColors.Info
s.Opaque = true
s.Border.BorderStyle = BorderStyleEnum.Solid
' ラベルを各セグメントに添付します。
Dim i As Integer
For i = 0 To data.Count ? 1
Dim lbl As C1.Win.C1Chart.Label = _
C1Chart1.ChartLabels.LabelsCollection.AddNewLabel()
lbl.Text = string.Format("{0} ({1:c})", _
data(i)("ProductName"), data(i)("UnitPrice"))
lbl.Compass = LabelCompassEnum.Radial
lbl.Offset = 20
lbl.Connected = True
lbl.Visible = True
lbl.AttachMethod = AttachMethodEnum.DataIndex
Dim am As AttachMethodData = lbl.AttachMethodData
am.GroupIndex = 0
am.SeriesIndex = I
am.PointIndex = 0
Next i
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
// 凡例を非表示にし、ラベルを設定します。
c1Chart1.Legend.Visible =
false;
Style s = c1Chart1.ChartLabels.DefaultLabelStyle;
s.Font = new Font("Tahoma", 7);
s.BackColor = SystemColors.Info;
s.Opaque = true;
s.Border.BorderStyle = BorderStyleEnum.Solid;
// ラベルを各セグメントに添付します。
for (int i = 0; i < data.Count; i++)
{
C1.Win.C1Chart.Label lbl = c1Chart1.ChartLabels.LabelsCollection.AddNewLabel();
lbl.Text = string.Format("{0} ({1:c})",
data[i]["ProductName"], data[i]["UnitPrice"]);
lbl.Compass = LabelCompassEnum.Radial;
lbl.Offset = 20;
lbl.Connected = true;
lbl.Visible = true;
lbl.AttachMethod = AttachMethodEnum.DataIndex;
AttachMethodData am = lbl.AttachMethodData;
am.GroupIndex = 0;
am.SeriesIndex = i;
am.PointIndex = 0;
}
|
|