2

私は ASP.NET Charting を初めて使用し、棒グラフにカスタム コンポーネントを追加することについて質問があります。表形式でカスタム凡例を作成しようとしています。つまり、私の凡例のスタイルはテーブルです。そして、データベースの値から各 LegendItem を作成し、それを chart.Legends[0].CustomItems コレクションに追加しています。

データを取得しましたが、すべての LegendItems を 1 行で取得しています。各 LegendItem を新しい行に表示したい。私の現在のコードは次のようになります -

chart.Legends.Add(new Legend
{
LegendStyle = LegendStyle.Table,
BorderColor = Color.Black,
BorderWidth = 1,
BorderDashStyle = ChartDashStyle.Solid,
Alignment = StringAlignment.Center,
DockedToChartArea = areaCounter.ToString(),
Docking = Docking.Bottom,
Name = "CustomLegend",
IsTextAutoFit = true,
InterlacedRows = true,
TableStyle = LegendTableStyle.Auto,
IsDockedInsideChartArea = false
});

LegendItem newItem = new LegendItem();
newItem.Cells.Add(LegendCellType.Text, " - value1 - ", ContentAlignment.MiddleCenter);
newItem.Cells.Add(LegendCellType.Text, " - State Average = - ", ContentAlignment.MiddleCenter);
newItem.Cells[1].CellSpan = 2;
newItem.BorderColor = Color.Black;
newItem.Cells.Add(LegendCellType.Text, " - ", ContentAlignment.MiddleCenter);
newItem.Cells.Add(LegendCellType.Text, " - top - ", ContentAlignment.MiddleCenter);
chart.Legends[1].CustomItems.Add(newItem);



LegendItem newItem1 = new LegendItem();
newItem1.Cells.Add(LegendCellType.Text, "value1", ContentAlignment.MiddleCenter);
newItem1.Cells.Add(LegendCellType.Text, "State Average =", ContentAlignment.MiddleCenter);
newItem1.Cells[1].CellSpan = 2;
newItem1.BorderColor = Color.Black;
newItem1.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleCenter);
newItem1.Cells.Add(LegendCellType.Text, "top", ContentAlignment.MiddleCenter);
chart.Legends[1].CustomItems.Add(newItem1);

newItem と newItem1 は両方とも凡例と同じ行に表示されます。この問題を解決するのを手伝ってくれませんか? よろしくお願いします。

4

1 に答える 1

4

HeaderSeparator をカスタム Legend オブジェクトに追加し、チャット オブジェクトの CustomizeLegend イベントを処理すると、思いどおりに機能することがわかりました。カスタム項目は別々の行に表示されます。これが私が行った変更です。

chart.Legends.Add(new Legend
                {
                    LegendStyle = LegendStyle.Table,
                    BorderColor = Color.Black,
                    BorderWidth = 1,
                    BorderDashStyle = ChartDashStyle.Solid,
                    Alignment = StringAlignment.Center,
                    DockedToChartArea = areaCounter.ToString(),
                    Docking = Docking.Bottom,
                    Name = "CustomLegend",
                    IsTextAutoFit = true,
                    InterlacedRows = true,
                    TableStyle = LegendTableStyle.Tall,
                    HeaderSeparator = LegendSeparatorStyle.Line,
                    HeaderSeparatorColor = Color.Gray,
                    IsDockedInsideChartArea = false
                });

                LegendItem newItem3 = new LegendItem();
                var strVal = item.Value;
                foreach (var val in strVal)
                {
                    newItem3.Cells.Add(LegendCellType.Text, val, ContentAlignment.BottomCenter);
                }
                chart.Legends["CustomLegend"].CustomItems.Add(newItem3);

                chart.CustomizeLegend += chart_CustomizeLegend;



        void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e)
        {
            Chart chart = sender as Chart;
            if (chart == null) return;
            foreach (var item in e.LegendItems)
            {
                item.SeparatorType = LegendSeparatorStyle.Line;
                item.SeparatorColor = Color.Black;
            }

        }
于 2014-02-18T18:35:10.503 に答える