1

を使用してアプリケーションでグラフを作成していますSystem.Web.UI.DataVisualization.Charting。上付きテキストを含めるには、凡例などの特定のテキスト要素が必要です。

これどうやってするの?

これまでHTMLタグを使用してみましたが、認識されません。タグはそのまま表示されます。boolまた、HTML形式の文字列を許可するプロパティが見つかりません。

4

1 に答える 1

1

残念ながら、組み込み関数はありません。
唯一の方法は、PostPaint イベントを処理し、複雑なフォーマットをサポートするレンダラーを使用して独自のテキストを描画することです。

たとえば、Graphics オブジェクトに html を描画できるHtmlRendererを使用できます。

使用例を次に示します。

public Form1()
{
    InitializeComponent();

    // subrscribe PostPaint event
    this.chart1.PostPaint += new EventHandler<ChartPaintEventArgs>(chart1_PostPaint);

    // fill the chart with fake data
    var values = Enumerable.Range(0, 10).Select(x => new { X = x, Y = x }).ToList();
    this.chart1.Series.Clear();
    this.chart1.DataSource = values;
    // series name will be replaced
    var series = this.chart1.Series.Add("SERIES NAME"); 
    series.XValueMember = "X";
    series.YValueMembers = "Y";
}

void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
    var cell = e.ChartElement as LegendCell;
    if (cell != null && cell.CellType == LegendCellType.Text)
    {
        // get coordinate of cell rectangle
        var rect = e.ChartGraphics.GetAbsoluteRectangle(e.Position.ToRectangleF());
        var topLeftCorner = new PointF(rect.Left, rect.Top);
        var size = new SizeF(rect.Width, rect.Height);

        // clear the original text by coloring the rectangle (yellow just to highlight it...)
        e.ChartGraphics.Graphics.FillRectangle(Brushes.Yellow, rect);

        // prepare html text (font family and size copied from Form.Font)
        string html = string.Format(System.Globalization.CultureInfo.InvariantCulture,
            "<div style=\"font-family:{0}; font-size:{1}pt;\">Series <sup>AAA</sup></div>",
            this.Font.FontFamily.Name,
            this.Font.SizeInPoints);

        // call html renderer
        HtmlRenderer.HtmlRender.Render(e.ChartGraphics.Graphics, html, topLeftCorner, size);
    }
}

結果のスナップショットは次のとおりです。

ここに画像の説明を入力

于 2012-12-21T18:42:38.170 に答える