UNIX　X軸

### 固定JST文字列表示
Y軸の設定した値に対して、順番にインデックスをつけて表示
```
import UIKit
import Charts

class ViewController: UIViewController , ChartViewDelegate{


    @IBOutlet weak var lineChartView: LineChartView!
    
    // モック用データ
    let rawDataGraph: [Double] = [20, 24, 21, 25, 26, 30, 31, 20]
    
    // JST時刻のモック用データ
    let jstTimeData: [String] = ["09/01\n00:00", "09/01\n01:00", "09/01\n02:00", "09/01\n03:00", "09/01\n04:00", "09/01\n05:00", "09/01\n06:00", "09/01\n07:00"]
    
    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() {
        let entries = rawDataGraph.enumerated().map { (index, value) -> ChartDataEntry in
// インデックスをX軸の値として、value（温度データ）をY軸の値としてデータエントリーを生成
            return ChartDataEntry(x: Double(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
    }
    
    func setupXAxis() {
        let xAxis = lineChartView.xAxis
        xAxis.enabled = true
        xAxis.labelPosition = .bottom
        xAxis.labelTextColor = .black
        xAxis.drawAxisLineEnabled = true
        xAxis.drawGridLinesEnabled = false
        
        xAxis.valueFormatter = IndexAxisValueFormatter(values: jstTimeData)
    }
    
    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 index = Int(entry.x)
        let jstDateTime = jstTimeData[index]
        let yValue = entry.y
        
        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)
    }
```


### UNIXを変換したJST文字列表示
Y軸の設定した値に対して、順番にインデックスをつけて表示、プロットの軸とは異なる



```

import UIKit
import Charts

class ViewController: UIViewController , ChartViewDelegate{


    @IBOutlet weak var lineChartView: LineChartView!
    
    // モック用データ
     let rawDataGraph: [Double] = [20, 24, 21, 25, 26, 30, 31, 20]
     
     // UNIX時刻のモック用データ
        let unixTimeData: [Double] = [1630400000, 1630403600, 1630407200, 1630410800, 1630414400, 1630418000, 1630421600, 1630425200]
    
    // JST時刻のモック用データ
        let jstTimeData: [String] = ["09/01\n00:00", "09/01\n01:00", "09/01\n02:00", "09/01\n03:00", "09/01\n04:00", "09/01\n05:00", "09/01\n06:00", "09/01\n07:00"]
        
     
     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() {
         let entries = rawDataGraph.enumerated().map { (index, value) -> ChartDataEntry in
           //  let unixTime = unixTimeData[index]
             return ChartDataEntry(x: Double(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
     }
    
     func setupXAxis() {
         let xAxis = lineChartView.xAxis
         xAxis.enabled = true // X軸を表示する
        // let firstUnixTime = unixTimeData.first ?? 0.0
        // xAxis.axisMinimum = firstUnixTime
         
         xAxis.labelPosition = .bottom
         xAxis.labelTextColor = .black
         xAxis.drawAxisLineEnabled = true
         xAxis.drawGridLinesEnabled = false
         

         
         let dateFormatter = DateFormatter()
         dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo") // JSTタイムゾーンを指定
         dateFormatter.dateFormat = "MM/dd\nHH:mm" // 例: "09/01 12:34" の形式にフォーマット
         xAxis.valueFormatter = IndexAxisValueFormatter(values: unixTimeData.map { unixTime in
             let date = Date(timeIntervalSince1970: unixTime)
             //print(dateFormatter.string(from: date))
             return dateFormatter.string(from: date)
     //        xAxis.valueFormatter = IndexAxisValueFormatter(values: unixTimeData)

         })
          
     }
     
    /*
    func setupXAxis() {
        let xAxis = lineChartView.xAxis
        xAxis.enabled = true // X軸を表示する
        xAxis.labelPosition = .bottom
        xAxis.labelTextColor = .black
        xAxis.drawAxisLineEnabled = true
        xAxis.drawGridLinesEnabled = false

        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo") // JSTタイムゾーンを指定
        dateFormatter.dateFormat = "MM/dd HH:mm" // 例: "09/01 12:34" の形式にフォーマット

        // キリの良い間隔でラベルを設定する
        let interval: TimeInterval = 60 * 60 * 2 // 2時間ごとにラベルを表示
        var xValues: [Double] = []

        for unixTime in unixTimeData {
            if unixTime.truncatingRemainder(dividingBy: interval) == 0 {
                xValues.append(unixTime)
            }
        }
     
      

        xAxis.valueFormatter = IndexAxisValueFormatter(values: xValues.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
     }
     
     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") // JSTタイムゾーンを指定
         dateFormatter.dateFormat = "MM/dd HH:mm" // 例: "09/01 12:34" の形式にフォーマット
         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)
     }
 }