簡體   English   中英

在 C# WinForms .NET 6 中創建圖表

[英]Creating a chart in C# WinForms .NET 6

所以我試圖在我的 .NET 6 WinForm 應用程序中創建一個圖表,但我在我的工具箱中找不到圖表選項。 有誰知道它為什么這樣做? 他們是否刪除了 .NET 6 中的圖表? 如果是這樣,我該如何創建圖表? 謝謝

WinForms 已經過時了,它的許多功能還沒有進入 .NET 6。沒有計划改變這一點。 如果您找到替代解決方案,請分享。

有關更多信息,請參見: https ://github.com/dotnet/winforms/discussions/6163

所以,我找到了一個解決方法。 它不是最優雅的,也不是特別高效的,但因為我只是想要一個用於內部測試的快速可視化工具,所以它做得很好。 這是我最終得到的結果(基本散點圖,但原理可以適用於折線圖和箱線圖等):

在此處輸入圖像描述

過程

  1. 在表單中添加圖片框
  2. 用一個空位圖填充到您要顯示的比例
  3. 對於要繪制的每個數據點,使用 Graphics.DrawEllipse 在位圖上的這些坐標處繪制一個圓圈

示例代碼

    private void PlotCoordinates(List<Coordinates> coordinateList, Pen pen)
    {
        List<Rectangle> boundingBoxes = GenerateBoundingBoxes(coordinateList);

        using (Graphics g = Graphics.FromImage(image))
        {
            foreach (Rectangle boundingBox in boundingBoxes)
            {
                g.DrawEllipse(pen, boundingBox);
            }
        }
    }

    private List<Rectangle> GenerateBoundingBoxes(List<Coordinates> coordinateList)
    {
        var boundingBoxes = new List<Rectangle>(coordinateList.Count);

        //dataPointDiameterPixels is the diameter you want your ellipse to be on the plot
        //it needs to be an odd to be accurate
        int dataPointCetralisingOffset = dataPointDiameterPixels / 2;
        int rectLength = dataPointDiameterPixels;

        foreach (Coordinates coordinates in coordinateList)
        {
            int topLeftX = Maths.Max(coordinates.X - dataPointCetralisingOffset, 0);
            int topLeftY = Maths.Max(coordinates.Y - dataPointCetralisingOffset, 0);

            var box = new Rectangle(topLeftX, topLeftY, rectLength, rectLength);
            boundingBoxes.Add(box);
        }

        return boundingBoxes;
    }

Kirsan 創建了一個與 .NET 6 一起使用的分叉

https://github.com/kirsan31/winforms-datavisualization

暫無
暫無

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

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