Giter Club home page Giter Club logo

how-to-create-violin-chart-in-wpf's Introduction

How to create violin chart in WPF (SfChart)

A Violin Plot is used to visualise the distribution of the data and its probability density. It has been achieved by customizing the BoxAndWhiskerSeries in WPF Chart.

A Violin Plot (Hybrid of Box Plot Chart) is used to visualize the distribution of the data and its probability density. WPF Charts provides support to draw violin basic model by customizing the BoxAndWhiskerSeries segment rendering.

Basic Violin Plot

The following steps are explaining how to create a basic violin plot using the Box and Whisker Chart in WPF.

Step 1: Create an extended Box Plot Series (BoxAndWhiskerSeriesExt) and override its default segment with an extended Box Plot (BoxAndWhiskerSegmentExt) Segment as shown below.

public class BoxAndWhiskerSeriesExt : BoxAndWhiskerSeries
    {
        protected override ChartSegment CreateSegment()
        {
            return new BoxAndWhiskerSegmentExt(this);
        }
    }

Step 2: In extended Box Plot Segment involves the appearance changing by using the CreateVisual method which is used to create a violin path.

public class BoxAndWhiskerSegmentExt : BoxAndWhiskerSegment
{
    …
    public override UIElement CreateVisual(Size size)
    {
        var element = base.CreateVisual(size);
        violinPath = new Path()
        {
            Tag = this,
            Stretch = Stretch.Fill,
        };

        SetVisualBindings(violinPath);
        (element as Canvas).Children.Add(violinPath);
        return element;
    }
}

Step3: The Geometry path of the violin plot has been created and update as violinPath in the Update method as shown below.

public class BoxAndWhiskerSegmentExt : BoxAndWhiskerSegment
{
    …
    public override void Update(IChartTransformer transformer)
    {
        base.Update(transformer);

        BoxAndWhiskerSegment baseSegment = this as BoxAndWhiskerSegment;
        
        …
        //Right side path
        BezierSegment segment1 = new BezierSegment();
        segment1.Point1 = new Point(centerLinePosition, minimumPoint.Y + (extensionValue * 2));
        segment1.Point2 = new Point(tlPoint.X + (centerLinePosition - tlPoint.X) / 2 + extensionValue, brPoint.Y + (minimumPoint.Y - brPoint.Y) / 2);
        segment1.Point3 = new Point(tlPoint.X, brPoint.Y);
        pathFigure.Segments.Add(segment1);
        …

        PathGeometry geometry = new PathGeometry();
        geometry.Figures.Add(pathFigure);
        violinPath.Data = geometry;
    }
}

Step4: This is for finding how to define in XAML to get in visual.

<chart:SfChart x:Name="boxWhiskerChart" AreaBorderBrush="#8e8e8e" 
                       Background="White" Margin="10,20,20,20" 
                       VerticalAlignment="Bottom" AreaBorderThickness="0,1,1,1">
	…
    <local:BoxAndWhiskerSeriesExt ItemsSource="{Binding BoxWhiskerData}"  
                                       XBindingPath="Department"   ShowOutlier="False"
                                       YBindingPath="Age" ShowTooltip="True"
                                       BoxPlotMode="Normal"
                                       x:Name="boxSeries"/>
</chart:SfChart>

Violin Chart WPF

Violin Plot Customization

To obtain the smooth edges and interior filling in the violin chart, use the pre-defined values for minimum, maximum, and median points with the provided SVG path as shown below.

public class BoxAndWhiskerSegmentExt : BoxAndWhiskerSegment
{
    …
    public override void Update(IChartTransformer transformer)
    {
        base.Update(transformer);

        …
        string data = "M " + maximumPoint.X + "," + maximumPoint.Y
             + " C " + (brPoint.X - (maximumPoint.X - tlPoint.X) / 2 - extensionValue) + "," + (tlPoint.Y - (tlPoint.Y - maximumPoint.Y) / 2)
             + " " + brPoint.X + "," + tlPoint.Y
             + " " + brPoint.X + "," + medianPoint.Y
             + " C " + brPoint.X + "," + brPoint.Y
             + " " + (brPoint.X - (centerLinePosition - tlPoint.X) / 2 - extensionValue) + "," + (brPoint.Y + (minimumPoint.Y - brPoint.Y) / 2)
             + " " + centerLinePosition + "," + (minimumPoint.Y + (extensionValue * 2))
             + " C " + (tlPoint.X + (centerLinePosition - tlPoint.X) / 2 + extensionValue) + "," + (brPoint.Y + (minimumPoint.Y - brPoint.Y) / 2)
             + " " + tlPoint.X + "," + brPoint.Y
             + " " + tlPoint.X + "," + medianPoint.Y
             + " C " + tlPoint.X + "," + tlPoint.Y
             + " " + (tlPoint.X + (maximumPoint.X - tlPoint.X) / 2 + extensionValue) + "," + (tlPoint.Y - (tlPoint.Y - maximumPoint.Y) / 2)
             + " " + maximumPoint.X + "," + maximumPoint.Y;

        violinPath.Data = Geometry.Parse(data);
    }
}

Customized Violin Chart WPF

Knowledge Base

KB article - How to create violin chart in WPF

See also

How to customize the default shape of any series with the required shapes

How to create the Tornado Chart in WPF Charts

How to customize the outlier of Box Plot in WPF Charts

how-to-create-violin-chart-in-wpf's People

Contributors

devakumard avatar mrhemalatha avatar muneeshkumarg avatar saravanan-madhesh avatar sarubala20 avatar suryakaran2143 avatar vinothkumar-ganesan avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

suryakaran2143

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.