簡體   English   中英

如何為圖表自定義標簽配置圖像

[英]How configure images for chart custom labels

我想將c#圖表上的自定義標簽替換為背景圖像,然后在其上寫標簽文本。

我嘗試使用圖像標簽屬性和文本,但所做的只是將它們彼此相鄰放置。

這可能嗎? 圖表可以通過y坐標嗎,所以我也許可以使用繪制函數?

在此處輸入圖片說明 在此處輸入圖片說明

謝謝

這是動態創建圖表圖像的示例。 您將需要根據需要使用不同的文本和不同的名稱創建盡可能多的它們。

Image img = Image.FromFile(someTemplateImage);

// draw a text into it:
using (Graphics G = Graphics.FromImage(img))
    G.DrawString("Hello", Font, Brushes.Blue, 1, 1);
// add it to the chart images with a name:
chart.Images.Add(new NamedImage("img01", img));

現在,我們可以創建一個顯示圖像的自定義標簽:

Axis ax = chart.ChartAreas[0].AxisX;
CustomLabel cl = new CustomLabel();
cl.FromPosition = chart.Series[0].Points[0].XValue;  // some values, which will place 
cl.ToPosition = chart.Series[0].Points[1].XValue;    // the cl between two points
cl.Text = "";   // no text, please!

cl.Image = "img01";  // this is our NamedImage

ax.CustomLabels.Add(cl);  // now we can add the CL

當然,使用字體和匹配樣式設置繪制的字符串的樣式完全取決於您。

這只是一個具有單個圖像和單個 cl的示例。 根據您的需要,您將需要考慮一種命名圖像並添加其文本的方案。

您可能需要查看那里的帖子,以查看涉及移動的示例: 在這里,我動態地創建了大量標記圖像,在這里 ,markerimages用於創建熱圖。

這是一個裝飾函數,可將模板圖像添加到軸上的每個自定義標簽上。:

public void AddornCLs(Chart chart, Axis axis, Image template, Font font, Color color)
{
    Rectangle rect = new Rectangle(Point.Empty, template.Size);
    TextFormatFlags format = TextFormatFlags.HorizontalCenter 
                           | TextFormatFlags.VerticalCenter;

    foreach(CustomLabel cl in axis.CustomLabels)
    {
        string text = cl.Text;
        if (text == "") text = cl.Tag.ToString(); else cl.Tag = cl.Text;
        cl.Text = "";
        if (cl.Name == "") cl.Name = axis.CustomLabels.IndexOf(cl).ToString("CL000");
        Image img = (Image)template.Clone();
        using (Graphics G = Graphics.FromImage(img))
            TextRenderer.DrawText(G, text, font, rect, color, format);
        chart.Images.Add(new NamedImage(cl.Name, img));
        cl.Image = cl.Name;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM