Reputation: 319
I am trying to use Daniel Gindi's Charts library and what I what I want is for the bar or line chart to only show a portion of the data, but allow the user to scroll to the right to see the rest of the data.
Right now, the chart shows ALL the data inside the view, so the bars are too small or invisible, and if there was only data points at the start of the graph, the line graph is scrunched so that you cannot see the data.
I've tried setting the xAxis.axisMaximum to a certain value, and that does display the data better, but then you cannot scroll to the right to see the rest of the chart. I want both: to see the data at a reasonable size, and to be able to scroll to the right to see the rest of the chart if the chart needs it, which it often will with my software.
Here is my bar and line chart code:
override func makeChart(resultChoice: chartData)
{
let chart = getChartObject() as! BarLineChartViewBase
chart.autoScaleMinMaxEnabled = true // I've tried this as false //!chart.autoScaleMinMaxEnabled
chart.chartDescription?.enabled = false
chart.drawGridBackgroundEnabled = false
chart.dragEnabled = true
chart.setScaleEnabled(true) //I've tried setting this to false
chart.pinchZoomEnabled = true
chart.leftAxis.axisMinimum = getAxisMinimum()
chart.leftAxis.axisMaximum = getAxisMaximum()
chart.clipValuesToContentEnabled = false
let xAxis = chart.xAxis
xAxis.avoidFirstLastClippingEnabled = true
xAxis.labelPosition = XAxis.LabelPosition.bottom
chart.rightAxis.enabled = false
xAxis.drawGridLinesEnabled = false
xAxis.drawAxisLineEnabled = true
xAxis.granularity = 1.0
xAxis.labelFont = MyVariables.chartFont!
//xAxis.axisMinimum = 0
//xAxis.axisMaximum = 30
//xAxis.setValuesForKeys(<#T##keyedValues: [String : Any]##[String : Any]#>)
yAxis = chart.leftAxis
yAxis.labelFont = MyVariables.chartFont!
chart.rightAxis.enabled = false
chart.legend.enabled = true
yAxis.granularity = 1.0
yAxis.drawGridLinesEnabled = false
//var counter = 1.0
var myEntries : [ChartDataEntry] = []
var myXValues : [String] = []
if !resultChoice.plotPoints.isEmpty
{
startDate = resultChoice.plotPoints[0].xValue
for item in resultChoice.plotPoints
{
/*let timeString : String =
{
let formatter = DateFormatter()
formatter.dateFormat = "h:mm a"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"
return formatter.string(from: item.xValue)
}()
myXValues.append(timeString)*/
/*if(item.showXValue)
{
myXValues.append(timeString)
}
else
{
myXValues.append("")
}*/
let myEntry = getChartDataEntry(xval: item.xValue.timeIntervalSince(startDate), yval: Double(item.yValue))
getColors(value: myEntry.y)
//counter += 1
myEntries.append(myEntry)
}
let myFormatter = MyXAxisValueFormatter(_startDate: startDate)
xAxis.valueFormatter = myFormatter
setYAxisFormatter()
createSet(myEntries: myEntries)
}
}
And here is my bar chart only code, to show that I did set a bar width:
override func createSet(myEntries: [ChartDataEntry])
{
let chart = getChartObject() as! BarChartView
let mySet = BarChartDataSet(values: myEntries, label: "Time Elapsing")
mySet.colors = myColors
mySet.valueColors = myColors
mySet.valueFont = MyVariables.chartFont!
setValueFormatter(set: mySet)
let myBarChartData = BarChartData(dataSet: mySet)
myBarChartData.barWidth = 0.8
chart.data = myBarChartData
}
Also, I should note that according to my business partner, the software used to behave the way I'm trying to get it to behave now. I don't remember this, but the fact that he thinks it used to work right makes me think I must have changed a setting that caused the problem. Also, recently I went from adding the chart entirely programmatically to adding the chart as a subview of another view, which was added through the interface builder. I don't think that should matter to this problem though. Also, the reason my business partner liked how the data was displayed before might not be because of any setting change. I have changed the x-axis recently so that time is displayed uniformly. This change was necessary, or it would be difficult to know when an event that the chart displays happened.
Upvotes: 2
Views: 3506
Reputation: 319
I solved the problem by calling
chart.setVisibleXRangeMaximum(30)
AFTER the data has been set, and calling
chart.scaleYEnabled = false
& chart.scaleXEnabled = true
when I set the other chart settings.
Upvotes: 3