为图形轴描述绘制文本
Draw text for graph axis description
我正在使用 SkiaSharp 绘制图表。现在我想在 X 轴上添加轴描述(星期一、星期二、...)。
这里重要的代码是:
foreach(string key in surveyAnswers.Keys)
{
SKPath currentTextPath = new SKPath();
currentTextPath.MoveTo((float)(info.Width * borderOffset + i * xStepSize - 70), (float)(info.Height * borderOffset + (answerPossibilitiesCount - 1) * yStepSize + 70));
currentTextPath.RLineTo(70, -70);
canvas.DrawTextOnPath(key, currentTextPath, 0, 0, paint);
i++;
}
它为我提供了正确定位的路径,我正在该路径上绘制文本。问题是路径的长度是固定的。我将其设置为 70 高和宽,这使得 space 保留在较短的文本上并剪切较长的文本。
可以找到示例 here。
我想出了一些解决问题的方法,但 none 可行,因为它们需要不受支持的操作或我没有的信息。
如何使文本与图表对齐?
我已经用取决于字体大小和文本长度的 pathLength 变量解决了这个问题。
float pathLenght = (float)((key.Length * (17.0f/25.0f * textSize)) / Math.Sqrt(2));
17/25 的比例似乎适用于许多字体大小。
如果有人需要它,我 post 上面的代码插入了解决方案
foreach(string key in surveyAnswers.Keys)
{
float pathLenght = (float)((key.Length * (17.0f/25.0f * textSize)) / Math.Sqrt(2));
SKPath currentTextPath = new SKPath();
currentTextPath.MoveTo((float)(info.Width * borderOffset + i * xStepSize - pathLenght), (float)(info.Height * borderOffset + (answerPossibilitiesCount - 1) * yStepSize + pathLenght));
currentTextPath.RLineTo(pathLenght, -pathLenght);
canvas.DrawTextOnPath(key, currentTextPath, 0, 0, paint);
i++;
}
我正在使用 SkiaSharp 绘制图表。现在我想在 X 轴上添加轴描述(星期一、星期二、...)。
这里重要的代码是:
foreach(string key in surveyAnswers.Keys)
{
SKPath currentTextPath = new SKPath();
currentTextPath.MoveTo((float)(info.Width * borderOffset + i * xStepSize - 70), (float)(info.Height * borderOffset + (answerPossibilitiesCount - 1) * yStepSize + 70));
currentTextPath.RLineTo(70, -70);
canvas.DrawTextOnPath(key, currentTextPath, 0, 0, paint);
i++;
}
它为我提供了正确定位的路径,我正在该路径上绘制文本。问题是路径的长度是固定的。我将其设置为 70 高和宽,这使得 space 保留在较短的文本上并剪切较长的文本。
可以找到示例 here。
我想出了一些解决问题的方法,但 none 可行,因为它们需要不受支持的操作或我没有的信息。
如何使文本与图表对齐?
我已经用取决于字体大小和文本长度的 pathLength 变量解决了这个问题。
float pathLenght = (float)((key.Length * (17.0f/25.0f * textSize)) / Math.Sqrt(2));
17/25 的比例似乎适用于许多字体大小。
如果有人需要它,我 post 上面的代码插入了解决方案
foreach(string key in surveyAnswers.Keys)
{
float pathLenght = (float)((key.Length * (17.0f/25.0f * textSize)) / Math.Sqrt(2));
SKPath currentTextPath = new SKPath();
currentTextPath.MoveTo((float)(info.Width * borderOffset + i * xStepSize - pathLenght), (float)(info.Height * borderOffset + (answerPossibilitiesCount - 1) * yStepSize + pathLenght));
currentTextPath.RLineTo(pathLenght, -pathLenght);
canvas.DrawTextOnPath(key, currentTextPath, 0, 0, paint);
i++;
}