翻譯|使用教程|編輯:莫成敏|2020-03-26 11:52:39.767|閱讀 817 次
概述:本文介紹了如何在Xamarin.Forms中創(chuàng)建交互式圖表。探討了如何利用Syncfusion Charts來簡化生活,減少計算量,并允許我們在Xamarin.Forms中使用跨平臺API !
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Essential Studio for Xamarin是全面的Xamarin.iOS、Xamarin.Android和Xamarin.Forms組件套包,包含最快的圖表和網格。本文介紹了如何在Xamarin.Forms中創(chuàng)建交互式圖表。
點擊下載Essential Studio for Xamarin正式版
在移動應用程序中使用圖表顯示數據是很常見的,但是繪制圖表可能非常困難。它需要進行大量計算,找出所有點之間的距離,然后將其繪制在圖表上。
為了使事情變得更加困難,我們需要使用特定于平臺的iOS和Android API,以便在屏幕上繪制圖表并實現觸摸手勢。
讓我們探討如何利用Syncfusion Charts來簡化生活,減少計算量,并允許我們在Xamarin.Forms中使用跨平臺API !
在這里您可以找到完整的代碼示例:
Android
iOS
在Syncfusion之前繪制圖表
要在不使用Syncfusion的情況下繪制圖表,我們使用iOS和Android 上的CoreGraphics.CGContext和CoreGraphics.CGPoint等庫,以及Android上的Android.Graphics.Path和Android.Graphics.Paint。這些庫是特定于平臺的,這意味著我們不能在Xamarin.Forms跨平臺UI中使用它們。
例如,從XWeather(使用Xamarin.iOS和Xamarin.Android構建的天氣應用程序)中簽出此代碼。
XWeather,iOS示例
var graphRect = new CGRect (rect.X + padding, rect.Y + padding, rect.Width - (padding * 2), rect.Height - (padding * 2)); // ... var scaleHigh = NMath.Round (highest, MidpointRounding.AwayFromZero); var scaleLow = lowest < 0 ? NMath.Round (lowest, MidpointRounding.AwayFromZero) : NMath.Round (lowest); // ... var rangePadding = Settings.UomTemperature.IsImperial () ? scalePadding : (scalePadding / 2); using var cgContex = UIGraphics.GetCurrentContext(); using var point = new CGPath (); point.MoveToPoint(graphRect.GetMinX (), graphRect.GetMaxY ()); point.AddLines(new [] { new CGPoint(graphRect.GetMinX (), graphRect.GetMinY ()), new CGPoint(graphRect.GetMinX (), graphRect.GetMaxY ()), new CGPoint(graphRect.GetMaxX (), graphRect.GetMaxY ()) }); cgContex.AddPath(p); cgContex.DrawPath(CGPathDrawingMode.Stroke);
XWeather,Android示例
var graphRect = new RectF (padding, padding, canvas.Width - padding, canvas.Height - padding); using var path = new Path(); path.MoveTo(graphRect.Left, graphRect.Top); path.LineTo(graphRect.Left, graphRect.Bottom); path.LineTo(graphRect.Right, graphRect.Bottom); paint.SetStyle(Paint.Style.Stroke); canvas.DrawPath(path, paint);
所有這些代碼,我們甚至還沒有涉及添加平移和縮放之類的觸摸手勢。
讓我們看看如何使用Syncfusion做到這一點。
使用Syncfusion繪制圖表
Syncfusion通過使用SfCharts庫使我們的生活更輕松。它不需要任何復雜的計算,而且最重要的是,它是跨平臺的,可以在我們的Xamarin.Forms項目中使用!
讓我們看一下如何安裝和實現SfCharts。
0.開始
安裝Syncfusion NuGet軟件包
將軟件包安裝到每個項目中,例如.NET Standard項目,Xamarin.iOS項目和Xamarin.Android項目。
初始化Syncfusion圖表,iOS
在AppDelegate.cs的FinishedLaunching方法中,添加Syncfusion.SfChart.XForms.iOS.Renderers.SfChartRenderer.Init();在Xamarin.Forms.Forms.Init()之后。
這里有一個例子。
public class AppDelegate : Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { Xamarin.Forms.Forms.Init(); Syncfusion.SfChart.XForms.iOS.Renderers.SfChartRenderer.Init(); LoadApplication(new App()); return base.FinishedLaunching(app, options); } }
本文內容較多,請點擊下方鏈接查看后半部分內容,感興趣的朋友可以下載Essential Studio for Xamarin試用版免費體驗~您也可以關注我們慧都網了解更多產品資訊~
相關內容推薦:
Essential Studio for Xamarin教程:如何在Xamarin.Forms中創(chuàng)建交互式圖表(下)
Essential Studio for Xamarin教程:如何導出帶注釋的PDF,節(jié)省內存?
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: