セグメント

```
２グラフ時系列
import UIKit
import Charts

class ViewController: UIViewController , ChartViewDelegate{


    @IBOutlet weak var lineChartView: LineChartView!
    
        
    let graphData: [[Double]] = [
        [20, 24, 21, 25, 26, 30, 31, 20],  // グラフ1のデータ
        [15, 18, 16, 20, 22, 25, 28, 18]   // グラフ2のデータ
    ]
    
    // グラフごとのUNIX時刻のデータ
    let unixTimeData: [[Double]] = [
        [1630400000, 1630403600, 1630407200, 1630410800, 1630414400, 1630418000, 1630421600, 1630425200],  // グラフ1のUNIX時刻データ
        [1630400000 + 3600, 1630403600 + 3600, 1630407200 + 3600, 1630410800 + 3600, 1630414400 + 3600, 1630418000 + 3600, 1630421600 + 3600, 1630425200 + 3600]  // グラフ2のUNIX時刻データ
    ]
    
    // グラフごとのタイトル
    let graphTitles: [String] = ["グラフ1", "グラフ2"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureLineChartView()
        setupData()
        setupXAxis()
        setupYAxis()
        lineChartView.delegate = self
    }
    
    func configureLineChartView() {
        lineChartView.chartDescription.text = "JST日時をわかるように表示したグラフ"
        lineChartView.dragEnabled = true
        lineChartView.setScaleEnabled(true)
        lineChartView.pinchZoomEnabled = true
        lineChartView.rightAxis.enabled = false
    }
    
    func setupData() {
        var dataSets: [LineChartDataSet] = []
        
        for i in 0..<graphData.count {
            let entries = graphData[i].enumerated().map { (index, value) -> ChartDataEntry in
                let unixTime = unixTimeData[i][index]
                return ChartDataEntry(x: unixTime, y: value)
            }
            
            let dataSet = LineChartDataSet(entries: entries, label: graphTitles[i])
            dataSet.lineWidth = 2.0
            dataSet.circleRadius = 4.0
            dataSet.circleHoleRadius = 2.0
            dataSet.setColor(randomColor())  // ランダムな色を設定
           // dataSet.setCircleColor.color
            dataSet.valueTextColor = .black
            
            dataSets.append(dataSet)
        }
        
        let data = LineChartData(dataSets: dataSets)
        lineChartView.data = data
    }
    
    func setupXAxis() {
        let xAxis = lineChartView.xAxis
        xAxis.enabled = true
        xAxis.labelPosition = .bottom
        xAxis.labelTextColor = .black
        xAxis.drawAxisLineEnabled = true
        xAxis.drawGridLinesEnabled = false
        
        /*
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
        dateFormatter.dateFormat = "MM/dd HH:mm"
        xAxis.valueFormatter = DefaultAxisValueFormatter(formatter: dateFormatter)
        */
    }
    
    func setupYAxis() {
        let leftAxis = lineChartView.leftAxis
        leftAxis.axisMinimum = 0.0
        leftAxis.axisMaximum = 40.0
        leftAxis.drawZeroLineEnabled = true
        leftAxis.zeroLineColor = .systemGray
        
        leftAxis.labelTextColor = .black
        leftAxis.gridColor = .systemGray
        leftAxis.drawAxisLineEnabled = false
    }
    
    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        let unixTime = entry.x
        let yValue = entry.y
        
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
        dateFormatter.dateFormat = "MM/dd HH:mm"
        let jstDateTime = dateFormatter.string(from: Date(timeIntervalSince1970: unixTime))
        
        let graphIndex = chartView.data?.dataSets.firstIndex { dataSet in
            dataSet.entryForXValue(unixTime, closestToY: yValue) != nil
        }
        
        if let graphIndex = graphIndex {
            let graphTitle = graphTitles[graphIndex]
            
            let alertController = UIAlertController(title: "特定のプロットが選択されました",
                                                    message: "\(graphTitle)\nJST日時: \(jstDateTime)\nY値: \(yValue)",
                                                    preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(okAction)
            present(alertController, animated: true, completion: nil)
        }
    }
    
    func randomColor() -> UIColor {
        let red = CGFloat.random(in: 0.0...1.0)
        let green = CGFloat.random(in: 0.0...1.0)
        let blue = CGFloat.random(in: 0.0...1.0)
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
}
```

