2017-07-04 34 views
1

私のアプリは4四半期のスポーツのスコアを管理しています。私の棒グラフはx軸ラベルを正しく表示しません。それはQtr 1とQtr 3を示していますが、それぞれのバーの下に4四半期すべてを表示する必要があります。 Screen shot of Bar ChartiOSチャートX軸ラベルがすべて表示されていません

func setChart(dataPoints: [String], values1: [Double], values2: [Double]) { 

    let legend = barChartView.legend 
    legend.enabled = true 
    legend.textColor = Style.labelTextColor 

    let xaxis = barChartView.xAxis 
    xaxis.drawGridLinesEnabled = true 
    xaxis.labelPosition = .bottom 
    xaxis.axisMinimum = 0 
    xaxis.centerAxisLabelsEnabled = true 
    xaxis.granularityEnabled = true 
    xaxis.labelCount = 4 
    xaxis.granularity = 2 
    xaxis.labelTextColor = Style.labelTextColor 

    let leftAxisFormatter = NumberFormatter() 
    leftAxisFormatter.maximumFractionDigits = 1 

    let yaxis = barChartView.leftAxis 
    yaxis.spaceTop = 0.35 
    yaxis.axisMinimum = 0 
    yaxis.drawGridLinesEnabled = true 
    yaxis.labelTextColor = Style.labelTextColor 
    yaxis.gridColor = Style.labelTextColor 
    yaxis.zeroLineColor = Style.labelTextColor 
    yaxis.axisLineColor = Style.labelTextColor 

    barChartView.rightAxis.enabled = false 

    barChartView.noDataText = "You need to provide data for the chart." 
    barChartView.chartDescription?.text = nil 

    var dataEntries1: [BarChartDataEntry] = []; var dataEntries2: [BarChartDataEntry] = [] 

    for i in 0..<dataPoints.count { 
     let dataEntry1 = BarChartDataEntry(x: Double(i), y: values1[i]) 
     dataEntries1.append(dataEntry1) 

     let dataEntry2 = BarChartDataEntry(x: Double(i), y: values2[i]) 
     dataEntries2.append(dataEntry2) 
    } 

    let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: gCurrentMatch.teamA) 
    let chartDataSet2 = BarChartDataSet(values: dataEntries2, label: gCurrentMatch.teamB) 
    chartDataSet1.colors = [Style.chartColorA]; chartDataSet1.valueColors = [Style.labelTextColor] 
    chartDataSet2.colors = [Style.chartColorB]; chartDataSet2.valueColors = [Style.labelTextColor] 

    let dataSets: [BarChartDataSet] = [chartDataSet1, chartDataSet2] 
    let chartData = BarChartData(dataSets: dataSets) 

    let groupSpace = 0.3 
    let barSpace = 0.05 
    let barWidth = 1.0 
    // (0.3 + 0.05) * 2 + 0.3 = 1.00 -> interval per "group" 

    let groupCount = self.qtr.count 

    chartData.barWidth = barWidth 
    let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace) 

    barChartView.xAxis.axisMaximum = Double(0) + gg * Double(groupCount) 
    barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints) 

    chartData.groupBars(fromX: Double(0), groupSpace: groupSpace, barSpace: barSpace) 
    barChartView.notifyDataSetChanged() 
    barChartView.data = chartData 
    //chart animation 
    barChartView.animate(yAxisDuration: 0.5, easingOption: .linear) 

} 

dataPointsのそれが動作しない理由を私はうまくいかないことができます...

"クォーター1"、 "クォーター2" などを含む文字列の配列です。

+0

外観は多分https://stackoverflow.com/questions/44877031/difficulties-following-how-to、何らかの方法であなたを助けることができます-use-ios-charts-api-to-create-beautiful-chart-in/44878289#44878289 –

答えて

1

フォーマットするあなたのx軸に次のコードを使用:この答えで

xaxis.setLabelCount(4, force: true) 
関連する問題