Coder Social home page Coder Social logo

syncfusionexamples / how-to-create-vector-image-in-xamarin.forms-chart Goto Github PK

View Code? Open in Web Editor NEW
0.0 4.0 0.0 545 KB

This example demonstrates how to create vector image in Xamarin Forms Chart

C# 100.00%
chart charts vector-image xamarin xamarin-forms

how-to-create-vector-image-in-xamarin.forms-chart's Introduction

How to create vector image in Xamarin.Forms Charts

Vector images can be scaled to any size without losing quality. This article explains how to add vector images in SfChart using the DataMarker LabelTemplate property.

You can achieve this requirement in two ways:

• By adding SVG vector file directly into the DataMarker LabelTemplate property.

• Drawing vector images using SkiaSharp.

Add Vector image in SfChart

Adding an SVG vector file directly into a DataMarker Template

Step 1:

Add the Xamarin.Forms.Svg NuGet package in the project and it provides support to add SvgImageSource to Xamarin.Forms ImageSource.

Step 2:

Initialize the SvgImage.Init to make it render in each platform-specific projects, as shown in the following code sample.

Android [C#]

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
        …
    Xamarin.Forms.Svg.Droid.SvgImage.Init(this);
    LoadApplication(new App());
}

iOS [C#]

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
   {
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());
            Xamarin.Forms.Svg.iOS.SvgImage.Init();
            SfChartRenderer.Init();
            return base.FinishedLaunching(app, options);
   }

In PCL: Add SvgImageSource.RegisterAssembly().

[C#]

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
        …
    Xamarin.Forms.Svg.Droid.SvgImage.Init(this);
    LoadApplication(new App());
}

Step 3:

Add the image to your project and set the Build Action to Embedded Resource for your SVG image.

Step 4:

Define a HeightConverter to calculate the height for the image.