```
import UIKit
import Charts

class ViewController: UIViewController, ChartViewDelegate {

    @IBOutlet weak var lineChartView: LineChartView!
    @IBOutlet weak var segmentControl: UISegmentedControl!

    // モック用データ
    let rawDataGraph: [Double] = [20, 24, 21, 25, 26, 30, 31, 20]
    let unixTimeData: [Double] = [1630400000, 1630403600, 1630407200, 1630410800, 1630414400, 1630418000, 1630421600, 1630425200]

    override func viewDidLoad() {
        super.viewDidLoad()

        configureLineChartView()
        setupData()
        setupXAxis()
        setupYAxis()
        lineChartView.delegate = self

        segmentControl.addTarget(self, action: #selector(segmentControlValueChanged), for: .valueChanged)
    }

    func configureLineChartView() {
        lineChartView.chartDescription.text = "JST日時をわかるように表示したグラフ"
        lineChartView.dragEnabled = true
        lineChartView.setScaleEnabled(true)
        lineChartView.pinchZoomEnabled = true
        lineChartView.rightAxis.enabled = false
    }

    func setupData() {
        let entries = rawDataGraph.enumerated().map { (index, value) -> ChartDataEntry in
            return ChartDataEntry(x: unixTimeData[index], y: value)
        }

        let dataSet = LineChartDataSet(entries: entries, label: "Data")
        dataSet.lineWidth = 2.0
        dataSet.circleRadius = 4.0
        dataSet.circleHoleRadius = 2.0
        dataSet.setColor(.blue)
        dataSet.setCircleColor(.blue)
        dataSet.valueTextColor = .black

        let data = LineChartData(dataSet: dataSet)
        lineChartView.data = data
    }

    var xAxis: XAxis!

    func setupXAxis() {
        xAxis = lineChartView.xAxis
        xAxis.enabled = true
        xAxis.labelPosition = .bottom
        xAxis.labelTextColor = .black
        xAxis.drawAxisLineEnabled = true
        xAxis.drawGridLinesEnabled = false

        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
        dateFormatter.dateFormat = "MM/dd HH:mm"
        xAxis.valueFormatter = IndexAxisValueFormatter(values: unixTimeData.map { unixTime in
            let date = Date(timeIntervalSince1970: unixTime)
            return dateFormatter.string(from: date)
        })
    }

    func setupYAxis() {
        let leftAxis = lineChartView.leftAxis
        leftAxis.axisMinimum = 0.0
        leftAxis.axisMaximum = 40.0
        leftAxis.drawZeroLineEnabled = true
        leftAxis.zeroLineColor = .systemGray

        leftAxis.labelTextColor = .black
        leftAxis.gridColor = .systemGray
        leftAxis.drawAxisLineEnabled = false
    }

    @objc func segmentControlValueChanged(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0: // デフォルト表示
            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
            dateFormatter.dateFormat = "MM/dd HH:mm"
            xAxis.valueFormatter = IndexAxisValueFormatter(values: unixTimeData.map { unixTime in
                let date = Date(timeIntervalSince1970: unixTime)
                return dateFormatter.string(from: date)
            })
        case 1: // 2時間ごとの表示
            let interval: TimeInterval = 60 * 60 * 2
            let xValues = unixTimeData.filter { unixTime in
                unixTime.truncatingRemainder(dividingBy: interval) == 0
            }
            xAxis.valueFormatter = IndexAxisValueFormatter(values: xValues)
        default:
            break
        }
    }

    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        let unixTime = entry.x
        let yValue = entry.y

        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
        dateFormatter.dateFormat = "MM/dd HH:mm"
        let jstDateTime = dateFormatter.string(from: Date(timeIntervalSince1970: unixTime))

        let alertController = UIAlertController(title: "特定のプロットが選択されました",
                                                message: "JST日時: \(jstDateTime)\nY値: \(yValue)",
                                                preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(okAction)
        present(alertController, animated: true, completion: nil)
    }
}


```