Charts: 命名轴支持?

创建于 2015-04-18  ·  36评论  ·  资料来源: danielgindi/Charts

2015-04-19 00-48-45 lifts rimmer sk last -1

查看了android wiki,但没有找到命名x或y轴的可能性。

那可能吗?

feature help wanted ★★★

最有用的评论

我也碰到了这一点。 这项功能要求已经开放了大约4年,这真是太糟糕了。 为了实现它,我编写了自己的自定义y轴渲染器。 要在左轴上完成这样的标题,您可以执行以下操作:

在您的图表配置中:

leftAxis.xOffset = 45
leftYAxisRenderer = MyYAxisRenderer(viewPortHandler: viewPortHandler, yAxis: leftAxis, transformer: getTransformer(forAxis: .left))

并创建您的自定义渲染器:

class MyYAxisRenderer: YAxisRenderer {
    private static let titleLabelPadding: CGFloat = 20

    /**
     Unfortunately iOS Charts has marked many of its methods with internal visibily
     so they cannot be customized. Instead you often need to re-implement logic from
     the charting framework.
    */
    override func renderAxisLabels(context: CGContext) {
        // Render the y-labels.
        super.renderAxisLabels(context: context)

        // Render the y-axis title using our custom renderer.
        renderTitle(title: "Lorem ipsum dolor sit amet", inContext: context, x: MyYAxisRenderer.titleLabelPadding)
    }
}

// MARK: Y-Axis titles.
private extension MyYAxisRenderer {
    func renderTitle(title: String, inContext context: CGContext, x: CGFloat) {
        guard let yAxis = self.axis as? YAxis else { return }

        let attributes: [NSAttributedString.Key: Any] = [
            .font: yAxis.labelFont,
            .foregroundColor: yAxis.labelTextColor
        ]

        // Determine the chart title's y-position.
        let titleSize = title.size(withAttributes: attributes)
        let verticalTitleSize = CGSize(width: titleSize.height, height: titleSize.width)
        let point = CGPoint(x: x, y: (viewPortHandler.chartHeight - verticalTitleSize.height) / 2)

        // Render the chart title.
        ChartUtils.drawText(context: context,
                            text: title,
                            point: point,
                            attributes: attributes,
                            anchor: .zero,
                            angleRadians: .pi / -2)
    }
}

此示例将以20像素的左偏移/填充呈现y轴标题,并以45像素的左偏移呈现y轴标签。

我希望这对某些人有帮助...

所有36条评论

此处完全相同的API支持Android版本中支持的所有功能!

@danielgindi我理解这一点,并且已经阅读了文档,但是也许我错过了一些东西,您知道我的问题的答案吗?

好吧,目前还没有实施-我目前还没有计划实施,因此您可以做到这一点:-)
或者,您也可以在MPAndroidChart存储库中要求它,完成后我也将其拉到此处

+1,这将是一个了不起的且重要的功能-自小学以来,我们都被教导要为自己的轴命名! :)

有可能的。 我以前做过。 就像旋转x轴标签一样。 整个点都在计算正确的旋转锚点。

我可以做一个PR,但问题是,我做的只是简单地旋转90圈,并以简单的文本和文本颜色,字体在y轴的中心居中。 一旦我们正式支持它,人们就会提出更多要求,例如不同的角度,位置,多条线等。

我不太理解您的解释,因为没有轴标签属性可对其应用旋转和居中。 您只是意味着您手动向图表视图添加了标签并对其进行了手动转换吗?

对此,我建议即使是基本轴标签也将是一个巨大的好处,并且您不会被迫支持比您想要的更多的功能(毕竟,您目前不打算支持此功能)。 希望您会重新考虑!

@UberJason我的意思是我将

我正在休假..所以我可以检查我的旧代码并提交v3的PR。

+1很想看看这个功能

+1很想看看这个功能

+1很想看看这个功能

+1很想看看这个功能

这实现了吗?

+1很想看看这个功能

+1

有没有一种方法可以获取可用于插入任何自定义UIView的框架位置?

+1很乐意看到该功能

这实现了吗? 我需要这个。

有一个PR#2387,但尚未审查。

+1很乐意看到该功能

+1很乐意看到该功能

+1很乐意看到此功能。 .....

+1很乐意看到该功能

+1此功能很棒!!

+1

看#2452和#2387

+1很乐意看到该功能

我也+1

真的可以使用此功能。

我也确实想要此功能。

等待中...

我也是............

我也想要这个功能。

我也确实想要此功能。.每次重写更新时我都很累

我已经看到了很多关于在多个线程上添加轴标题的讨论,但是仍然没有找到如何添加轴标题的代码示例。 我正在使用Swift 4.2。 除了无法显示轴标题以外,我的图表运行良好。 有人有代码示例吗?

我也碰到了这一点。 这项功能要求已经开放了大约4年,这真是太糟糕了。 为了实现它,我编写了自己的自定义y轴渲染器。 要在左轴上完成这样的标题,您可以执行以下操作:

在您的图表配置中:

leftAxis.xOffset = 45
leftYAxisRenderer = MyYAxisRenderer(viewPortHandler: viewPortHandler, yAxis: leftAxis, transformer: getTransformer(forAxis: .left))

并创建您的自定义渲染器:

class MyYAxisRenderer: YAxisRenderer {
    private static let titleLabelPadding: CGFloat = 20

    /**
     Unfortunately iOS Charts has marked many of its methods with internal visibily
     so they cannot be customized. Instead you often need to re-implement logic from
     the charting framework.
    */
    override func renderAxisLabels(context: CGContext) {
        // Render the y-labels.
        super.renderAxisLabels(context: context)

        // Render the y-axis title using our custom renderer.
        renderTitle(title: "Lorem ipsum dolor sit amet", inContext: context, x: MyYAxisRenderer.titleLabelPadding)
    }
}

// MARK: Y-Axis titles.
private extension MyYAxisRenderer {
    func renderTitle(title: String, inContext context: CGContext, x: CGFloat) {
        guard let yAxis = self.axis as? YAxis else { return }

        let attributes: [NSAttributedString.Key: Any] = [
            .font: yAxis.labelFont,
            .foregroundColor: yAxis.labelTextColor
        ]

        // Determine the chart title's y-position.
        let titleSize = title.size(withAttributes: attributes)
        let verticalTitleSize = CGSize(width: titleSize.height, height: titleSize.width)
        let point = CGPoint(x: x, y: (viewPortHandler.chartHeight - verticalTitleSize.height) / 2)

        // Render the chart title.
        ChartUtils.drawText(context: context,
                            text: title,
                            point: point,
                            attributes: attributes,
                            anchor: .zero,
                            angleRadians: .pi / -2)
    }
}

此示例将以20像素的左偏移/填充呈现y轴标题,并以45像素的左偏移呈现y轴标签。

我希望这对某些人有帮助...

使用SwiftUI尝试更简单的解决方法:
https://github.com/danielgindi/Charts/issues/4405

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

JW00332 picture JW00332  ·  4评论

Shunshine07 picture Shunshine07  ·  3评论

kirti0525 picture kirti0525  ·  3评论

coop44483 picture coop44483  ·  3评论

valeIT picture valeIT  ·  3评论