如何使用xamarin表格绘制速度表或圆形图表
最近,我开始在如何使用xamarin表格绘制速度表或圆形图表,xamarin,xamarin.forms,Xamarin,Xamarin.forms,最近,我开始在visualstudio中使用Xamarin.Forms进行开发。 我需要画这样的东西: 是否可以使用xamarin表单,或者是否有任何示例代码可供参考 谢谢我想看看 见下图: 我不能百分之百肯定许可证,但我认为他们提供了社区许可证对不起,我的英语很差 我制作了类似的东西,请参见下图 我从一些论坛复制了一些代码,并做了一些我自己的AJUST。但原则是存在的,也许调整一些事情,你就能实现你想要的。我所做的调整不符合以xaml(宽度,高度)为单位的比例大小,我使用的请求是=200
visualstudio
中使用Xamarin.Forms
进行开发。
我需要画这样的东西:
是否可以使用xamarin表单,或者是否有任何示例代码可供参考
谢谢我想看看
见下图:
我不能百分之百肯定许可证,但我认为他们提供了社区许可证对不起,我的英语很差
我制作了类似的东西,请参见下图
我从一些论坛复制了一些代码,并做了一些我自己的AJUST。但原则是存在的,也许调整一些事情,你就能实现你想要的。我所做的调整不符合以xaml(宽度,高度)为单位的比例大小,我使用的请求是=200。我希望它能帮助你
using System;
using System.Collections.Generic;
using System.Text;
using Microcharts.Forms;
using Microcharts;
using SkiaSharp;
using System.Linq;
namespace MES_App1.Chart
{
class SpeedometerChart : RadialGaugeChart
{
public SpeedometerChart()
{
BackgroundColor = SKColors.White;
}
private const float startAngle = 150;
private const float backgroundSweepAngle = 240;
public override void DrawContent(SKCanvas canvas, int width, int height)
{
var diferenceX = width > height ? (width - height) / 2 : 0;
var diferenceY = height > width ? (height - width) / 2 : 0;
var strokeWidth = (4 * (width - diferenceX)) / 100;
var rect = new SKRect(
5 + strokeWidth + diferenceX,
5 + strokeWidth + diferenceY + 50,
width - (5 + strokeWidth + diferenceX),
height - (5 + strokeWidth + diferenceY)+50);
var paint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColor.Parse("#008000"),
StrokeWidth = strokeWidth*4,
IsAntialias = true,
IsStroke = true
};
float[] angulos = { 1, 0.85f, 0.7f, 0.55f, 0.4f, .25f };
string[] angulosStr = { "100%", "85%", "70%", "55%", "40%", "25%" };
string[] cores = { "#008000", "#32CD32", "#5F9EA0", "#FFA500", "#FFD700", "#FF0000" };
for (int i = 0; i < angulos.Length; i++)
{
using (SKPath backgroundPath = new SKPath())
{
paint.Color = SKColor.Parse(cores[i]);
backgroundPath.AddArc(rect, startAngle, backgroundSweepAngle * angulos[i]);
canvas.DrawPath(backgroundPath, paint);
}
using (SKPath labelPath = new SKPath())
{
var rectLabels = new SKRect
{
Left=rect.Left-strokeWidth*2-20,
Top=rect.Top-strokeWidth*2-20,
Right=rect.Right+strokeWidth*2+20,
Bottom=rect.Bottom+strokeWidth*2+20
};
var labelPaint = new SKPaint
{
Style = SKPaintStyle.Stroke,
BlendMode = SKBlendMode.Clear,
Color = SKColors.Black,
StrokeWidth = 0,
IsAntialias = true,
IsStroke = true
};
labelPath.AddArc(rectLabels, startAngle, backgroundSweepAngle * angulos[i]);
canvas.DrawPath(labelPath, labelPaint);
canvas.DrawCaptionLabels(string.Empty, SKColor.Empty, angulosStr[i], SKColors.Black, 20,labelPath.LastPoint, SKTextAlign.Center);
}
}
float[] angulosLabel = { 1f, 0.85f, 0.7f, 0.55f, .25f };
float[] offsetLabels = { 20, 25, 20, 30, 30};
string[] labelsStr = { "Ideal", "Alta", "Tipico", "Precisa Melhoria", "Inaceitavel" };
for (int i = angulosLabel.Length-1; i >= 0; i--)
{
float anguloInicial;
if (i == angulosLabel.Length-1)
{
anguloInicial = startAngle;
}
else
{
anguloInicial = startAngle+backgroundSweepAngle * angulosLabel[i + 1];
}
using (SKPath labelPath = new SKPath())
{
var labelPaint = new SKPaint
{
TextSize=18
};
labelPath.AddArc(rect, anguloInicial, backgroundSweepAngle * angulosLabel[i]);
canvas.DrawTextOnPath(labelsStr[i], labelPath, offsetLabels[i], -10, labelPaint);
if (labelsStr[i] == "Alta")
{
labelPaint.TextSize = 16;
canvas.DrawTextOnPath("Performance", labelPath, 0, 10, labelPaint);
}
}
}
using (SKPath circlePath = new SKPath())
{
var circlePaint = new SKPaint
{
Style = SKPaintStyle.Fill,
Color = SKColors.Black,
IsAntialias = true
};
circlePath.AddCircle(rect.MidX, rect.MidY, 20);
canvas.DrawPath(circlePath, circlePaint);
}
foreach (var entry in Entries.OrderByDescending(e => e.Value))
{
using (SKPath pointerPath = new SKPath())
{
var colors = new[]
{
SKColors.SlateGray,
SKColors.Gray,
SKColors.Black
};
var shader = SKShader.CreateLinearGradient(
new SKPoint(128.0f, 0.0f),
new SKPoint(128.0f,256.0f),
colors,
null,
SKShaderTileMode.Clamp);
var labelPaint = new SKPaint
{
Style = SKPaintStyle.Fill,
StrokeJoin = SKStrokeJoin.Miter,
Shader = shader,
IsAntialias = true
};
canvas.Save();
canvas.RotateDegrees(entry.Value/100*backgroundSweepAngle-120, rect.MidX, rect.MidY);
pointerPath.MoveTo(rect.MidX-10, rect.MidY);
pointerPath.LineTo(rect.MidX, rect.Top);
canvas.DrawCaptionLabels(string.Empty, SKColor.Empty, entry.Value.ToString() + "%", SKColors.Black, 20, pointerPath.LastPoint, SKTextAlign.Center);
pointerPath.LineTo(10+rect.MidX, rect.MidY);
pointerPath.Close();
canvas.DrawPath(pointerPath, labelPaint);
canvas.Restore();
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用微刻线形式;
使用微型切割器;
使用SkiaSharp;
使用System.Linq;
名称空间MES_App1.Chart
{
等级速度表图表:RadialGaugeChart
{
公共车速表()
{
BackgroundColor=SKColors.White;
}
私有常量浮点startAngle=150;
private const float backgroundSweepAngle=240;
公共覆盖无效DrawContent(SKCanvas画布、整型宽度、整型高度)
{
变量差异=宽度>高度?(宽度-高度)/2:0;
变量差异=高度>宽度?(高度-宽度)/2:0;
var strokeWidth=(4*(宽度-差异))/100;
var rect=新的SKRect(
5+冲程宽度+差值,
5+冲程宽度+差值+50,
宽度-(5+冲程宽度+差值),
高度-(5+冲程宽度+差值)+50);
var paint=新SKPaint
{
Style=SKPaintStyle.Stroke,
Color=SKColor.Parse(“#008000”),
冲程宽度=冲程宽度*4,
Isatarias=正确,
IsStroke=true
};
float[]angulos={1,0.85f,0.7f,0.55f,0.4f,.25f};
字符串[]angulosStr={“100%”、“85%”、“70%”、“55%”、“40%”、“25%”;
字符串[]核心={“#008000”,“#32CD32”,“#5F9EA0”,“#FFA500”,“#FFD700”,“#FF0000”};
对于(int i=0;i=0;i--)
{
浮动角度;
if(i==angulosLabel.Length-1)
{
anguloInicial=星形缠结;
}
其他的
{
anguloInicial=startAngle+背景扫描角度*angulosLabel[i+1];
}
使用(SKPath labelPath=new SKPath())
{
var labelPaint=新SKPaint
{
TextSize=18
};
labelPath.AddArc(矩形、角度、背景扫描角度*角度标签[i]);
DrawTextOnPath(labelstr[i],labelPath,offsetLabels[i],-10,labelPaint);
如果(标签STR[i]=“Alta”)
{
labelPaint.TextSize=16;
DrawTextOnPath(“性能”,labelPath,0,10,labelPaint);
}
}
}
使用(SKPath circlePath=new SKPath())
{
var circlePaint=新SKPaint
{
Style=SKPaintStyle.Fill,
颜色=黑色,
Isatarias=真
};
circlePath.AddCircle(rect.MidX,rect.MidY,20);
画布绘制路径(圆形路径、圆形绘制);
}
foreach(Entries.OrderByDescending(e=>e.Value)中的var条目)
{
使用(SKPath pointerPath=new SKPath())
{
var colors=new[]
{
我是灰色的,
颜色,灰色,
黑色,黑色