create a chart with 2 parameter in swift

To create a chart with 2 parameters in Swift, you can use the popular Charts library. Here is an example of how to create a Line Chart with 2 datasets:

main.swift
import Charts

// Declare data for 2 lines
let line1Data = [1.0, 3.0, 4.0, 5.0, 6.0]
let line2Data = [2.0, 4.0, 2.0, 1.0, 7.0]

// Create an array of chart data entries for each line
var line1Entries: [ChartDataEntry] = []
var line2Entries: [ChartDataEntry] = []

for i in 0..<line1Data.count {
    let line1Entry = ChartDataEntry(x: Double(i), y: line1Data[i])
    line1Entries.append(line1Entry)

    let line2Entry = ChartDataEntry(x: Double(i), y: line2Data[i])
    line2Entries.append(line2Entry)
}

// Create data set for each line
let line1Set = LineChartDataSet(entries: line1Entries, label: "Line 1")
let line2Set = LineChartDataSet(entries: line2Entries, label: "Line 2")

// Add line data sets to chart data
let data = LineChartData()
data.addDataSet(line1Set)
data.addDataSet(line2Set)

// Configure chart appearance and behavior
let chartView = LineChartView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
chartView.data = data
chartView.xAxis.labelPosition = .bottom
chartView.xAxis.axisMinimum = 0.0
chartView.xAxis.axisMaximum = Double(line1Data.count - 1)
chartView.leftAxis.axisMinimum = 0.0

// Add chart to view
view.addSubview(chartView)
1164 chars
38 lines

This code creates a Line Chart with 2 lines using the Charts library. It first creates arrays of data for each line, then creates an array of ChartDataEntrys for each line using a loop. It then creates a LineChartDataSet for each line and adds them to a new LineChartData object. Finally, it configures the chart appearance and adds the chart to the view.

Note that this is just a basic example and you can customize the appearance and data to fit your specific use case.

gistlibby LogSnag