使用C# WinForms .NET 6创建图表

8

我正在尝试在我的.NET 6 WinForm应用程序中创建图表,但在我的工具箱中找不到图表选项。 有人知道为什么会这样吗? .NET 6中是否删除了图表? 如果是这样,我该如何创建一个图表呢?谢谢。


MSChart 应该在数据部分。 - TaW
1
好的,它不在那里。 - Someone with many names
那么你的安装不正确或者你并没有真正创建一个WinForms项目。 - TaW
1
你读过 https://developercommunity.visualstudio.com/t/port-the-ms-chart-conmtrol-to-dot-net-core/1116076 吗?它提供了一些关于安装的指示。 - Caius Jard
stuartd。我明白了...如果是这样,那么是否有任何替代微软图表库的选择? - Someone with many names
Caius Jard,我确实读了关于它的帖子。我已经下载了库,但它仍然没有显示在工具箱中。我只能通过在代码中创建图表对象来制作它。 - Someone with many names
3个回答

5

1
非常非常感谢您建议使用这个分支。它解决了我的一个大问题。我将我的Win Forms应用程序从.Net Framework迁移到.Net 7,但图表无法工作。在过去的两个星期里,我花了很多时间,但找不到解决方案。这个分支在.Net 7中运行良好。 - undefined

2
所以,我找到了一个解决方法。它并不是最优雅的,也不是特别高效的,但由于我只想要一个快速的内部测试可视化工具,这个方法非常有效。这就是我最终得到的东西(基本散点图,但原理可以适应线图和箱形图等):

enter image description here

过程

  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;
    }

1
这是解决这个问题的一种非常有趣的方式。谢谢! - Someone with many names

-1

1
Winforms charts 已被弃用。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接