Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line Chart Issue in CombinedChartView #3268

Closed
BertPorter opened this issue Feb 14, 2018 · 2 comments
Closed

Line Chart Issue in CombinedChartView #3268

BertPorter opened this issue Feb 14, 2018 · 2 comments

Comments

@BertPorter
Copy link

I am creating a combination line chart and scatter chart.
The line chart is used to draw a limit outline on the graph and the scatter chart is used to plot the data. I use an array of 8 points to create the line chart.
When I iterate over the entire array, it won't display the line chart.
I had to break the data into 3 ChartDataEntry arrays in order to display the entire line chart.
Consequently I have 3 legend items that I don't know how to get rid of.
Out of curiosity I used the same array as scatter points and they display correctly (see second photo).

In Model:
let longCGLimitsChartWtsArray = [10275.0, 9702.0, 9702.0, 10694.0, 14994.1, 14994.0, 11400.0, 10275.0]
let longCGLimitsChartCGsArray = [196.9, 199.6, 218.0, 220.3, 215.7, 206.2, 196.9, 196.9]

In View Controller:

   func longCGGraphUpdate() {
    
    // Set up GG limits chart
    var longCGLimitsChartDataEntry = [ChartDataEntry]()
    for i in 0..<5 {
        let value = ChartDataEntry(x: WTBLData.sharedInstance.longCGLimitsChartCGsArray[i], y: WTBLData.sharedInstance.longCGLimitsChartWtsArray[i])
        longCGLimitsChartDataEntry.append(value)
    }
    let longCGLimitsChartValues = LineChartDataSet(values: longCGLimitsChartDataEntry, label: "")
    longCGLimitsChartValues.colors = [NSUIColor.black]
    longCGLimitsChartValues.drawCirclesEnabled = false
    longCGLimitsChartValues.drawValuesEnabled = false
    
    var longCGLimitsChartDataEntry2 = [ChartDataEntry]()
    for i in 4..<6 {
        let value = ChartDataEntry(x: WTBLData.sharedInstance.longCGLimitsChartCGsArray[i], y: WTBLData.sharedInstance.longCGLimitsChartWtsArray[i])
        longCGLimitsChartDataEntry2.append(value)
    }
    let longCGLimitsChartValues2 = LineChartDataSet(values: longCGLimitsChartDataEntry2, label: "")
    longCGLimitsChartValues2.colors = [NSUIColor.black]
    longCGLimitsChartValues2.drawCirclesEnabled = false
    longCGLimitsChartValues2.drawValuesEnabled = false
    
    var longCGLimitsChartDataEntry3 = [ChartDataEntry]()
    for i in 5..<8{
        let value = ChartDataEntry(x: WTBLData.sharedInstance.longCGLimitsChartCGsArray[i], y: WTBLData.sharedInstance.longCGLimitsChartWtsArray[i])
        longCGLimitsChartDataEntry3.append(value)
    }
    let longCGLimitsChartValues3 = LineChartDataSet(values: longCGLimitsChartDataEntry3, label: "")
    longCGLimitsChartValues3.colors = [NSUIColor.black]
    longCGLimitsChartValues3.drawCirclesEnabled = false
    longCGLimitsChartValues3.drawValuesEnabled = false

    let lineData = LineChartData()
    lineData.addDataSet(longCGLimitsChartValues)
    lineData.addDataSet(longCGLimitsChartValues2)
    lineData.addDataSet(longCGLimitsChartValues3)

     // Set up empty wt chart value
    let emptyWtChartValues = ScatterChartDataSet(values: [ChartDataEntry(x: WTBLData.sharedInstance.emptyWtLongCG, y: WTBLData.sharedInstance.wTBLPlistItems.emptyWt)], label: "Empty Wt")
    emptyWtChartValues.colors = [NSUIColor.blue]
    emptyWtChartValues.setScatterShape(.square)
    emptyWtChartValues.drawValuesEnabled = false
    
    // Set up tof wt chart value
    let tofWtChartValues = ScatterChartDataSet(values: [ChartDataEntry(x: WTBLData.sharedInstance.tOFGrossWtLongCG, y: WTBLData.sharedInstance.tOFGrossWt)], label: "TOF Wt")
    tofWtChartValues.colors = [NSUIColor.magenta]
    tofWtChartValues.setScatterShape(.triangle)
    tofWtChartValues.drawValuesEnabled = false
    
    // Set up fuel burn chart values
    var fuelBurnChartDataEntry = [ChartDataEntry]()
    for i in 0..<WTBLData.sharedInstance.fuelBurnWtsArray.count {
        let value = ChartDataEntry(x: WTBLData.sharedInstance.fuelBurnLongCGsArray[i], y: WTBLData.sharedInstance.fuelBurnWtsArray[i])
        fuelBurnChartDataEntry.append(value)
    }
    let fuelBurnChartValues = ScatterChartDataSet(values: fuelBurnChartDataEntry, label: "Fuel Burn")
    fuelBurnChartValues.colors = [NSUIColor.green]
    fuelBurnChartValues.setScatterShape(.circle)
    fuelBurnChartValues.drawValuesEnabled = false

    // Combine all scatter data chart values
    let scatterData = ScatterChartData()
    scatterData.addDataSet(emptyWtChartValues)
    scatterData.addDataSet(fuelBurnChartValues)
    scatterData.addDataSet(tofWtChartValues)

    // Combine Scatter and Line data
    let combinedData = CombinedChartData()
    combinedData.scatterData = scatterData
    combinedData.lineData = lineData
    
    longCGGraphView.data = combinedData
    
    // Refresh chart with new data
    longCGGraphView.notifyDataSetChanged()
}

line chart

scatter chart

@BertPorter
Copy link
Author

I am using constants for the graph axes.

func longCGGraphSetup() {
    
    // Set up border around view
    longCGGraphView.layer.borderWidth = 2
    longCGGraphView.layer.cornerRadius = 10
    
    // Set up border around chart
    longCGGraphView.gridBackgroundColor = NSUIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 0.4)  // same as default except reduced alpha from 1.0 to 0.4
    longCGGraphView.drawGridBackgroundEnabled = true
    longCGGraphView.borderLineWidth = 1.0
    longCGGraphView.drawBordersEnabled = true

    // Set up chart description
    longCGGraphView.chartDescription?.text = "Longitudinal CG Station - in."
    longCGGraphView.chartDescription?.font = NSUIFont.systemFont(ofSize: 12.0)
    longCGGraphView.chartDescription?.textColor = NSUIColor.blue

    // Set up X-Axis
    longCGGraphView.xAxis.labelPosition = .bottom
    longCGGraphView.xAxis.axisMinimum = 196.0
    longCGGraphView.xAxis.axisMaximum = 222.0

    // Set up Y-Axis
    longCGGraphView.leftAxis.axisMinimum = 9000.0
    longCGGraphView.leftAxis.axisMaximum = 15500.0
    longCGGraphView.rightAxis.enabled = false
}

@liuxuan30
Copy link
Member

I remember the line chart x values should be sorted. Random order will have troubles, as inside it's using XBound or similar thing. I wonder it won't be fixed in a short time.
but you can come up a new chart with similar render logic to get rid of the sort limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants