用图表绘制正弦和余弦有问题
plotting sine and cosine with charts have kinks
我使用另一个用户更改后的代码来绘制正弦和余弦图
import UIKit
import Charts
class ViewController: UIViewController, ChartViewDelegate{
var lineChartView: LineChartView!
override func viewDidLoad() {
lineChartView = LineChartView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
lineChartView?.delegate = self
self.view.addSubview(lineChartView!)
let ys1 = Array(0..<10).map { x in return sin(Double(x)) }
let ys2 = Array(0..<10).map { x in return cos(Double(x)) }
let yse1 = ys1.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
let yse2 = ys2.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
let data = LineChartData()
let ds1 = LineChartDataSet(entries: yse1, label: "Hello")
ds1.colors = [NSUIColor.red]
ds1.drawCirclesEnabled = false
ds1.drawValuesEnabled = false
ds1.mode = .cubicBezier
data.addDataSet(ds1)
let ds2 = LineChartDataSet(entries: yse2, label: "World")
ds2.colors = [NSUIColor.blue]
ds2.drawCirclesEnabled = false
ds2.drawValuesEnabled = false
ds2.mode = .cubicBezier
data.addDataSet(ds2)
self.lineChartView.data = data
self.viewChart.gridBackgroundColor = NSUIColor.white
self.lineChartView.chartDescription?.text = "Linechart Demo"
}
但是我的图表看起来不太好,因为如您所见,生成的图表有一些问题。
如何平滑非线性函数的图形?
提前致谢。
这里:
let ys1 = Array(0..<10).map { x in return sin(Double(x)) }
let ys2 = Array(0..<10).map { x in return cos(Double(x)) }
let yse1 = ys1.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
let yse2 = ys2.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
您只使用了 10 个值。曲线不平滑也就不足为奇了。即使您正在做 ds1.mode = .cubicBezier
,但图表库只能为您做这么多平滑处理。这条线不是魔法。
为了使曲线更平滑,我们可以使用更多靠得更近的sin(x)和cos(x)值。让我们使用从 0 到 10 的 100 个值,而不是 10,每次步长为 0.1。
let ys1 = Array(0..<100).map { x in ChartDataEntry(x: Double(x) / 10, y: sin(Double(x) / 10)) }
let ys2 = Array(0..<100).map { x in ChartDataEntry(x: Double(x) / 10, y: cos(Double(x) / 10)) }
我使用另一个用户更改后的代码来绘制正弦和余弦图
import UIKit
import Charts
class ViewController: UIViewController, ChartViewDelegate{
var lineChartView: LineChartView!
override func viewDidLoad() {
lineChartView = LineChartView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
lineChartView?.delegate = self
self.view.addSubview(lineChartView!)
let ys1 = Array(0..<10).map { x in return sin(Double(x)) }
let ys2 = Array(0..<10).map { x in return cos(Double(x)) }
let yse1 = ys1.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
let yse2 = ys2.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
let data = LineChartData()
let ds1 = LineChartDataSet(entries: yse1, label: "Hello")
ds1.colors = [NSUIColor.red]
ds1.drawCirclesEnabled = false
ds1.drawValuesEnabled = false
ds1.mode = .cubicBezier
data.addDataSet(ds1)
let ds2 = LineChartDataSet(entries: yse2, label: "World")
ds2.colors = [NSUIColor.blue]
ds2.drawCirclesEnabled = false
ds2.drawValuesEnabled = false
ds2.mode = .cubicBezier
data.addDataSet(ds2)
self.lineChartView.data = data
self.viewChart.gridBackgroundColor = NSUIColor.white
self.lineChartView.chartDescription?.text = "Linechart Demo"
}
但是我的图表看起来不太好,因为如您所见,生成的图表有一些问题。
如何平滑非线性函数的图形? 提前致谢。
这里:
let ys1 = Array(0..<10).map { x in return sin(Double(x)) }
let ys2 = Array(0..<10).map { x in return cos(Double(x)) }
let yse1 = ys1.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
let yse2 = ys2.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
您只使用了 10 个值。曲线不平滑也就不足为奇了。即使您正在做 ds1.mode = .cubicBezier
,但图表库只能为您做这么多平滑处理。这条线不是魔法。
为了使曲线更平滑,我们可以使用更多靠得更近的sin(x)和cos(x)值。让我们使用从 0 到 10 的 100 个值,而不是 10,每次步长为 0.1。
let ys1 = Array(0..<100).map { x in ChartDataEntry(x: Double(x) / 10, y: sin(Double(x) / 10)) }
let ys2 = Array(0..<100).map { x in ChartDataEntry(x: Double(x) / 10, y: cos(Double(x) / 10)) }