12

So I'm trying to create a chart in my .NET 6 WinForm application, and I can't find the charts option in my toolbox. Does anyone know why it is doing it? Did they remove charts in .NET 6? And if so, how can I create a chart? Thanks

6
  • MSChart should be in the Data section. Commented May 15, 2022 at 18:53
  • 1
    Well, it isn't there Commented May 15, 2022 at 18:54
  • Then your installation is not correct or maybe you are not really creating a WinForms project. Commented May 15, 2022 at 18:56
  • 2
    Have you read developercommunity.visualstudio.com/t/… - it gives some pointers about what to install .. Commented May 15, 2022 at 18:56
  • stuartd. I see... If so, are there any alternatives for the Microsoft library of charts? Commented May 15, 2022 at 18:57

2 Answers 2

4

So, I found a work-around. It's not the most elegant, nor particularly performant, but since I just wanted a quick visualisation tool for internal testing, this did the trick nicely. Here's what I ended up with (basic scatter plot, but the principle can be adapted for line charts and box charts etc):

enter image description here

Process

  1. Add a picture box to your form
  2. populate it with an empty bitmap to the scale that you want to display
  3. for each data point you want to plot, draw a circle on the bitmap at those coordinates using Graphics.DrawEllipse

Example Code

    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;
    }
Sign up to request clarification or add additional context in comments.

Comments

-1

WinForms has been depreciated and a lot of its functionality hasn't made it into .NET 6. There are no plans to change this. If you find an alternative solution please share it.

See this for more info: https://github.com/dotnet/winforms/discussions/6163

2 Comments

Winforms charts has been deprecated.
Winforms has not been deprecated. It might have been depreciated though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.