[C#]

public class HeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        SfChart chart = parameter as SfChart;
        ChartDataMarkerLabel dataMarkerLabel = value as ChartDataMarkerLabel;


        if (chart != null && dataMarkerLabel != null)
        {
            var height = chart.ValueToPoint(chart.SecondaryAxis, (dataMarkerLabel.Data as Model).YValue);

            if (Device.RuntimePlatform == Device.Android)
            {
                double DPI = DependencyService.Get<IChartDependencyService>().GetDPI();
                return (chart.SeriesBounds.Height - height) / DPI;
            }
            else
            {
                return chart.SeriesBounds.Height - height;
            }
        }

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

Furthermore, in android, height is calculated using the screen density. For this purpose, it is necessary to implement the interface called IChartDependencyService.

[C#]

public interface IChartDependencyService
    {
        double GetDPI();
    } 

Android[C#]

[assembly: Xamarin.Forms.Dependency(typeof(ChartDependencyService))]
namespace ChartSample.Droid
{
    public class ChartDependencyService : IChartDependencyService
    {
        public double GetDPI()
        {
            return Application.Context.Resources.DisplayMetrics.Density;
        }
    }
}

[C#]

public interface IChartDependencyService
    {
        double GetDPI();
    } 

Step 5:

Add SvgImageSource to the ImageSource property of the image in DataMarker LabelTemplate and set the height and width for the image. Also, set the series color as Transparent to make the series disappear in the background. You can achieve this, as shown in the following code sample.

[Xaml]

<chart:SfChart.Resources>
        <local:Converter x:Key="heightConverter"/>
</chart:SfChart.Resources>
        ...
<chart:ColumnSeries DataMarkerPosition="Bottom" Color="Transparent" ItemsSource="{Binding Data}" XBindingPath="XValue" YBindingPath="YValue"> 
    <chart:ColumnSeries.DataMarker> 
        <chart:ChartDataMarker ShowLabel="True" LabelContent="DataMarkerLabel"> 
            <chart:ChartDataMarker.LabelTemplate> 
                <DataTemplate> 
                        <Image Aspect="Fill" HeightRequest="{Binding Converter={StaticResource heightConverter}, ConverterParameter={x:Reference chart}}" WidthRequest="55">
                            <Image.Source>
                                    <svg:SvgImageSource Source="Image1.svg" Width="100" Height="100" />
                            </Image.Source>
                        </Image>                            
                    </DataTemplate> 
            </chart:ChartDataMarker.LabelTemplate> 
            <chart:ChartDataMarker.LabelStyle> 
                <chart:DataMarkerLabelStyle LabelPosition="Inner"/> 
            </chart:ChartDataMarker.LabelStyle> 
        </chart:ChartDataMarker> 
    </chart:ColumnSeries.DataMarker> 
</chart:ColumnSeries> 

Drawing the vector images using SkiaSharp

Step 1:

For setting up SkiaSharp, add SkiaSharp.Views.Forms NuGet package to your project.

Step 2:

Add an image to the project and set Build Action to Embedded Resource for your image.

Step 3:

Refer to step 3 of how to add SVG vector file directly into the DataMarker LabelTemplate property to define the height converter that will be used to calculate the height for the image.

Step 4:

In the DataMarker LabelTemplate, add the SKCanvasView and set the series color as Transparent to make the series disappear in the background.

[Xaml]

<chart:SfChart x:Name="chart" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

    <chart:SfChart.Resources>
        <local:Converter x:Key="heightConverter"/>
    </chart:SfChart.Resources>
    . . .
    <chart:SfChart.Series>
        <chart:ColumnSeries DataMarkerPosition="Bottom" Color="Transparent" ItemsSource="{Binding Data}" XBindingPath="XValue" YBindingPath="YValue">
            <chart:ColumnSeries.DataMarker>
                <chart:ChartDataMarker ShowLabel="True" LabelContent="DataMarkerLabel">
                    <chart:ChartDataMarker.LabelTemplate>
                        <DataTemplate>
                            <skia:SKCanvasView x:Name="canvasView" HeightRequest="{Binding Converter={StaticResource heightConverter}, ConverterParameter={x:Reference chart}}" WidthRequest="55"   PaintSurface="OnCanvasViewPaintSurface" />
                        </DataTemplate>
                    </chart:ChartDataMarker.LabelTemplate>
                    <chart:ChartDataMarker.LabelStyle>
                        <chart:DataMarkerLabelStyle LabelPosition="Inner"/>
                    </chart:ChartDataMarker.LabelStyle>
                </chart:ChartDataMarker>
            </chart:ColumnSeries.DataMarker>
        </chart:ColumnSeries>
    </chart:SfChart.Series>          
</chart:SfChart>

Step 5:

Load the image resource as SKBitmap from the assembly and draw the image on the canvas by using the PaintSurface event.

[C#]

public partial class MainPage : ContentPage
{
    static readonly SKBitmap originalBitmap =
        BitmapExtensions.LoadBitmapResource(typeof(MainPage),
            "ChartSample.Media.Image.png");

    public MainPage()
    {
        InitializeComponent();
    }

    void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
    {
        SKImageInfo info = args.Info;
        SKSurface surface = args.Surface;
        SKCanvas canvas = surface.Canvas;

        canvas.Clear();
        canvas.DrawBitmap(originalBitmap, info.Rect); //Draw own vector image. 
    }
}

static class BitmapExtensions
{
    public static SKBitmap LoadBitmapResource(Type type, string resourceID)
    {
        Assembly assembly = type.GetTypeInfo().Assembly;

        using (Stream stream = assembly.GetManifestResourceStream(resourceID))
        {
            return SKBitmap.Decode(stream);
        }
    }
}

KB article - How to create vector image in Xamarin.Forms Charts

how-to-create-vector-image-in-xamarin.forms-chart's People

Contributors

devakumard avatar karthikeyanvis avatar muneeshkumarg avatar sarubala20 avatar vinothkumar-ganesan avatar

Watchers

 avatar  avatar  avatar  avatar

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.